diff --git a/src/base_object-inl.h b/src/base_object-inl.h index 498ab9afb07666..675a472dab48f1 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -73,14 +73,24 @@ Realm* BaseObject::realm() const { return realm_; } +void BaseObject::TagNodeObject(v8::Local object) { + DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount); + object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, + &kNodeEmbedderId); +} + +void BaseObject::SetInternalFields(v8::Local object, void* slot) { + TagNodeObject(object); + object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot); +} + BaseObject* BaseObject::FromJSObject(v8::Local value) { v8::Local obj = value.As(); - DCHECK_GE(obj->InternalFieldCount(), BaseObject::kSlot); + DCHECK_GE(obj->InternalFieldCount(), BaseObject::kInternalFieldCount); return static_cast( obj->GetAlignedPointerFromInternalField(BaseObject::kSlot)); } - template T* BaseObject::FromJSObject(v8::Local object) { return static_cast(FromJSObject(object)); diff --git a/src/base_object.cc b/src/base_object.cc index cdf1285bef06fa..d0444573c85ac7 100644 --- a/src/base_object.cc +++ b/src/base_object.cc @@ -17,10 +17,7 @@ BaseObject::BaseObject(Realm* realm, Local object) : persistent_handle_(realm->isolate(), object), realm_(realm) { CHECK_EQ(false, object.IsEmpty()); CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount); - object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, - &kNodeEmbedderId); - object->SetAlignedPointerInInternalField(BaseObject::kSlot, - static_cast(this)); + SetInternalFields(object, static_cast(this)); realm->AddCleanupHook(DeleteMe, static_cast(this)); realm->modify_base_object_count(1); } @@ -80,16 +77,19 @@ void BaseObject::LazilyInitializedJSTemplateConstructor( const FunctionCallbackInfo& args) { DCHECK(args.IsConstructCall()); CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount); - args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, - &kNodeEmbedderId); - args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); + SetInternalFields(args.This(), nullptr); } Local BaseObject::MakeLazilyInitializedJSTemplate( Environment* env) { + return MakeLazilyInitializedJSTemplate(env->isolate_data()); +} + +Local BaseObject::MakeLazilyInitializedJSTemplate( + IsolateData* isolate_data) { Local t = NewFunctionTemplate( - env->isolate(), LazilyInitializedJSTemplateConstructor); - t->Inherit(BaseObject::GetConstructorTemplate(env)); + isolate_data->isolate(), LazilyInitializedJSTemplateConstructor); + t->Inherit(BaseObject::GetConstructorTemplate(isolate_data)); t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount); return t; } diff --git a/src/base_object.h b/src/base_object.h index 25a380cb746c01..779573362268a6 100644 --- a/src/base_object.h +++ b/src/base_object.h @@ -73,6 +73,9 @@ class BaseObject : public MemoryRetainer { // was also passed to the `BaseObject()` constructor initially. // This may return `nullptr` if the C++ object has not been constructed yet, // e.g. when the JS object used `MakeLazilyInitializedJSTemplate`. + static inline void SetInternalFields(v8::Local object, + void* slot); + static inline void TagNodeObject(v8::Local object); static void LazilyInitializedJSTemplateConstructor( const v8::FunctionCallbackInfo& args); static inline BaseObject* FromJSObject(v8::Local object); @@ -98,7 +101,8 @@ class BaseObject : public MemoryRetainer { // Utility to create a FunctionTemplate with one internal field (used for // the `BaseObject*` pointer) and a constructor that initializes that field // to `nullptr`. - // TODO(legendecas): Disentangle template with env. + static v8::Local MakeLazilyInitializedJSTemplate( + IsolateData* isolate); static v8::Local MakeLazilyInitializedJSTemplate( Environment* env);