You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2f0dc38
Browse filesBrowse the repository at this point in the historyBrowse files
A private WeakMap maps every canary that stands for a key to that key, so
lookups of canaries make no native call, and a value missing there was never
a key. The runtime does not call into JS for each canary it builds for a
keyed object: it counts them in a shared Int32Array, and the first lookup
that misses while the count is nonzero takes them all in one call. The key
lives on the object's canary record, where that call reads it, so keying an
object no longer gives it a state.
keyFor rules out plain JS objects first with maybeCanary, a V8 fast call, so
set, add, register and new WeakRef make no native call for them. Each WeakRef
keeps a native WeakRef to its canary for deref. A weakCollectionNativeCalls
counter counts the calls into the runtime.
Copy file name to clipboardExpand all lines: docs/ns-builtin-modules.md
+20-12Lines changed: 20 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,7 +104,7 @@ wrapper.
104
104
|`setConfig(key, value)`| Sets a runtime config key. Throws `TypeError` on an unknown key, an invalid value, or (for process-wide keys) when called from a worker isolate. |
105
105
|`getConfig(key)`| Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. |
106
106
|`getWrapperLifetimeStats()`| Wrapper lifetime counters for the calling isolate (see [Wrapper lifetime](#wrapper-lifetime)). A fresh object on every call. |
107
-
|`getWrapperClassCounts()`| Per Java class name (`<unknown>` for a link whose class was never recorded), the calling isolate's live `wrappers`, the `canaries` among them and the canary `states`, which include those made only for a weak-collection key (see [Wrapper lifetime](#wrapper-lifetime)). |
107
+
|`getWrapperClassCounts()`| Per Java class name (`<unknown>` for a link whose class was never recorded), the calling isolate's live `wrappers`, the `canaries` among them and the canary `states` (see [Wrapper lifetime](#wrapper-lifetime)). |
108
108
|`escapeException(x)`| Returns an `Error` branded so that, when it is thrown back across a native callback boundary, the native caller receives the original native exception `x` carries rather than the runtime's wrapper exception. Idempotent: a branded error is returned unchanged. Throws `TypeError` when called with no argument. The global `interop.escapeException` is this same function (see [Legacy globals](#legacy-globals)). |
109
109
110
110
*Platform note (Android):* the original exception is the Java throwable `x`
@@ -189,16 +189,18 @@ every build on cold paths only:
189
189
|`collected`| wrappers released because Java had collected their object |
190
190
|`releasedExplicitly`| links released through `__releaseNativeCounterpart`|
191
191
|`releasedAccesses`| lookups of the Java object of a wrapper whose link was released or whose Java object Java had already collected: calling its methods, using its fields or passing it to Java. Resolving an overloaded method looks its arguments up too, so one call can count more than once |
192
-
|`canaryStates`|`"canary"` only: state objects created to hold JS properties, or to hold the key a weak collection made for the object, in which case no JS property was written|
192
+
|`canaryStates`|`"canary"` only: state objects created to hold JS properties |
193
193
|`canaryNativeDeaths`|`"canary"` only: state objects let go of because Java collected their object |
194
194
|`canaryInterceptedWrites`|`"canary"` only: writes to canaries the write handler saw. Canaries without state have no handler |
195
195
|`canaryCachedWrites`|`"canary"` only: of those writes, rewrites of a name the handler knew to be a plain data property of the state, mirrored with one store |
196
196
|`canaryRebuilds`|`"canary"` only: canaries built anew for a JS-extended instance whose previous canary died while Java kept the object |
197
+
|`weakCollectionNativeCalls`|`"canary"` only: calls the weak collections make into the runtime: to make the key of a canary's Java object, to find the canary for a key, and to learn about canaries built for keyed objects since the last such call. Lookups of a value that was never a key make none |
197
198
198
199
In `"canary"` mode only array buffers backed by Java buffers resurrect;
199
200
`weakened` counts objects held weakly because their canary died while they
200
-
had a record (JS state, or a JS-extended instance), and `collected` counts
201
-
canaries whose object had none, which Java no longer holds for JS.
201
+
had a record (JS state, a weak-collection key, or a JS-extended instance),
202
+
and `collected` counts canaries whose object had none, which Java no longer
203
+
holds for JS.
202
204
203
205
`"canary"` is the resurrection-free model the iOS runtime and the Node-API
204
206
runtimes share. Every wrapper of a Java object is a canary: a phantom-weak JS
@@ -273,21 +275,27 @@ on it, and lives as long as it does.
273
275
old one without keeping it, so in canary mode a builtin
274
276
(`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, `WeakRef` and
275
277
`FinalizationRegistry` with subclasses that key a Java object whose wrapper is
276
-
a canary by an opaque, frozen key its state holds. Entries and targets
277
-
therefore live exactly as long as the Java object; `WeakRef.prototype.deref`
278
+
a canary by an opaque, frozen key the object's record holds. Entries and
279
+
targets therefore live exactly as long as the Java object; `WeakRef.prototype.deref`
278
280
(and the `get` alias) returns the live canary, a new one with the object's
279
281
state, or `undefined` once Java collected the object. `set`, `add`,
280
-
`getOrInsert`, `new WeakRef` and `register` create the key; lookups never do,
281
-
so probing an object gives it no state. A Proxy around a Java object stays a
282
+
`getOrInsert`, `new WeakRef` and `register` create the key; lookups never do.
283
+
Neither gives the object a state. A Proxy around a Java object stays a
282
284
key of its own, and every other value passes through to the standard
283
285
collection. The cost is the other side of the same rooting: a `WeakMap` value
284
-
that references its Java key keeps the key, its state and so the Java object
286
+
that references its Java key keeps the key, its record and so the Java object
285
287
alive for as long as the map lives. And `deref` builds a canary, which holds
286
288
the Java object strongly until it dies again, so a `WeakRef` to a Java object
287
289
that is dereferenced on every frame or event may keep the object from ever
288
-
being collected. In canary mode, an operation on an object costs a native
289
-
call, and one that can create the key also checks its receiver first so that
290
-
a call that fails leaves no key behind; that check names `has` (or
290
+
being collected. In canary mode, an operation that can create the key costs a
291
+
native call for a Java object the collection does not hold yet; a plain JS
292
+
object costs only a V8 fast call that rules it out. Lookups (`get`, `has`,
293
+
`delete`, `unregister`, `deref`) make no native call, except a `deref` that
294
+
has to build a canary, and the first lookup that misses after the runtime
295
+
built canaries for keyed objects, which makes one call for all of them. An
296
+
operation that can create the
297
+
key also checks its receiver first so that a call that fails leaves no key
298
+
behind; that check names `has` (or
291
299
`unregister`) in its `TypeError`. The collections are subclasses of the
292
300
standard ones, which shows: `Object.getPrototypeOf(WeakMap.prototype)` is the
293
301
standard prototype, and `WeakMap.toString()` is not native code.
0 commit comments