Skip to content

Commit accfac5

Browse files
authored
Fix WASM greeting demo and adjust build (#19)
1 parent b86c36e commit accfac5

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

.github/workflows/build-wasm.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727

2828
- name: Build wasm
2929
run: |
30-
emcc -std=c89 -s STANDALONE_WASM=1 wasm/hello.c -o wasm/hello.wasm
30+
emcc -std=c89 --no-entry -s STANDALONE_WASM=1 wasm/hello.c -o wasm/hello.wasm
3131
3232
# If you use Jekyll, build here; otherwise just ship the repo contents.
3333
- name: Configure Pages

pages/dummy.html

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,31 @@ <h2>WebAssembly Greeting Demo</h2>
3030
const wasmUrl = '/wasm/hello.wasm';
3131
const loadButton = document.getElementById('load-wasm');
3232
const output = document.getElementById('wasm-result');
33+
const utf8Decoder = new TextDecoder('utf-8');
34+
35+
function createWasiImports() {
36+
const noop = () => 0;
37+
38+
return {
39+
wasi_snapshot_preview1: {
40+
proc_exit(code) {
41+
if (code !== 0) {
42+
throw new Error(`WebAssembly module exited with code ${code}`);
43+
}
44+
},
45+
fd_close: noop,
46+
fd_fdstat_get: noop,
47+
fd_seek: noop,
48+
fd_write: noop,
49+
environ_get: noop,
50+
environ_sizes_get: noop,
51+
args_get: noop,
52+
args_sizes_get: noop,
53+
clock_time_get: noop,
54+
random_get: noop,
55+
},
56+
};
57+
}
3358

3459
async function loadGreetingFromWasm() {
3560
output.textContent = 'Loading greeting...';
@@ -41,23 +66,31 @@ <h2>WebAssembly Greeting Demo</h2>
4166
}
4267

4368
const buffer = await response.arrayBuffer();
44-
const { instance } = await WebAssembly.instantiate(buffer, {});
69+
const imports = createWasiImports();
70+
const { instance } = await WebAssembly.instantiate(buffer, imports);
4571
const { exports } = instance;
4672

47-
if (typeof exports.get_greeting !== 'function' || typeof exports.get_greeting_length !== 'function') {
73+
const getGreeting = exports.get_greeting || exports._get_greeting;
74+
const getGreetingLength = exports.get_greeting_length || exports._get_greeting_length;
75+
76+
if (typeof getGreeting !== 'function' || typeof getGreetingLength !== 'function') {
4877
throw new Error('Expected WebAssembly exports were not found.');
4978
}
5079

51-
const pointer = exports.get_greeting();
52-
const length = exports.get_greeting_length();
80+
if (typeof exports._initialize === 'function') {
81+
exports._initialize();
82+
}
83+
5384
const memory = exports.memory;
5485

5586
if (!(memory instanceof WebAssembly.Memory)) {
5687
throw new Error('WebAssembly memory export is missing.');
5788
}
5889

90+
const pointer = getGreeting();
91+
const length = getGreetingLength();
5992
const bytes = new Uint8Array(memory.buffer, pointer, length);
60-
const greeting = new TextDecoder('utf-8').decode(bytes);
93+
const greeting = utf8Decoder.decode(bytes);
6194

6295
output.textContent = greeting;
6396
} catch (error) {

wasm/hello.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,3 @@ size_t get_greeting_length(void)
1414
{
1515
return sizeof(GREETING) - 1;
1616
}
17-
18-
int main(void)
19-
{
20-
return 0;
21-
}

0 commit comments

Comments
 (0)