Skip to content

Commit ee97b64

Browse files
committed
docs(runtime): weak collections: released canaries, polled WeakRefs, counts
- A released canary keeps what weak collections keyed on its object. - A WeakRef to a Java object dereferenced on every frame or event builds a canary each time its wrapper died, holding the object strongly, so it may never clear. - canaryStates and getWrapperClassCounts().states include states made only to hold a weak-collection key. - The receiver check names has/unregister in its TypeError, and the collections show that they are subclasses.
1 parent 7e4f548 commit ee97b64

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

‎docs/ns-builtin-modules.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ wrapper.
104104
| `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. |
105105
| `getConfig(key)` | Returns the current value of a config key. Throws `TypeError` on an unknown key. Readable from any isolate. |
106106
| `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` (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`, which include those made only for a weak-collection key (see [Wrapper lifetime](#wrapper-lifetime)). |
108108
| `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)). |
109109

110110
*Platform note (Android):* the original exception is the Java throwable `x`
@@ -189,7 +189,7 @@ every build on cold paths only:
189189
| `collected` | wrappers released because Java had collected their object |
190190
| `releasedExplicitly` | links released through `__releaseNativeCounterpart` |
191191
| `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 |
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 |
193193
| `canaryNativeDeaths` | `"canary"` only: state objects let go of because Java collected their object |
194194
| `canaryInterceptedWrites` | `"canary"` only: writes to canaries the write handler saw. Other wrappers have no handler |
195195
| `canaryRebuilds` | `"canary"` only: canaries built anew for a JS-extended instance whose previous canary died while Java kept the object |
@@ -256,7 +256,9 @@ no prototype change to the handler). Native `#private` fields are branded on
256256
the canary and do not carry over.
257257
A canary built after another died is a new JS object, which `===` cannot tell
258258
because nothing kept the old one. `__releaseNativeCounterpart` drops the
259-
object's state with its link.
259+
object's state with its link, and the released canary is a plain JS object
260+
from then on: what the weak collections keyed on its Java object stays keyed
261+
on it, and lives as long as it does.
260262

261263
**Weak collections.** What could notice a new canary is whatever keys on the
262264
old one without keeping it, so in canary mode a builtin
@@ -271,8 +273,15 @@ so probing an object gives it no state. A Proxy around a Java object stays a
271273
key of its own, and every other value passes through to the standard
272274
collection. The cost is the other side of the same rooting: a `WeakMap` value
273275
that references its Java key keeps the key, its state and so the Java object
274-
alive for as long as the map lives. Each operation costs one native call in
275-
canary mode, whatever the key.
276+
alive for as long as the map lives. And `deref` builds a canary, which holds
277+
the Java object strongly until it dies again, so a `WeakRef` to a Java object
278+
that is dereferenced on every frame or event may keep the object from ever
279+
being collected. In canary mode, an operation on an object costs a native
280+
call, and one that can create the key also checks its receiver first so that
281+
a call that fails leaves no key behind; that check names `has` (or
282+
`unregister`) in its `TypeError`. The collections are subclasses of the
283+
standard ones, which shows: `Object.getPrototypeOf(WeakMap.prototype)` is the
284+
standard prototype, and `WeakMap.toString()` is not native code.
276285

277286
Remote-module security (`security.allowRemoteModules`,
278287
`security.remoteModuleAllowlist`) is **not** part of this surface. Those

0 commit comments

Comments
 (0)