@@ -96,6 +96,7 @@ Typical ways of accessing the current `Isolate` in the Node.js code are:
9696 using ` args.GetIsolate() ` .
9797* Given a [ ` Context ` ] [ ] , using ` context->GetIsolate() ` .
9898* Given a [ ` Environment ` ] [ ] , using ` env->isolate() ` .
99+ * Given a [ ` Realm ` ] [ ] , using ` realm->isolate() ` .
99100
100101### V8 JavaScript values
101102
@@ -264,19 +265,25 @@ heap. Node.js exposes this ability through the [`vm` module][].
264265V8 refers to each of these global objects and their associated builtins as a
265266`Context`.
266267
267- Currently, in Node.js there is one main `Context` associated with an
268- [`Environment`][] instance, and most Node.js features will only work inside
269- that context. (The only exception at the time of writing are
270- [`MessagePort`][] objects.) This restriction is not inherent to the design of
271- Node.js, and a sufficiently committed person could restructure Node.js to
272- provide built-in modules inside of `vm.Context`s.
268+ Currently, in Node.js there is one main `Context` associated with the
269+ principal [`Realm`][] of an [`Environment`][] instance, and a number of
270+ subsidiary `Context`s that are created with `vm.Context` or associated with
271+ [`ShadowRealm`][].
272+
273+ Most Node.js features will only work inside a context associated with a
274+ `Realm`. The only exception at the time of writing are [`MessagePort`][]
275+ objects. This restriction is not inherent to the design of Node.js, and a
276+ sufficiently committed person could restructure Node.js to provide built-in
277+ modules inside of `vm.Context`s.
273278
274279Often, the `Context` is passed around for [exception handling][].
275280Typical ways of accessing the current `Context` in the Node.js code are:
276281
277282* Given an [`Isolate`][], using `isolate->GetCurrentContext()`.
278283* Given an [`Environment`][], using `env->context()` to get the `Environment`'s
279- main context.
284+ principal [`Realm`][]'s context.
285+ * Given a [`Realm`][], using `realm->context()` to get the `Realm`'s
286+ context.
280287
281288<a id="event-loop"></a>
282289
@@ -303,15 +310,11 @@ Currently, every `Environment` class is associated with:
303310
304311* One [event loop][]
305312* One [`Isolate`][]
306- * One main [`Context `][]
313+ * One principal [`Realm `][]
307314
308315The `Environment` class contains a large number of different fields for
309- different Node.js modules, for example a libuv timer for `setTimeout()` or
310- the memory for a `Float64Array` that the `fs` module uses for storing data
311- returned from a `fs.stat()` call.
312-
313- It also provides [cleanup hooks][] and maintains a list of [`BaseObject`][]
314- instances.
316+ different built-in modules that can be shared across different `Realm`
317+ instances, for example, the inspector agent, async hooks info.
315318
316319Typical ways of accessing the current `Environment` in the Node.js code are:
317320
@@ -325,6 +328,45 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
325328* Given an [`Isolate`][], using `Environment::GetCurrent(isolate)`. This looks
326329 up the current [`Context`][] and then uses that.
327330
331+ <a id="realm"></a>
332+
333+ ### `Realm`
334+
335+ The `Realm` class is a container for a set of JavaScript objects and functions
336+ that are associated with a particular [ECMAScript realm][].
337+
338+ Each ECMAScript realm comes with a global object and a set of intrinsic
339+ objects. An ECMAScript realm has a `[[HostDefined]]` field, which represents
340+ the Node.js [`Realm`][] object.
341+
342+ Every `Realm` instance is created for a particular [`Context`][]. A `Realm`
343+ can be a principal realm or a synthetic realm. A principal realm is created
344+ for each `Environment`'s main [`Context`][]. A synthetic realm is created
345+ for the [`Context`][] of each [`ShadowRealm`][] constructed from the JS API. No
346+ `Realm` is created for the [`Context`][] of a `vm.Context`.
347+
348+ Native bindings and built-in modules can be evaluated in either a principal
349+ realm or a synthetic realm.
350+
351+ The `Realm` class contains a large number of different fields for
352+ different built-in modules, for example the memory for a `Uint32Array` that
353+ the `url` module uses for storing data returned from a
354+ `urlBinding.update()` call.
355+
356+ It also provides [cleanup hooks][] and maintains a list of [`BaseObject`][]
357+ instances.
358+
359+ Typical ways of accessing the current `Realm` in the Node.js code are:
360+
361+ * Given a `FunctionCallbackInfo` for a [binding function][],
362+ using `Realm::GetCurrent(args)`.
363+ * Given a [`BaseObject`][], using `realm()` or `self->realm()`.
364+ * Given a [`Context`][], using `Realm::GetCurrent(context)`.
365+ This requires that `context` has been associated with the `Realm`
366+ instance, e.g. is the principal `Realm` for the `Environment`.
367+ * Given an [`Isolate`][], using `Realm::GetCurrent(isolate)`. This looks
368+ up the current [`Context`][] and then uses its `Realm`.
369+
328370<a id="isolate-data"></a>
329371
330372### `IsolateData`
@@ -509,7 +551,7 @@ implement them. Otherwise, add the id and the class name to the
509551// In the HTTP parser source code file:
510552class BindingData : public BaseObject {
511553 public:
512- BindingData(Environment * env , Local<Object > obj) : BaseObject(env , obj) {}
554+ BindingData(Realm * realm , Local<Object > obj) : BaseObject(realm , obj) {}
513555
514556 SET_BINDING_ID(http_parser_binding_data)
515557
@@ -525,7 +567,7 @@ static void New(const FunctionCallbackInfo<Value>& args) {
525567 new Parser(binding_data, args.This());
526568}
527569
528- // ... because the initialization function told the Environment to store the
570+ // ... because the initialization function told the Realm to store the
529571// BindingData object:
530572void InitializeHttpParser(Local<Object > target,
531573 Local<Value > unused,
@@ -710,11 +752,13 @@ any resources owned by it, e.g. memory or libuv requests/handles.
710752
711753#### Cleanup hooks
712754
713- Cleanup hooks are provided that run before the [ ` Environment ` ] [ ]
714- is destroyed. They can be added and removed through by using
755+ Cleanup hooks are provided that run before the [ ` Environment ` ] [ ] or the
756+ [ ` Realm ` ] [ ] is destroyed. They can be added and removed by using
715757` env->AddCleanupHook(callback, hint); ` and
716- ` env->RemoveCleanupHook(callback, hint); ` , where callback takes a ` void* hint `
717- argument.
758+ ` env->RemoveCleanupHook(callback, hint); ` , or
759+ ` realm->AddCleanupHook(callback, hint); ` and
760+ ` realm->RemoveCleanupHook(callback, hint); ` respectively, where callback takes
761+ a ` void* hint ` argument.
718762
719763Inside these cleanup hooks, new asynchronous operations _ may_ be started on the
720764event loop, although ideally that is avoided as much as possible.
@@ -776,7 +820,7 @@ need to be tied together. `BaseObject` is the main abstraction for that in
776820Node.js, and most classes that are associated with JavaScript objects are
777821subclasses of it. It is defined in [ ` base_object.h ` ] [ ] .
778822
779- Every ` BaseObject ` is associated with one [ ` Environment ` ] [ ] and one
823+ Every ` BaseObject ` is associated with one [ ` Realm ` ] [ ] and one
780824` v8::Object ` . The ` v8::Object ` needs to have at least one [ internal field] [ ]
781825that is used for storing the pointer to the C++ object. In order to ensure this,
782826the V8 ` SetInternalFieldCount() ` function is usually used when setting up the
@@ -1038,6 +1082,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10381082
10391083[ C++ coding style ] : ../doc/contributing/cpp-style-guide.md
10401084[ Callback scopes ] : #callback-scopes
1085+ [ ECMAScript realm ] : https://tc39.es/ecma262/#sec-code-realms
10411086[ JavaScript value handles ] : #js-handles
10421087[ N-API ] : https://nodejs.org/api/n-api.html
10431088[ `BaseObject` ] : #baseobject
@@ -1050,7 +1095,9 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10501095[ `Local` ] : #local-handles
10511096[ `MakeCallback()` ] : #makecallback
10521097[ `MessagePort` ] : https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport
1098+ [ `Realm` ] : #realm
10531099[ `ReqWrap` ] : #reqwrap
1100+ [ `ShadowRealm` ] : https://github.com/tc39/proposal-shadowrealm
10541101[ `async_hooks` module ] : https://nodejs.org/api/async_hooks.html
10551102[ `async_wrap.h` ] : async_wrap.h
10561103[ `base_object.h` ] : base_object.h
0 commit comments