Created
May 19, 2017 17:28
-
-
Save SpaceManiac/daf03e0ac6ed56e7a7723ccdeaf5cfe2 to your computer and use it in GitHub Desktop.
Hello World in raw 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
<meta charset="utf-8"> | |
<script type="text/javascript" src="hello.js"></script> |
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
var memory = new WebAssembly.Memory({initial:256, maximum:256}); | |
var print = function(ptr, len) { | |
console.log(new TextDecoder().decode(new DataView(memory.buffer, ptr, len))); | |
}; | |
var imports = { | |
'env': { | |
'memory': memory, | |
'print': print, | |
} | |
}; | |
fetch('hello.wasm') | |
.then(response => response.arrayBuffer()) | |
.then(bytes => WebAssembly.instantiate(bytes, imports)) | |
.then(results => results.instance.exports._main()); |
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
; convert to wasm with wast2wasm from https://github.com/WebAssembly/wabt | |
(module | |
(type (;0;) (func (param i32 i32))) | |
(type (;1;) (func)) | |
(import "env" "memory" (memory (;0;) 256 256)) | |
(import "env" "print" (func (;0;) (type 0))) | |
(func (;1;) (type 1) | |
i32.const 1400 | |
i32.const 13 ;; length | |
call 0) | |
(export "_main" (func 1)) | |
(data (i32.const 1400) "Hello, world!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment