Skip to content

Commit 653ad3a

Browse files
committed
docs(runtime): weak collections under canary mode
1 parent 121ac83 commit 653ad3a

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎docs/knowledge/v8-resurrecting-finalizers.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,12 @@ rooted at canary death instead, because a state reachable only through the canar
268268
the same collection. The cost: state that references its own object (a closure over `this`)
269269
keeps it alive. A canary built after a death is a new object, but `===` cannot tell: only one
270270
canary lives at a time, and anything still referencing the old one would have kept it alive.
271-
What does notice is whatever keys on the canary without keeping it: `WeakMap` keys and `WeakRef`s
272-
of the old canary, and native `#private` fields, which are branded on it. Other
271+
What could notice is whatever keys on the canary without keeping it. The weak collections
272+
don't: in canary mode a builtin (`js/weak-collections.js`) replaces `WeakMap`, `WeakSet`,
273+
`WeakRef` and `FinalizationRegistry` with subclasses that key a native object by an opaque,
274+
frozen key held by its state, so entries and targets live exactly as long as the native object,
275+
and `WeakRef.prototype.deref` returns the live canary, a new one, or `undefined` once the object
276+
is gone. Native `#private` fields are branded on the canary and do not carry over. Other
273277
limits: `Object.freeze`, `seal` and `preventExtensions` throw on a canary (V8 refuses them on
274278
objects with interceptors); index-keyed properties are not forwarded and die with the canary;
275279
a write through an object whose prototype is a canary lands in the canary's state, since

‎types/ns-runtime.d.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ declare module "ns:runtime" {
5454
* - `"canary"`: no resurrection. A wrapper in scope dies with its JS
5555
* references; JS properties set on it live on a state object kept until
5656
* the native object is deallocated, and a wrapper built for the object
57-
* later has them again. Weak references to the old wrapper (WeakMap keys,
58-
* WeakRef) and native `#private` fields do not carry over.
57+
* later has them again. WeakMap, WeakSet, WeakRef and FinalizationRegistry
58+
* key native objects by their lifetime in this mode, so they carry over
59+
* too; native `#private` fields do not.
5960
* State that references its own object keeps that object alive.
6061
*/
6162
wrapperLifetimeMode: "resurrecting" | "rooted" | "canary";

0 commit comments

Comments
 (0)