add minimal zig wasm testing ground

This commit is contained in:
Felix Roos
2024-01-10 17:04:10 +01:00
parent f768e1eece
commit 0106a129c9
5 changed files with 34 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
# zig testing ground
<https://dev.to/sleibrock/webassembly-with-zig-part-1-4onm>
```sh
# (re)compile add.zig
zig build-lib add.zig -target wasm32-freestanding -dynamic -rdynamic
# run server
npx http-server -o
```
BIN
View File
Binary file not shown.
+3
View File
@@ -0,0 +1,3 @@
export fn add(a: i32, b: i32) i32 {
return a + b;
}
+10
View File
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>ZIG / WASM Demo</title>
</head>
<body>
<h3>check the console</h3>
</body>
<script src="./main.js"></script>
</html>
+11
View File
@@ -0,0 +1,11 @@
async function run() {
let res = await fetch('./add.wasm');
res = await res.arrayBuffer();
res = await WebAssembly.instantiate(res, {
env: {},
});
const { add } = res.instance.exports;
console.log(add(3, 5));
}
run();