Skip to content

Instantly share code, notes, and snippets.

@doccaico
Created December 25, 2020 06:12
Show Gist options
  • Select an option

  • Save doccaico/131fdc79e7f5ad8febb71b205fa2ec88 to your computer and use it in GitHub Desktop.

Select an option

Save doccaico/131fdc79e7f5ad8febb71b205fa2ec88 to your computer and use it in GitHub Desktop.
Zig and WASM
$ zig version
0.7.1+ba2f2e139

$ zig build-lib math.zig -target wasm32-freestanding
$ python3 -m http.server 8000

# then open browser
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zig and WASM</title>
</head>
<body>
<script>
const env = {
print(n) { console.log(n + 100); }
};
WebAssembly.instantiateStreaming(fetch('math.wasm'), {env})
.then(obj => {
obj.instance.exports.add(1, 2); // 103
env.print(9); // 109
});
</script>
</body>
</html>
extern fn print(i32) void;
export fn add(a: i32, b: i32) void {
print(a + b);
}
@doccaico

Copy link
Copy Markdown
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment