Skip to content

Instantly share code, notes, and snippets.

@SpaceManiac
Created May 19, 2017 17:28
Show Gist options
  • Save SpaceManiac/daf03e0ac6ed56e7a7723ccdeaf5cfe2 to your computer and use it in GitHub Desktop.
Save SpaceManiac/daf03e0ac6ed56e7a7723ccdeaf5cfe2 to your computer and use it in GitHub Desktop.
Hello World in raw WebAssembly
<meta charset="utf-8">
<script type="text/javascript" src="hello.js"></script>
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());
; 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