GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/base_object-inl.h Lines: 44 44 100.0 %
Date: 2019-01-07 12:15:22 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
313022
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
35
    : persistent_handle_(env->isolate(), object),
36
626045
      env_(env) {
37
313023
  CHECK_EQ(false, object.IsEmpty());
38
313023
  CHECK_GT(object->InternalFieldCount(), 0);
39
313023
  object->SetAlignedPointerInInternalField(0, static_cast<void*>(this));
40
313023
  env_->AddCleanupHook(DeleteMe, static_cast<void*>(this));
41
313022
}
42
43
44

927708
BaseObject::~BaseObject() {
45
309236
  env_->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
46
47
618470
  if (persistent_handle_.IsEmpty()) {
48
    // This most likely happened because the weak callback below cleared it.
49
37989
    return;
50
  }
51
52
  {
53
271246
    v8::HandleScope handle_scope(env_->isolate());
54
542494
    object()->SetAlignedPointerInInternalField(0, nullptr);
55
  }
56
271247
}
57
58
59
563300
Persistent<v8::Object>& BaseObject::persistent() {
60
563300
  return persistent_handle_;
61
}
62
63
64
2042180
v8::Local<v8::Object> BaseObject::object() const {
65
2042180
  return PersistentToLocal::Default(env_->isolate(), persistent_handle_);
66
}
67
68
16167
v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
69
16167
  v8::Local<v8::Object> handle = object();
70
71
  DCHECK_EQ(handle->CreationContext()->GetIsolate(), isolate);
72
  DCHECK_EQ(env_->isolate(), isolate);
73
74
16169
  return handle;
75
}
76
77
7504870
Environment* BaseObject::env() const {
78
7504870
  return env_;
79
}
80
81
82
1145557
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Object> obj) {
83
1145557
  CHECK_GT(obj->InternalFieldCount(), 0);
84
2291116
  return static_cast<BaseObject*>(obj->GetAlignedPointerFromInternalField(0));
85
}
86
87
88
template <typename T>
89
186404
T* BaseObject::FromJSObject(v8::Local<v8::Object> object) {
90
186404
  return static_cast<T*>(FromJSObject(object));
91
}
92
93
94
96107
void BaseObject::MakeWeak() {
95
  persistent_handle_.SetWeak(
96
      this,
97
172082
      [](const v8::WeakCallbackInfo<BaseObject>& data) {
98
37987
        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
37987
        obj->persistent_handle_.Reset();
104
364297
      }, v8::WeakCallbackType::kParameter);
105
96108
}
106
107
108
1457
void BaseObject::ClearWeak() {
109
1457
  persistent_handle_.ClearWeak();
110
1457
}
111
112
113
v8::Local<v8::FunctionTemplate>
114
13228
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
115
55094
  auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
116
    DCHECK(args.IsConstructCall());
117
    DCHECK_GT(args.This()->InternalFieldCount(), 0);
118
41866
    args.This()->SetAlignedPointerInInternalField(0, nullptr);
119
55094
  };
120
121
13228
  v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
122
26456
  t->InstanceTemplate()->SetInternalFieldCount(1);
123
13228
  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_