diff --git a/lib/api/src/js/trap.rs b/lib/api/src/js/trap.rs index 64fc56774c6..e4565ed9440 100644 --- a/lib/api/src/js/trap.rs +++ b/lib/api/src/js/trap.rs @@ -57,6 +57,8 @@ impl Trap { #[wasm_bindgen] impl Trap { + /// A marker method to indicate that an object is an instance of the `Trap` + /// class. pub fn __wbg_wasmer_trap() {} } @@ -108,17 +110,29 @@ fn downcast_from_ptr(value: &JsValue) -> Option { let class = prototype.constructor(); let key = JsValue::from_str("__wbg_wasmer_trap"); - let _marker_func: js_sys::Function = Reflect::get(&class, &key) + let marker_func: Option = Reflect::get(&class, &key) .and_then(|v: JsValue| v.dyn_into()) - .ok()?; + .ok(); - // Note: this assumes the wrapper class generated by #[wasm_bindgen] will - // always store the pointer in a field called "__wbg_ptr". This is valid - // as of wasm-bindgen version 0.2.87 - let key = JsValue::from_str("__wbg_ptr"); - let ptr = Reflect::get(value, &key).ok().and_then(|v| v.as_f64())?; + if marker_func.is_none() { + // We couldn't find the marker, so it's something else. + return None; + } - // Safety: The marker function exists, therefore it's safe to cast back to a trap. + // Note: this assumes the wrapper class generated by #[wasm_bindgen] will + // always have a `__destroy_into_raw()` method which consumes the `Trap` + // wrapper and returns a pointer. + // + // This is valid as of wasm-bindgen version 0.2.87 + let key = JsValue::from_str("__destroy_into_raw"); + let ptr = Reflect::get(value, &key) + .ok() + .and_then(|v| v.dyn_into::().ok()) + .and_then(|destroy_into_raw| destroy_into_raw.call0(value).ok()) + .and_then(|ret| ret.as_f64())?; + + // Safety: The marker function exists, therefore it's safe to convert back + // to a Trap. unsafe { Some(::from_abi( ptr as u32, diff --git a/lib/wasi-web/Cargo.lock b/lib/wasi-web/Cargo.lock index 9eb55fa06b0..d8441bdfbd4 100644 --- a/lib/wasi-web/Cargo.lock +++ b/lib/wasi-web/Cargo.lock @@ -2339,29 +2339,6 @@ dependencies = [ "wasm-bindgen-shared", ] -[[package]] -name = "wasm-bindgen-downcast" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dac026d43bcca6e7ce1c0956ba68f59edf6403e8e930a5d891be72c31a44340" -dependencies = [ - "js-sys", - "once_cell", - "wasm-bindgen", - "wasm-bindgen-downcast-macros", -] - -[[package]] -name = "wasm-bindgen-downcast-macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5020cfa87c7cecefef118055d44e3c1fc122c7ec25701d528ee458a0b45f38f" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "wasm-bindgen-futures" version = "0.4.37" @@ -2453,7 +2430,6 @@ dependencies = [ "target-lexicon", "thiserror", "wasm-bindgen", - "wasm-bindgen-downcast", "wasmer-compiler", "wasmer-derive", "wasmer-types",