0

I am reading a beautiful book about WebAssembly, and I am trying to learn how to import JS functions into wasm without using "glue code".

So this is the C file where 2 extern functions are declared

extern int jsClearRect();
extern int jsFillRect();

I then compile the c code into wasm with the following instructions:

emcc src/main.c -Os -s STANDALONE_WASM=1 -s SIDE_MODULE=1  -o main.wasm

and then I am instructed to write a JS script which instantiate the wasm file, defines jsFillRect() and jsClearRect() and import them into the module's env by using an import object.

// define the import Objects
const importObj = {
    "env": {
        "__memory_base":0,
        "tableBase":0,
        "memory": new WebAssembly.Memory({ initial: 256 }),
        "table": new WebAssembly.Table({ initial: 8, element: 'anyfunc' }),
        jsClearRect(): function() {/*function definition here*/},
        jsFillRect(): function() {/*function definition here*/},
    }
}
// import the module
fetch('main.wasm')
        .then(obj => WebAssembly.instantiateStreaming(obj,importObject))
        .then(result => console.log(result))
        .catch(err => console.log(err))

and I get an Error:

TypeError: import object field 'GOT.mem' is not an Object

The import object that I am presenting here is already a modified version of the orginal one (that you can find here). In this example the functions are declared in JS as _jsClearRect() but the module could not find a definition of jsClearRect(). Then the it could not find a definition of __memory_base because it was declared as memoryBasebut now I don't know what king of Object represent GOT.mem.

I have been looking around and my feeling is that I am using an old API, but I cannot find a proper solution to achieve this.

So my QUESTION is:

How do I import a Javascript functions into a wasm module?

ddgg
  • 145
  • 1
  • 10

1 Answers1

0

The GOT.mem come from the dynamic linking ABI used by emscripten. In this case I don't think you want/need the dyanamic linking stuff and you can/should just remove -sSIDE_MODULE.

This should simplify/reduce the imports you need to provide. (For example, you won't need to provide table or memory).

sbc100
  • 2,619
  • 14
  • 11