1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
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: {
const info = @typeInfo(T);
if (info != .pointer) break :blk false;
const ptr = info.pointer;
if (ptr.attrs.@"volatile" or ptr.attrs.@"allowzero") break :blk false;
if (ptr.size == .slice) {
break :blk ptr.child == u8;
}
if (ptr.size == .one) {
const child = @typeInfo(ptr.child);
if (child == .array) {
const arr = &child.array;
break :blk arr.child == u8;
}
}
break :blk false;
};
}
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: *anyopaque,
string: extern struct {
ptr: [*]const u8,
len: usize,
},
int: isize,
uint: usize,
float: f64,
undefined: void,
future: usize,
},
pub fn from(value: anytype) Value {
const T = @TypeOf(value);
return switch (T) {
comptime_int => if (value >= 0) .{
.kind = .uint,
.value = .{ .uint = value },
} else .{
.kind = .int,
.value = .{ .int = value },
},
usize => .{
.kind = .uint,
.value = .{ .uint = value },
},
isize => .{
.kind = .int,
.value = .{ .int = value },
},
f64, f32, f16 => .{
.kind = .float,
.value = .{ .float = value },
},
void => .{
.kind = .undefined,
.value = .{ .undefined = {} },
},
AnyFuture => .{
.kind = .future,
.value = .{ .future = value.unresolved.id },
},
else => blk: {
if (comptime isString(T)) {
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},
));
}
},
};
}
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},
));
}
},
};
}
pub const Kind = enum(u8) {
object = 0,
string = 1,
int = 2,
uint = 3,
float = 4,
undefined = 5,
future = 6,
pub fn asType(comptime self: Kind) type {
return switch (self) {
.object => return *Object,
.string => return []const u8,
.int => return isize,
.uint => return usize,
.float => f64,
.undefined => return void,
.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},
));
}
},
};
}
};
};
|