Files
scriptc/docs/content/limitations/weak-collections-and-memory.mdx
T
Chris Tate 83d426f9be Support FinalizationRegistry with bookkeeping and no cleanup callbacks
wasm-bindgen output constructs a FinalizationRegistry per exported class
when its module loads, so every wasm-bindgen package stopped at load with
SC2020. `new FinalizationRegistry(cleanup)` now creates a native registry:
- register(target, held, token) and unregister(token) check their
  arguments and answer as Node does, with Node's TypeError messages;
- the unregister tokens are keys of an internal weak map, so the registry
  keeps no token alive;
- the cleanup callback never runs, which ECMAScript allows, so the
  registry keeps neither the callback nor the held values.
The global answers typeof "function", instanceof, Object.prototype.toString
and util.inspect as Node does. Symbol targets and tokens are refused with
SC2020.

`"encodeInto" in encoder` on a TextEncoder, which wasm-bindgen's loaders
test at load, answered false because the encoder is a brand record without
fields for its methods. `in` over such a builtin brand record now answers
from the members the standard library declares, plus Object.prototype's.

The limitations page says that memory a package frees only from a cleanup
callback, such as the WebAssembly memory behind wasm-bindgen wrappers the
program drops without free(), is not reclaimed until the program exits.

Fixes #1678.
2026-10-22 12:14:23 -05:00

19 lines
1.5 KiB
Plaintext

`WeakMap` and `WeakSet` accept checked native objects and arrays, functions, numeric typed arrays, ArrayBuffers, native weak collections, and typed records, arrays, tuples, and classes that preserve identity through checked storage.
Copied values, other native handles, and symbol keys are unsupported.
Constructor seeds support arrays; custom iterators and detached methods are unsupported.
Weak collections do not retain their keys, but values that refer back to a key can keep it alive until deletion or collection release.
`FinalizationRegistry` keeps its registrations: `register` and `unregister` check their arguments and answer as in Node.js.
Its cleanup callbacks never run, which the ECMAScript specification allows.
Program output matches Node.js, which runs cleanup callbacks only when its garbage collector decides to.
Memory that a package frees only from a cleanup callback is not reclaimed until the program exits.
For example, a wasm-bindgen package frees the WebAssembly memory behind a wrapper object from its cleanup callback, so a long-running server that drops such wrappers without calling their `free()` method keeps that memory.
Call `free()` on wrappers the program no longer needs.
Symbol targets and unregister tokens are unsupported.
`WeakRef` is unsupported.
Memory uses reference counting.
Acyclic values are freed deterministically, and cycles are collected at defined collection points.
Native checked objects, arrays, Sets, and captured closures participate in cycle collection.
Cycles through opaque native capsules can remain allocated.