From b1b4df39abb41a4ae71733e2d139dc70cc71acd8 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 17 Oct 2026 10:13:13 +0200 Subject: zilch: implement callable and promise --- src/zilch/Env/future.zig | 19 +++-------- src/zilch/Env/value.zig | 84 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 78 insertions(+), 25 deletions(-) (limited to 'src/zilch/Env') diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig index 8b88cd8..1b44d85 100644 --- a/src/zilch/Env/future.zig +++ b/src/zilch/Env/future.zig @@ -3,7 +3,7 @@ const Env = @import("root.zig"); const z = @import("../root.zig"); -pub fn Future(comptime kind: Env.Value.Kind) type { +pub fn Future(Kind: type) type { return struct { const Self = @This(); @@ -13,8 +13,8 @@ pub fn Future(comptime kind: Env.Value.Kind) type { return .{ .future = future }; } - pub fn await(self: *const Self, env: *Env) !kind.asType() { - return self.future.await(env, kind); + pub fn await(self: *const Self, env: *Env) !Kind { + return self.future.await(env, Kind); } }; } @@ -29,18 +29,9 @@ pub const AnyFuture = union(enum) { resolved: Env.Value, err: anyerror, - pub fn await(self: *Self, env: *Env, comptime kind: Env.Value.Kind) !kind.asType() { + pub fn await(self: *Self, env: *Env, Kind: type) !Kind { const result = try self.awaitAny(env); - - if (kind != result.kind) { - std.log.err("unexpected {}, expected {}", .{ result.kind, kind }); - @panic("type error"); - } - - return switch (kind) { - .string => result.value.string.ptr[0..result.value.string.len], - else => @field(result.value, @tagName(kind)), - }; + return result.unwrap(Kind); } pub fn awaitAny(self: *Self, env: *Env) !Env.Value { diff --git a/src/zilch/Env/value.zig b/src/zilch/Env/value.zig index cedaa89..b135366 100644 --- a/src/zilch/Env/value.zig +++ b/src/zilch/Env/value.zig @@ -26,10 +26,20 @@ pub fn isString(comptime T: type) bool { }; } +pub fn isObjectRef(comptime T: type) bool { + comptime { + const info = @typeInfo(T); + if (info != .pointer) return false; + + const ptr = &info.pointer; + return ptr.size == .one and @sizeOf(ptr.child) == 0; + } +} + pub const Value = extern struct { kind: Kind, value: extern union { - object: *Object, + object: *anyopaque, string: extern struct { ptr: [*]const u8, len: usize, @@ -45,10 +55,6 @@ pub const Value = extern struct { const T = @TypeOf(value); return switch (T) { - *Object => .{ - .kind = .object, - .value = .{ .object = value }, - }, comptime_int => if (value >= 0) .{ .kind = .uint, .value = .{ .uint = value }, @@ -76,21 +82,54 @@ pub const Value = extern struct { .kind = .future, .value = .{ .future = value.unresolved.id }, }, - else => str: { + else => blk: { if (comptime isString(T)) { - break :str .{ + break :blk .{ .kind = .string, .value = .{ .string = .{ .ptr = @ptrCast(value), .len = value.len, }, - }, - }; + }, + }; + } else if (comptime isObjectRef(T)) { + break :blk .{ + .kind = .object, + .value = .{ .object = @ptrCast(value) }, + }; } else { @compileError(std.fmt.comptimePrint( - "cannot auto convert type '{}'.", - .{T}, + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } + + pub fn unwrap(self: *const Value, T: type) T { + if (Kind.fromType(T) != self.kind) { + std.log.err("unexpected {}, expected {}", .{ self.kind, T }); + @panic("type error"); + } + + return switch (T) { + comptime_int => self.value.int, + usize => self.value.uint, + isize => self.value.int, + f64, f32, f16 => self.value.float, + void => {}, + AnyFuture => @panic("unsupported return type"), + else => blk: { + if (comptime isString(T)) { + break :blk self.value.string.ptr[0..self.value.string.len]; + } else if (comptime isObjectRef(T)) { + break :blk @ptrCast(self.value.object); + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, )); } }, @@ -117,5 +156,28 @@ pub const Value = extern struct { .future => AnyFuture, }; } + + pub fn fromType(T: type) Kind { + return switch (T) { + comptime_int => .int, + usize => .uint, + isize => .int, + f64, f32, f16 => .float, + void => .undefined, + AnyFuture => .future, + else => blk: { + if (comptime isString(T)) { + break :blk .string; + } else if (comptime isObjectRef(T)) { + break :blk .object; + } else { + @compileError(std.fmt.comptimePrint( + "cannot auto convert type '{}'.", + .{T}, + )); + } + }, + }; + } }; }; -- cgit v1.2.3-70-g09d2