Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
79e5284
feat(runtime): weak collections key Java objects in canary mode
edusperoni Nov 20, 2026
8355bea
test(runtime): weak collections across a real wrapper change, plain o…
edusperoni Nov 20, 2026
9ec742d
test(runtime): give the polling weak collection specs a timeout past …
edusperoni Nov 20, 2026
7df60a8
refactor(runtime): keep the canary members in their original order
edusperoni Nov 20, 2026
6c767af
fix(runtime): a released canary keeps what weak collections keyed on it
edusperoni Nov 20, 2026
e1501b0
fix(runtime): weak collections validate before they make a key
edusperoni Nov 20, 2026
7f3d068
docs(runtime): weak collections: released canaries, polled WeakRefs, …
edusperoni Nov 20, 2026
fe9d162
test(runtime): weak collection specs for constructors, subclasses, re…
edusperoni Nov 20, 2026
1f0ceff
perf(runtime): weak collections look a value up as is before unwrappi…
edusperoni Nov 21, 2026
c94ad4d
test(runtime): weak collections key canaries rebuilt from a shell by …
edusperoni Nov 21, 2026
f9e7a09
perf(runtime): weak collections learn rebuilt keyed canaries lazily
edusperoni Nov 22, 2026
cc2e77d
test(runtime): weak collections on plain canaries without state
edusperoni Nov 22, 2026
39b704c
docs(runtime): a lookup miss can pay for learning rebuilt keyed canaries
edusperoni Nov 22, 2026
72597fa
perf(runtime): keep one queued rekey per id when the queue is pruned
edusperoni Nov 22, 2026
b395518
test(runtime): FinalizationRegistry polling specs name their registry
edusperoni Nov 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,13 @@ every build on cold paths only:
| `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 |
| `canaryCachedFieldWrites` | `"canary"` only: of those writes, Java field writes the handler placed from an earlier write of the same name through an object with the same prototype, without looking the name up along the prototype chain |
| `canaryRebuilds` | `"canary"` only: canaries built anew for a JS-extended instance whose previous canary died while Java kept the object |
| `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. A lookup of a value that was never a key makes none of its own, but the first lookup that misses after the runtime built such canaries, whatever value it looks up, makes that last call |

In `"canary"` mode only array buffers backed by Java buffers resurrect;
`weakened` counts objects held weakly because their canary died while they
had a record (JS state, or a JS-extended instance), and `collected` counts
canaries whose object had none, which Java no longer holds for JS.
had a record (JS state, a weak-collection key, or a JS-extended instance),
and `collected` counts canaries whose object had none, which Java no longer
holds for JS.

`"canary"` is the resurrection-free model the iOS runtime and the Node-API
runtimes share. Every wrapper of a Java object is a canary: a phantom-weak JS
Expand Down Expand Up @@ -237,7 +239,9 @@ their JS properties:
object for JS; when Java hands the object back later, its wrapper is a new
object without the old one's JS properties. JS properties on plain native
objects are therefore not preserved under `"extended"`: keep such state on a
JS object of your own, or use `"all"`, which gives every wrapper state.
JS object of your own, such as a `WeakMap` keyed by the native object (in
canary mode its entries live exactly as long as the Java object, see below),
or use `"all"`, which gives every wrapper state.
Array buffers backed by Java buffers keep the `"resurrecting"` lifetime under
either scope.

Expand Down Expand Up @@ -265,9 +269,39 @@ overrides either) is linked as a plain wrapper: under `"extended"`, a canary
built later for one of its objects has the Java class's prototype, not
`X.prototype`, and none of the old canary's JS properties.
A canary built after another died is a new JS object, which `===` cannot tell
because nothing kept the old one, but which keys `WeakMap`, `WeakSet`,
`WeakRef` and `FinalizationRegistry` entries separately.
`__releaseNativeCounterpart` drops the object's state with its link.
because nothing kept the old one. `__releaseNativeCounterpart` drops the
object's state with its link, and the released canary is a plain JS object
from then on: what the weak collections keyed on its Java object stays keyed
on it, and lives as long as it does.

**Weak collections.** What could notice a new canary is whatever keys on the
old one without keeping it, so in canary mode a builtin
(`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`, `WeakRef` and
`FinalizationRegistry` with subclasses that key a Java object whose wrapper is
a canary by an opaque, frozen key the object's record holds. Entries and
targets therefore live exactly as long as the Java object; `WeakRef.prototype.deref`
(and the `get` alias) returns the live canary, a new one with the object's
state, or `undefined` once Java collected the object. `set`, `add`,
`getOrInsert`, `new WeakRef` and `register` create the key; lookups never do.
Neither gives the object a state. A Proxy around a Java object stays a
key of its own, and every other value passes through to the standard
collection. The cost is the other side of the same rooting: a `WeakMap` value
that references its Java key keeps the key, its record and so the Java object
alive for as long as the map lives. And `deref` builds a canary, which holds
the Java object strongly until it dies again, so a `WeakRef` to a Java object
that is dereferenced on every frame or event may keep the object from ever
being collected. In canary mode, an operation that can create the key costs a
native call for a Java object the collection does not hold yet; a plain JS
object costs only a V8 fast call that rules it out. Lookups (`get`, `has`,
`delete`, `unregister`, `deref`) make no native call, except a `deref` that
has to build a canary, and the first lookup that misses after the runtime
built canaries for keyed objects, which makes one call for all of them. An
operation that can create the
key also checks its receiver first so that a call that fails leaves no key
behind; that check names `has` (or
`unregister`) in its `TypeError`. The collections are subclasses of the
standard ones, which shows: `Object.getPrototypeOf(WeakMap.prototype)` is the
standard prototype, and `WeakMap.toString()` is not native code.

Remote-module security (`security.allowRemoteModules`,
`security.remoteModuleAllowlist`) is **not** part of this surface. Those
Expand Down
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require("./tests/testWrapperLifetimeModes");
require("./tests/testWrapperLifetimeStateWrites");
require("./tests/testWrapperLifetimeFieldWrites");
require("./tests/testWrapperLifetimeRecordSharing");
require("./tests/testWrapperLifetimeWeakCollections");
require("./tests/testsMemoryManagement");
require("./tests/testFieldGetSet");
require("./tests/extendedClassesTests");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe("Wrapper lifetime", function () {
var stats = runtime.getWrapperLifetimeStats();
expect(Object.keys(stats).sort()).toEqual(
["canaryCachedFieldWrites", "canaryCachedWrites", "canaryInterceptedWrites", "canaryNativeDeaths",
"canaryRebuilds", "canaryStates", "collected", "releasedAccesses", "releasedExplicitly", "resurrections", "weakened"]);
"canaryRebuilds", "canaryStates", "collected", "releasedAccesses", "releasedExplicitly", "resurrections",
"weakCollectionNativeCalls", "weakened"]);
Object.keys(stats).forEach(function (key) {
expect(typeof stats[key]).toBe("number");
});
Expand Down
Loading