-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.js
42 lines (37 loc) · 917 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const importObject = {
imports: {
display: arg => {
document.querySelector("div").innerHTML = arg;
}
}
};
const code = `
(module
(import "imports" "display" (func $display (param i32)))
(export "more" (func $more))
(global $counter (mut i32) (i32.const 0))
(func $inc (param i32) (result i32)
local.get 0
i32.const 1
i32.add)
(func $more
global.get $counter
call $inc
global.set $counter
global.get $counter
call $display
)
)
`;
async function init() {
const wabt = await WabtModule();
const module = wabt.parseWat("example.wat", code);
module.resolveNames();
module.validate();
const binary = module.toBinary({});
const wasm = new WebAssembly.Module(binary.buffer);
const instance = new WebAssembly.Instance(wasm, importObject);
const handler = instance.exports.more;
document.querySelector("button").onclick = handler;
};
init();