diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-15 11:25:14 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-15 11:25:14 +0200 |
| commit | a1dc55be6e695a1c04e5e9e0f1ce7406a5dd3dff (patch) | |
| tree | 46e78cce4fcc9ec7f04b3ae69bf53d9a81710dbe /src | |
| parent | 0b2af471b6d5dfc4eaf53639102c1b854bbb324b (diff) | |
add function struct to register js functions dynamically
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/components/navbutton/root.zig | 1 | ||||
| -rw-r--r-- | src/app/root.zig | 1 | ||||
| -rw-r--r-- | src/zilch/App/object-store.js | 49 | ||||
| -rw-r--r-- | src/zilch/App/root.zig | 25 | ||||
| -rw-r--r-- | src/zilch/Env/Function.zig | 13 | ||||
| -rw-r--r-- | src/zilch/Env/call.zig | 13 | ||||
| -rw-r--r-- | src/zilch/Env/future.zig | 2 | ||||
| -rw-r--r-- | src/zilch/Env/js.zig | 2 | ||||
| -rw-r--r-- | src/zilch/Env/root.zig | 129 | ||||
| -rw-r--r-- | src/zilch/Object.zig | 62 | ||||
| -rw-r--r-- | src/zilch/root.zig | 6 |
11 files changed, 122 insertions, 181 deletions
diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig index 3505840..f2b71e6 100644 --- a/src/app/components/navbutton/root.zig +++ b/src/app/components/navbutton/root.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const app = @import("root").app; const zilch = @import("zilch"); const Self = @This(); diff --git a/src/app/root.zig b/src/app/root.zig index d172b23..25865d2 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -11,6 +11,7 @@ const components = @import("components/root.zig").components; pub const app: zilch.App(.{ .name = "Memora", + .functions = &zilch.Object.functions, .globals = &.{ zilch.Object.global(.window), zilch.Object.global(.Object), 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 = ""; diff --git a/src/zilch/Env/Function.zig b/src/zilch/Env/Function.zig new file mode 100644 index 0000000..692ce2b --- /dev/null +++ b/src/zilch/Env/Function.zig @@ -0,0 +1,13 @@ +const std = @import("std"); + +const Self = @This(); + +content: []const u8, +id: u32, + +pub fn from(content: []const u8) Self { + return .{ + .content = content, + .id = std.hash.XxHash32.hash(4654, content), + }; +} diff --git a/src/zilch/Env/call.zig b/src/zilch/Env/call.zig index a5db835..1681165 100644 --- a/src/zilch/Env/call.zig +++ b/src/zilch/Env/call.zig @@ -1,16 +1,5 @@ pub const Call = extern struct { - kind: Kind, + kind: u32, num_args: u8, - - const Kind = enum(u8) { - free = 0, - get_prop = 1, - set_prop = 2, - del_prop = 3, - call_method = 4, - call = 5, - new = 6, - }; }; - diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig index a014990..6e0cc63 100644 --- a/src/zilch/Env/future.zig +++ b/src/zilch/Env/future.zig @@ -44,7 +44,7 @@ pub const AnyFuture = union(enum) { switch (self.*) { .err => |err| return err, .result => |result| { - defer env.pool.destroy(result); + defer env.release(self.*); if (result.kind != .uninit) { return result.*; diff --git a/src/zilch/Env/js.zig b/src/zilch/Env/js.zig index c1ff85d..ed8592a 100644 --- a/src/zilch/Env/js.zig +++ b/src/zilch/Env/js.zig @@ -2,7 +2,7 @@ const std = @import("std"); const Env = @import("root.zig"); pub extern "object_store" fn process( - [*]const Env.Call, + [*]const u16, [*]const Env.Value, [*]*Env.Value, length: usize, diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig index 6fd2bab..5ab3fbd 100644 --- a/src/zilch/Env/root.zig +++ b/src/zilch/Env/root.zig @@ -1,15 +1,13 @@ const std = @import("std"); -const Self = @This(); - const js = @import("js.zig"); -const future = @import("future.zig"); const Object = @import("../Object.zig"); pub const Call = @import("call.zig").Call; -pub const AnyFuture = future.AnyFuture; -pub const Future = future.Future; +pub const AnyFuture = @import("future.zig").AnyFuture; +pub const Future = @import("future.zig").Future; pub const Value = @import("value.zig").Value; +pub const Function = @import("Function.zig"); calls: std.ArrayList(Call) = .empty, values: std.ArrayList(Value) = .empty, @@ -17,124 +15,24 @@ pool: std.heap.MemoryPool(Value) = .empty, results: std.ArrayList(*Value) = .empty, gpa: std.mem.Allocator, -pub const default: Self = .{ .gpa = std.heap.wasm_allocator }; - -pub fn init(gpa: std.mem.Allocator) Self { - return .{ .gpa = gpa }; -} - -pub fn getProp(self: *Self, object: *Object, key: anytype) AnyFuture { - self.calls.append(self.gpa, .{ - .kind = .get_prop, - .num_args = 2, - }) catch |err| return .{ .err = err }; - self.values.append(self.gpa, .from(object)) catch |err| return .{ .err = err }; - self.values.append(self.gpa, .from(key)) catch |err| return .{ .err = err }; - - return self.registerFuture() catch |err| .{ .err = err }; -} - -pub fn setProp( - self: *Self, - object: *Object, - key: anytype, - value: anytype, -) Future(.undefined) { - self.calls.append(self.gpa, .{ - .kind = .set_prop, - .num_args = 3, - }) catch |err| return .fromAny(.{ .err = err }); - - self.values.append(self.gpa, .from(object)) catch |err| return .fromAny(.{ .err = err }); - self.values.append(self.gpa, .from(key)) catch |err| return .fromAny(.{ .err = err }); - self.values.append(self.gpa, .from(value)) catch |err| return .fromAny(.{ .err = err }); - - return .fromAny(self.registerFuture() catch |err| .{ .err = err }); -} - -pub fn deleteProp( - self: *Self, - object: *Object, - key: anytype, -) Future(.undefined) { - self.calls.append(self.gpa, .{ - .kind = .del_prop, - .num_args = 2, - }) catch |err| return .fromAny(.{ .err = err }); - - self.values.append(self.gpa, .from(object)) catch |err| return .fromAny(.{ .err = err }); - self.values.append(self.gpa, .from(key)) catch |err| return .fromAny(.{ .err = err }); - - return .fromAny(self.registerFuture() catch |err| .{ .err = err }); -} - -pub fn callMethod( - self: *Self, - object: *Object, - key: []const u8, - args: anytype, -) AnyFuture { - self.calls.append(self.gpa, .{ - .kind = .call_method, - .num_args = 2 + args.len, - }) catch |err| return .{ .err = err }; - - self.values.append(self.gpa, .from(object)) catch |err| return .{ .err = err }; - self.values.append(self.gpa, .from(key)) catch |err| return .{ .err = err }; - - inline for (args) |arg| { - self.values.append(self.gpa, .from(arg)) catch |err| return .{ .err = err }; - } +const Self = @This(); - return self.registerFuture() catch |err| return .{ .err = err }; -} +pub const default: Self = .{ .gpa = std.heap.wasm_allocator }; -pub fn call(self: *Self, object: *Object, args: anytype) AnyFuture { +pub fn call(self: *Self, function: Function, args: anytype) AnyFuture { self.calls.append(self.gpa, .{ - .kind = .call, - .num_args = 1 + args.len, + .kind = function.id, + .num_args = args.len, }) catch |err| return .{ .err = err }; - self.values.append(self.gpa, .from(object)) catch |err| return .{ .err = err }; - inline for (args) |arg| { self.values.append(self.gpa, .from(arg)) catch |err| return .{ .err = err }; } - return self.registerFuture() catch |err| return .{ .err = err }; -} - -pub fn free(self: *Self, object: *Object) Future(.undefined) { - self.calls.append(self.gpa, .{ - .kind = .free, - .num_args = 1, - }) catch |err| return .fromAny(.{ .err = err }); - - self.values.append(self.gpa, .from(object)) catch |err| return .fromAny(.{ .err = err }); - - return .fromAny(self.registerFuture() catch |err| .{ .err = err }); -} - -pub fn new(self: *Self, object: *Object, args: anytype) Future(.object) { - self.calls.append(self.gpa, .{ - .kind = .new, - .num_args = 1 + args.len, - }) catch |err| return .fromAny(.{ .err = err }); - - self.values.append(self.gpa, .from(object)) catch |err| return .fromAny(.{ .err = err }); - - inline for (args) |arg| { - self.values.append(self.gpa, .from(arg)) catch |err| return .fromAny(.{ .err = err }); - } - - return .fromAny(self.registerFuture() catch |err| .{ .err = err }); -} - -fn registerFuture(self: *Self) !AnyFuture { - const result = try self.pool.create(self.gpa); + const result = self.pool.create(self.gpa) catch |err| return .{ .err = err }; errdefer self.pool.destroy(result); result.kind = .uninit; - try self.results.append(self.gpa, result); + self.results.append(self.gpa, result) catch |err| return .{ .err = err }; return .{ .result = result }; } @@ -150,6 +48,13 @@ pub fn flush(self: *Self) void { self.results.clearRetainingCapacity(); } +pub fn release(self: *Self, future: AnyFuture) void { + switch (future) { + .err => {}, + .result => |result| self.pool.destroy(result), + } +} + pub fn deinit(self: *Self) void { self.flush(); diff --git a/src/zilch/Object.zig b/src/zilch/Object.zig index 064d48b..6c0eeee 100644 --- a/src/zilch/Object.zig +++ b/src/zilch/Object.zig @@ -3,33 +3,83 @@ const Options = mod.Options; const Env = mod.Env; const AnyFuture = Env.AnyFuture; const Future = Env.Future; +const Function = Env.Function; const Self = @This(); pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture { - return env.getProp(self, key); + return env.call(bindings.get_prop, .{ self, key }); } pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(.undefined) { - return env.setProp(self, key, value); + return .fromAny(env.call(bindings.set_prop, .{ self, key, value })); } pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(.undefined) { - return env.deleteProp(self, key); + return .fromAny(env.call(bindings.delete_prop, .{ self, key })); } pub inline fn call(self: *Self, env: *Env, method: []const u8, args: anytype) AnyFuture { - return env.callMethod(self, method, args); + return env.call(bindings.call_method, .{ self, method } ++ args); } pub inline fn free(self: *Self, env: *Env) Future(.undefined) { - return env.free(self); + return .fromAny(env.call(bindings.free, .{self})); } pub fn new(self: *Self, env: *Env) Future(.object) { - return env.new(self, .{}); + return .fromAny(env.call(bindings.new, .{self})); } pub fn global(tag: @EnumLiteral()) Options.Global { return .{ .name = @tagName(tag), .type = *Self }; } + +const bindings = struct { + const get_prop: Function = .from( + \\function(target, key) { + \\ return target[key]; + \\} + ); + + const set_prop: Function = .from( + \\function(target, key, value) { + \\ target[key] = value; + \\} + ); + + const delete_prop: Function = .from( + \\function(target, key) { + \\ delete target[key]; + \\} + ); + + const call_method: Function = .from( + \\function(target, method, ...args) { + \\ return target[method](...args); + \\} + ); + + const new: Function = .from( + \\function(target, ...args) { + \\ return new target(...args); + \\} + ); + + const free: Function = .from( + \\function(target) { + \\ const id = target[ZsId]; + \\ delete target[ZsId]; + \\ delete ObjectStore.store[id]; + \\} + ); +}; + +pub const functions = [_]Function{ + bindings.get_prop, + bindings.set_prop, + bindings.delete_prop, + bindings.call_method, + bindings.new, + bindings.free, +}; diff --git a/src/zilch/root.zig b/src/zilch/root.zig index 321bde4..4940c77 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -1,8 +1,12 @@ const builtin = @import("builtin"); const std = @import("std"); -pub const log = @import("log/root.zig"); pub const Env = @import("Env/root.zig"); +pub const Call = Call.Env; +pub const AnyFuture = AnyFuture.Env; +pub const Future = Future.Env; +pub const Value = Value.Env; +pub const log = @import("log/root.zig"); pub const Object = @import("Object.zig"); pub const html = @import("html/root.zig"); |