-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (44 loc) · 1.34 KB
/
index.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
43
44
45
46
47
48
var instance;
var memory;
const POINTER_WIDTH = 4;
function getI32(ptr) {
var slice = new Uint8Array(memory.buffer).slice(ptr, ptr + POINTER_WIDTH);
var view = new DataView(slice.buffer);
return view.getUint32(0, true);
}
var importObject = {
env: {
test_js: function(arg) {
console.log("test_js", arg);
},
draw_html: function(ptr) {
//var ptr = instance.exports.alloc(3 * POINTER_WIDTH);
var start = getI32(ptr);
var size = getI32(ptr + POINTER_WIDTH);
var memView = new Uint8Array(memory.buffer);
var strData = new Uint8Array(memView.slice(start, start + size));
var decoder = new TextDecoder("UTF-8");
var asUtf8 = decoder.decode(strData);
document.body.innerHTML = asUtf8;
console.log("draw", asUtf8);
}
},
imports: {
env: {
test_js: function() {
console.log("test_js");
}
}
}
}
fetch('main.wasm').then(response => response.arrayBuffer())
.then(bytes => {
return WebAssembly.instantiate(bytes, importObject)
})
.then(results => {
instance = results.instance;
memory = instance.exports.memory;
console.log(instance);
results.instance.exports.browser()
});
console.log("Test");