-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
123 lines (95 loc) · 3.62 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" id="name" value="Daniel" required />
<button id="btn-greet">Greet</button>
<script type="module">
import ZigWASMWrapper from "./helper.js";
let wasm = await ZigWASMWrapper.initialize("./main.wasm");
// === Interface Test ===
console.log("=== Testing silence");
wasm.silence();
console.log("=== Testing void type");
console.log(wasm.testVoid());
wasm.printVoid(undefined);
console.log("=== Testing bool type");
console.log(wasm.testBool());
wasm.printBool(true);
console.log("=== Testing integer type");
console.log(wasm.testInt());
wasm.printInt(-12345);
console.log("=== Testing unsigned integer type");
console.log(wasm.testUint());
wasm.printUint(12345);
console.log("=== Testing float type");
console.log(wasm.testFloat());
wasm.printFloat(1.2345);
console.log("=== Testing bytes type");
console.log(wasm.testBytes());
wasm.printBytes(new TextEncoder().encode("Hello World"));
console.log("=== Testing string type");
console.log(wasm.testString());
wasm.printString("Bye World");
console.log("=== Testing json type");
console.log(wasm.testJSON());
wasm.printJSON({ message: "Greetings" });
console.log("=== Testing zig function type");
const testFunctionRef = wasm.testFunctionRef();
console.log(testFunctionRef());
console.log(wasm.testFunction(testFunctionRef));
console.log(wasm.testFunctionWithArgs(testFunctionRef, ["Hello", "from", "JS"]));
console.log("=== Testing js function type");
console.log(wasm.testFunction((...args) => {
console.log('I got called by reference from Zig with these arguments =', args)
return "JS says hello!"
}))
console.log(wasm.testFunctionWithArgs((...args) => {
console.log('I got called by reference from Zig with these arguments =', args)
return "JS says hello!"
}, ["Hello", "from", "JS"]))
console.log("=== Testing greet");
console.log(wasm.greet("Daniel"));
// === Example Application ===
console.log("=== Testing example app");
function measure(label, func) {
const t0 = performance.now();
func();
const t1 = performance.now();
console.log(`${label} took ${t1 - t0}ms.`);
}
document.getElementById("btn-greet").addEventListener("click", () => {
measure("greet and hash", () => {
const name = document.getElementById("name").value;
console.log(wasm.greet(name));
console.log(wasm.blake2b(`this is a hashing input for ${name}`));
});
});
measure("initial greet and hash", () => {
console.log(wasm.greet("Daniel"));
console.log(
wasm.blake2b("This is just some hashing input to think about")
);
});
// === Hash Benchmark ===
console.log("=== Testing hash benchmark");
const ROUNDS = 1000;
const t0 = performance.now();
for (let i = 0; i < ROUNDS; i++) {
// NOTE: There is an obvious performance penalty here, in JS and Zig,
// compared to raw native performance.
wasm.blake2b("Some hash");
}
const t1 = performance.now();
console.log(
`Benchmark (hash with ${ROUNDS}rounds): total=${t1 - t0}ms; per round=${
(t1 - t0) / ROUNDS
}`
);
</script>
</body>
</html>