Fix an issue where closure rewriting required class internals

Surfaced through previous sanity-checking commits, this reorders some
internal operations to...

Closes #1174
This commit is contained in:
Alex Crichton 2019-01-14 13:13:38 -08:00
parent b21489368c
commit 20e871f676
3 changed files with 38 additions and 2 deletions

View File

@ -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

View File

@ -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();
};

View File

@ -63,6 +63,8 @@ extern "C" {
fn drop_during_call_save(a: &Closure<Fn()>);
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<ClosureType>);
#[wasm_bindgen]
pub struct BadStruct {}
#[wasm_bindgen]
pub fn closure_returner() -> Result<Object, JsValue> {
let o = Object::new();
let some_fn = Closure::wrap(Box::new(move || BadStruct {}) as Box<ClosureType>);
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)
}
}