Skip to content

Commit df9ef76

Browse files
committed
src: document the Node.js context emebdder data
1 parent 3036066 commit df9ef76

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/node_context_data.h

+71
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,85 @@ namespace node {
4747
#define NODE_CONTEXT_TAG 39
4848
#endif
4949

50+
/**
51+
* These are indexes used for to set Node.js-specific data in the V8 contexts
52+
* via v8::Context::SetAlignedPointerInEmbedderData() (for pointers) and
53+
* v8::Context::SetEmbedderData() (for v8::Values).
54+
*
55+
* There are five types of contexts in Node.js:
56+
* 1. Default V8 context, with nothing Node.js-specific. This is normally only
57+
* created by the embedders that uses v8::Context APIs directly and is not
58+
* available through the Node.js JS APIs. Its context snapshot in the
59+
* built-in v8 startup snapshot is stored using
60+
* v8::SnapshotCreator::SetDefaultContext()
61+
* - effectively at index 0.
62+
* 2. Default Node.js main context. When Node.js is launched typically there is
63+
* a main thread on which a main node::Environment is created. The
64+
* Environment is associated with its own v8::Isolate (env->isolate()) and a
65+
* main v8::Context (env->context()). The corresponding data structure is
66+
* node::NodeMainInstance and the Environment is created in
67+
* node::NodeMainInstance::Run(). Its context snapshot in the built-in V8
68+
* startup snapshot is stored at node::SnapshotData::kNodeMainContextIndex.
69+
* 3. Node.js worker context. When a Worker instance is created via
70+
* new worker_threads.Worker(), it gets its own OS thread, its
71+
* own node::Environment, its own v8::Isolate and its own v8::Context.
72+
* The corresponding data structure is node::Worker and the Environment
73+
* is created in node::NodeMainInstance::Run().
74+
* Its context snapshot in the built-in V8 startup snapshot is stored at
75+
* node::SnapshotData::kNodeBaseContextIndex.
76+
* 4. Contextified vm context: When a contextified context is created via
77+
* vm.createContext() or other related vm APIs, the vm.Context instance
78+
* gets its own v8::Context. It shares the thread, the v8::Isolate and
79+
* the node::Environment with the context where the vm APIs are called.
80+
* The corresponding data structure is node::ContextifyContext and
81+
* the initialization code is in node::ContextifyContext::New().
82+
* Its context snapshot in the built-in V8 startup snapshot is stored at
83+
* node::SnapshotData::kNodeVMContextIndex.
84+
* 5. ShadowRealm context: When a JS ShadowRealm is created via new ShadowRealm,
85+
* it gets its own v8::Context. It also shares the thread, the v8::Isolate
86+
* and the node::Environment with the context where the ShadowRealm
87+
* constructor is called. The corresponding data structure is
88+
* node::ShadowRealm and the initialization code is in
89+
* node::ShadowRealm::New().
90+
*/
5091
enum ContextEmbedderIndex {
92+
// Pointer to the node::Environment associated with the context. Only set for
93+
// context type 2-5. Used by Environment::GetCurrent(context) to retrieve
94+
// the node::Environment associated with any Node.js context in the
95+
// V8 callbacks.
5196
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
97+
// A v8::Value which is the sandbox object used to create the contextified vm
98+
// context. For example the first argument of vm.createContext().
99+
// Only set for context type 4 and used in ContextifyContext methods.
52100
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
101+
// A v8::Value indicating whether the context allows WebAssembly code
102+
// generation.
103+
// Only set for context type 2-5, and for them the default is v8::True.
104+
// For context type 4 it's configurable via options.codeGeneration.wasm in the
105+
// vm APIs. Used in the default v8::AllowWasmCodeGenerationCallback.
53106
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
107+
// A v8::Value indicating whether the context allows code generation via
108+
// eval() or new Function().
109+
// Only set for context type 2-5, and for them the default is v8::True.
110+
// For context type 4 it's configurable via options.codeGeneration.strings in
111+
// the
112+
// vm APIs. Used in the default v8::AllowCodeGenerationFromStringsCallback.
54113
kAllowCodeGenerationFromStrings =
55114
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
115+
// Pointer to the node::ContextifyContext associated with vm.Contexts.
116+
// Only set for context type 4. Used by ContextifyContext::Get() to get
117+
// to the ContextifyContext from the property interceptors.
56118
kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX,
119+
// Pointer to the node::Realm associated with the context.
120+
// Only set for context type 2-3 and 5. For context type 2-3, it points to a
121+
// node::PrincipalRealm. For context type 5 it points to a node::ShadowRealm.
122+
// Used by Realm::GetCurrent(context) to retrieve the associated node::Realm
123+
// with any Node.js context in the V8 callbacks.
57124
kRealm = NODE_CONTEXT_REALM_INDEX,
125+
// Pointer to a constant address which is
126+
// node::ContextEmbedderTag::kNodeContextTagPtr.
127+
// Only set for context type 2-5. Used by ContextEmbedderTag::IsNodeContext()
128+
// to check whether a context is a Node.js context.
58129
kContextTag = NODE_CONTEXT_TAG,
59130
};
60131

0 commit comments

Comments
 (0)