diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-27 17:45:13 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2026-04-27 17:45:13 +0200 |
| commit | 3b0e9424a66058da82d11d432da886ec7b6ce7eb (patch) | |
| tree | 0ad906e3b7d945405cdfeb9ff95b02546e1ed4bb /src/eval/Root.zig | |
| parent | ef5c0341ca15f6862294802103b02992b29609e8 (diff) | |
perf(eval): reduce memory syscall
Diffstat (limited to 'src/eval/Root.zig')
| -rw-r--r-- | src/eval/Root.zig | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/eval/Root.zig b/src/eval/Root.zig new file mode 100644 index 0000000..1834dd0 --- /dev/null +++ b/src/eval/Root.zig @@ -0,0 +1,53 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const Arena = std.heap.ArenaAllocator; +const HTML = @import("html/Element.zig"); +const Element = @import("Element.zig"); + +const Self = @This(); + +content: std.ArrayList(Element), +arena: Arena, + +pub fn init(parent: Allocator) !*Self { + var s = Self{ + .content = undefined, + .arena = .init(parent), + }; + var alloc = s.arena.allocator(); + s.content = try .initCapacity(alloc, 2); + const v = try alloc.create(Self); + v.* = s; + return v; +} + +pub fn deinit(self: *Self) void { + self.arena.deinit(); +} + +pub fn allocator(self: *Self) Allocator { + return self.arena.allocator(); +} + +pub fn append(self: *Self, el: Element) !void { + try self.content.append(self.allocator(), el); +} + +pub fn element(self: *Self) Element { + return .{ .vtable = .{ .html = html }, .ptr = self }; +} + +pub fn renderHTML(self: *Self, alloc: Allocator) HTML.Error![]const u8 { + return try self.element().renderHTML(alloc); +} + +fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML { + const self: *Self = @ptrCast(@alignCast(context)); + const el = try HTML.Root.init(alloc); + errdefer el.deinit(); + if (self.content.items.len == 0) return el.element(); + for (self.content.items) |it| { + try el.append(try it.html(el.allocator())); + } + return el.element(); +} |
