Stdweb: Exposing Javascript functions to Rust


Stdweb has examples for how to call Rust-functions from Javascript in paragraph titled Exposing Rust functions to JavaScript, but how do you expose your custom Javascript functions to Rust?

Mozilla WebAssembly documentation defines that Javascript functions can be imported into wasm using importObject definition, but stdweb generates the glue javascript file, where the importObject contains the stdweb supported WEB API functions (as well as Rust exported functions).

So one possible solution I can see for this is, that you can export a Rust-function which takes a Javascript function as a function parameter. The function parameter value is stored into some Rust internal storage and is called when the javascript functionality is required.

Is it possible to do that kind of handling or is there a better way?


I think this demo is what you’re looking for:

It includes the JS “FFI” in the importObject of WebAssembly.instantiate


My question is actually about stdweb-library, which unfortunately hides the WebAssembly.instantiate() call to its generated javascript file.

I created a similar test app as you linked and managed to make it work with simple primitive types. Wasm doesn’t support byte arrays or strings as function parameters which stdweb has implemented with complex memory logic for exported Rust-functions. I was hoping I could use similar methods for calling JS-functions from Rust.