2

Importer module:

(module
    (type $GG (func (param i32) (result i32)))

       (import "low" "load" (func $load (param i32) (result i32)))
       (table (import "low" "table") 2 anyfunc)
       (func (export "func_0") (result i32) 
        (call_indirect $GG (i32.const 0) (i32.const 0))  
       )
)

Exporter module:

(module
  (table 2 anyfunc)
  (memory $0 100)
  (export "memory" (memory $0))
  (export "load" (func $local))
  (func $local (param $0 i32) (result i32)
    (i32.load (get_local $0))
  )
)

I am trying to compile following two modules to wasm and then load using JS API. I am trying to use following JS code to make the import work:

var h = fetch("test.wasm")
.then(function(response) {
    return response.arrayBuffer();
})
var l = fetch("low.wasm").
then(function(response){
        return response.arrayBuffer();
    })

var exp  = l.then( function(buffer){ 
    var moduleBufferView = new Uint8Array(buffer);  
    WebAssembly.instantiate(moduleBufferView)  
        .then(function(instantiated) {
            const instance = instantiated.instance;
            return instance.exports
        })
})

fetch("test.wasm")
.then(function(response) {
    return response.arrayBuffer();
})
.then(function(buffer) {
    var moduleBufferView = new Uint8Array(buffer);
    WebAssembly.instantiate(moduleBufferView, exp)
        .then(function(instantiated) {
            const instance = instantiated.instance;
            document.getElementById('res').innerHTML = instance.exports.func_0();


        })
});

While compiling using reference interpreter I am getting following error: test.wast:4.8-4.67: link failure: unknown import "low"."load".

Error does make sense that test module is unaware of low while compiling, but how can I link them together?

1 Answer 1

2

By default, the reference interpreter interprets (i.e., executes and instantiates) the module definitions in its input script. If you want to use it to convert files between formats then you have to invoke like e.g.

wasm -d module.wat -o module.wasm

See the section on converting files in the README of the interpreter.

Btw, the table in your program is never initialised, so the call_indirect will just trap. Your JavaScript glue also looks a bit strange: for example, instance.exports.() is not valid JavaScript, and why are you trying to assign it to a DOM node?

Sign up to request clarification or add additional context in comments.

2 Comments

instance.exports.() is a typo. It should have beeen instance.exports.func_0 and high module should export func_0. I have corrected it.
You're now exporting it twice. ;).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.