From ffe8ad52458fce0eda07303b65bc5229d579ecb1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 28 Jul 2020 13:15:03 -0700 Subject: [PATCH] Fix breakage from changing `Closure::forget` Add a new method for now instead of changing `Closure::forget` --- guide/src/reference/weak-references.md | 4 ++-- src/closure.rs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) 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