forked from OCamlPro/wasocaml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
63 lines (55 loc) · 1.44 KB
/
test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const memory = new WebAssembly.Memory({
initial: 1,
maximum: 1,
});
function print_string(str) {
console.log('print_string');
var res = "";
for (i = 0; i < get_length(str); i++) {
res = res + String.fromCharCode(get_char(str, i));
}
console.log(res);
};
var str_buff = "";
function print_string_mem(off, len) {
// console.log('print_string_mem');
const buff = new Uint8Array(memory.buffer);
// console.log(buff);
var i = 0;
for (i = 0; i < len; i++) {
var char = String.fromCharCode(buff[i+off]);
str_buff = str_buff + char;
}
};
function print_i32(arg) {
str_buff = str_buff + arg.toString();
};
function print_f64(arg) {
console.log(arg);
};
function print_endline() {
console.log(str_buff);
str_buff = "";
}
function putchar(i_char) {
var char = String.fromCharCode(i_char);
str_buff = str_buff + char;
};
function flush() {
console.log(str_buff);
str_buff = "";
}
const bindings = {
"print_i32": print_i32,
"print_f64": print_f64,
"print_string": print_string,
"print_string_mem": print_string_mem,
"print_endline": print_endline,
"putchar": putchar,
"flush": flush,
"memory": memory
}
const src = "./a.out.wasm"
const code = fetch(src)
const imports = {"js_runtime":bindings}
const wasmModule = await WebAssembly.instantiateStreaming(code,imports)