wasm-by-example icon indicating copy to clipboard operation
wasm-by-example copied to clipboard

Need Rust example that shows how to import a custom function from JS

Open Zireael07 opened this issue 5 years ago • 1 comments

i.e. something I wrote in index.js, not something that's already available.

Zireael07 avatar Mar 07 '20 13:03 Zireael07

@Zireael07 Hello! :smile:

Could you give an example of what you are trying to do? If you mean, import a function that you would be defining I think you can do:

Javascript

window.myCustomJsFunctions = {
  myLog: message => console.log(message)
}

Rust

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = myCustomJsFunctions)]
    fn myLog(s: &str);
}

myLog("hello, console!");

I referenced this in the wasm-bindgen guide. But there may be a btter way to do this, but I am not sure how to :joy: If, this was what you were reffering to :smile: And I will add this to the docs later if this is what you meant :smile:

torch2424 avatar Mar 09 '20 05:03 torch2424