Skip to content

Commit

Permalink
vm: make ContextifyContext template context-independent
Browse files Browse the repository at this point in the history
Instead of creating an object template for every ContextifyContext,
we now create one object template that can be reused by all
contexts. The native pointer can be obtained through an embdder
pointer field in the creation context of the receiver in the
interceptors, because the interceptors are only meant to be invoked
on the global object of the contextified contexts. This makes
the ContextifyContext template context-independent and therefore
snapshotable.

PR-URL: #44252
Refs: #44014
Refs: #37476
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
joyeecheung authored and RafaelGSS committed Sep 5, 2022
1 parent 65c1f40 commit bce827e
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 113 deletions.
11 changes: 10 additions & 1 deletion src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "memory_tracker-inl.h"
#include "node_buffer.h"
#include "node_context_data.h"
#include "node_contextify.h"
#include "node_errors.h"
#include "node_internals.h"
#include "node_options-inl.h"
Expand Down Expand Up @@ -429,6 +430,8 @@ void IsolateData::CreateProperties() {
#undef V

// TODO(legendecas): eagerly create per isolate templates.
set_contextify_global_template(
contextify::ContextifyContext::CreateGlobalTemplate(isolate_));
}

IsolateData::IsolateData(Isolate* isolate,
Expand Down Expand Up @@ -497,13 +500,19 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {

void Environment::AssignToContext(Local<v8::Context> context,
const ContextInfo& info) {
ContextEmbedderTag::TagNodeContext(context);
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
this);
// Used to retrieve bindings
context->SetAlignedPointerInEmbedderData(
ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));

// ContextifyContexts will update this to a pointer to the native object.
context->SetAlignedPointerInEmbedderData(
ContextEmbedderIndex::kContextifyContext, nullptr);

// This must not be done before other context fields are initialized.
ContextEmbedderTag::TagNodeContext(context);

#if HAVE_INSPECTOR
inspector_agent()->ContextCreated(context, info);
#endif // HAVE_INSPECTOR
Expand Down
3 changes: 2 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class NoArrayBufferZeroFillScope {
V(nistcurve_string, "nistCurve") \
V(node_string, "node") \
V(nsname_string, "nsname") \
V(object_string, "Object") \
V(ocsp_request_string, "OCSPRequest") \
V(oncertcb_string, "oncertcb") \
V(onchange_string, "onchange") \
Expand Down Expand Up @@ -477,6 +478,7 @@ class NoArrayBufferZeroFillScope {
V(binding_data_ctor_template, v8::FunctionTemplate) \
V(blob_constructor_template, v8::FunctionTemplate) \
V(blocklist_constructor_template, v8::FunctionTemplate) \
V(contextify_global_template, v8::ObjectTemplate) \
V(compiled_fn_entry_template, v8::ObjectTemplate) \
V(dir_instance_template, v8::ObjectTemplate) \
V(fd_constructor_template, v8::ObjectTemplate) \
Expand Down Expand Up @@ -560,7 +562,6 @@ class NoArrayBufferZeroFillScope {
V(primordials_safe_weak_set_prototype_object, v8::Object) \
V(promise_hook_handler, v8::Function) \
V(promise_reject_callback, v8::Function) \
V(script_data_constructor_function, v8::Function) \
V(snapshot_serialize_callback, v8::Function) \
V(snapshot_deserialize_callback, v8::Function) \
V(snapshot_deserialize_main, v8::Function) \
Expand Down
7 changes: 6 additions & 1 deletion src/node_context_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ namespace node {
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
#endif

#ifndef NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX
#define NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX 37
#endif

// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
// check on the number of embedder data fields can assure the presence of all
// embedder indexes.
#ifndef NODE_CONTEXT_TAG
#define NODE_CONTEXT_TAG 37
#define NODE_CONTEXT_TAG 38
#endif

enum ContextEmbedderIndex {
Expand All @@ -46,6 +50,7 @@ enum ContextEmbedderIndex {
kBindingListIndex = NODE_BINDING_LIST_INDEX,
kAllowCodeGenerationFromStrings =
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX,
kContextTag = NODE_CONTEXT_TAG,
};

Expand Down
Loading

0 comments on commit bce827e

Please sign in to comment.