$ zig version
0.7.1+ba2f2e139
$ zig build-lib math.zig -target wasm32-freestanding
$ python3 -m http.server 8000
# then open browser
Created
December 25, 2020 06:12
-
-
Save doccaico/131fdc79e7f5ad8febb71b205fa2ec88 to your computer and use it in GitHub Desktop.
Zig and WASM
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> | |
| <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> |
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
| extern fn print(i32) void; | |
| export fn add(a: i32, b: i32) void { | |
| print(a + b); | |
| } |
doccaico
commented
Dec 25, 2020
Author

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