/
index.html
82 lines (69 loc) · 2 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
<h1>Web Assembly</h1>
<div class="inline-wasm">
<h3>When wasm is inline, result should be 42</h3>
<button class="run">Click to run</button>
<span class="result"></span>
</div>
<div class="output-wasm">
<h3>When wasm is output, result should be 24</h3>
<button class="run">Click to run</button>
<span class="result"></span>
</div>
<div class="init-returns-instance">
<h3>init function returns WebAssembly.Instance</h3>
<button class="run">Click to run</button>
<span class="result"></span>
</div>
<div>
<h3>Importing as URL</h3>
<span class="url"></span>
</div>
<div class="worker-wasm">
<h3>worker wasm</h3>
<span class="result"></span>
</div>
<script type="module">
import light from './light.wasm?init'
import heavy from './heavy.wasm?init'
import myWorker from './worker?worker'
const w = new myWorker()
w.addEventListener('message', (ev) => {
text('.worker-wasm .result', ev.data.result)
})
async function testWasm(init, resultElement) {
const { exported_func } = await init({
imports: {
imported_func: (res) => (resultElement.textContent = res)
}
}).then((i) => i.exports)
exported_func()
}
function text(el, text) {
document.querySelector(el).textContent = text
}
document
.querySelector('.inline-wasm .run')
.addEventListener('click', async () =>
testWasm(light, document.querySelector('.inline-wasm .result'))
)
document
.querySelector('.output-wasm .run')
.addEventListener('click', async () =>
testWasm(heavy, document.querySelector('.output-wasm .result'))
)
document
.querySelector('.init-returns-instance .run')
.addEventListener('click', async () => {
const res = await light({
imports: {
imported_func: (res) => (resultElement.textContent = res)
}
})
text(
'.init-returns-instance .result',
res instanceof WebAssembly.Instance
)
})
import lightUrl from './light.wasm?url'
text('.url', lightUrl)
</script>