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 |
1466227 |
BaseObject::BaseObject(Realm* realm, Local<Object> object) |
|
17 |
2932454 |
: persistent_handle_(realm->isolate(), object), realm_(realm) { |
|
18 |
✗✓ | 1466227 |
CHECK_EQ(false, object.IsEmpty()); |
19 |
✗✓ | 1466227 |
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount); |
20 |
1466227 |
SetInternalFields(object, static_cast<void*>(this)); |
|
21 |
1466227 |
realm->AddCleanupHook(DeleteMe, static_cast<void*>(this)); |
|
22 |
1466227 |
realm->modify_base_object_count(1); |
|
23 |
1466227 |
} |
|
24 |
|||
25 |
✓✓✓✓ |
7959980 |
BaseObject::~BaseObject() { |
26 |
2900202 |
realm()->modify_base_object_count(-1); |
|
27 |
2900202 |
realm()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this)); |
|
28 |
|||
29 |
✓✓ | 2900202 |
if (UNLIKELY(has_pointer_data())) { |
30 |
395598 |
PointerData* metadata = pointer_data(); |
|
31 |
✗✓ | 395598 |
CHECK_EQ(metadata->strong_ptr_count, 0); |
32 |
395598 |
metadata->self = nullptr; |
|
33 |
✓✓ | 395598 |
if (metadata->weak_ptr_count == 0) delete metadata; |
34 |
} |
||
35 |
|||
36 |
✓✓ | 2900202 |
if (persistent_handle_.IsEmpty()) { |
37 |
// This most likely happened because the weak callback below cleared it. |
||
38 |
2159576 |
return; |
|
39 |
} |
||
40 |
|||
41 |
{ |
||
42 |
740626 |
HandleScope handle_scope(realm()->isolate()); |
|
43 |
1481252 |
object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); |
|
44 |
} |
||
45 |
} |
||
46 |
|||
47 |
1329514 |
void BaseObject::MakeWeak() { |
|
48 |
✓✓ | 1329514 |
if (has_pointer_data()) { |
49 |
44519 |
pointer_data()->wants_weak_jsobj = true; |
|
50 |
✓✓ | 44519 |
if (pointer_data()->strong_ptr_count > 0) return; |
51 |
} |
||
52 |
|||
53 |
2657786 |
persistent_handle_.SetWeak( |
|
54 |
this, |
||
55 |
1079787 |
[](const WeakCallbackInfo<BaseObject>& data) { |
|
56 |
1079787 |
BaseObject* obj = data.GetParameter(); |
|
57 |
// Clear the persistent handle so that ~BaseObject() doesn't attempt |
||
58 |
// to mess with internal fields, since the JS object may have |
||
59 |
// transitioned into an invalid state. |
||
60 |
// Refs: https://github.com/nodejs/node/issues/18897 |
||
61 |
1079787 |
obj->persistent_handle_.Reset(); |
|
62 |
✓✓✗✓ ✗✓ |
1079787 |
CHECK_IMPLIES(obj->has_pointer_data(), |
63 |
obj->pointer_data()->strong_ptr_count == 0); |
||
64 |
1079787 |
obj->OnGCCollect(); |
|
65 |
1079787 |
}, |
|
66 |
WeakCallbackType::kParameter); |
||
67 |
} |
||
68 |
|||
69 |
// This just has to be different from the Chromium ones: |
||
70 |
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a |
||
71 |
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will |
||
72 |
// misinterpret the data stored in the embedder fields and try to garbage |
||
73 |
// collect them. |
||
74 |
uint16_t kNodeEmbedderId = 0x90de; |
||
75 |
|||
76 |
23733 |
void BaseObject::LazilyInitializedJSTemplateConstructor( |
|
77 |
const FunctionCallbackInfo<Value>& args) { |
||
78 |
DCHECK(args.IsConstructCall()); |
||
79 |
✗✓ | 23733 |
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount); |
80 |
23733 |
SetInternalFields(args.This(), nullptr); |
|
81 |
23733 |
} |
|
82 |
|||
83 |
22225 |
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate( |
|
84 |
Environment* env) { |
||
85 |
22225 |
return MakeLazilyInitializedJSTemplate(env->isolate_data()); |
|
86 |
} |
||
87 |
|||
88 |
23034 |
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate( |
|
89 |
IsolateData* isolate_data) { |
||
90 |
Local<FunctionTemplate> t = NewFunctionTemplate( |
||
91 |
23034 |
isolate_data->isolate(), LazilyInitializedJSTemplateConstructor); |
|
92 |
23034 |
t->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); |
|
93 |
46068 |
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount); |
|
94 |
23034 |
return t; |
|
95 |
} |
||
96 |
|||
97 |
3401627 |
BaseObject::PointerData* BaseObject::pointer_data() { |
|
98 |
✓✓ | 3401627 |
if (!has_pointer_data()) { |
99 |
200870 |
PointerData* metadata = new PointerData(); |
|
100 |
200870 |
metadata->wants_weak_jsobj = persistent_handle_.IsWeak(); |
|
101 |
200870 |
metadata->self = this; |
|
102 |
200870 |
pointer_data_ = metadata; |
|
103 |
} |
||
104 |
✗✓ | 3401627 |
CHECK(has_pointer_data()); |
105 |
3401627 |
return pointer_data_; |
|
106 |
} |
||
107 |
|||
108 |
849153 |
void BaseObject::decrease_refcount() { |
|
109 |
✗✓ | 849153 |
CHECK(has_pointer_data()); |
110 |
849153 |
PointerData* metadata = pointer_data(); |
|
111 |
✗✓ | 849153 |
CHECK_GT(metadata->strong_ptr_count, 0); |
112 |
849153 |
unsigned int new_refcount = --metadata->strong_ptr_count; |
|
113 |
✓✓ | 849153 |
if (new_refcount == 0) { |
114 |
✓✓ | 265771 |
if (metadata->is_detached) { |
115 |
191159 |
OnGCCollect(); |
|
116 |
✓✓✓✗ ✓✓ |
74612 |
} else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) { |
117 |
43898 |
MakeWeak(); |
|
118 |
} |
||
119 |
} |
||
120 |
849153 |
} |
|
121 |
|||
122 |
852251 |
void BaseObject::increase_refcount() { |
|
123 |
852251 |
unsigned int prev_refcount = pointer_data()->strong_ptr_count++; |
|
124 |
✓✓✓✓ ✓✓ |
852251 |
if (prev_refcount == 0 && !persistent_handle_.IsEmpty()) |
125 |
268695 |
persistent_handle_.ClearWeak(); |
|
126 |
852251 |
} |
|
127 |
|||
128 |
169841 |
void BaseObject::DeleteMe(void* data) { |
|
129 |
169841 |
BaseObject* self = static_cast<BaseObject*>(data); |
|
130 |
✓✓✓✓ ✓✓ |
169841 |
if (self->has_pointer_data() && self->pointer_data()->strong_ptr_count > 0) { |
131 |
904 |
return self->Detach(); |
|
132 |
} |
||
133 |
✓✗ | 168937 |
delete self; |
134 |
} |
||
135 |
|||
136 |
1786 |
bool BaseObject::IsDoneInitializing() const { |
|
137 |
1786 |
return true; |
|
138 |
} |
||
139 |
|||
140 |
976 |
Local<Object> BaseObject::WrappedObject() const { |
|
141 |
976 |
return object(); |
|
142 |
} |
||
143 |
|||
144 |
1767 |
bool BaseObject::IsRootNode() const { |
|
145 |
3534 |
return !persistent_handle_.IsWeak(); |
|
146 |
} |
||
147 |
|||
148 |
58244 |
Local<FunctionTemplate> BaseObject::GetConstructorTemplate( |
|
149 |
IsolateData* isolate_data) { |
||
150 |
58244 |
Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template(); |
|
151 |
✓✓ | 58244 |
if (tmpl.IsEmpty()) { |
152 |
809 |
tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr); |
|
153 |
809 |
tmpl->SetClassName( |
|
154 |
FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject")); |
||
155 |
809 |
isolate_data->set_base_object_ctor_template(tmpl); |
|
156 |
} |
||
157 |
58244 |
return tmpl; |
|
158 |
} |
||
159 |
|||
160 |
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const { |
||
161 |
return IsWeakOrDetached(); |
||
162 |
} |
||
163 |
|||
164 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |