@@ -96,6 +96,7 @@ Typical ways of accessing the current `Isolate` in the Node.js code are:
96
96
using ` args.GetIsolate() ` .
97
97
* Given a [ ` Context ` ] [ ] , using ` context->GetIsolate() ` .
98
98
* Given a [ ` Environment ` ] [ ] , using ` env->isolate() ` .
99
+ * Given a [ ` Realm ` ] [ ] , using ` realm->isolate() ` .
99
100
100
101
### V8 JavaScript values
101
102
@@ -264,19 +265,25 @@ heap. Node.js exposes this ability through the [`vm` module][].
264
265
V8 refers to each of these global objects and their associated builtins as a
265
266
`Context`.
266
267
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.
273
278
274
279
Often, the `Context` is passed around for [exception handling][].
275
280
Typical ways of accessing the current `Context` in the Node.js code are:
276
281
277
282
* Given an [`Isolate`][], using `isolate->GetCurrentContext()`.
278
283
* 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.
280
287
281
288
<a id="event-loop"></a>
282
289
@@ -303,15 +310,11 @@ Currently, every `Environment` class is associated with:
303
310
304
311
* One [event loop][]
305
312
* One [`Isolate`][]
306
- * One main [`Context `][]
313
+ * One principal [`Realm `][]
307
314
308
315
The `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.
315
318
316
319
Typical ways of accessing the current `Environment` in the Node.js code are:
317
320
@@ -325,6 +328,45 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
325
328
* Given an [`Isolate`][], using `Environment::GetCurrent(isolate)`. This looks
326
329
up the current [`Context`][] and then uses that.
327
330
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
+
328
370
<a id="isolate-data"></a>
329
371
330
372
### `IsolateData`
@@ -495,7 +537,7 @@ be `cares_wrap`).
495
537
// In the HTTP parser source code file:
496
538
class BindingData : public BaseObject {
497
539
public:
498
- BindingData(Environment * env , Local<Object > obj) : BaseObject(env , obj) {}
540
+ BindingData(Realm * realm , Local<Object > obj) : BaseObject(realm , obj) {}
499
541
500
542
static constexpr FastStringKey type_name { "http_parser" };
501
543
@@ -511,7 +553,7 @@ static void New(const FunctionCallbackInfo<Value>& args) {
511
553
new Parser(binding_data, args.This());
512
554
}
513
555
514
- // ... because the initialization function told the Environment to store the
556
+ // ... because the initialization function told the Realm to store the
515
557
// BindingData object:
516
558
void InitializeHttpParser(Local<Object > target,
517
559
Local<Value > unused,
@@ -702,11 +744,13 @@ any resources owned by it, e.g. memory or libuv requests/handles.
702
744
703
745
#### Cleanup hooks
704
746
705
- Cleanup hooks are provided that run before the [ ` Environment ` ] [ ]
706
- is destroyed. They can be added and removed through by using
747
+ Cleanup hooks are provided that run before the [ ` Environment ` ] [ ] or the
748
+ [ ` Realm ` ] [ ] is destroyed. They can be added and removed by using
707
749
` env->AddCleanupHook(callback, hint); ` and
708
- ` env->RemoveCleanupHook(callback, hint); ` , where callback takes a ` void* hint `
709
- argument.
750
+ ` env->RemoveCleanupHook(callback, hint); ` , or
751
+ ` realm->AddCleanupHook(callback, hint); ` and
752
+ ` realm->RemoveCleanupHook(callback, hint); ` respectively, where callback takes
753
+ a ` void* hint ` argument.
710
754
711
755
Inside these cleanup hooks, new asynchronous operations _ may_ be started on the
712
756
event loop, although ideally that is avoided as much as possible.
@@ -768,7 +812,7 @@ need to be tied together. `BaseObject` is the main abstraction for that in
768
812
Node.js, and most classes that are associated with JavaScript objects are
769
813
subclasses of it. It is defined in [ ` base_object.h ` ] [ ] .
770
814
771
- Every ` BaseObject ` is associated with one [ ` Environment ` ] [ ] and one
815
+ Every ` BaseObject ` is associated with one [ ` Realm ` ] [ ] and one
772
816
` v8::Object ` . The ` v8::Object ` needs to have at least one [ internal field] [ ]
773
817
that is used for storing the pointer to the C++ object. In order to ensure this,
774
818
the V8 ` SetInternalFieldCount() ` function is usually used when setting up the
@@ -1030,6 +1074,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
1030
1074
1031
1075
[ C++ coding style ] : ../doc/contributing/cpp-style-guide.md
1032
1076
[ Callback scopes ] : #callback-scopes
1077
+ [ ECMAScript realm ] : https://tc39.es/ecma262/#sec-code-realms
1033
1078
[ JavaScript value handles ] : #js-handles
1034
1079
[ N-API ] : https://nodejs.org/api/n-api.html
1035
1080
[ `BaseObject` ] : #baseobject
@@ -1042,7 +1087,9 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
1042
1087
[ `Local` ] : #local-handles
1043
1088
[ `MakeCallback()` ] : #makecallback
1044
1089
[ `MessagePort` ] : https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport
1090
+ [ `Realm` ] : #realm
1045
1091
[ `ReqWrap` ] : #reqwrap
1092
+ [ `ShadowRealm` ] : https://github.com/tc39/proposal-shadowrealm
1046
1093
[ `async_hooks` module ] : https://nodejs.org/api/async_hooks.html
1047
1094
[ `async_wrap.h` ] : async_wrap.h
1048
1095
[ `base_object.h` ] : base_object.h
0 commit comments