From 20e871f676ad6c8660fa94c21478eb1c5e0a2b97 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 14 Jan 2019 13:13:38 -0800 Subject: [PATCH] Fix an issue where closure rewriting required class internals Surfaced through previous sanity-checking commits, this reorders some internal operations to... Closes #1174 --- crates/cli-support/src/js/mod.rs | 3 +-- tests/wasm/closures.js | 5 +++++ tests/wasm/closures.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index fa34d795a..12d3fd6ec 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -171,8 +171,6 @@ impl<'a> Context<'a> { } pub fn finalize(&mut self, module_name: &str) -> Result<(String, String), Error> { - self.write_classes()?; - self.bind("__wbindgen_object_clone_ref", &|me| { me.expose_get_object(); me.expose_add_heap_object(); @@ -429,6 +427,7 @@ impl<'a> Context<'a> { })?; closures::rewrite(self).with_context(|_| "failed to generate internal closure shims")?; + self.write_classes()?; self.unexport_unused_internal_exports(); // Handle the `start` function, if one was specified. If we're in a diff --git a/tests/wasm/closures.js b/tests/wasm/closures.js index e1048bedc..641d56a6a 100644 --- a/tests/wasm/closures.js +++ b/tests/wasm/closures.js @@ -1,4 +1,5 @@ const assert = require('assert'); +const wasm = require('wasm-bindgen-test'); exports.works_call = a => { a(); @@ -97,3 +98,7 @@ exports.drop_during_call_save = f => { DROP_DURING_CALL = f; }; exports.drop_during_call_call = () => DROP_DURING_CALL(); + +exports.js_test_closure_returner = () => { + wasm.closure_returner().someKey(); +}; diff --git a/tests/wasm/closures.rs b/tests/wasm/closures.rs index 443d270e6..8e8fb5cf2 100644 --- a/tests/wasm/closures.rs +++ b/tests/wasm/closures.rs @@ -63,6 +63,8 @@ extern "C" { fn drop_during_call_save(a: &Closure); fn drop_during_call_call(); + + fn js_test_closure_returner(); } #[wasm_bindgen_test] @@ -279,3 +281,33 @@ fn drop_during_call_ok() { assert!(HIT); } } + +#[wasm_bindgen_test] +fn test_closure_returner() { + type ClosureType = FnMut() -> BadStruct; + + use js_sys::{Object, Reflect}; + use wasm_bindgen::JsCast; + + js_test_closure_returner(); + + #[wasm_bindgen] + pub struct ClosureHandle(Closure); + + #[wasm_bindgen] + pub struct BadStruct {} + + #[wasm_bindgen] + pub fn closure_returner() -> Result { + + let o = Object::new(); + + let some_fn = Closure::wrap(Box::new(move || BadStruct {}) as Box); + Reflect::set(&o, &JsValue::from("someKey"), &some_fn.as_ref().unchecked_ref()) + .unwrap(); + Reflect::set(&o, &JsValue::from("handle"), &JsValue::from(ClosureHandle(some_fn))) + .unwrap(); + + Ok(o) + } +}