aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-26 17:15:16 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-26 17:15:16 +0100
commitd38e1ffb1533565ecea6dd83cca88d36690fadef (patch)
tree4946ffec374d8d0353a909b2ae314b81cbf07031
parent8dd8f96b24710dc4f2d29065928244a40a432379 (diff)
zilch: implement async
-rw-r--r--src/zilch/Env/Function.zig2
-rw-r--r--src/zilch/Promise.zig12
-rw-r--r--src/zilch/app/event.js6
-rw-r--r--src/zilch/app/load.js2
-rw-r--r--src/zilch/app/object-store.js19
-rw-r--r--src/zilch/app/root.document.zig2
-rw-r--r--src/zilch/component.template.js4
7 files changed, 32 insertions, 15 deletions
diff --git a/src/zilch/Env/Function.zig b/src/zilch/Env/Function.zig
index 692ce2b..c37b1c7 100644
--- a/src/zilch/Env/Function.zig
+++ b/src/zilch/Env/Function.zig
@@ -4,10 +4,12 @@ const Self = @This();
content: []const u8,
id: u32,
+await: bool,
pub fn from(content: []const u8) Self {
return .{
.content = content,
.id = std.hash.XxHash32.hash(4654, content),
+ .await = std.mem.startsWith(u8, content, "async ") or std.mem.startsWith(u8, content, "async("),
};
}
diff --git a/src/zilch/Promise.zig b/src/zilch/Promise.zig
index 76362e7..7832b66 100644
--- a/src/zilch/Promise.zig
+++ b/src/zilch/Promise.zig
@@ -34,8 +34,9 @@ pub inline fn then(self: anytype, env: *Env, func: anytype, ud: *anyopaque) Futu
}));
}
-pub fn as(self: *Self, T: type) *T {
- return @ptrCast(self);
+pub inline fn await(self: anytype, env: *Env, T: type) !T {
+ var future = env.call(bindings.await, .{ self });
+ return future.await(env, T);
}
pub const bindings = struct {
@@ -47,8 +48,15 @@ pub const bindings = struct {
\\ })
\\}
);
+
+ pub const await: Function = .from(
+ \\async function(promise) {
+ \\ return promise;
+ \\}
+ );
};
pub const functions = [_]Function{
bindings.then,
+ bindings.await,
};
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 ++ "}";
diff --git a/src/zilch/component.template.js b/src/zilch/component.template.js
index bd6d634..553fe11 100644
--- a/src/zilch/component.template.js
+++ b/src/zilch/component.template.js
@@ -9,10 +9,10 @@ customElements.define("{s}-{s}", class extends HTMLElement {{
}}
connectedCallback() {{
if (this[ZsPtr] === null) {{
- wasm_ready.then(() => {{
+ wasm_ready.then(async () => {{
const id = ObjectStore.generateFreeId();
ObjectStore.registerObject(id, this);
- this[ZsPtr] = wasm.exports['export::{s}::init'](id);
+ this[ZsPtr] = await WebAssembly.promising(wasm.exports['export::{s}::init'])(id);
}});
}}
}}