aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/Env/value.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-17 10:13:13 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-17 10:13:13 +0200
commitb1b4df39abb41a4ae71733e2d139dc70cc71acd8 (patch)
tree3d72b1dd75841e0b950282099ec9c837aa03d869 /src/zilch/Env/value.zig
parentdd24e86dad0402917b1713d1afeefaf65c3d1e51 (diff)
zilch: implement callable and promise
Diffstat (limited to 'src/zilch/Env/value.zig')
-rw-r--r--src/zilch/Env/value.zig84
1 files changed, 73 insertions, 11 deletions
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},
+ ));
+ }
+ },
+ };
+ }
};
};