All Projects → dominictarr → C2wasm

dominictarr / C2wasm

Licence: mit

c2wasm

I jumped through all the hoops for you, so you can convert c to wasm.

pieced together from various clues:

performance optimizations

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

imports and exports

wasm method, accessable from javascript

//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

javascript method, accessable from wasm

#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.

future work

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!

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].