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

2200884
BaseObject::~BaseObject() {
44
733626
  RemoveCleanupHook();
45
46
1467256
  if (persistent_handle_.IsEmpty()) {
47
    // This most likely happened because the weak callback below cleared it.
48
418920
    return;
49
  }
50
51
  {
52
314708
    v8::HandleScope handle_scope(env_->isolate());
53
629420
    object()->SetAlignedPointerInInternalField(0, nullptr);
54
  }
55
314707
}
56
57
734243
void BaseObject::RemoveCleanupHook() {
58
734243
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
59
734248
}
60
61
799032
v8::Global<v8::Object>& BaseObject::persistent() {
62
799032
  return persistent_handle_;
63
}
64
65
66
3337202
v8::Local<v8::Object> BaseObject::object() const {
67
3337202
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
68
}
69
70
81399
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
71
81399
  v8::Local<v8::Object> handle = object();
72
73
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
74
  DCHECK_EQ(env_->isolate(), isolate);
75
76
81399
  return handle;
77
}
78
79
13551371
Environment* BaseObject::env() const {
80
13551371
  return env_;
81
}
82
83
84
1818644
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
85
1818644
  CHECK_GT(obj->InternalFieldCount(), 0);
86
3637285
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
87
}
88
89
90
template <typename T>
91
144482
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
92
144482
  return static_cast<T*>(FromJSObject(object));
93
}
94
95
96
473330
void BaseObject::MakeWeak() {
97
  persistent_handle_.SetWeak(
98
      this,
99
1311168
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
100
418919
        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
418919
        obj->persistent_handle_.Reset();
106
2257828
      }, v8::WeakCallbackType::kParameter);
107
473330
}
108
109
110
2668
void BaseObject::ClearWeak() {
111
2668
  persistent_handle_.ClearWeak();
112
2668
}
113
114
115
v8::Local<v8::FunctionTemplate>
116
18281
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
117
81009
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
118
    DCHECK(args.IsConstructCall());
119
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
120
62728
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
121
81009
  };
122
123
18281
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
124
36562
  t->InstanceTemplate()->SetInternalFieldCount(1);
125
18281
  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
154383
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
154383
  CHECK(((*value)->*typecheck)());
141
308766
  info.This()->SetInternalField(Field, value);
142
154383
}
143
144
}  // namespace node
145
146
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
147
148
#endif  // SRC_BASE_OBJECT_INL_H_