Skip to content

Commit bce827e

Browse files
joyeecheungRafaelGSS
authored andcommitted
vm: make ContextifyContext template context-independent
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>
1 parent 65c1f40 commit bce827e

6 files changed

+162
-113
lines changed

src/env.cc

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "memory_tracker-inl.h"
77
#include "node_buffer.h"
88
#include "node_context_data.h"
9+
#include "node_contextify.h"
910
#include "node_errors.h"
1011
#include "node_internals.h"
1112
#include "node_options-inl.h"
@@ -429,6 +430,8 @@ void IsolateData::CreateProperties() {
429430
#undef V
430431

431432
// TODO(legendecas): eagerly create per isolate templates.
433+
set_contextify_global_template(
434+
contextify::ContextifyContext::CreateGlobalTemplate(isolate_));
432435
}
433436

434437
IsolateData::IsolateData(Isolate* isolate,
@@ -497,13 +500,19 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
497500

498501
void Environment::AssignToContext(Local<v8::Context> context,
499502
const ContextInfo& info) {
500-
ContextEmbedderTag::TagNodeContext(context);
501503
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
502504
this);
503505
// Used to retrieve bindings
504506
context->SetAlignedPointerInEmbedderData(
505507
ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));
506508

509+
// ContextifyContexts will update this to a pointer to the native object.
510+
context->SetAlignedPointerInEmbedderData(
511+
ContextEmbedderIndex::kContextifyContext, nullptr);
512+
513+
// This must not be done before other context fields are initialized.
514+
ContextEmbedderTag::TagNodeContext(context);
515+
507516
#if HAVE_INSPECTOR
508517
inspector_agent()->ContextCreated(context, info);
509518
#endif // HAVE_INSPECTOR

src/env.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ class NoArrayBufferZeroFillScope {
349349
V(nistcurve_string, "nistCurve") \
350350
V(node_string, "node") \
351351
V(nsname_string, "nsname") \
352+
V(object_string, "Object") \
352353
V(ocsp_request_string, "OCSPRequest") \
353354
V(oncertcb_string, "oncertcb") \
354355
V(onchange_string, "onchange") \
@@ -477,6 +478,7 @@ class NoArrayBufferZeroFillScope {
477478
V(binding_data_ctor_template, v8::FunctionTemplate) \
478479
V(blob_constructor_template, v8::FunctionTemplate) \
479480
V(blocklist_constructor_template, v8::FunctionTemplate) \
481+
V(contextify_global_template, v8::ObjectTemplate) \
480482
V(compiled_fn_entry_template, v8::ObjectTemplate) \
481483
V(dir_instance_template, v8::ObjectTemplate) \
482484
V(fd_constructor_template, v8::ObjectTemplate) \
@@ -560,7 +562,6 @@ class NoArrayBufferZeroFillScope {
560562
V(primordials_safe_weak_set_prototype_object, v8::Object) \
561563
V(promise_hook_handler, v8::Function) \
562564
V(promise_reject_callback, v8::Function) \
563-
V(script_data_constructor_function, v8::Function) \
564565
V(snapshot_serialize_callback, v8::Function) \
565566
V(snapshot_deserialize_callback, v8::Function) \
566567
V(snapshot_deserialize_main, v8::Function) \

src/node_context_data.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ namespace node {
3232
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
3333
#endif
3434

35+
#ifndef NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX
36+
#define NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX 37
37+
#endif
38+
3539
// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
3640
// check on the number of embedder data fields can assure the presence of all
3741
// embedder indexes.
3842
#ifndef NODE_CONTEXT_TAG
39-
#define NODE_CONTEXT_TAG 37
43+
#define NODE_CONTEXT_TAG 38
4044
#endif
4145

4246
enum ContextEmbedderIndex {
@@ -46,6 +50,7 @@ enum ContextEmbedderIndex {
4650
kBindingListIndex = NODE_BINDING_LIST_INDEX,
4751
kAllowCodeGenerationFromStrings =
4852
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
53+
kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX,
4954
kContextTag = NODE_CONTEXT_TAG,
5055
};
5156

0 commit comments

Comments
 (0)