GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base_object-inl.h Lines: 48 50 96.0 %
Date: 2019-05-05 22:32:45 Branches: 11 16 68.8 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_BASE_OBJECT_INL_H_
23
#define SRC_BASE_OBJECT_INL_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "base_object.h"
28
#include "env-inl.h"
29
#include "util-inl.h"
30
#include "v8.h"
31
32
namespace node {
33
34
655495
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
35
    : persistent_handle_(env->isolate(), object),
36
1310990
      env_(env) {
37
655495
  CHECK_EQ(false, object.IsEmpty());
38
655495
  CHECK_GT(object->InternalFieldCount(), 0);
39
655496
  object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
40
655495
  env_->AddCleanupHook(DeleteMe, static_cast<void*>(this));
41
655496
}
42
43
44

1962539
BaseObject::~BaseObject() {
45
654180
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
46
47
1308356
  if (persistent_handle_.IsEmpty()) {
48
    // This most likely happened because the weak callback below cleared it.
49
434131
    return;
50
  }
51
52
  {
53
220047
    v8::HandleScope handle_scope(env_->isolate());
54
440097
    object()->SetAlignedPointerInInternalField(0, nullptr);
55
  }
56
220047
}
57
58
59
628076
v8::Global<v8::Object>& BaseObject::persistent() {
60
628076
  return persistent_handle_;
61
}
62
63
64
2434355
v8::Local<v8::Object> BaseObject::object() const {
65
2434355
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
66
}
67
68
18071
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
69
18071
  v8::Local<v8::Object> handle = object();
70
71
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
72
  DCHECK_EQ(env_->isolate(), isolate);
73
74
18071
  return handle;
75
}
76
77
9620994
Environment* BaseObject::env() const {
78
9620994
  return env_;
79
}
80
81
82
1567883
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
83
1567883
  CHECK_GT(obj->InternalFieldCount(), 0);
84
3135770
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
85
}
86
87
88
template <typename T>
89
90204
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
90
90204
  return static_cast<T*>(FromJSObject(object));
91
}
92
93
94
492909
void BaseObject::MakeWeak() {
95
  persistent_handle_.SetWeak(
96
      this,
97
1361172
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
98
434131
        std::unique_ptr<BaseObject> obj(data.GetParameter());
99
        // Clear the persistent handle so that ~BaseObject() doesn't attempt
100
        // to mess with internal fields, since the JS object may have
101
        // transitioned into an invalid state.
102
        // Refs: https://github.com/nodejs/node/issues/18897
103
434131
        obj->persistent_handle_.Reset();
104
2346991
      }, v8::WeakCallbackType::kParameter);
105
492910
}
106
107
108
2812
void BaseObject::ClearWeak() {
109
2812
  persistent_handle_.ClearWeak();
110
2812
}
111
112
113
v8::Local<v8::FunctionTemplate>
114
16364
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
115
77896
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
116
    DCHECK(args.IsConstructCall());
117
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
118
61532
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
119
77896
  };
120
121
16364
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
122
32728
  t->InstanceTemplate()->SetInternalFieldCount(1);
123
16364
  return t;
124
}
125
126
template <int Field>
127
void BaseObject::InternalFieldGet(
128
    v8::Local<v8::String> property,
129
    const v8::PropertyCallbackInfo<v8::Value>& info) {
130
  info.GetReturnValue().Set(info.This()->GetInternalField(Field));
131
}
132
133
template <int Field, bool (v8::Value::* typecheck)() const>
134
126509
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
135
                                  v8::Local<v8::Value> value,
136
                                  const v8::PropertyCallbackInfo<void>& info) {
137
  // This could be e.g. value->IsFunction().
138
126509
  CHECK(((*value)->*typecheck)());
139
253018
  info.This()->SetInternalField(Field, value);
140
126509
}
141
142
}  // namespace node
143
144
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
145
146
#endif  // SRC_BASE_OBJECT_INL_H_