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-10-07 22:40:39 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
735358
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
35
    : persistent_handle_(env->isolate(), object),
36
1470715
      env_(env) {
37
735357
  CHECK_EQ(false, object.IsEmpty());
38
735357
  CHECK_GT(object->InternalFieldCount(), 0);
39
735358
  object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
40
735358
  env_->AddCleanupHook(DeleteMe, static_cast<void*>(this));
41
735358
}
42
43

2194978
BaseObject::~BaseObject() {
44
731659
  RemoveCleanupHook();
45
46
1463320
  if (persistent_handle_.IsEmpty()) {
47
    // This most likely happened because the weak callback below cleared it.
48
423198
    return;
49
  }
50
51
  {
52
308462
    v8::HandleScope handle_scope(env_->isolate());
53
616924
    object()->SetAlignedPointerInInternalField(0, nullptr);
54
  }
55
308462
}
56
57
732282
void BaseObject::RemoveCleanupHook() {
58
732282
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
59
732282
}
60
61
789481
v8::Global<v8::Object>& BaseObject::persistent() {
62
789481
  return persistent_handle_;
63
}
64
65
66
3299859
v8::Local<v8::Object> BaseObject::object() const {
67
3299859
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
68
}
69
70
84289
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
71
84289
  v8::Local<v8::Object> handle = object();
72
73
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
74
  DCHECK_EQ(env_->isolate(), isolate);
75
76
84288
  return handle;
77
}
78
79
13378845
Environment* BaseObject::env() const {
80
13378845
  return env_;
81
}
82
83
84
1845246
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
85
1845246
  CHECK_GT(obj->InternalFieldCount(), 0);
86
3690496
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
87
}
88
89
90
template <typename T>
91
146779
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
92
146779
  return static_cast<T*>(FromJSObject(object));
93
}
94
95
96
478503
void BaseObject::MakeWeak() {
97
  persistent_handle_.SetWeak(
98
      this,
99
1324895
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
100
423196
        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
423196
        obj->persistent_handle_.Reset();
106
2281901
      }, v8::WeakCallbackType::kParameter);
107
478503
}
108
109
110
2636
void BaseObject::ClearWeak() {
111
2636
  persistent_handle_.ClearWeak();
112
2636
}
113
114
115
v8::Local<v8::FunctionTemplate>
116
18127
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
117
79015
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
118
    DCHECK(args.IsConstructCall());
119
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
120
60888
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
121
79015
  };
122
123
18127
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
124
36254
  t->InstanceTemplate()->SetInternalFieldCount(1);
125
18127
  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
153755
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
153755
  CHECK(((*value)->*typecheck)());
141
307510
  info.This()->SetInternalField(Field, value);
142
153755
}
143
144
}  // namespace node
145
146
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
147
148
#endif  // SRC_BASE_OBJECT_INL_H_