Skip to content

Commit a907870

Browse files
committed
fixup! src: add Realm document in the src README.md
1 parent 136cf14 commit a907870

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,16 @@ heap. Node.js exposes this ability through the [`vm` module][].
265265
V8 refers to each of these global objects and their associated builtins as a
266266
`Context`.
267267
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.
274278
275279
Often, the `Context` is passed around for [exception handling][].
276280
Typical ways of accessing the current `Context` in the Node.js code are:
@@ -310,7 +314,7 @@ Currently, every `Environment` class is associated with:
310314
311315
The `Environment` class contains a large number of different fields for
312316
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.
314318
315319
Typical ways of accessing the current `Environment` in the Node.js code are:
316320
@@ -329,20 +333,25 @@ Typical ways of accessing the current `Environment` in the Node.js code are:
329333
### `Realm`
330334
331335
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][].
333337
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.
335341
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`.
339347
340348
Native bindings and built-in modules can be evaluated in either a principal
341349
realm or a synthetic realm.
342350
343351
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.
346355
347356
It also provides [cleanup hooks][] and maintains a list of [`BaseObject`][]
348357
instances.
@@ -356,7 +365,7 @@ Typical ways of accessing the current `Realm` in the Node.js code are:
356365
This requires that `context` has been associated with the `Realm`
357366
instance, e.g. is the principal `Realm` for the `Environment`.
358367
* 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`.
360369
361370
<a id="isolate-data"></a>
362371
@@ -1073,6 +1082,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10731082

10741083
[C++ coding style]: ../doc/contributing/cpp-style-guide.md
10751084
[Callback scopes]: #callback-scopes
1085+
[ECMAScript realm]: https://tc39.es/ecma262/#sec-code-realms
10761086
[JavaScript value handles]: #js-handles
10771087
[N-API]: https://nodejs.org/api/n-api.html
10781088
[`BaseObject`]: #baseobject
@@ -1087,6 +1097,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10871097
[`MessagePort`]: https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport
10881098
[`Realm`]: #realm
10891099
[`ReqWrap`]: #reqwrap
1100+
[`ShadowRealm`]: https://github.com/tc39/proposal-shadowrealm
10901101
[`async_hooks` module]: https://nodejs.org/api/async_hooks.html
10911102
[`async_wrap.h`]: async_wrap.h
10921103
[`base_object.h`]: base_object.h

0 commit comments

Comments
 (0)