From 3d0d5d88458112e5cc276f650e02609dc85c5b87 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Oct 2026 19:26:47 +0200 Subject: juicy event handler --- src/app/components/hello/root.zig | 15 ++++++++++-- src/z/App/main.js | 15 +++++++++++- src/z/Component.zig | 48 +++++++++++++++++++++++++++++++++------ 3 files changed, 68 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig index 761f46b..6dafd98 100644 --- a/src/app/components/hello/root.zig +++ b/src/app/components/hello/root.zig @@ -18,7 +18,18 @@ pub fn init(self: *Self, element: *z.Object) void { self.counter = 0; } -pub fn clicked(self: *Self) void { +pub fn clicked(self: *Self, event: *z.Object) !void { + var env: z.Env = .init(std.heap.wasm_allocator); + defer env.deinit(); + self.counter += 1; - std.log.info("clicked {} times", .{self.counter}); + + const x = event.get(&env, "x"); + const y = event.get(&env, "y"); + + std.log.info("clicked {} times at ({}, {})", .{ + self.counter, + try x.await(&env, .uint), + try y.await(&env, .uint), + }); } diff --git a/src/z/App/main.js b/src/z/App/main.js index 5e0c176..7ac2cdb 100644 --- a/src/z/App/main.js +++ b/src/z/App/main.js @@ -18,9 +18,22 @@ const imports = { } } +async function wasm_handle_event(name, event, pass_event) { + await wasm_ready; + + if (pass_event) { + const id = ObjectStore.generateFreeId(); + ObjectStore.registerObject(id, event); + wasm.exports[name](this["@ptr"], id); + ObjectStore.free(event); + } else { + wasm.exports[name](this["@ptr"]); + } +} + const wasm_ready = WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( ({ instance, module }) => { - window.wasm = { + wasm = { exports: instance.exports, memory: instance.exports.memory, } diff --git a/src/z/Component.zig b/src/z/Component.zig index deab261..122975f 100644 --- a/src/z/Component.zig +++ b/src/z/Component.zig @@ -11,16 +11,49 @@ html: Fragment, css: []const u8, link: type, +fn handlerNeedsEvent(func: anytype) bool { + const T = @TypeOf(func); + return @typeInfo(T).@"fn".params.len == 2; +} + +fn handlerNeedsErrorHandling(func: anytype) bool { + const T = @TypeOf(func); + const ret_info = @typeInfo(@typeInfo(T).@"fn".return_type orelse void); + return ret_info == .error_union; +} + fn exportFromHtml(self: *const Self, element: Element) void { comptime { switch (element) { .tag => |tag| for (tag.attributes) |attr| { if (attr.value.len > 0 and attr.value[0] == '@') { - const func = struct { - pub fn func(obj: *self.link) callconv(.c) void { - @field(self.link, attr.value[1..])(obj); - } - }.func; + const event_handler = @field(self.link, attr.value[1..]); + + const func = + if (handlerNeedsEvent(event_handler)) + struct { + pub fn func(obj: *self.link, event: *Object) callconv(.c) void { + if (comptime handlerNeedsErrorHandling(event_handler)) { + event_handler(obj, event) catch |err| { + std.log.err("error: {}", .{err}); + }; + } else { + event_handler(obj, event); + } + } + }.func + else + struct { + pub fn func(obj: *self.link) callconv(.c) void { + if (comptime handlerNeedsErrorHandling(event_handler)) { + event_handler(obj) catch |err| { + std.log.err("error: {}", .{err}); + }; + } else { + event_handler(obj); + } + } + }.func; @export( &func, @@ -74,11 +107,12 @@ fn linkAttributes(self: *const Self, attributes: []const Attribute) []const Attr for (attributes) |*attr| { if (attr.value.len > 0 and attr.value[0] == '@') { + const event_handler = @field(self.link, attr.value[1..]); const attribute: []const Attribute = &.{.{ .name = attr.name, .value = std.fmt.comptimePrint( - "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])", - .{ self.name, attr.value[1..] }, + "wasm_handle_event(\"export::{s}::{s}\", event, {})", + .{ self.name, attr.value[1..], handlerNeedsEvent(event_handler) }, ), }}; linked = linked ++ attribute; -- cgit v1.2.3-70-g09d2