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/value.zig | 84 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 11 deletions(-) (limited to 'src/zilch/Env/value.zig') 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