Skip to content

Commit 2d2e71c

Browse files
joyeecheungdanielleadams
authored andcommitted
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 7b992cc commit 2d2e71c

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/base_object-inl.h

+12-2
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));

src/base_object.cc

+9-9
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
}

src/base_object.h

+5-1
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);
@@ -98,7 +101,8 @@ class BaseObject : public MemoryRetainer {
98101
// Utility to create a FunctionTemplate with one internal field (used for
99102
// the `BaseObject*` pointer) and a constructor that initializes that field
100103
// to `nullptr`.
101-
// TODO(legendecas): Disentangle template with env.
104+
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
105+
IsolateData* isolate);
102106
static v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
103107
Environment* env);
104108

0 commit comments

Comments
 (0)