Skip to content

Commit

Permalink
src: register external references of BaseObject for snapshot
Browse files Browse the repository at this point in the history
PR-URL: nodejs#39961
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
joyeecheung committed Sep 6, 2021
1 parent b9ed7e5 commit afc7760
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@ bool BaseObject::IsWeakOrDetached() const {
return pd->wants_weak_jsobj || pd->is_detached;
}

void BaseObject::LazilyInitializedJSTemplateConstructor(
const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
}

v8::Local<v8::FunctionTemplate>
BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
auto constructor = [](const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
};

v8::Local<v8::FunctionTemplate> t = env->NewFunctionTemplate(constructor);
v8::Local<v8::FunctionTemplate> t =
env->NewFunctionTemplate(LazilyInitializedJSTemplateConstructor);
t->Inherit(BaseObject::GetConstructorTemplate(env));
t->InstanceTemplate()->SetInternalFieldCount(
BaseObject::kInternalFieldCount);
Expand Down
2 changes: 2 additions & 0 deletions src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Value>& args);
static inline BaseObject* FromJSObject(v8::Local<v8::Value> object);
template <typename T>
static inline T* FromJSObject(v8::Local<v8::Value> object);
Expand Down
3 changes: 3 additions & 0 deletions src/node_external_reference.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "node_external_reference.h"
#include <cinttypes>
#include <vector>
#include "base_object-inl.h"
#include "util.h"

namespace node {
Expand All @@ -13,6 +14,8 @@ const std::vector<intptr_t>& ExternalReferenceRegistry::external_references() {
}

ExternalReferenceRegistry::ExternalReferenceRegistry() {
this->Register(BaseObject::LazilyInitializedJSTemplateConstructor);

#define V(modname) _register_external_reference_##modname(this);
EXTERNAL_REFERENCE_BINDING_LIST(V)
#undef V
Expand Down

0 comments on commit afc7760

Please sign in to comment.