Skip to content

dominictarr/c2wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published