forked from vitejs/vite
/
index.html
54 lines (45 loc) · 1.31 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
<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="worker-wasm">
<h3>worker wasm</h3>
<span class="result"></span>
</div>
<script type="module">
import light from './light.wasm'
import heavy from './heavy.wasm'
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)
}
})
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'))
)
</script>