Skip to content

Use FinalizationRegistry to auto-deinit Closures #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nmn opened this issue Jun 1, 2021 · 3 comments
Closed

Use FinalizationRegistry to auto-deinit Closures #131

nmn opened this issue Jun 1, 2021 · 3 comments

Comments

@nmn
Copy link

nmn commented Jun 1, 2021

There is a new-ish JS API called FinalizationRegistry that lets you add a finalization callback to values in Javascript that gets run sometime after a value is Garbage Collected in Javascript.

Together with WeakRef, this API could be used to automatically release/deinit JSClosures which currently need to managed manually.

Here's an article describing it's use within JS: https://v8.dev/features/weak-references
Another article describing it's use for Wasm/Wasi: https://rob-blackbourn.github.io/blog/webassembly/wasm/wasi/javascript/c/clang/wasi-sdk/marshalling/finalizer/finalizationregistry/2020/07/07/wasi-finalizers-1.html

There is probably some cost associated with using this API, so I don't think that all JSClosures should have this behaviour. Frameworks such as Tokamak can probably handle event listeners just fine, but in other cases it would make things much easier.

The feature is supported in all modern browsers (including Safari 14.1 +)

@carson-katri
Copy link
Member

@j-f1 has a PR to implement this I believe at #128

@j-f1
Copy link
Member

j-f1 commented Jun 1, 2021

Ooh interesting idea: I could restore the old closure behavior as JSUnmanagedClosure, with the plain JSClosure throwing if it’s unsupported.

@j-f1
Copy link
Member

j-f1 commented Sep 10, 2021

Implemented in #128

@j-f1 j-f1 closed this as completed Sep 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants