diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 16:00:59 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-26 16:00:59 +0100 |
| commit | 8dd8f96b24710dc4f2d29065928244a40a432379 (patch) | |
| tree | 192fcdbef2373a983673e929835bff64ad8edb48 | |
| parent | feeedb0bf31314b182e7bbc135182f6e0bc859b2 (diff) | |
zilch: update App structure and split-up document and wasm logic
| -rw-r--r-- | src/zilch/App/root.zig | 136 | ||||
| -rw-r--r-- | src/zilch/app/Options.zig | 17 | ||||
| -rw-r--r-- | src/zilch/app/event.js (renamed from src/zilch/App/event.js) | 0 | ||||
| -rw-r--r-- | src/zilch/app/load.js (renamed from src/zilch/App/load.js) | 0 | ||||
| -rw-r--r-- | src/zilch/app/object-store.js (renamed from src/zilch/App/object-store.js) | 0 | ||||
| -rw-r--r-- | src/zilch/app/root.document.zig | 87 | ||||
| -rw-r--r-- | src/zilch/app/root.wasm.zig | 36 | ||||
| -rw-r--r-- | src/zilch/app/root.zig | 15 | ||||
| -rw-r--r-- | src/zilch/app/symbols.js (renamed from src/zilch/App/symbols.js) | 0 | ||||
| -rw-r--r-- | src/zilch/root.zig | 4 |
10 files changed, 157 insertions, 138 deletions
diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig deleted file mode 100644 index e40b4e0..0000000 --- a/src/zilch/App/root.zig +++ /dev/null @@ -1,136 +0,0 @@ -const builtin = @import("builtin"); -const std = @import("std"); -const mod = @import("../root.zig"); -const Element = mod.html.Element; -const Document = mod.html.Document; -const Fragment = mod.html.Fragment; -const Component = mod.Component; -const Env = mod.Env; - -pub const Options = struct { - name: []const u8, - favicon: []const u8, - functions: []const Env.Function, - globals: []const Global, - components: []const Component, - body: Fragment, - theme: []const u8, - - pub const Global = struct { - name: []const u8, - type: type, - }; -}; - -pub fn App(comptime options: Options) type { - if (builtin.target.cpu.arch.isWasm()) { - var names: [options.globals.len][]const u8 = undefined; - var types: [options.globals.len]type = undefined; - var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined; - - for (options.globals, 0..) |field, index| { - names[index] = field.name; - types[index] = field.type; - attrs[index] = .{}; - } - - const G = @Struct(.auto, null, &names, &types, &attrs); - return struct { - globals: G = g: { - var gl: G = undefined; - - for (options.globals, 1..) |field, id| { - @field(gl, field.name) = @ptrFromInt(id); - } - - break :g gl; - }, - - pub const init: @This() = .{}; - - comptime { - for (options.components) |component| { - component.exportLinks(); - } - } - }; - } else { - 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, - }); - } - - 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 ++ "}"; - - break :func buf; - }; - - return struct { - const node: Document = .{ - .root = .node("html", .{}, &.{ - .node("head", .{}, &.{ - .node("title", .{}, &.{.text(options.name)}), - .void("link", .{ .rel = "icon", .href = options.favicon }), - .void("meta", .{ - .name = "viewport", - .content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0", - }), - .void("meta", .{ .charset = "utf8" }), - .node("style", .{}, &.{.text(options.theme)}), - .node("script", .{}, &.{.text(@embedFile("symbols.js"))}), - .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), - .node("script", .{}, &.{.text(functions)}), - .node("script", .{}, &.{.text(@embedFile("event.js"))}), - .node("script", .{}, &.{.text(@embedFile("load.js"))}), - .node("script", .{}, &.{.text(reg: { - var register_code: []const u8 = ""; - - for (options.globals, 1..) |field, id| { - register_code = register_code ++ std.fmt.comptimePrint( - "ObjectStore.registerObject({}, {s});", - .{ id, field.name }, - ); - } - - break :reg register_code; - })}), - }), - .node( - "body", - .{}, - &[_]Element{ - .node("script", .{}, &.{.text(registry)}), - } ++ components ++ options.body.elements, - ), - }), - }; - - html: []const u8 = str: { - @setEvalBranchQuota(100_000); - break :str std.fmt.comptimePrint("{f}", .{node}); - }, - - pub const init: @This() = .{}; - }; - } -} diff --git a/src/zilch/app/Options.zig b/src/zilch/app/Options.zig new file mode 100644 index 0000000..db1742d --- /dev/null +++ b/src/zilch/app/Options.zig @@ -0,0 +1,17 @@ +const mod = @import("../root.zig"); +const Fragment = mod.html.Fragment; +const Component = mod.Component; +const Env = mod.Env; + +name: []const u8, +favicon: []const u8, +functions: []const Env.Function, +globals: []const Global, +components: []const Component, +body: Fragment, +theme: []const u8, + +pub const Global = struct { + name: []const u8, + type: type, +}; diff --git a/src/zilch/App/event.js b/src/zilch/app/event.js index c55637c..c55637c 100644 --- a/src/zilch/App/event.js +++ b/src/zilch/app/event.js diff --git a/src/zilch/App/load.js b/src/zilch/app/load.js index e93a4a6..e93a4a6 100644 --- a/src/zilch/App/load.js +++ b/src/zilch/app/load.js diff --git a/src/zilch/App/object-store.js b/src/zilch/app/object-store.js index 9a68478..9a68478 100644 --- a/src/zilch/App/object-store.js +++ b/src/zilch/app/object-store.js diff --git a/src/zilch/app/root.document.zig b/src/zilch/app/root.document.zig new file mode 100644 index 0000000..8e2ca61 --- /dev/null +++ b/src/zilch/app/root.document.zig @@ -0,0 +1,87 @@ +const std = @import("std"); + +const mod = @import("../root.zig"); +const Element = mod.html.Element; +const Document = mod.html.Document; + +const Options = @import("Options.zig"); + +pub fn App(comptime options: Options) type { + 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, + }); + } + + 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 ++ "}"; + + break :func buf; + }; + + return struct { + const node: Document = .{ + .root = .node("html", .{}, &.{ + .node("head", .{}, &.{ + .node("title", .{}, &.{.text(options.name)}), + .void("link", .{ .rel = "icon", .href = options.favicon }), + .void("meta", .{ + .name = "viewport", + .content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0", + }), + .void("meta", .{ .charset = "utf8" }), + .node("style", .{}, &.{.text(options.theme)}), + .node("script", .{}, &.{.text(@embedFile("symbols.js"))}), + .node("script", .{}, &.{.text(@embedFile("object-store.js"))}), + .node("script", .{}, &.{.text(functions)}), + .node("script", .{}, &.{.text(@embedFile("event.js"))}), + .node("script", .{}, &.{.text(@embedFile("load.js"))}), + .node("script", .{}, &.{.text(reg: { + var register_code: []const u8 = ""; + + for (options.globals, 1..) |field, id| { + register_code = register_code ++ std.fmt.comptimePrint( + "ObjectStore.registerObject({}, {s});", + .{ id, field.name }, + ); + } + + break :reg register_code; + })}), + }), + .node( + "body", + .{}, + &[_]Element{ + .node("script", .{}, &.{.text(registry)}), + } ++ components ++ options.body.elements, + ), + }), + }; + + html: []const u8 = str: { + @setEvalBranchQuota(100_000); + break :str std.fmt.comptimePrint("{f}", .{node}); + }, + + pub const init: @This() = .{}; + }; +} diff --git a/src/zilch/app/root.wasm.zig b/src/zilch/app/root.wasm.zig new file mode 100644 index 0000000..ea04192 --- /dev/null +++ b/src/zilch/app/root.wasm.zig @@ -0,0 +1,36 @@ +const std = @import("std"); + +const Options = @import("Options.zig"); + +pub fn App(comptime options: Options) type { + var names: [options.globals.len][]const u8 = undefined; + var types: [options.globals.len]type = undefined; + var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined; + + for (options.globals, 0..) |field, index| { + names[index] = field.name; + types[index] = field.type; + attrs[index] = .{}; + } + + const G = @Struct(.auto, null, &names, &types, &attrs); + return struct { + globals: G = g: { + var gl: G = undefined; + + for (options.globals, 1..) |field, id| { + @field(gl, field.name) = @ptrFromInt(id); + } + + break :g gl; + }, + + pub const init: @This() = .{}; + + comptime { + for (options.components) |component| { + component.exportLinks(); + } + } + }; +} diff --git a/src/zilch/app/root.zig b/src/zilch/app/root.zig new file mode 100644 index 0000000..b2a8863 --- /dev/null +++ b/src/zilch/app/root.zig @@ -0,0 +1,15 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +const wasm = @import("root.wasm.zig"); +const document = @import("root.document.zig"); + +pub const Options = @import("Options.zig"); + +pub fn App(comptime options: Options) type { + if (builtin.target.cpu.arch.isWasm()) { + return wasm.App(options); + } else { + return document.App(options); + } +} diff --git a/src/zilch/App/symbols.js b/src/zilch/app/symbols.js index c0870c5..c0870c5 100644 --- a/src/zilch/App/symbols.js +++ b/src/zilch/app/symbols.js diff --git a/src/zilch/root.zig b/src/zilch/root.zig index 25dfae7..e6112ae 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -14,8 +14,8 @@ pub const Object = @import("Object.zig"); pub const Promise = @import("Promise.zig"); pub const Callable = @import("Callable.zig"); -pub const App = @import("App/root.zig").App; -pub const Options = @import("App/root.zig").Options; +pub const App = @import("app/root.zig").App; +pub const Options = @import("app/root.zig").Options; pub const Component = @import("Component.zig"); pub const panic = @import("panic.zig").panic; |