-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ade84a9
commit 7a663bc
Showing
6 changed files
with
104 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,64 @@ | ||
|
||
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; | ||
} | ||
}; | ||
let res = ""; | ||
for (i = 0; i < get_length(str); i++) { | ||
res = res + String.fromCharCode(get_char(str, i)); | ||
} | ||
console.log(res); | ||
}; | ||
|
||
let str_buff = ""; | ||
|
||
function print_i32(arg) { | ||
str_buff = str_buff + arg.toString(); | ||
}; | ||
str_buff = str_buff + arg.toString(); | ||
}; | ||
|
||
function print_f64(arg) { | ||
console.log(arg); | ||
}; | ||
console.log(arg); | ||
}; | ||
|
||
function print_endline() { | ||
console.log(str_buff); | ||
str_buff = ""; | ||
console.log(str_buff); | ||
str_buff = ""; | ||
} | ||
|
||
function putchar(i_char) { | ||
var char = String.fromCharCode(i_char); | ||
str_buff = str_buff + char; | ||
let char = String.fromCharCode(i_char); | ||
str_buff = str_buff + char; | ||
}; | ||
|
||
function flush() { | ||
console.log(str_buff); | ||
str_buff = ""; | ||
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 | ||
"print_i32": print_i32, | ||
"print_f64": print_f64, | ||
"print_string": print_string, | ||
"print_endline": print_endline, | ||
"putchar": putchar, | ||
"flush": flush, | ||
"atan2": Math.atan2, | ||
"sin": Math.sin, | ||
"asin": Math.asin, | ||
"fmod": (x, y) => x % y, | ||
"cos": Math.cos, | ||
} | ||
|
||
const src = "./a.out.wasm" | ||
const code = fetch(src, { | ||
// ... | ||
referrerPolicy: "unsafe-url" | ||
}); | ||
const imports = {"js_runtime":bindings} | ||
const imports = { | ||
"js_runtime" : bindings | ||
} | ||
|
||
const wasmModule = await WebAssembly.instantiateStreaming(code, imports).then(module => { | ||
console.log("module loaded! listing its exports:"); | ||
for (let key in module.instance.exports) { | ||
console.log(key); | ||
} | ||
console.log("module loaded!"); | ||
console.log("done!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters