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/zilch/Object.zig | |
| parent | 0b2af471b6d5dfc4eaf53639102c1b854bbb324b (diff) | |
add function struct to register js functions dynamically
Diffstat (limited to 'src/zilch/Object.zig')
| -rw-r--r-- | src/zilch/Object.zig | 62 |
1 files changed, 56 insertions, 6 deletions
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, +}; |