Created
December 29, 2017 04:13
-
-
Save dduan/e3a348ff28c4d0c4a1278b8229116eb3 to your computer and use it in GitHub Desktop.
"Hello, World!" In WebAssembly
This file contains hidden or 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
(module | |
(memory (export "memory") 1) | |
(data (i32.const 0) "Hello, world!") | |
(global (export "length") i32 (i32.const 12)) | |
(global (export "position") i32 (i32.const 0))) |
This file contains hidden or 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
compile: | |
wat2wasm main.wat -o main.wasm | |
serve: | |
python -m SimpleHTTPServer |
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<script src="play.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
function main(wasm) { | |
const memory = wasm.exports.memory; | |
const length = wasm.exports.length; | |
const position = wasm.exports.position; | |
const bytes = new Uint8Array(memory.buffer, position, length); | |
const s = new TextDecoder('utf8').decode(bytes); | |
console.log(s); | |
} | |
fetch("main.wasm").then(reponse => | |
reponse.arrayBuffer() | |
).then(bytes => | |
WebAssembly.instantiate(bytes, {}) | |
).then(result => | |
result.instance | |
).then(main); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist helped me getting started. Noticed the length was one short.