Skip to content
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

Fix breakage from changing Closure::forget #2258

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions guide/src/reference/weak-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ in Rust, for example:
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.
* Rust closures have `Closure::{into_js_value,forget}` methods which explicitly
do not 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
Expand Down
7 changes: 6 additions & 1 deletion src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,16 @@ where
/// JS closure is GC'd. Weak references are not enabled by default since
/// they're still a proposal for the JS standard. They can be enabled with
/// `WASM_BINDGEN_WEAKREF=1` when running `wasm-bindgen`, however.
pub fn forget(self) -> JsValue {
pub fn into_js_value(self) -> JsValue {
let idx = self.js.idx;
mem::forget(self);
JsValue::_new(idx)
}

/// Same as `into_js_value`, but doesn't return a value.
pub fn forget(self) {
drop(self.into_js_value());
}
}

// NB: we use a specific `T` for this `Closure<T>` impl block to avoid every
Expand Down