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.
  • Loading branch information
joyeecheung committed Aug 16, 2022
1 parent 46f6d22 commit 3b50a7c
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 114 deletions.
5 changes: 5 additions & 0 deletions 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 @@ -444,6 +445,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 @@ -771,6 +774,8 @@ void Environment::InitializeMainContext(Local<Context> context,
const EnvSerializeInfo* env_info) {
context_.Reset(context->GetIsolate(), context);
AssignToContext(context, ContextInfo(""));
context->SetAlignedPointerInEmbedderData(
ContextEmbedderIndex::kContextifyContext, nullptr);
if (env_info != nullptr) {
DeserializeProperties(env_info);
} else {
Expand Down
9 changes: 5 additions & 4 deletions 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 Expand Up @@ -1487,6 +1488,9 @@ class Environment : public MemoryRetainer {
template <typename T>
void ForEachBaseObject(T&& iterator);

static void* const kNodeContextTagPtr;
static int const kNodeContextTag;

private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
const char* errmsg);
Expand Down Expand Up @@ -1573,9 +1577,6 @@ class Environment : public MemoryRetainer {
uint64_t thread_id_;
std::unordered_set<worker::Worker*> sub_worker_contexts_;

static void* const kNodeContextTagPtr;
static int const kNodeContextTag;

#if HAVE_INSPECTOR
std::unique_ptr<inspector::Agent> inspector_agent_;
bool is_in_inspector_console_call_ = false;
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 @@ -33,14 +33,19 @@ namespace node {
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 37
#endif

#ifndef NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX
#define NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX 38
#endif

enum ContextEmbedderIndex {
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
kContextTag = NODE_CONTEXT_TAG,
kBindingListIndex = NODE_BINDING_LIST_INDEX,
kAllowCodeGenerationFromStrings =
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX
};

} // namespace node
Expand Down
Loading

0 comments on commit 3b50a7c

Please sign in to comment.