How to use WebAssembly::instantiate_buffer to import a wasm module from another wasm module

I've looked at the js examples for WebAssembly.instantiate and the values given for the importObjects parameter, but I'm still confused as to what this importObjects parameter is supposed to be. In my case I'm trying to create a wasm module that wraps some js, so I can use it inside my other wasm modules. The first set of code I show below is my wrapper wasm module. As you can see I import Irys and the new constructor and export a function called connect_to_irys. Then the next code is my failing attempt at importing this wrapper module into another wasm module. I get the error "Import #0 module="./wasm_game_of_life_bg.js": module is not an object or function" at the line WebAssembly::instantiate_buffer.
For this sample code what is the importObjects parameter supposed to look like? If my wrapper module is already importing this code from a js file why do I need to import it again from an importing wasm module?

the wrapper module

#[wasm_bindgen(module = "/src/irys_interface.js")]
extern "C" {
    pub type Irys; // this type and the constructor come from js code defined in the irys_interface.js file
    #[wasm_bindgen(constructor)]
    pub fn new(url: Option<String>, token: Option<String>, key: Option<String>) -> Irys;
}

#[wasm_bindgen]
pub fn connect_to_irys() -> Irys {
    let irys = Irys::new(None, None, None);
    //println!("irys {}", irys);
    irys
}

The module importing the wrapper module:

let a = JsFuture::from(WebAssembly::instantiate_buffer(WASM, &Object::new())).await.unwrap();
let b: WebAssembly::Instance = Reflect::get(&a, &"instance".into()).unwrap().dyn_into().unwrap();
let c = b.exports();
let connect_to_irys = Reflect::get(c.as_ref(), &"connect_to_irys".into()).unwrap()
            .dyn_into::<Function>()
            .expect("connect_to_irys export wasn't a function");
let irys = connect_to_irys.call0(&JsValue::undefined());
log!("irys {:?}", irys);

When using wasm-bindgen, wasm-bindgen will generate some javascript which internally calls WebAssembly.instantiate and passes an importObject. You have to use this javascript code rather than calling instantiate yourself. Only the wasm-bindgen tool knows the exact imports that the wasm-bindgen library expects.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.