diff --git a/guide/src/reference/weak-references.md b/guide/src/reference/weak-references.md index c8a48eca9ee..ab557191349 100644 --- a/guide/src/reference/weak-references.md +++ b/guide/src/reference/weak-references.md @@ -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 diff --git a/src/closure.rs b/src/closure.rs index c8b81431b70..04f688163b6 100644 --- a/src/closure.rs +++ b/src/closure.rs @@ -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` impl block to avoid every