aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/App/root.zig
blob: 01e2695d8b629544b5d2d87cbcef54decb431230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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",
                        }),
                        .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() = .{};
        };
    }
}