This lets code compiled to linear memory wasm (like a C++ or Rust game engine) use the host GC instead of shipping its own, with the benefits of
- Not shipping a GC.
- Not needing to manage a shadow stack in userspace.
- Other host GC benefits, like existing optimzations there, integration with system memory pressure events, etc.
Atm the API is implemented in JS to use WeakRefs and FinalizationRegistry. As we need to call out to JS for those, using WasmGC for the rest of this library does not seem optimal. If WasmGC added weak references and finalization, we could move entirely to wasm.
See gc.h for the API, gc.js for the API implementation, and test.cpp for
an example.
echo "build"
~/Dev/emscripten/emcc test.cpp -I. --js-library gc.js
echo "run"
nodejs --expose_gc a.out.js
build
run
[iter 1]
new node
Node(42)
[iter 2]
unroot it to allow collection
[iter 3]
~Node(42)
[iter 4]
[iter 5]
another new node
Node(1337)
[iter 6]
link to itself (once linked, it can be collected)
[iter 7]
~Node(1337)
[iter 8]
[iter 9]
tree with 2 branches
Node(10)
Node(20)
Node(-1)
[iter 10]
[iter 11]
[iter 12]
[iter 13]
[iter 14]
[iter 15]
[iter 16]
unlink one arm, see collection
~Node(10)
[iter 17]
[iter 18]
unroot the root, see collection of it and the last branch
[iter 19]
~Node(-1)
~Node(20)
[iter 20]
[iter 21]
new node, which will never be collected
Node(99999)
[iter 22]
[iter 23]
[iter 24]
[iter 25]
[iter 26]
[iter 27]
[iter 28]
[iter 29]
[iter 30]
[iter 31]
[iter 32]