A new standard being developed in a W3C Community Group with Apple, Google, Microsoft and Mozilla which defines:
Is just a postorder serialization of an AST. They claim its better than Java bytecode.
Great! Firefox and Chrome has Wasm support under flags. For the compiler follow the step by step.
Callstack and machine code is not in address space.
The only dangerous opcodes are .load
and .store
operations, as they index into memory. They employ a cool trick in 64bit
machines: mmap 4GBs of continouos virtual memory, you can't over-index
that with an i32 address. Runtime bound-checks on 32bits.
emcc -s BINARYEN=1 -s 'EXPORTED_FUNCTIONS=["_fib"]' fib.c -o fib.js
function module(global, env, buffer) {
"use asm";
...
function fib(n) {
n = n|0;
var a = 0, b = 1, t = 0;
while ((n|0) >= 0) {
t = b;
b = (a + b)|0;
a = t;
n = (n - 1)|0;
}
return b|0;
}
return {fib: fib};
}