-
Notifications
You must be signed in to change notification settings - Fork 0
/
foo.js
46 lines (40 loc) · 1.13 KB
/
foo.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
import * as fs from 'fs';
const bytes = fs.readFileSync('./foo.wasm');
const print_bool = (value, dataView) => {
if (value & 1) {
switch (value >> 1) {
case 0: process.stdout.write('true');
case 1: process.stdout.write('false');
}
}
};
const importObject = {
env: {
write_int(value) {
process.stdout.write(value.toString());
},
write_char(value) {
process.stdout.write(String.fromCharCode(value));
},
},
};
(async () => {
const object = await WebAssembly.instantiate(
new Uint8Array(bytes), importObject,
);
try {
const start = Date.now();
object.instance.exports.main_function();
const stop = Date.now();
const memory = object.instance.exports.memory;
const dataView = new DataView(memory.buffer);
const res_value = object.instance.exports.result.value;
process.stdout.write('====> ');
print_bool(res_value, dataView);
const bytes = object.instance.exports.bytes_used.value;
console.log(`\n====> used ${bytes} bytes of memory, took ${(stop - start)} (Node.js) ms.`);
} catch (error) {
console.log(error);
process.exit(1);
}
})();