Wasm-bindgen: Exporting imported Rust struct to JS

Hello,

So, I have been experimenting with the awesome Rust + WASM lately. I have managed to put THIS and THIS together. These small apps use the code in THIS SIMULATION ENGINE. I am loving it, and I am trying to make it more official/stable/modular/reusable.

My initial thought was to create a separate crate dedicated to WASM, importing objects and exporting them. Something like

// This struct (and others) have several methods. For example, 
// getters and setters are produced by a `derive` macro.
use simple_model::model::SimpleModel

// Export this struct and all its methods
#[wasm_bindgen]
SimpleModel

Is this possible? Or do I need to go and modify the original library?

Thanks a lot!

You have to modify the original library. The wasm_bindgen macro only "sees" the literal tokens of the item you apply it to, so it can't do anything useful unless you put it on the actual definition of the type.

1 Like