aboutsummaryrefslogtreecommitdiff
path: root/src/z
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-08 06:22:41 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-08 06:22:41 +0200
commit0a2f6f8cc48258fca60df2f193790b2d94a9d0f6 (patch)
treeebc38d94f1e4d9e5c2cb30a26db5d4ec6c9b38b5 /src/z
parent4e86e3e18e9b43e442a35361ac82b30dd6c0103a (diff)
wasm add resources tracking
Diffstat (limited to 'src/z')
-rw-r--r--src/z/App/main.js2
-rw-r--r--src/z/App/root.zig2
-rw-r--r--src/z/Component.zig40
3 files changed, 38 insertions, 6 deletions
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}
),
),
),