diff --git a/crates/cli-support/src/js/binding.rs b/crates/cli-support/src/js/binding.rs index f62bf1a17c9..643937635b9 100644 --- a/crates/cli-support/src/js/binding.rs +++ b/crates/cli-support/src/js/binding.rs @@ -124,15 +124,14 @@ impl<'a, 'b> Builder<'a, 'b> { drop(params.next()); if js.cx.config.debug { js.prelude( - "if (this.ptr == 0) throw new Error('Attempt to use a moved value');\n", + "if (this.ptr == 0) throw new Error('Attempt to use a moved value');", ); } if consumes_self { - js.prelude("var ptr = this.ptr;"); - js.prelude("this.ptr = 0;"); - js.args.push("ptr".to_string()); + js.prelude("const ptr = this.__destroy_into_raw();"); + js.args.push("ptr".into()); } else { - js.args.push("this.ptr".to_string()); + js.args.push("this.ptr".into()); } } None => {} diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index 537f2107911..614a5d12278 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -850,10 +850,15 @@ impl<'a> Context<'a> { dst.push_str(&format!( " - free() {{ + __destroy_into_raw() {{ const ptr = this.ptr; this.ptr = 0; {} + return ptr; + }} + + free() {{ + const ptr = this.__destroy_into_raw(); wasm.{}(ptr); }} ",