I jumped through all the hoops for you, so you can convert c to wasm.
pieced together from various clues:
- prebuilt wasm producing clang yurydelendik/clang-heroku-slug (as used in webassembly-explorer)
- hints about compiler and linker args from aransentin
- also from wasmception
- inspecting output with wabt
- learning the raw web assembly format from webassembly semantics
The first output I generated was surprisingly bloated, (seemed to be inlining too much or something?,
creating lots of i32 variables) also, wasn't faster than my carefully optimized javascript.
but turns out clang has lots of optimization options - I ended up using -O3
//make this weird macro
#define export __attribute__ ((visibility ("default")))
//then at least this looks reasonable.
export
int add (int a, int b) {
return a + b
}
compile to wasm
cd examples/add
c2wasm add.c -o add.wasm
see ./example/add/add.js
for how to call this from javascript
#define export __attribute__ ((visibility ("default")))
//since this is undefined, it's treated as an import
void console_log (char* string, int length);
export
void hello () {
char string[]="hello world";
console_log(&string, sizeof(string));
}
compile the same as before, and again, see ./examples/hello/hello.js
for how to call
from javascript, and pass import in from javascript too.
it's kinda lame to be downloading (and compiling) all these nasty c compilers to build a tiny bit of wasm. isn't the point of wasm to be an actually portable binary target? why can't we compile clang to wasm and then install clang.wasm from npm?
clang-in-browser does that. somebody should wrap that into a node module!
MIT