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: 51 53 96.2 %
Date: 2019-07-28 22:34:34 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.h"
30
#include "v8.h"
31
32
namespace node {
33
34
699711
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
35
    : persistent_handle_(env->isolate(), object),
36
1399422
      env_(env) {
37
699711
  CHECK_EQ(false, object.IsEmpty());
38
699711
  CHECK_GT(object->InternalFieldCount(), 0);
39
699711
  object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
40
699711
  env_->AddCleanupHook(DeleteMe, static_cast<void*>(this));
41
699711
}
42
43

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