This crate implements a simple calculator where:
add()
is defined by Csubtract()
is defined by Rustmultiply()
is defined by Rust but callsadd()
in C multiple timesdivide()
is defined by C but callssubtract()
in Rust multiple times
All methods are exported from a single .wasm
file and can be called from JS.
This crate demonstrates how to build and link Rust + C manually.
Check out the build.sh
script for the specific steps. You need llvm, clang, and Rust nightly.
To see it working in your browser, use your preferred local server:
npx serve
Then, visit http://localhost:3000.
Optionally, for inspecting the compiled binaries, it's a good idea to install WebAssembly Binary Toolkit. The build scripts assume you have it installed and call wasm2wat
, but you can also comment these lines.
Originally, my main idea behind the build scripts was to:
- Transpile C to LLVM internal representation.
- Transpile Rust to LLVM internal representation.
- Compile all
.ll
files with LLVM compilerllc
. - Link the compiled files with
wasm-ld
.
I've got this idea while reading the excellent article Compiling C to WebAssembly without Emscripten by @surma.
These LLVM IR intermediate steps have been removed from this repository with a generous contribution from the original author.
So, while we're not using this approach anymore, I'll leave the link to the article here for credit and historical reasons.