Becavalier / Becavalier/Book-DISO-WebAssembly

[Emscripten] Use "-s MODULARIZE" to initialize module in an async style.

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Emscripten Solution
Dominant language
WebAssembly
Stars
72
Forks
16
PR merge metrics
No merged PRs in 30d

Description

It seems like the latest Emscripten toolchain has a decision to remove the "--post-js" option, here we can use "-s MODULARIZE" to initialize module as a replacement. FYI:

modularize.cc
```C++
#include
#include

using namespace std;

int main (int argc, char **argv) {
cout << "Hello, Emscripten!" << endl;
return 0;
}

extern "C" int EMSCRIPTEN_KEEPALIVE add (int x, int y) {
return x + y;
}
```

modularize.html (generated by "emcc", need to revise)
```html
...

window.onload = function() {
MyModule().then(function(Module) {
console.log(Module['asm']['_add'](1, 2));
});
}

...
```

CMD:
```shell
sudo emcc modularize.cc -s WASM=1 -o modularize.html -O3 -s MODULARIZE=1 -s 'EXPORT_NAME="MyModule"'
```

*BTW, we can also use `--pre-js` param to append JavaScript code into the resulting script file.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the emcc command shown in the issue and inspect the generated modularize.html and module.js behavior. Compare the generated page with modularize.cc and verify that MODULARIZE initializes MyModule asynchronously and that calling _add(1, 2) produces 3. Done means the example works without relying on the removed --post-js option.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, wasm
Domain
build-system, web-dev
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.