How to bind window global object? #3238
-
Hi, sorry this maybe a stupid question, but I am stuck for several weeks already.
It fail to cast to SDK object and I am not sure why it cannot do it. Can someone suggest something please? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It's possible to get it to work the way you're trying to do it, getting the You can import methods from use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = SDK)]
fn start();
}
pub fn execute() {
if js_sys::Reflect::has(&js_sys::global(), &JsValue::from_str("SDK")).unwrap() {
log!("SDK exists");
start();
} else {
log!("SDK does not exists");
}
} Calling |
Beta Was this translation helpful? Give feedback.
It's possible to get it to work the way you're trying to do it, getting the
SDK
fromjs_sys::global
and then casting it, but I'd recommend doing it this way instead.You can import methods from
SDK
by addingjs_namespace = SDK
to a regular function import:Calling
start
will runSDK.start()
on the JS side.