diff options
Diffstat (limited to 'src/zilch/Env')
| -rw-r--r-- | src/zilch/Env/future.zig | 26 | ||||
| -rw-r--r-- | src/zilch/Env/root.zig | 17 | ||||
| -rw-r--r-- | src/zilch/Env/value.zig | 10 |
3 files changed, 33 insertions, 20 deletions
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig index 6e0cc63..8b88cd8 100644 --- a/src/zilch/Env/future.zig +++ b/src/zilch/Env/future.zig @@ -22,10 +22,14 @@ pub fn Future(comptime kind: Env.Value.Kind) type { pub const AnyFuture = union(enum) { const Self = @This(); - result: *Env.Value, + unresolved: struct { + id: usize, + value: *Env.Value, + }, + resolved: Env.Value, err: anyerror, - pub fn await(self: *const Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() { + pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() { const result = try self.awaitAny(env); if (kind != result.kind) { @@ -34,25 +38,25 @@ pub const AnyFuture = union(enum) { } return switch (kind) { - .uninit => @panic("uninitialized result"), .string => result.value.string.ptr[0..result.value.string.len], else => @field(result.value, @tagName(kind)), }; } - pub fn awaitAny(self: *const Self, env: *Env) !Env.Value { + pub fn awaitAny(self: *Self, env: *Env) !Env.Value { + defer self.* = undefined; switch (self.*) { .err => |err| return err, - .result => |result| { - defer env.release(self.*); - - if (result.kind != .uninit) { - return result.*; - } + .resolved => |result| return result, + .unresolved => |result| { + const value = result.value; + defer env.release(value); env.flush(); - return result.*; + self.* = .{ .resolved = value.* }; + + return value.*; } } } diff --git a/src/zilch/Env/root.zig b/src/zilch/Env/root.zig index 5ab3fbd..5244830 100644 --- a/src/zilch/Env/root.zig +++ b/src/zilch/Env/root.zig @@ -20,6 +20,8 @@ const Self = @This(); pub const default: Self = .{ .gpa = std.heap.wasm_allocator }; pub fn call(self: *Self, function: Function, args: anytype) AnyFuture { + const id = self.calls.items.len; + self.calls.append(self.gpa, .{ .kind = function.id, .num_args = args.len, @@ -31,9 +33,13 @@ pub fn call(self: *Self, function: Function, args: anytype) AnyFuture { const result = self.pool.create(self.gpa) catch |err| return .{ .err = err }; errdefer self.pool.destroy(result); - result.kind = .uninit; self.results.append(self.gpa, result) catch |err| return .{ .err = err }; - return .{ .result = result }; + return .{ + .unresolved = .{ + .id = id, + .value = result, + }, + }; } pub fn flush(self: *Self) void { @@ -48,11 +54,8 @@ 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 release(self: *Self, value: *Value) void { + self.pool.destroy(value); } pub fn deinit(self: *Self) void { diff --git a/src/zilch/Env/value.zig b/src/zilch/Env/value.zig index 59b788f..cedaa89 100644 --- a/src/zilch/Env/value.zig +++ b/src/zilch/Env/value.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Object = @import("../Object.zig"); +const AnyFuture = @import("future.zig").AnyFuture; pub fn isString(comptime T: type) bool { return comptime blk: { @@ -37,6 +38,7 @@ pub const Value = extern struct { uint: usize, float: f64, undefined: void, + future: usize, }, pub fn from(value: anytype) Value { @@ -70,6 +72,10 @@ pub const Value = extern struct { .kind = .undefined, .value = .{ .undefined = {} }, }, + AnyFuture => .{ + .kind = .future, + .value = .{ .future = value.unresolved.id }, + }, else => str: { if (comptime isString(T)) { break :str .{ @@ -92,13 +98,13 @@ pub const Value = extern struct { } pub const Kind = enum(u8) { - uninit = 0, object = 1, string = 2, int = 3, uint = 4, float = 5, undefined = 6, + future = 7, pub fn asType(comptime self: Kind) type { return switch (self) { @@ -108,7 +114,7 @@ pub const Value = extern struct { .uint => return usize, .float => f64, .undefined => return void, - .uninit => @compileError("uninit has no associated type."), + .future => AnyFuture, }; } }; |