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: 44 44 100.0 %
Date: 2019-02-13 22:28:58 Branches: 10 14 71.4 %

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

2358436
BaseObject::~BaseObject() {
45
786145
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
46
47
1572292
  if (persistent_handle_.IsEmpty()) {
48
    // This most likely happened because the weak callback below cleared it.
49
330452
    return;
50
  }
51
52
  {
53
455694
    v8::HandleScope handle_scope(env_->isolate());
54
911387
    object()->SetAlignedPointerInInternalField(0, nullptr);
55
  }
56
455693
}
57
58
59
772141
Persistent<v8::Object>& BaseObject::persistent() {
60
772141
  return persistent_handle_;
61
}
62
63
64
3475299
v8::Local<v8::Object> BaseObject::object() const {
65
3475299
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
66
}
67
68
14432
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
69
14432
  v8::Local<v8::Object> handle = object();
70
71
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
72
  DCHECK_EQ(env_->isolate(), isolate);
73
74
14430
  return handle;
75
}
76
77
13532519
Environment* BaseObject::env() const {
78
13532519
  return env_;
79
}
80
81
82
2734434
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
83
2734434
  CHECK_GT(obj->InternalFieldCount(), 0);
84
5468870
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
85
}
86
87
88
template <typename T>
89
201545
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
90
201545
  return static_cast<T*>(FromJSObject(object));
91
}
92
93
94
505634
void BaseObject::MakeWeak() {
95
  persistent_handle_.SetWeak(
96
      this,
97
1166538
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
98
330452
        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
330452
        obj->persistent_handle_.Reset();
104
2177806
      }, v8::WeakCallbackType::kParameter);
105
505634
}
106
107
108
1938
void BaseObject::ClearWeak() {
109
1938
  persistent_handle_.ClearWeak();
110
1938
}
111
112
113
v8::Local<v8::FunctionTemplate>
114
15465
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
115
75811
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
116
    DCHECK(args.IsConstructCall());
117
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
118
60346
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
119
75811
  };
120
121
15465
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
122
30930
  t->InstanceTemplate()->SetInternalFieldCount(1);
123
15465
  return t;
124
}
125
126
}  // namespace node
127
128
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
129
130
#endif  // SRC_BASE_OBJECT_INL_H_