aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/App
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 11:25:14 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-15 11:25:14 +0200
commita1dc55be6e695a1c04e5e9e0f1ce7406a5dd3dff (patch)
tree46e78cce4fcc9ec7f04b3ae69bf53d9a81710dbe /src/zilch/App
parent0b2af471b6d5dfc4eaf53639102c1b854bbb324b (diff)
add function struct to register js functions dynamically
Diffstat (limited to 'src/zilch/App')
-rw-r--r--src/zilch/App/object-store.js49
-rw-r--r--src/zilch/App/root.zig25
2 files changed, 26 insertions, 48 deletions
diff --git a/src/zilch/App/object-store.js b/src/zilch/App/object-store.js
index 096d085..d1fe42f 100644
--- a/src/zilch/App/object-store.js
+++ b/src/zilch/App/object-store.js
@@ -17,17 +17,8 @@ class ObjectStore {
"undefined": 6,
});
- static methods = Object.freeze({
- 0: 'free',
- 1: 'getProp',
- 2: 'setProp',
- 3: 'delProp',
- 4: 'callMethod',
- 5: 'call',
- 6: 'new',
- });
-
static store = { 0: null };
+ static methods = {};
static generateFreeId() {
let id = 0;
@@ -39,49 +30,19 @@ class ObjectStore {
return id;
}
- static free(target) {
- const id = target[ZsId];
- delete target[ZsId];
- delete ObjectStore.store[id];
- }
-
- static getProp(target, key) {
- return target[key];
- }
-
- static setProp(target, key, value) {
- target[key] = value;
- }
-
- static delProp(target, key) {
- delete target[key];
- }
-
- static callMethod(target, method, ...args) {
- return target[method](...args);
- }
-
- static call(target, args) {
- return target(args);
- }
-
- static new(target, ...args) {
- return new target(...args);
- }
-
static decodeString(base, len) {
const slice = new Uint8Array(wasm.memory.buffer, base, len);
return new TextDecoder().decode(slice);
}
static decodeCall(view, offset) {
- const kind = ObjectStore.methods[view.getUint8(offset, true)]
+ const kind = ObjectStore.methods[view.getUint32(offset, true)]
if (!kind) throw "unknown method, cannot decode";
return {
kind: kind,
- num_args: view.getUint8(offset + 1, true),
+ num_args: view.getUint8(offset + 4, true),
}
}
@@ -192,7 +153,7 @@ class ObjectStore {
for (let i = 0; i < length; ++i) {
const method = ObjectStore.decodeCall(calls_view, offset.call);
- offset.call += 2;
+ offset.call += 8;
let args = new Array(method.num_args).fill().map(_ => {
const value = ObjectStore.decodeValue(values_view, offset.value);
@@ -200,7 +161,7 @@ class ObjectStore {
return value;
});
- const result = ObjectStore[method.kind](...args);
+ const result = method.kind(...args);
const result_ptr = results_view.getUint32(offset.result, true);
offset.result += 4;
diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig
index 52cbfc2..94f311b 100644
--- a/src/zilch/App/root.zig
+++ b/src/zilch/App/root.zig
@@ -1,12 +1,15 @@
const builtin = @import("builtin");
const std = @import("std");
-const Element = @import("../root.zig").html.Element;
-const Document = @import("../root.zig").html.Document;
-const Fragment = @import("../root.zig").html.Fragment;
-const Component = @import("../root.zig").Component;
+const mod = @import("../root.zig");
+const Element = mod.html.Element;
+const Document = mod.html.Document;
+const Fragment = mod.html.Fragment;
+const Component = mod.Component;
+const Env = mod.Env;
pub const Options = struct {
name: []const u8,
+ functions: []const Env.Function,
globals: []const Global,
components: []const Component,
body: Fragment,
@@ -41,6 +44,7 @@ pub fn App(comptime options: Options) type {
break :g gl;
},
+ Methods: type = enum(u16) {},
pub const init: @This() = .{};
@@ -61,6 +65,18 @@ pub fn App(comptime options: Options) type {
break :comp cs;
});
+ 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 ++ "}";
+
+ break :func buf;
+ };
+
return struct {
const node: Document = .{
.root = .node("html", .{}, &.{
@@ -73,6 +89,7 @@ pub fn App(comptime options: Options) type {
.node("style", .{}, &.{.text(options.theme)}),
.node("script", .{}, &.{.text(@embedFile("symbols.js"))}),
.node("script", .{}, &.{.text(@embedFile("object-store.js"))}),
+ .node("script", .{}, &.{.text(functions)}),
.node("script", .{}, &.{.text(@embedFile("main.js"))}),
.node("script", .{}, &.{.text(reg: {
var register_code: []const u8 = "";