-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.js
44 lines (36 loc) · 988 Bytes
/
template.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
let blob = atob('${BLOB}');
var array = new Uint8Array(new ArrayBuffer(blob.length));
var i = 0|0;
for(i = 0; i < blob.length; i++) {
array[i] = blob.charCodeAt(i);
}
var memory = new WebAssembly.Memory({initial:2});
var HEAPU8 = new Uint8Array(memory.buffer);
let dec = new TextDecoder("utf8");
function s(ptr) {
var len;
for(len = 0|0; HEAPU8[ptr+len] != 0|0; len++);
return dec.decode(HEAPU8.subarray(ptr, ptr+len));
}
let wasmExports;
const obj = {
env: {
memory: memory,
print: console.log,
printc: (str) => { console.log(s(str)) },
}
};
WebAssembly.instantiate(array, obj).then({instance} => {
wasmExports = instance.exports;
console.log(instance);
instance.exports.main();
const jsArray = [1, 2, 3, 4, 5];
const cArrayPointer = instance.exports.malloc(jsArray.length * 4);
const cArray = new Uint32Array(
memory.buffer,
cArrayPointer,
jsArray.length
);
cArray.set(jsArray);
console.log(instance.exports.sum(cArrayPointer, cArray.length));
});