Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5ce285

Browse filesBrowse files
committed
src: refactor BaseObject methods
- Wrap the initialization of the kSlot and kEmbedderType fields into a BaseObject::SetInternalFields() method. - Move the tagging of kEmbedderType field into BaseObject::TagNodeObject() - Add a variant of BaseObject::MakeLazilyInitializedJSTemplate() that only needs IsolateData. This makes it easier to create BaseObject subclasses. PR-URL: #44796 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 00e5617 commit d5ce285
Copy full SHA for d5ce285

File tree

Expand file treeCollapse file tree

3 files changed

+26
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-12
lines changed
Open diff view settings
Collapse file

‎src/base_object-inl.h‎

Copy file name to clipboardExpand all lines: src/base_object-inl.h
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,24 @@ Realm* BaseObject::realm() const {
7373
return realm_;
7474
}
7575

76+
void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
77+
DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
78+
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
79+
&kNodeEmbedderId);
80+
}
81+
82+
void BaseObject::SetInternalFields(v8::Local<v8::Object> object, void* slot) {
83+
TagNodeObject(object);
84+
object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot);
85+
}
86+
7687
BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
7788
v8::Local<v8::Object> obj = value.As<v8::Object>();
78-
DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot);
89+
DCHECK_GE(obj->InternalFieldCount(), BaseObject::kInternalFieldCount);
7990
return static_cast<BaseObject*>(
8091
obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
8192
}
8293

83-
8494
template <typename T>
8595
T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
8696
return static_cast<T*>(FromJSObject(object));
Collapse file

‎src/base_object.cc‎

Copy file name to clipboardExpand all lines: src/base_object.cc
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ BaseObject::BaseObject(Realm* realm, Local<Object> object)
1717
: persistent_handle_(realm->isolate(), object), realm_(realm) {
1818
CHECK_EQ(false, object.IsEmpty());
1919
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
20-
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
21-
&kNodeEmbedderId);
22-
object->SetAlignedPointerInInternalField(BaseObject::kSlot,
23-
static_cast<void*>(this));
20+
SetInternalFields(object, static_cast<void*>(this));
2421
realm->AddCleanupHook(DeleteMe, static_cast<void*>(this));
2522
realm->modify_base_object_count(1);
2623
}
@@ -80,16 +77,19 @@ void BaseObject::LazilyInitializedJSTemplateConstructor(
8077
const FunctionCallbackInfo<Value>& args) {
8178
DCHECK(args.IsConstructCall());
8279
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount);
83-
args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
84-
&kNodeEmbedderId);
85-
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
80+
SetInternalFields(args.This(), nullptr);
8681
}
8782

8883
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
8984
Environment* env) {
85+
return MakeLazilyInitializedJSTemplate(env->isolate_data());
86+
}
87+
88+
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
89+
IsolateData* isolate_data) {
9090
Local<FunctionTemplate> t = NewFunctionTemplate(
91-
env->isolate(), LazilyInitializedJSTemplateConstructor);
92-
t->Inherit(BaseObject::GetConstructorTemplate(env));
91+
isolate_data->isolate(), LazilyInitializedJSTemplateConstructor);
92+
t->Inherit(BaseObject::GetConstructorTemplate(isolate_data));
9393
t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
9494
return t;
9595
}
Collapse file

‎src/base_object.h‎

Copy file name to clipboardExpand all lines: src/base_object.h
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class BaseObject : public MemoryRetainer {
7373
// was also passed to the `BaseObject()` constructor initially.
7474
// This may return `nullptr` if the C++ object has not been constructed yet,
7575
// e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
76+
static inline void SetInternalFields(v8::Local<v8::Object> object,
77+
void* slot);
78+
static inline void TagNodeObject(v8::Local<v8::Object> object);
7679
static void LazilyInitializedJSTemplateConstructor(
7780
const v8::FunctionCallbackInfo<v8::Value>& args);
7881
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);
@@ -96,7 +99,8 @@ class BaseObject : public MemoryRetainer {
9699
// Utility to create a FunctionTemplate with one internal field (used for
97100
// the `BaseObject*` pointer) and a constructor that initializes that field
98101
// to `nullptr`.
99-
// TODO(legendecas): Disentangle template with env.
102+
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
103+
IsolateData* isolate);
100104
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
101105
Environment* env);
102106

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.