diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 17:15:16 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 17:15:16 +0100 |
| commit | d38e1ffb1533565ecea6dd83cca88d36690fadef (patch) | |
| tree | 4946ffec374d8d0353a909b2ae314b81cbf07031 /src/zilch/app | |
| parent | 8dd8f96b24710dc4f2d29065928244a40a432379 (diff) | |
zilch: implement async
Diffstat (limited to 'src/zilch/app')
| -rw-r--r-- | src/zilch/app/event.js | 6 | ||||
| -rw-r--r-- | src/zilch/app/load.js | 2 | ||||
| -rw-r--r-- | src/zilch/app/object-store.js | 19 | ||||
| -rw-r--r-- | src/zilch/app/root.document.zig | 2 |
4 files changed, 18 insertions, 11 deletions
diff --git a/src/zilch/app/event.js b/src/zilch/app/event.js index c55637c..9183bb5 100644 --- a/src/zilch/app/event.js +++ b/src/zilch/app/event.js @@ -3,12 +3,14 @@ async function wasm_handle_event(target, name, event, pass_event) { const component = target.getRootNode().host; + const handler = WebAssembly.promising(wasm.exports[name]) + if (pass_event) { const id = ObjectStore.generateFreeId(); ObjectStore.registerObject(id, event); - wasm.exports[name](component[ZsPtr], id); + handler(component[ZsPtr], id); ObjectStore.free(event); } else { - wasm.exports[name](component[ZsPtr]); + handler(component[ZsPtr]); } } diff --git a/src/zilch/app/load.js b/src/zilch/app/load.js index e93a4a6..5cc569f 100644 --- a/src/zilch/app/load.js +++ b/src/zilch/app/load.js @@ -1,6 +1,6 @@ const imports = { object_store: { - process: ObjectStore.processWasm, + process: new WebAssembly.Suspending(ObjectStore.processWasm), }, console: { log(buffer, length) { diff --git a/src/zilch/app/object-store.js b/src/zilch/app/object-store.js index 9a68478..8a4715d 100644 --- a/src/zilch/app/object-store.js +++ b/src/zilch/app/object-store.js @@ -36,12 +36,12 @@ class ObjectStore { } static decodeCall(view, offset) { - const kind = ObjectStore.methods[view.getUint32(offset, true)] + const method = ObjectStore.methods[view.getUint32(offset, true)] - if (!kind) throw "unknown method, cannot decode"; + if (!method) throw "unknown method, cannot decode"; return { - kind: kind, + method: method, num_args: view.getUint8(offset + 4, true), } } @@ -141,7 +141,7 @@ class ObjectStore { } } - static processWasm( + static async processWasm( calls_base, values_base, result_base, @@ -159,16 +159,21 @@ class ObjectStore { const results = []; for (let i = 0; i < length; ++i) { - const method = ObjectStore.decodeCall(calls_view, offset.call); + const call = ObjectStore.decodeCall(calls_view, offset.call); offset.call += wasm.size_of.Call; - let args = new Array(method.num_args).fill().map(_ => { + let args = new Array(call.num_args).fill().map(_ => { const value = ObjectStore.decodeValue(values_view, offset.value, results); offset.value += wasm.size_of.Value; return value; }); - const result = method.kind(...args); + let result = call.method.fn(...args); + + if (call.method.await) { + result = await result; + } + const result_ptr = results_view.getUint32(offset.result, true); offset.result += wasm.size_of.Ptr; diff --git a/src/zilch/app/root.document.zig b/src/zilch/app/root.document.zig index 8e2ca61..6191746 100644 --- a/src/zilch/app/root.document.zig +++ b/src/zilch/app/root.document.zig @@ -29,7 +29,7 @@ pub fn App(comptime options: Options) type { var buf: []const u8 = "ObjectStore.methods = {"; for (options.functions) |function| { - buf = buf ++ std.fmt.comptimePrint("{}: {s},", .{ function.id, function.content }); + buf = buf ++ std.fmt.comptimePrint("{}: {{await: {}, fn: {s}}},", .{ function.id, function.await, function.content }); } buf = buf ++ "}"; |