From 91d9b6992ac0ebee13704a3faefa8fd41a00075e Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 26 Oct 2026 09:23:55 +0100 Subject: zilch: dynamic element creation --- src/zilch/App/root.zig | 16 +++++++--- src/zilch/Component.zig | 71 +++++++++++++++++++++++++++++------------ src/zilch/component.template.js | 18 +++++------ src/zilch/panic.zig | 9 ++++++ src/zilch/root.zig | 7 +--- 5 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 src/zilch/panic.zig (limited to 'src/zilch') diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig index a7700d9..01e2695 100644 --- a/src/zilch/App/root.zig +++ b/src/zilch/App/root.zig @@ -45,7 +45,6 @@ pub fn App(comptime options: Options) type { break :g gl; }, - Methods: type = enum(u16) {}, pub const init: @This() = .{}; @@ -56,21 +55,29 @@ pub fn App(comptime options: Options) type { } }; } else { - const components: []const Element = (comp: { + const components: []const Element, const registry: []const u8 = (comp: { var cs: []const Element = &.{}; + var registry: []const u8 = "const component_registry = {"; for (options.components) |component| { cs = cs ++ component.toElement(); + registry = registry ++ std.fmt.comptimePrint("{}: '{s}-{s}',", .{ + component.id, + component.namespace, + component.name, + }); } - break :comp cs; + registry = registry ++ "};"; + + break :comp .{ cs, registry }; }); const functions: []const u8 = func: { 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("{}: {s},", .{ function.id, function.content }); } buf = buf ++ "}"; @@ -111,6 +118,7 @@ pub fn App(comptime options: Options) type { "body", .{}, &[_]Element{ + .node("script", .{}, &.{.text(registry)}), } ++ components ++ options.body.elements, ), }), diff --git a/src/zilch/Component.zig b/src/zilch/Component.zig index 35fafbe..592b464 100644 --- a/src/zilch/Component.zig +++ b/src/zilch/Component.zig @@ -3,14 +3,37 @@ const Fragment = @import("root.zig").html.Fragment; const Element = @import("root.zig").html.Element; const Attribute = @import("root.zig").html.Attribute; const Object = @import("Object.zig"); +const Env = @import("root.zig").Env; +const Function = Env.Function; +const Future = Env.Future; const Self = @This(); -namespace: []const u8 = "app", +pub const Options = struct { + namespace: []const u8 = "app", + name: []const u8, + html: Fragment, + css: []const u8, + link: type, +}; + +namespace: []const u8, name: []const u8, html: Fragment, css: []const u8, link: type, +id: u32, + +pub fn build(comptime options: Options) Self { + return .{ + .namespace = options.namespace, + .name = options.name, + .html = options.html, + .css = options.css, + .link = options.link, + .id = std.hash.XxHash32.hash(4654, options.namespace ++ "-" ++ options.name), + }; +} fn handlerNeedsEvent(func: anytype) bool { const T = @TypeOf(func); @@ -93,25 +116,6 @@ pub fn exportLinks(self: *const Self) void { }, ); - const deinit = struct { - pub fn deinit(obj: *self.link) callconv(.c) void { - if (@hasDecl(self.link, "deinit")) { - wrapHandler(obj.deinit()); - } - std.heap.wasm_allocator.destroy(obj); - } - }.deinit; - - @export( - &deinit, - .{ - .name = std.fmt.comptimePrint( - "export::{s}::deinit", - .{self.name}, - ), - }, - ); - for (self.html.elements) |element| { comptime self.exportFromHtml(element); } @@ -188,9 +192,34 @@ pub fn toElement(self: *const Self) []const Element { &.{.text( std.fmt.comptimePrint( @embedFile("component.template.js"), - .{ self.namespace, self.name, self.name, self.name, self.name }, + .{ + self.namespace, + self.name, + self.name, + self.name, + }, ), )}, ), }; } + +pub fn new(self: Self, obj: *self.link, env: *Env) Future(*Object) { + return .fromAny(env.call(bindings.new, .{ @as(usize, @intCast(self.id)), @intFromPtr(obj) })); +} + +const bindings = struct { + pub const new: Function = .from( + \\function(name_id, ptr) { + \\ const el = document.createElement(component_registry[name_id]); + \\ el[ZsPtr] = ptr; + \\ const id = ObjectStore.generateFreeId(); + \\ ObjectStore.registerObject(id, el); + \\ return el; + \\} + ); +}; + +pub const functions = [_]Function{ + bindings.new, +}; diff --git a/src/zilch/component.template.js b/src/zilch/component.template.js index e8cb914..6addef2 100644 --- a/src/zilch/component.template.js +++ b/src/zilch/component.template.js @@ -8,14 +8,12 @@ customElements.define("{s}-{s}", class extends HTMLElement {{ this[ZsPtr] = null; }} connectedCallback() {{ - wasm_ready.then(() => {{ - const id = ObjectStore.generateFreeId(); - ObjectStore.registerObject(id, this); - this[ZsPtr] = wasm.exports['export::{s}::init'](id); - }}); + if (this[ZsPtr] === null) {{ + wasm_ready.then(() => {{ + const id = ObjectStore.generateFreeId(); + ObjectStore.registerObject(id, this); + this[ZsPtr] = wasm.exports['export::{s}::init'](id, this[ZsPtr]); + }}); + }} }} - disconnectedCallback() {{ - wasm.exports['export::{s}::deinit'](this[ZsId]); - ObjectStore.free(this); - }} -}}) +}}); diff --git a/src/zilch/panic.zig b/src/zilch/panic.zig new file mode 100644 index 0000000..7163c6d --- /dev/null +++ b/src/zilch/panic.zig @@ -0,0 +1,9 @@ +const log = @import("log/root.zig"); +const std = @import("std"); + +fn defaultPanic(msg: []const u8, _: ?usize) noreturn { + log.js.err(msg); + @trap(); +} + +pub const panic = std.debug.FullPanic(defaultPanic); diff --git a/src/zilch/root.zig b/src/zilch/root.zig index 9bca612..25dfae7 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -18,12 +18,7 @@ pub const App = @import("App/root.zig").App; pub const Options = @import("App/root.zig").Options; pub const Component = @import("Component.zig"); -fn defaultPanic(msg: []const u8, _: ?usize) noreturn { - log.js.err(msg); - @trap(); -} - -pub const panic = std.debug.FullPanic(defaultPanic); +pub const panic = @import("panic.zig").panic; const Ptr = struct { o: *anyopaque }; comptime { -- cgit v1.2.3-70-g09d2