diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-19 15:14:01 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-10-19 15:16:33 +0200 |
| commit | bbd1364de53186af975ef1694fdc8451208ce626 (patch) | |
| tree | 467e72f70b0fca12fb053618913ee247f7721137 /src/zilch | |
| parent | 5b7b5c48e50d377b794dbbfc1c270ec6fd387e4b (diff) | |
zilch: make object calls type agnostic
Diffstat (limited to 'src/zilch')
| -rw-r--r-- | src/zilch/App/object-store.js | 2 | ||||
| -rw-r--r-- | src/zilch/Callable.zig | 2 | ||||
| -rw-r--r-- | src/zilch/Env/future.zig | 17 | ||||
| -rw-r--r-- | src/zilch/Env/root.zig | 9 | ||||
| -rw-r--r-- | src/zilch/Object.zig | 12 | ||||
| -rw-r--r-- | src/zilch/Promise.zig | 2 | ||||
| -rw-r--r-- | src/zilch/root.zig | 1 |
7 files changed, 35 insertions, 10 deletions
diff --git a/src/zilch/App/object-store.js b/src/zilch/App/object-store.js index 6699c41..9a68478 100644 --- a/src/zilch/App/object-store.js +++ b/src/zilch/App/object-store.js @@ -90,6 +90,8 @@ class ObjectStore { } static writeValue(ptr, value) { + if (ptr === 0) return; + const mem = new DataView(wasm.memory.buffer, ptr); if (typeof value === 'object' || typeof value === 'function') { diff --git a/src/zilch/Callable.zig b/src/zilch/Callable.zig index 6732c69..d668953 100644 --- a/src/zilch/Callable.zig +++ b/src/zilch/Callable.zig @@ -6,7 +6,7 @@ const Function = Env.Function; const Self = @This(); -pub inline fn call(self: *Self, env: *Env, args: anytype) AnyFuture { +pub inline fn call(self: anytype, env: *Env, args: anytype) AnyFuture { return env.call(bindings.call, .{ self } ++ args); } diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig index 1b44d85..f829009 100644 --- a/src/zilch/Env/future.zig +++ b/src/zilch/Env/future.zig @@ -13,15 +13,20 @@ pub fn Future(Kind: type) type { return .{ .future = future }; } - pub fn await(self: *const Self, env: *Env) !Kind { + pub fn await(self: *Self, env: *Env) !Kind { return self.future.await(env, Kind); } + + pub fn ignore(self: *Self, env: *Env) void { + return self.future.ignore(env); + } }; } pub const AnyFuture = union(enum) { const Self = @This(); + ignored: void, unresolved: struct { id: usize, value: *Env.Value, @@ -37,6 +42,7 @@ pub const AnyFuture = union(enum) { pub fn awaitAny(self: *Self, env: *Env) !Env.Value { defer self.* = undefined; switch (self.*) { + .ignored => @panic("awaited ignored value"), .err => |err| return err, .resolved => |result| return result, .unresolved => |result| { @@ -51,4 +57,13 @@ pub const AnyFuture = union(enum) { } } } + + pub fn ignore(self: *Self, env: *Env) void { + defer self.* = .ignored; + + switch (self.*) { + .unresolved => env.ignore(self), + else => {}, + } + } }; diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig index 5244830..d169b5f 100644 --- a/src/zilch/Env/root.zig +++ b/src/zilch/Env/root.zig @@ -12,7 +12,7 @@ pub const Function = @import("Function.zig"); calls: std.ArrayList(Call) = .empty, values: std.ArrayList(Value) = .empty, pool: std.heap.MemoryPool(Value) = .empty, -results: std.ArrayList(*Value) = .empty, +results: std.ArrayList(?*Value) = .empty, gpa: std.mem.Allocator, const Self = @This(); @@ -58,6 +58,13 @@ pub fn release(self: *Self, value: *Value) void { self.pool.destroy(value); } +pub fn ignore(self: *Self, future: *const AnyFuture) void { + if (self.results.items.len < future.unresolved.id and self.results.items[future.unresolved.id] == future.unresolved.value) { + self.pool.destroy(future.unresolved.value); + self.results.items[future.unresolved.id] = null; + } +} + pub fn deinit(self: *Self) void { self.flush(); diff --git a/src/zilch/Object.zig b/src/zilch/Object.zig index b37f40b..1d30b8a 100644 --- a/src/zilch/Object.zig +++ b/src/zilch/Object.zig @@ -7,27 +7,27 @@ const Function = Env.Function; const Self = @This(); -pub inline fn get(self: *Self, env: *Env, key: anytype) AnyFuture { +pub inline fn get(self: anytype, env: *Env, key: anytype) AnyFuture { return env.call(bindings.get, .{ self, key }); } -pub inline fn set(self: *Self, env: *Env, key: anytype, value: anytype) Future(void) { +pub inline fn set(self: anytype, env: *Env, key: anytype, value: anytype) Future(void) { return .fromAny(env.call(bindings.set, .{ self, key, value })); } -pub inline fn delete(self: *Self, env: *Env, key: anytype) Future(void) { +pub inline fn delete(self: anytype, env: *Env, key: anytype) Future(void) { return .fromAny(env.call(bindings.delete, .{ self, key })); } -pub inline fn call(self: *Self, env: *Env, method: []const u8, args: anytype) AnyFuture { +pub inline fn call(self: anytype, env: *Env, method: []const u8, args: anytype) AnyFuture { return env.call(bindings.call, .{ self, method } ++ args); } -pub inline fn free(self: *Self, env: *Env) Future(void) { +pub inline fn free(self: anytype, env: *Env) Future(void) { return .fromAny(env.call(bindings.free, .{self})); } -pub inline fn new(self: *Self, env: *Env) Future(*Self) { +pub inline fn new(self: anytype, env: *Env) Future(*Self) { return .fromAny(env.call(bindings.new, .{self})); } diff --git a/src/zilch/Promise.zig b/src/zilch/Promise.zig index 09654e3..76362e7 100644 --- a/src/zilch/Promise.zig +++ b/src/zilch/Promise.zig @@ -13,7 +13,7 @@ const Self = @This(); // else an allocation would be necessary! var promise_value: Value = undefined; -pub inline fn then(self: *Self, env: *Env, func: anytype, ud: *anyopaque) Future(void) { +pub inline fn then(self: anytype, env: *Env, func: anytype, ud: *anyopaque) Future(void) { const wrapper = struct { fn wrapper(userdata: *anyopaque) callconv(.c) void { const args = @typeInfo(@TypeOf(func)).@"fn".params; diff --git a/src/zilch/root.zig b/src/zilch/root.zig index 1ececbb..9bca612 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -6,6 +6,7 @@ pub const Call = Env.Call; pub const AnyFuture = Env.AnyFuture; pub const Future = Env.Future; pub const Value = Env.Value; + pub const log = @import("log/root.zig"); pub const html = @import("html/root.zig"); |