Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36bf069

Browse filesBrowse files
legendecasMoLow
authored andcommitted
src: add Realm document in the src README.md
PR-URL: #47932 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 6a82fbd commit 36bf069
Copy full SHA for 36bf069

File tree

Expand file treeCollapse file tree

1 file changed

+68
-21
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+68
-21
lines changed
Open diff view settings
Collapse file

‎src/README.md‎

Copy file name to clipboardExpand all lines: src/README.md
+68-21Lines changed: 68 additions & 21 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -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][].
264265
V8 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
274279
Often, the `Context` is passed around for [exception handling][].
275280
Typical 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
308315
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.
315318
316319
Typical 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`
@@ -495,7 +537,7 @@ be `cares_wrap`).
495537
// In the HTTP parser source code file:
496538
class BindingData : public BaseObject {
497539
public:
498-
BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {}
540+
BindingData(Realm* realm, Local<Object> obj) : BaseObject(realm, obj) {}
499541

500542
static constexpr FastStringKey type_name { "http_parser" };
501543

@@ -511,7 +553,7 @@ static void New(const FunctionCallbackInfo<Value>& args) {
511553
new Parser(binding_data, args.This());
512554
}
513555

514-
// ... because the initialization function told the Environment to store the
556+
// ... because the initialization function told the Realm to store the
515557
// BindingData object:
516558
void InitializeHttpParser(Local<Object> target,
517559
Local<Value> unused,
@@ -702,11 +744,13 @@ any resources owned by it, e.g. memory or libuv requests/handles.
702744

703745
#### Cleanup hooks
704746

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
707749
`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.
710754

711755
Inside these cleanup hooks, new asynchronous operations _may_ be started on the
712756
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
768812
Node.js, and most classes that are associated with JavaScript objects are
769813
subclasses of it. It is defined in [`base_object.h`][].
770814

771-
Every `BaseObject` is associated with one [`Environment`][] and one
815+
Every `BaseObject` is associated with one [`Realm`][] and one
772816
`v8::Object`. The `v8::Object` needs to have at least one [internal field][]
773817
that is used for storing the pointer to the C++ object. In order to ensure this,
774818
the V8 `SetInternalFieldCount()` function is usually used when setting up the
@@ -1030,6 +1074,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10301074

10311075
[C++ coding style]: ../doc/contributing/cpp-style-guide.md
10321076
[Callback scopes]: #callback-scopes
1077+
[ECMAScript realm]: https://tc39.es/ecma262/#sec-code-realms
10331078
[JavaScript value handles]: #js-handles
10341079
[N-API]: https://nodejs.org/api/n-api.html
10351080
[`BaseObject`]: #baseobject
@@ -1042,7 +1087,9 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
10421087
[`Local`]: #local-handles
10431088
[`MakeCallback()`]: #makecallback
10441089
[`MessagePort`]: https://nodejs.org/api/worker_threads.html#worker_threads_class_messageport
1090+
[`Realm`]: #realm
10451091
[`ReqWrap`]: #reqwrap
1092+
[`ShadowRealm`]: https://github.com/tc39/proposal-shadowrealm
10461093
[`async_hooks` module]: https://nodejs.org/api/async_hooks.html
10471094
[`async_wrap.h`]: async_wrap.h
10481095
[`base_object.h`]: base_object.h

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.