From 0a2f6f8cc48258fca60df2f193790b2d94a9d0f6 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 8 Oct 2026 06:22:41 +0200 Subject: wasm add resources tracking --- src/app/components/hello/index.css | 12 +++++++++++ src/app/components/hello/index.html | 2 ++ src/app/components/hello/root.zig | 15 ++++++++++++-- src/app/root.zig | 1 + src/app/theme.css | 17 ++++++++++++++++ src/z/App/main.js | 2 +- src/z/App/root.zig | 2 ++ src/z/Component.zig | 40 ++++++++++++++++++++++++++++++++----- 8 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/app/theme.css diff --git a/src/app/components/hello/index.css b/src/app/components/hello/index.css index e69de29..7b316cb 100644 --- a/src/app/components/hello/index.css +++ b/src/app/components/hello/index.css @@ -0,0 +1,12 @@ +div { + font-family: 'sans-serif'; + cursor: pointer; + user-select: none; + background: var(--background); + color: var(--color); + display: block; + width: fit-content; + padding: 10px 10px; + border-radius: 4px; + margin: 4px; +} diff --git a/src/app/components/hello/index.html b/src/app/components/hello/index.html index e69de29..8b6b6a4 100644 --- a/src/app/components/hello/index.html +++ b/src/app/components/hello/index.html @@ -0,0 +1,2 @@ +
+
diff --git a/src/app/components/hello/root.zig b/src/app/components/hello/root.zig index 6f68550..55bf2e6 100644 --- a/src/app/components/hello/root.zig +++ b/src/app/components/hello/root.zig @@ -1,6 +1,8 @@ const std = @import("std"); const z = @import("z"); +const Self = @This(); + pub const component: z.Component = .{ .name = "hello", .html = .native( @@ -12,6 +14,15 @@ pub const component: z.Component = .{ .link = @This(), }; -pub fn clicked() void { - z.log.js.debug("hi"); +element: *z.Object, +counter: usize, + +pub fn init(self: *Self, element: *z.Object) void { + self.element = element; + self.counter = 0; +} + +pub fn clicked(self: *Self) void { + self.counter += 1; + std.log.info("clicked {} times", .{self.counter}); } diff --git a/src/app/root.zig b/src/app/root.zig index f587cfb..eefc374 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -17,6 +17,7 @@ pub const app: z.App(.{ @import("components/hello/root.zig").component, }, .body = .native(.@"app-hello", .{}, .content("")), + .theme = @embedFile("theme.css"), }) = .init; comptime { diff --git a/src/app/theme.css b/src/app/theme.css new file mode 100644 index 0000000..de933dc --- /dev/null +++ b/src/app/theme.css @@ -0,0 +1,17 @@ + +:root { + --background: #282828; + --color: #ebdbb2; +} + +body { + width: 100vw; + height: 100vh; + background: #202020; + margin: 0; + overflow: hidden; +} + +div { + background: var(--background); +} diff --git a/src/z/App/main.js b/src/z/App/main.js index 34df302..5e0c176 100644 --- a/src/z/App/main.js +++ b/src/z/App/main.js @@ -18,7 +18,7 @@ const imports = { } } -WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( +const wasm_ready = WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then( ({ instance, module }) => { window.wasm = { exports: instance.exports, diff --git a/src/z/App/root.zig b/src/z/App/root.zig index a3d0916..42ef26f 100644 --- a/src/z/App/root.zig +++ b/src/z/App/root.zig @@ -8,6 +8,7 @@ pub const Options = struct { globals: []const Global, components: []const Component, body: h.Element, + theme: []const u8, pub const Global = struct { name: []const u8, @@ -56,6 +57,7 @@ pub fn App(comptime options: Options) type { .name = "viewport", .content = "width=device-width, initial-scale=1.0", }, .void), + .native(.style, .{}, .content(options.theme)), })), .native(.body, .{}, .children(&.{ .native(.script, .{}, .content(@embedFile("object-store.js"))), 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} ), ), ), -- cgit v1.2.3-70-g09d2