Skip to content

Commit b91bce5

Browse files
committed
Make echo a bit more intesting
1 parent e193fce commit b91bce5

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

examples/echo/host.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
async function roc_web_platform_run(wasm_filename, input, callback) {
2-
let exit_code;
3-
1+
async function load_wasm(file) {
42
const importObj = {
53
wasi_snapshot_preview1: {
64
proc_exit: (code) => {
75
if (code !== 0) {
86
console.error(`Exited with code ${code}`);
97
}
10-
exit_code = code;
118
},
129
fd_write: (x) => {
1310
console.error(`fd_write not supported: ${x}`);
@@ -20,7 +17,7 @@ async function roc_web_platform_run(wasm_filename, input, callback) {
2017
},
2118
};
2219

23-
const fetchPromise = fetch(wasm_filename);
20+
const fetchPromise = fetch(file);
2421

2522
let wasm;
2623
if (WebAssembly.instantiateStreaming) {
@@ -33,41 +30,43 @@ async function roc_web_platform_run(wasm_filename, input, callback) {
3330
wasm = await WebAssembly.instantiate(module_bytes, importObj);
3431
}
3532

36-
try {
37-
// Encode the input message as json
38-
const message = new TextEncoder().encode(JSON.stringify(input));
39-
40-
// Allocate enough memory in wasm
41-
const in_pointer = wasm.instance.exports.allocUint8(message.length);
33+
return async function (input, callback) {
34+
try {
35+
// Encode the input message as json
36+
const message = new TextEncoder().encode(JSON.stringify(input));
37+
38+
// Allocate enough memory in wasm
39+
const in_pointer = wasm.instance.exports.allocUint8(message.length);
40+
41+
// Init the wasm memory
42+
const memory_bytes = new Uint8Array(wasm.instance.exports.memory.buffer);
43+
44+
// Write the encoded input to the wasm memory
45+
memory_bytes.set(message, in_pointer);
4246

43-
// Init the wasm memory
44-
const memory_bytes = new Uint8Array(wasm.instance.exports.memory.buffer);
47+
// Call the roc code
48+
const out_pointer = wasm.instance.exports.run_roc(in_pointer, message.length);
4549

46-
// Write the encoded input to the wasm memory
47-
memory_bytes.set(message, in_pointer);
48-
49-
// Call the roc code
50-
const out_pointer = wasm.instance.exports.run_roc(in_pointer, message.length);
51-
52-
// Find the end of the roc return value (the first 0 byte)
53-
let stop;
54-
for (stop = out_pointer; memory_bytes[stop] != 0; stop++) ;
50+
// Find the end of the roc return value (the first 0 byte)
51+
let stop;
52+
for (stop = out_pointer; memory_bytes[stop] != 0; stop++);
5553

56-
// Decode the roc value
57-
let result = JSON.parse(new TextDecoder().decode(memory_bytes.slice(out_pointer, stop)));
54+
// Decode the roc value
55+
const result = JSON.parse(new TextDecoder().decode(memory_bytes.slice(out_pointer, stop)));
5856

59-
callback(result);
57+
callback(result);
6058

61-
} catch (e) {
62-
const is_ok = e.message === "unreachable" && exit_code === 0;
63-
if (!is_ok) {
64-
console.error(e);
59+
} catch (e) {
60+
const is_ok = e.message === "unreachable" && exit_code === 0;
61+
if (!is_ok) {
62+
console.error(e);
63+
}
6564
}
6665
}
6766
}
6867

6968
if (typeof module !== "undefined") {
7069
module.exports = {
71-
roc_web_platform_run,
70+
load_wasm,
7271
};
7372
}

examples/echo/index.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
<html>
22
<body>
3+
<input placeholder="Enter some text">
34
<div id="output"></div>
45
<script src="./host.js"></script>
56
<script>
6-
const elem = document.getElementById("output");
7-
roc_web_platform_run("./echo.wasm", "hello from javascript", (string_from_roc) => {
8-
elem.textContent = string_from_roc;
9-
});
7+
const input = document.querySelector("input");
8+
const output = document.getElementById("output");
9+
10+
load_wasm("./echo.wasm").then((run_roc) => {
11+
input.addEventListener("input", (event) => {
12+
run_roc(event.target.value, (string_from_roc) => {
13+
output.textContent = string_from_roc;
14+
});
15+
});
16+
});
1017
</script>
1118
</body>
1219
</html>

0 commit comments

Comments
 (0)