diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 06:22:41 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-08 06:22:41 +0200 |
| commit | 0a2f6f8cc48258fca60df2f193790b2d94a9d0f6 (patch) | |
| tree | ebc38d94f1e4d9e5c2cb30a26db5d4ec6c9b38b5 /src/z/Component.zig | |
| parent | 4e86e3e18e9b43e442a35361ac82b30dd6c0103a (diff) | |
wasm add resources tracking
Diffstat (limited to 'src/z/Component.zig')
| -rw-r--r-- | src/z/Component.zig | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/z/Component.zig b/src/z/Component.zig index 2bc293e..f3a1a99 100644 --- a/src/z/Component.zig +++ b/src/z/Component.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Element = @import("html.zig").Element; +const Object = @import("Object.zig"); const Self = @This(); @@ -13,8 +14,8 @@ fn exportFromHtml(self: *const Self, element: Element) void { for (element.attributes) |attr| { if (attr.value.len > 0 and attr.value[0] == '@') { const func = struct { - pub fn func() callconv(.c) void { - @field(self.link, attr.value[1..])(); + pub fn func(obj: *self.link) callconv(.c) void { + @field(self.link, attr.value[1..])(obj); } }.func; @@ -33,6 +34,24 @@ fn exportFromHtml(self: *const Self, element: Element) void { } pub fn exportLinks(self: *const Self) void { + const init = struct { + pub fn init(element: *Object) callconv(.c) ?*self.link { + const obj = std.heap.wasm_allocator.create(self.link) catch return null; + obj.init(element); + return obj; + } + }.init; + + @export( + &init, + .{ + .name = std.fmt.comptimePrint( + "export::{s}::init", + .{ self.name }, + ), + }, + ); + comptime self.exportFromHtml(self.html); } @@ -48,7 +67,7 @@ fn linkHtmlRecursive(self: *const Self, element: Element) Element { const attribute: []const Element.Attribute = &.{.{ .name = attr.name, .value = std.fmt.comptimePrint( - "wasm.exports[\"export::{s}::{s}\"]()", + "wasm.exports[\"export::{s}::{s}\"](this[\"@ptr\"])", .{ self.name, attr.value[1..] }, ), }}; @@ -69,7 +88,10 @@ pub fn toElement(self: *const Self) Element { .native( .template, .{ .id = "@" ++ self.name }, - .children(&.{self.linkHtmlRecursive(self.html)}), + .children(&.{ + .native(.style, .{}, .content(self.css)), + self.linkHtmlRecursive(self.html) + }), ), .native( .script, @@ -83,10 +105,18 @@ pub fn toElement(self: *const Self) Element { \\ const template = document.getElementById("@{s}"); \\ const node = document.importNode(template.content, true); \\ this.shadowRoot.appendChild(node); + \\ this['@ptr'] = null; + \\ }} + \\ connectedCallback() {{ + \\ wasm_ready.then(() => {{ + \\ const id = ObjectStore.generateFreeId(); + \\ ObjectStore.registerObject(id, this); + \\ this['@ptr'] = wasm.exports['export::{s}::init'](id) + \\ }}); \\ }} \\}}) , - .{self.name, self.name} + .{self.name, self.name, self.name} ), ), ), |