Commit Graph

17 Commits

Author SHA1 Message Date
Anna Henningsen
60a58f63a1 src: mark realm leaf classes final
Since these classes use virtual methods extensively, adding `final`
should allow compilers to optimize accesses better.

PR-URL: https://github.com/nodejs/node/pull/59355
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
2025-08-07 12:59:40 +02:00
Joyee Cheung
d2a13695bf src: track cppgc wrappers with a list in Realm
This allows us to perform cleanups of cppgc wrappers that rely on a
living Realm during Realm shutdown. Otherwise the cleanup may happen
during object destruction, which can be triggered by GC after Realm
shutdown, leading to invalid access to Realm.

The general pattern for this type of non-trivial destruction is
designed to be:

```
class MyWrap final : CPPGC_MIXIN(MyWrap) {
    public:
    ~MyWrap() { this->Finalize(); }
    void Clean(Realm* realm) override {
        // Do cleanup that relies on a living Realm. This would be
        // called by CppgcMixin::Finalize() first during Realm
        // shutdown, while the Realm is still alive. If the destructor
        // calls Finalize() again later during garbage collection that
        // happens after Realm shutdown, Clean() would be skipped,
        // preventing invalid access to the Realm.
    }
}
```

In addition, this allows us to trace external memory held by the
wrappers in the heap snapshots if we add synthethic edges between the
wrappers and other nodes in the embdder graph callback, or to perform
snapshot serialization for them.

PR-URL: https://github.com/nodejs/node/pull/56534
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-05-19 20:56:33 +00:00
Chengzhong Wu
84966703e0 src: track BaseObjects with an efficient list
PR-URL: https://github.com/nodejs/node/pull/55104
Refs: https://github.com/nodejs/node/pull/54880
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2024-09-26 18:40:33 +01:00
Joyee Cheung
6391b3b95d src: use per-realm GetBindingData() wherever applicable
This reduce the number of embedder slot accesses and also removes
the assumption in a few binding methods that the current realm is
the principal realm of the current environment (which is not true
for shadow realms).

PR-URL: https://github.com/nodejs/node/pull/49007
Refs: https://github.com/nodejs/node/pull/48836
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2023-08-16 02:32:50 +00:00
Joyee Cheung
faefe560ef src: remove ContextEmbedderIndex::kBindingDataStoreIndex
We can now get the binding data through the reference to the
realm directly, so remove it from the context embedder data
slot.

PR-URL: https://github.com/nodejs/node/pull/48836
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-07-26 13:26:08 +00:00
legendecas
ac0853c4ee src: make realm binding data store weak
The binding data must be weak so that it won't keep the realm reachable
from strong GC roots indefinitely. The wrapper object of binding data
should be referenced from JavaScript, thus the binding data should be
reachable throughout the lifetime of the realm.

PR-URL: https://github.com/nodejs/node/pull/47688
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-06-14 02:05:00 +00:00
Joyee Cheung
b68cedd4d8 src: make AliasedBuffers in the binding data weak
The binding data holds references to the AliasedBuffers directly
from their wrappers which already ensures that the AliasedBuffers
won't be accessed when the wrappers are GC'ed. So we can just
make the global references to the AliasedBuffers weak. This way
we can simply deserialize the typed arrays when deserialize the
binding data and avoid the extra Object::Set() calls. It also
eliminates the caveat in the JS land where aliased buffers must
be dynamically read from the binding.

PR-URL: https://github.com/nodejs/node/pull/47354
Refs: https://github.com/nodejs/node/issues/47353
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-04-20 03:28:35 +00:00
Joyee Cheung
583dcca226 src: track ShadowRealm native objects correctly in the heap snapshot
Previously the native ShadowRealm objects were never actually tracked
in the heap snapshot - the tracking starts with the Environment,
we don't call the tracking methods unless it's reachable natively from
the Environment. This patch adds a set in the Environment for tracking
shadow realms in the heap snapshot.

Drive-by: remove redundant MemoryInfo() overrides from the derived
classes of Realm and remove the tracking of `env` from
`Realm::MemoryInfo()` because the realms don't hold the Environment
alive (even for the principal realm, it's the other way around).

PR-URL: https://github.com/nodejs/node/pull/47389
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2023-04-06 00:25:09 +00:00
Joyee Cheung
c81e1143e4 bootstrap: store internal loaders in C++ via a binding
Instead of returning the internal loaders from the bootstrap script,
we can simply call a binding to store them in C++. This eliminates
the need for specializing the handling of this script.

PR-URL: https://github.com/nodejs/node/pull/47215
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-03-29 23:46:02 +08:00
Chengzhong Wu
e6b4d30a2f src: bootstrap Web [Exposed=*] APIs in the shadow realm
This is the initial work to bootstrap Web interfaces that are defined
with extended attributes `[Exposed=*]`.

The ShadowRealm instances are garbage-collected once it is
unreachable. However, V8 can not infer the reference cycles between
the per-realm strong persistent function handles and the realm's
context handle. To allow the context to be gc-ed once it is not
reachable, the per-realm persistent handles are attached to the
context's global object and the persistent handles are set as weak.

PR-URL: https://github.com/nodejs/node/pull/46809
Refs: https://github.com/nodejs/node/issues/42528
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-03-16 00:22:06 +08:00
Joyee Cheung
f32aa19400 src: use an array for faster binding data lookup
Locally the hashing of the binding names sometimes has significant
presence in the profile of bindings, because there can be collisions,
which makes the cost of adding a new binding data non-trivial,
but it's wasteful to spend time on hashing them or dealing with
collisions at all, since we can just use the EmbedderObjectType
enum as the key, as the string names are not actually used beyond
debugging purposes and can be easily matched with a macro.
And since we can just use the enum as the key, we do not even
need the map and can just use an array with the enum as indices
for the lookup.

PR-URL: https://github.com/nodejs/node/pull/46620
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2023-02-22 19:03:03 +01:00
Chengzhong Wu
ca07abab53 src: per-realm binding data
Binding data is inherited from BaseObject and created in a specific
realm. They need to be tracked on a per-realm basis so that they can
be released properly when a realm is disposed.

PR-URL: https://github.com/nodejs/node/pull/46556
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2023-02-21 22:43:25 +00:00
Shelley Vohr
cf105c4db5 src: remove unnecessary semicolons
PR-URL: https://github.com/nodejs/node/pull/46171
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
2023-01-13 07:52:56 +00:00
legendecas
43105220a7 src: define per-isolate internal bindings registration callback
Bindings that need to be loaded in distinct contexts of node::Realm in
the same node::Environment instance needs to share the per-isolate
templates.

Add a new binding registration callback, which is called with
per-IsolateData. This allows bindings to define templates and
share them across realms eagerly, and avoid accidental modification on
the templates when the per-context callback is called multiple times.

This also tracks loaded bindings and built-in modules with node::Realm.

PR-URL: https://github.com/nodejs/node/pull/45547
Refs: https://github.com/nodejs/node/issues/42528
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-12-28 11:55:14 +08:00
Chengzhong Wu
717465433c src: create BaseObject with node::Realm
BaseObject is a wrapper around JS objects. These objects should be
created in a node::Realm and destroyed when their associated realm is
cleaning up.

PR-URL: https://github.com/nodejs/node/pull/44348
Refs: https://github.com/nodejs/node/issues/42528
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2022-09-27 16:39:53 +08:00
Joyee Cheung
78508028e3 bootstrap: generate bootstrapper arguments in BuiltinLoader
PR-URL: https://github.com/nodejs/node/pull/44488
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2022-09-13 08:55:19 +00:00
Chengzhong Wu
81d9cdb8cd src: introduce node::Realm
To distinguish per-context values from the node::Environment, split
those values to a new node::Realm structure and consolidate
bootstrapping methods with it.

PR-URL: https://github.com/nodejs/node/pull/44179
Refs: https://github.com/nodejs/node/issues/42528
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2022-08-31 14:41:42 +00:00