Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: WebAssembly.instantiate(): Import #2 module="GOT.mem": module is not an object or function #20954

Open
SyavaSpb opened this issue Dec 19, 2023 · 3 comments

Comments

@SyavaSpb
Copy link

My code:

#ifdef __EMSCRIPTEN__
    #include <emscripten.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

    int value = 0;

    EMSCRIPTEN_KEEPALIVE
    int Increment() { return value++; }
    EMSCRIPTEN_KEEPALIVE
    int Decrement() { return value--; }

#ifdef __cplusplus
}
#endif

Connect with:

const moduleMemory = new WebAssembly.Memory({initial: 256})
const importedObject = {
    env: {
        __memory_base: 0,
        memory: moduleMemory,
    },
}
WebAssembly.instantiateStreaming(fetch("state.wasm"), importedObject).then(result => {
    moduleExports = result.instance.exports
})

I use the command:
emcc state.cpp -s SIDE_MODULE=1 -O1 -o state.wasm
And I get error Import #2 module="GOT.mem": module is not an object or function

When I use the command
emcc state.cpp -o state.js
And when I delete the state.js file and use only state.wasm, everything works fine

@sbc100
Copy link
Collaborator

sbc100 commented Dec 20, 2023

When you use SIDE_MODULE you are opting int emscripten's dynamic linking system, which means you need to build a MAIN_MODULE and use the emscripten-generated js file for the that main module to then load the side module.

If you just want to build a standalone wasm module and don't want use emscripten generated JS then the option you probably want is -sSTANDALONE_WASM (However in this you don't even need that since it is the default setting when build to and output file with the .wasm extension). See https://v8.dev/blog/emscripten-standalone-wasm for more

@SyavaSpb
Copy link
Author

Without SIDE_MODULE flag it throws an error undefined symbol: main

Trying to build code from the link

#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int x, int y) {
  return x + y;
}

with the command emcc -O3 add.c -o add.wasm and I get the same error undefined symbol: main

@sbc100
Copy link
Collaborator

sbc100 commented Dec 20, 2023

Without SIDE_MODULE flag it throws an error undefined symbol: main

Trying to build code from the link

#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int add(int x, int y) {
  return x + y;
}

with the command emcc -O3 add.c -o add.wasm and I get the same error undefined symbol: main

With STANDALONE_WASM you need to explicitly pass --no-entry if you don't have a main function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants