@@ -265,12 +265,16 @@ heap. Node.js exposes this ability through the [`vm` module][].
265
265
V8 refers to each of these global objects and their associated builtins as a
266
266
`Context`.
267
267
268
- Currently, in Node.js there is one main `Context` associated with a
269
- [`Realm`][] instance, and most Node.js features will only work inside
270
- that context. (The only exception at the time of writing are
271
- [`MessagePort`][] objects.) This restriction is not inherent to the design of
272
- Node.js, and a sufficiently committed person could restructure Node.js to
273
- 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.
274
278
275
279
Often, the `Context` is passed around for [exception handling][].
276
280
Typical ways of accessing the current `Context` in the Node.js code are:
@@ -310,7 +314,7 @@ Currently, every `Environment` class is associated with:
310
314
311
315
The `Environment` class contains a large number of different fields for
312
316
different built-in modules that can be shared across different `Realm`
313
- instances, for example a libuv timer for `setTimeout()` .
317
+ instances, for example, the inspector agent, async hooks info .
314
318
315
319
Typical ways of accessing the current `Environment` in the Node.js code are:
316
320
@@ -329,20 +333,25 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
329
333
### `Realm`
330
334
331
335
The `Realm` class is a container for a set of JavaScript objects and functions
332
- that are associated with a particular ECMAScript global environment .
336
+ that are associated with a particular [ ECMAScript realm][] .
333
337
334
- Every `Realm` instance is associated with a [`Context`][].
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.
335
341
336
- A `Realm` can be a principal realm or a synthetic realm. A principal realm is
337
- created with an `Environment` as its principal global environment to evaluate
338
- scripts. A synthetic realm is created with JS APIs like `ShadowRealm`.
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`.
339
347
340
348
Native bindings and built-in modules can be evaluated in either a principal
341
349
realm or a synthetic realm.
342
350
343
351
The `Realm` class contains a large number of different fields for
344
- different built-in modules, for example the memory for a `Float64Array` that
345
- the `fs` module uses for storing data returned from a `fs.stat()` call.
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.
346
355
347
356
It also provides [cleanup hooks][] and maintains a list of [`BaseObject`][]
348
357
instances.
@@ -356,7 +365,7 @@ Typical ways of accessing the current `Realm` in the Node.js code are:
356
365
This requires that `context` has been associated with the `Realm`
357
366
instance, e.g. is the principal `Realm` for the `Environment`.
358
367
* Given an [`Isolate`][], using `Realm::GetCurrent(isolate)`. This looks
359
- up the current [`Context`][] and then uses that .
368
+ up the current [`Context`][] and then uses its `Realm` .
360
369
361
370
<a id="isolate-data"></a>
362
371
@@ -1073,6 +1082,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
1073
1082
1074
1083
[ C++ coding style ] : ../doc/contributing/cpp-style-guide.md
1075
1084
[ Callback scopes ] : #callback-scopes
1085
+ [ ECMAScript realm ] : https://tc39.es/ecma262/#sec-code-realms
1076
1086
[ JavaScript value handles ] : #js-handles
1077
1087
[ N-API ] : https://nodejs.org/api/n-api.html
1078
1088
[ `BaseObject` ] : #baseobject
@@ -1087,6 +1097,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
1087
1097
[ `MessagePort` ] : https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport
1088
1098
[ `Realm` ] : #realm
1089
1099
[ `ReqWrap` ] : #reqwrap
1100
+ [ `ShadowRealm` ] : https://github.com/tc39/proposal-shadowrealm
1090
1101
[ `async_hooks` module ] : https://nodejs.org/api/async_hooks.html
1091
1102
[ `async_wrap.h` ] : async_wrap.h
1092
1103
[ `base_object.h` ] : base_object.h
0 commit comments