GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "base_object.h" |
||
2 |
#include "env-inl.h" |
||
3 |
#include "node_realm-inl.h" |
||
4 |
|||
5 |
namespace node { |
||
6 |
|||
7 |
using v8::FunctionCallbackInfo; |
||
8 |
using v8::FunctionTemplate; |
||
9 |
using v8::HandleScope; |
||
10 |
using v8::Local; |
||
11 |
using v8::Object; |
||
12 |
using v8::Value; |
||
13 |
using v8::WeakCallbackInfo; |
||
14 |
using v8::WeakCallbackType; |
||
15 |
|||
16 |
1462911 |
BaseObject::BaseObject(Realm* realm, Local<Object> object) |
|
17 |
2925822 |
: persistent_handle_(realm->isolate(), object), realm_(realm) { |
|
18 |
✗✓ | 1462911 |
CHECK_EQ(false, object.IsEmpty()); |
19 |
✗✓ | 1462911 |
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount); |
20 |
1462911 |
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, |
|
21 |
&kNodeEmbedderId); |
||
22 |
1462911 |
object->SetAlignedPointerInInternalField(BaseObject::kSlot, |
|
23 |
static_cast<void*>(this)); |
||
24 |
1462911 |
realm->AddCleanupHook(DeleteMe, static_cast<void*>(this)); |
|
25 |
1462911 |
realm->modify_base_object_count(1); |
|
26 |
1462911 |
} |
|
27 |
|||
28 |
✓✓✓✓ |
7954380 |
BaseObject::~BaseObject() { |
29 |
2899300 |
realm()->modify_base_object_count(-1); |
|
30 |
2899300 |
realm()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this)); |
|
31 |
|||
32 |
✓✓ | 2899300 |
if (UNLIKELY(has_pointer_data())) { |
33 |
393260 |
PointerData* metadata = pointer_data(); |
|
34 |
✗✓ | 393260 |
CHECK_EQ(metadata->strong_ptr_count, 0); |
35 |
393260 |
metadata->self = nullptr; |
|
36 |
✓✓ | 393260 |
if (metadata->weak_ptr_count == 0) delete metadata; |
37 |
} |
||
38 |
|||
39 |
✓✓ | 2899300 |
if (persistent_handle_.IsEmpty()) { |
40 |
// This most likely happened because the weak callback below cleared it. |
||
41 |
2155780 |
return; |
|
42 |
} |
||
43 |
|||
44 |
{ |
||
45 |
743520 |
HandleScope handle_scope(realm()->isolate()); |
|
46 |
1487040 |
object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); |
|
47 |
} |
||
48 |
} |
||
49 |
|||
50 |
1326254 |
void BaseObject::MakeWeak() { |
|
51 |
✓✓ | 1326254 |
if (has_pointer_data()) { |
52 |
42704 |
pointer_data()->wants_weak_jsobj = true; |
|
53 |
✓✓ | 42704 |
if (pointer_data()->strong_ptr_count > 0) return; |
54 |
} |
||
55 |
|||
56 |
2652506 |
persistent_handle_.SetWeak( |
|
57 |
this, |
||
58 |
1077889 |
[](const WeakCallbackInfo<BaseObject>& data) { |
|
59 |
1077889 |
BaseObject* obj = data.GetParameter(); |
|
60 |
// Clear the persistent handle so that ~BaseObject() doesn't attempt |
||
61 |
// to mess with internal fields, since the JS object may have |
||
62 |
// transitioned into an invalid state. |
||
63 |
// Refs: https://github.com/nodejs/node/issues/18897 |
||
64 |
1077889 |
obj->persistent_handle_.Reset(); |
|
65 |
✓✓✗✓ ✗✓ |
1077889 |
CHECK_IMPLIES(obj->has_pointer_data(), |
66 |
obj->pointer_data()->strong_ptr_count == 0); |
||
67 |
1077889 |
obj->OnGCCollect(); |
|
68 |
1077889 |
}, |
|
69 |
WeakCallbackType::kParameter); |
||
70 |
} |
||
71 |
|||
72 |
// This just has to be different from the Chromium ones: |
||
73 |
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a |
||
74 |
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will |
||
75 |
// misinterpret the data stored in the embedder fields and try to garbage |
||
76 |
// collect them. |
||
77 |
uint16_t kNodeEmbedderId = 0x90de; |
||
78 |
|||
79 |
23676 |
void BaseObject::LazilyInitializedJSTemplateConstructor( |
|
80 |
const FunctionCallbackInfo<Value>& args) { |
||
81 |
DCHECK(args.IsConstructCall()); |
||
82 |
✗✓ | 23676 |
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount); |
83 |
23676 |
args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, |
|
84 |
&kNodeEmbedderId); |
||
85 |
23676 |
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); |
|
86 |
23676 |
} |
|
87 |
|||
88 |
21850 |
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate( |
|
89 |
Environment* env) { |
||
90 |
Local<FunctionTemplate> t = NewFunctionTemplate( |
||
91 |
21850 |
env->isolate(), LazilyInitializedJSTemplateConstructor); |
|
92 |
21850 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
|
93 |
43700 |
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount); |
|
94 |
21850 |
return t; |
|
95 |
} |
||
96 |
|||
97 |
3352956 |
BaseObject::PointerData* BaseObject::pointer_data() { |
|
98 |
✓✓ | 3352956 |
if (!has_pointer_data()) { |
99 |
199088 |
PointerData* metadata = new PointerData(); |
|
100 |
199088 |
metadata->wants_weak_jsobj = persistent_handle_.IsWeak(); |
|
101 |
199088 |
metadata->self = this; |
|
102 |
199088 |
pointer_data_ = metadata; |
|
103 |
} |
||
104 |
✗✓ | 3352956 |
CHECK(has_pointer_data()); |
105 |
3352956 |
return pointer_data_; |
|
106 |
} |
||
107 |
|||
108 |
836226 |
void BaseObject::decrease_refcount() { |
|
109 |
✗✓ | 836226 |
CHECK(has_pointer_data()); |
110 |
836226 |
PointerData* metadata = pointer_data(); |
|
111 |
✗✓ | 836226 |
CHECK_GT(metadata->strong_ptr_count, 0); |
112 |
836226 |
unsigned int new_refcount = --metadata->strong_ptr_count; |
|
113 |
✓✓ | 836226 |
if (new_refcount == 0) { |
114 |
✓✓ | 263957 |
if (metadata->is_detached) { |
115 |
190674 |
OnGCCollect(); |
|
116 |
✓✓✓✗ ✓✓ |
73283 |
} else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) { |
117 |
42703 |
MakeWeak(); |
|
118 |
} |
||
119 |
} |
||
120 |
836226 |
} |
|
121 |
|||
122 |
838836 |
void BaseObject::increase_refcount() { |
|
123 |
838836 |
unsigned int prev_refcount = pointer_data()->strong_ptr_count++; |
|
124 |
✓✓✓✓ ✓✓ |
838836 |
if (prev_refcount == 0 && !persistent_handle_.IsEmpty()) |
125 |
266400 |
persistent_handle_.ClearWeak(); |
|
126 |
838836 |
} |
|
127 |
|||
128 |
172031 |
void BaseObject::DeleteMe(void* data) { |
|
129 |
172031 |
BaseObject* self = static_cast<BaseObject*>(data); |
|
130 |
✓✓✓✓ ✓✓ |
172031 |
if (self->has_pointer_data() && self->pointer_data()->strong_ptr_count > 0) { |
131 |
910 |
return self->Detach(); |
|
132 |
} |
||
133 |
✓✗ | 171121 |
delete self; |
134 |
} |
||
135 |
|||
136 |
1178 |
bool BaseObject::IsDoneInitializing() const { |
|
137 |
1178 |
return true; |
|
138 |
} |
||
139 |
|||
140 |
649 |
Local<Object> BaseObject::WrappedObject() const { |
|
141 |
649 |
return object(); |
|
142 |
} |
||
143 |
|||
144 |
1298 |
bool BaseObject::IsRootNode() const { |
|
145 |
2596 |
return !persistent_handle_.IsWeak(); |
|
146 |
} |
||
147 |
|||
148 |
56303 |
Local<FunctionTemplate> BaseObject::GetConstructorTemplate( |
|
149 |
IsolateData* isolate_data) { |
||
150 |
56303 |
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template(); |
|
151 |
✓✓ | 56303 |
if (tmpl.IsEmpty()) { |
152 |
792 |
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr); |
|
153 |
792 |
tmpl->SetClassName( |
|
154 |
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject")); |
||
155 |
792 |
isolate_data->set_base_object_ctor_template(tmpl); |
|
156 |
} |
||
157 |
56303 |
return tmpl; |
|
158 |
} |
||
159 |
|||
160 |
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const { |
||
161 |
return IsWeakOrDetached(); |
||
162 |
} |
||
163 |
|||
164 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |