-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummy.html
More file actions
70 lines (59 loc) · 2.29 KB
/
dummy.html
File metadata and controls
70 lines (59 loc) · 2.29 KB
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
---
layout: page
title: Dummy Page
permalink: /dummy/
---
<section class="dummy-page">
<h2>Dummy Content</h2>
<p>This is a placeholder page created for testing purposes. Feel free to update this content with real information later.</p>
<ul>
<li>Item one</li>
<li>Item two</li>
<li>Item three</li>
</ul>
<blockquote>
<p>"Sometimes you just need a blank canvas before the masterpiece arrives."</p>
</blockquote>
</section>
<section class="dummy-page">
<h2>WebAssembly Greeting Demo</h2>
<p>
The button below loads <code>wasm/hello.wasm</code>—a module compiled from <code>hello.c</code>—
and calls the exported <code>get_greeting</code> function to display its message.
</p>
<button type="button" id="load-wasm">Load greeting from WebAssembly</button>
<pre id="wasm-result" aria-live="polite">Click the button to fetch the WebAssembly module.</pre>
</section>
<script type="module">
const wasmUrl = "{{ '/wasm/hello.wasm' | relative_url }}";
const loadButton = document.getElementById('load-wasm');
const output = document.getElementById('wasm-result');
async function loadGreetingFromWasm() {
output.textContent = 'Loading greeting...';
try {
const response = await fetch(wasmUrl);
if (!response.ok) {
throw new Error(`Unexpected response: ${response.status} ${response.statusText}`);
}
const buffer = await response.arrayBuffer();
const { instance } = await WebAssembly.instantiate(buffer, {});
const { exports } = instance;
if (typeof exports.get_greeting !== 'function' || typeof exports.get_greeting_length !== 'function') {
throw new Error('Expected WebAssembly exports were not found.');
}
const pointer = exports.get_greeting();
const length = exports.get_greeting_length();
const memory = exports.memory;
if (!(memory instanceof WebAssembly.Memory)) {
throw new Error('WebAssembly memory export is missing.');
}
const bytes = new Uint8Array(memory.buffer, pointer, length);
const greeting = new TextDecoder('utf-8').decode(bytes);
output.textContent = greeting;
} catch (error) {
console.error('Failed to load WebAssembly module:', error);
output.textContent = `Failed to load greeting: ${error.message}`;
}
}
loadButton.addEventListener('click', loadGreetingFromWasm);
</script>