Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
src: register external references of BaseObject for snapshot
PR-URL: #39961
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
joyeecheung authored and BethGriggs committed Sep 21, 2021
1 parent 6fdf025 commit a2c1c3e
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
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
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
@@ -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 a2c1c3e

Please sign in to comment.