-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update support for weak references (#2248)
* Update support for weak referenes This commit updates the `WASM_BINDGEN_WEAKREF` feature support to the latest version of the weak references proposal in JS. This also updates the `Closure` type to use finalization registries to ensure closures are deallocated with the JS gc. This means that `Closure::forget` will not actually leak memory in a weakref-using application. * Add a flag for weak references
- Loading branch information
1 parent
60f3b1d
commit 664c3f8
Showing
11 changed files
with
144 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Support for Weak References | ||
|
||
By default wasm-bindgen does not uses the [TC39 weak references | ||
proposal](https://github.com/tc39/proposal-weakrefs). This proposal just | ||
advanced to stage 4 at the time of this writing, but it will still stake some | ||
time for support to percolate into all the major browsers. | ||
|
||
Without weak references your JS integration may be susceptible to memory leaks | ||
in Rust, for example: | ||
|
||
* You could forget to call `.free()` on a JS object, leaving the Rust memory | ||
allocated. | ||
* Rust closures converted to JS values (the `Closure` type) may not be executed | ||
and cleaned up. | ||
* Rust closures have a `Closure::forget` method which explicitly doesn't free | ||
the underlying memory. | ||
|
||
These issues are all solved with the weak references proposal in JS. The | ||
`--weak-refs` flag to the `wasm-bindgen` CLI will enable usage of | ||
`FinalizationRegistry` to ensure that all memory is cleaned up, regardless of | ||
whether it's explicitly deallocated or not. Note that explicit deallocation | ||
is always a possibility and supported, but if it's not called then memory will | ||
still be automatically deallocated with the `--weak-refs` flag. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters