From a4b2398aba2211ce5901ceb46f73057864bd1707 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 6 Oct 2026 14:12:03 +0200 Subject: add global registry --- src/z/App/root.zig | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/z/App/root.zig (limited to 'src/z/App/root.zig') diff --git a/src/z/App/root.zig b/src/z/App/root.zig new file mode 100644 index 0000000..d684248 --- /dev/null +++ b/src/z/App/root.zig @@ -0,0 +1,66 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const h = @import("html"); + +pub const Options = struct { name: []const u8 }; + +pub fn App(comptime options: Options, globals: anytype) type { + const info = @typeInfo(@TypeOf(globals)).@"struct"; + + if (builtin.target.cpu.arch.isWasm()) { + var names: [info.fields.len][]const u8 = undefined; + var types: [info.fields.len]type = undefined; + var attrs: [info.fields.len]std.builtin.Type.StructField.Attributes = undefined; + + for (info.fields, 0..) |field, index| { + names[index] = field.name; + types[index] = @field(globals, field.name); + attrs[index] = .{}; + } + + const G = @Struct(.auto, null, &names, &types, &attrs); + return struct { + globals: G = g: { + var gl: G = undefined; + + for (info.fields, 1..) |field, id| { + @field(gl, field.name) = @ptrFromInt(id); + } + + break :g gl; + }, + + pub const init: @This() = .{}; + }; + } else { + return struct { + html: []const u8 = h.Element.native(.html, .{}, .children(&.{ + .native(.head, .{}, .children(&.{ + .native(.title, .{}, .content(options.name)), + .native(.meta, .{ + .name = "viewport", + .content = "width=device-width, initial-scale=1.0", + }, .void), + })), + .native(.body, .{}, .children(&.{ + .native(.script, .{}, .content(@embedFile("object-store.js"))), + .native(.script, .{}, .content(reg: { + var register_code: []const u8 = ""; + + for (info.fields, 1..) |field, id| { + register_code = register_code ++ std.fmt.comptimePrint( + "ObjectStore.registerObject({}, {s});", + .{ id, field.name }, + ); + } + + break :reg register_code; + })), + .native(.script, .{}, .content(@embedFile("main.js"))), + })), + })).toDocument(), + + pub const init: @This() = .{}; + }; + } +} -- cgit v1.2.3-70-g09d2