diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-06 14:12:03 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-06 14:12:03 +0200 |
| commit | a4b2398aba2211ce5901ceb46f73057864bd1707 (patch) | |
| tree | 99c3d277ad991c189bf342238509be59b36b7889 /src/z/App/root.zig | |
| parent | dccd1987b418e47b4b82eda3aeaed40be18d9e45 (diff) | |
add global registry
Diffstat (limited to 'src/z/App/root.zig')
| -rw-r--r-- | src/z/App/root.zig | 66 |
1 files changed, 66 insertions, 0 deletions
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() = .{}; + }; + } +} |