aboutsummaryrefslogtreecommitdiff
path: root/src/zilch
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch')
-rw-r--r--src/zilch/App/root.zig16
-rw-r--r--src/zilch/Component.zig71
-rw-r--r--src/zilch/component.template.js18
-rw-r--r--src/zilch/panic.zig9
-rw-r--r--src/zilch/root.zig7
5 files changed, 80 insertions, 41 deletions
diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig
index a7700d9..01e2695 100644
--- a/src/zilch/App/root.zig
+++ b/src/zilch/App/root.zig
@@ -45,7 +45,6 @@ pub fn App(comptime options: Options) type {
break :g gl;
},
- Methods: type = enum(u16) {},
pub const init: @This() = .{};
@@ -56,21 +55,29 @@ pub fn App(comptime options: Options) type {
}
};
} else {
- const components: []const Element = (comp: {
+ 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,
+ });
}
- break :comp cs;
+ 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 ++ std.fmt.comptimePrint("{}: {s},", .{ function.id, function.content });
}
buf = buf ++ "}";
@@ -111,6 +118,7 @@ pub fn App(comptime options: Options) type {
"body",
.{},
&[_]Element{
+ .node("script", .{}, &.{.text(registry)}),
} ++ components ++ options.body.elements,
),
}),
diff --git a/src/zilch/Component.zig b/src/zilch/Component.zig
index 35fafbe..592b464 100644
--- a/src/zilch/Component.zig
+++ b/src/zilch/Component.zig
@@ -3,14 +3,37 @@ const Fragment = @import("root.zig").html.Fragment;
const Element = @import("root.zig").html.Element;
const Attribute = @import("root.zig").html.Attribute;
const Object = @import("Object.zig");
+const Env = @import("root.zig").Env;
+const Function = Env.Function;
+const Future = Env.Future;
const Self = @This();
-namespace: []const u8 = "app",
+pub const Options = struct {
+ namespace: []const u8 = "app",
+ name: []const u8,
+ html: Fragment,
+ css: []const u8,
+ link: type,
+};
+
+namespace: []const u8,
name: []const u8,
html: Fragment,
css: []const u8,
link: type,
+id: u32,
+
+pub fn build(comptime options: Options) Self {
+ return .{
+ .namespace = options.namespace,
+ .name = options.name,
+ .html = options.html,
+ .css = options.css,
+ .link = options.link,
+ .id = std.hash.XxHash32.hash(4654, options.namespace ++ "-" ++ options.name),
+ };
+}
fn handlerNeedsEvent(func: anytype) bool {
const T = @TypeOf(func);
@@ -93,25 +116,6 @@ pub fn exportLinks(self: *const Self) void {
},
);
- const deinit = struct {
- pub fn deinit(obj: *self.link) callconv(.c) void {
- if (@hasDecl(self.link, "deinit")) {
- wrapHandler(obj.deinit());
- }
- std.heap.wasm_allocator.destroy(obj);
- }
- }.deinit;
-
- @export(
- &deinit,
- .{
- .name = std.fmt.comptimePrint(
- "export::{s}::deinit",
- .{self.name},
- ),
- },
- );
-
for (self.html.elements) |element| {
comptime self.exportFromHtml(element);
}
@@ -188,9 +192,34 @@ pub fn toElement(self: *const Self) []const Element {
&.{.text(
std.fmt.comptimePrint(
@embedFile("component.template.js"),
- .{ self.namespace, self.name, self.name, self.name, self.name },
+ .{
+ self.namespace,
+ self.name,
+ self.name,
+ self.name,
+ },
),
)},
),
};
}
+
+pub fn new(self: Self, obj: *self.link, env: *Env) Future(*Object) {
+ return .fromAny(env.call(bindings.new, .{ @as(usize, @intCast(self.id)), @intFromPtr(obj) }));
+}
+
+const bindings = struct {
+ pub const new: Function = .from(
+ \\function(name_id, ptr) {
+ \\ const el = document.createElement(component_registry[name_id]);
+ \\ el[ZsPtr] = ptr;
+ \\ const id = ObjectStore.generateFreeId();
+ \\ ObjectStore.registerObject(id, el);
+ \\ return el;
+ \\}
+ );
+};
+
+pub const functions = [_]Function{
+ bindings.new,
+};
diff --git a/src/zilch/component.template.js b/src/zilch/component.template.js
index e8cb914..6addef2 100644
--- a/src/zilch/component.template.js
+++ b/src/zilch/component.template.js
@@ -8,14 +8,12 @@ customElements.define("{s}-{s}", class extends HTMLElement {{
this[ZsPtr] = null;
}}
connectedCallback() {{
- wasm_ready.then(() => {{
- const id = ObjectStore.generateFreeId();
- ObjectStore.registerObject(id, this);
- this[ZsPtr] = wasm.exports['export::{s}::init'](id);
- }});
+ if (this[ZsPtr] === null) {{
+ wasm_ready.then(() => {{
+ const id = ObjectStore.generateFreeId();
+ ObjectStore.registerObject(id, this);
+ this[ZsPtr] = wasm.exports['export::{s}::init'](id, this[ZsPtr]);
+ }});
+ }}
}}
- disconnectedCallback() {{
- wasm.exports['export::{s}::deinit'](this[ZsId]);
- ObjectStore.free(this);
- }}
-}})
+}});
diff --git a/src/zilch/panic.zig b/src/zilch/panic.zig
new file mode 100644
index 0000000..7163c6d
--- /dev/null
+++ b/src/zilch/panic.zig
@@ -0,0 +1,9 @@
+const log = @import("log/root.zig");
+const std = @import("std");
+
+fn defaultPanic(msg: []const u8, _: ?usize) noreturn {
+ log.js.err(msg);
+ @trap();
+}
+
+pub const panic = std.debug.FullPanic(defaultPanic);
diff --git a/src/zilch/root.zig b/src/zilch/root.zig
index 9bca612..25dfae7 100644
--- a/src/zilch/root.zig
+++ b/src/zilch/root.zig
@@ -18,12 +18,7 @@ pub const App = @import("App/root.zig").App;
pub const Options = @import("App/root.zig").Options;
pub const Component = @import("Component.zig");
-fn defaultPanic(msg: []const u8, _: ?usize) noreturn {
- log.js.err(msg);
- @trap();
-}
-
-pub const panic = std.debug.FullPanic(defaultPanic);
+pub const panic = @import("panic.zig").panic;
const Ptr = struct { o: *anyopaque };
comptime {