Skip to content

Commit

Permalink
Fix breakage from changing Closure::forget (#2258)
Browse files Browse the repository at this point in the history
Add a new method for now instead of changing `Closure::forget`
  • Loading branch information
alexcrichton committed Jul 28, 2020
1 parent ebc1e92 commit 1943e29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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

0 comments on commit 1943e29

Please sign in to comment.