From a2c1c3ef64d20a992fc4ce9cefb02474d4bd4ea0 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 31 Aug 2021 23:17:06 +0800 Subject: [PATCH] src: register external references of BaseObject for snapshot PR-URL: https://github.com/nodejs/node/pull/39961 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Michael Dawson --- src/base_object-inl.h | 16 +++++++++------- src/base_object.h | 2 ++ src/node_external_reference.cc | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/base_object-inl.h b/src/base_object-inl.h index ad900b6399f149..bb1e8d4b46bce3 100644 --- a/src/base_object-inl.h +++ b/src/base_object-inl.h @@ -148,15 +148,17 @@ bool BaseObject::IsWeakOrDetached() const { return pd->wants_weak_jsobj || pd->is_detached; } +void BaseObject::LazilyInitializedJSTemplateConstructor( + const v8::FunctionCallbackInfo& args) { + DCHECK(args.IsConstructCall()); + DCHECK_GT(args.This()->InternalFieldCount(), 0); + args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); +} + v8::Local BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) { - auto constructor = [](const v8::FunctionCallbackInfo& args) { - DCHECK(args.IsConstructCall()); - DCHECK_GT(args.This()->InternalFieldCount(), 0); - args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr); - }; - - v8::Local t = env->NewFunctionTemplate(constructor); + v8::Local t = + env->NewFunctionTemplate(LazilyInitializedJSTemplateConstructor); t->Inherit(BaseObject::GetConstructorTemplate(env)); t->InstanceTemplate()->SetInternalFieldCount( BaseObject::kInternalFieldCount); diff --git a/src/base_object.h b/src/base_object.h index ec9d4a69d537b2..d46a0f216009c6 100644 --- a/src/base_object.h +++ b/src/base_object.h @@ -65,6 +65,8 @@ 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 LazilyInitializedJSTemplateConstructor( + const v8::FunctionCallbackInfo& args); static inline BaseObject* FromJSObject(v8::Local object); template static inline T* FromJSObject(v8::Local object); diff --git a/src/node_external_reference.cc b/src/node_external_reference.cc index 73e1489865d3a4..94198719b6a002 100644 --- a/src/node_external_reference.cc +++ b/src/node_external_reference.cc @@ -1,6 +1,7 @@ #include "node_external_reference.h" #include #include +#include "base_object-inl.h" #include "util.h" namespace node { @@ -13,6 +14,8 @@ const std::vector& ExternalReferenceRegistry::external_references() { } ExternalReferenceRegistry::ExternalReferenceRegistry() { + this->Register(BaseObject::LazilyInitializedJSTemplateConstructor); + #define V(modname) _register_external_reference_##modname(this); EXTERNAL_REFERENCE_BINDING_LIST(V) #undef V