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

1033302
BaseObject::~BaseObject() {
45
344434
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
46
47
688868
  if (persistent_handle_.IsEmpty()) {
48
    // This most likely happened because the weak callback below cleared it.
49
6950
    return;
50
  }
51
52
  {
53
337484
    v8::HandleScope handle_scope(env_->isolate());
54
674968
    object()->SetAlignedPointerInInternalField(0, nullptr);
55
  }
56
337484
}
57
58
59
784194
Persistent<v8::Object>& BaseObject::persistent() {
60
784194
  return persistent_handle_;
61
}
62
63
64
3374430
v8::Local<v8::Object> BaseObject::object() const {
65
3374430
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
66
}
67
68
14863
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
69
14863
  v8::Local<v8::Object> handle = object();
70
71
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
72
  DCHECK_EQ(env_->isolate(), isolate);
73
74
14863
  return handle;
75
}
76
77
13160307
Environment* BaseObject::env() const {
78
13160307
  return env_;
79
}
80
81
82
1821229
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
83
1821229
  CHECK_GT(obj->InternalFieldCount(), 0);
84
3642458
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
85
}
86
87
88
template <typename T>
89
206689
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
90
206689
  return static_cast<T*>(FromJSObject(object));
91
}
92
93
94
68473
void BaseObject::MakeWeak() {
95
  persistent_handle_.SetWeak(
96
      this,
97
82372
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
98
6950
        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
6950
        obj->persistent_handle_.Reset();
104
219318
      }, v8::WeakCallbackType::kParameter);
105
68473
}
106
107
108
1934
void BaseObject::ClearWeak() {
109
1934
  persistent_handle_.ClearWeak();
110
1934
}
111
112
113
v8::Local<v8::FunctionTemplate>
114
15395
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
115
75795
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
116
    DCHECK(args.IsConstructCall());
117
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
118
60400
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
119
75795
  };
120
121
15395
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
122
30790
  t->InstanceTemplate()->SetInternalFieldCount(1);
123
15395
  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_