aboutsummaryrefslogtreecommitdiff
path: root/src/z/Env/value.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-10-05 16:58:46 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-10-05 16:58:46 +0200
commit5117cd94bdcc5b76ce4cb428b5ea47a6c5ab6ecf (patch)
treed4437942d58878d0622da5d924a5320e02957bcc /src/z/Env/value.zig
parent7797e27193ea74914cfdb7ef6cb06af8015d4714 (diff)
wasm implement callMethod
Diffstat (limited to 'src/z/Env/value.zig')
-rw-r--r--src/z/Env/value.zig114
1 files changed, 93 insertions, 21 deletions
diff --git a/src/z/Env/value.zig b/src/z/Env/value.zig
index 4630a76..b22b88a 100644
--- a/src/z/Env/value.zig
+++ b/src/z/Env/value.zig
@@ -1,7 +1,34 @@
+const std = @import("std");
+const Object = @import("root.zig").Object;
+
+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.is_volatile or ptr.is_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 const Value = extern struct {
kind: Kind,
value: extern union {
- object: u32,
+ object: *Object,
string: extern struct {
ptr: [*]const u8,
len: usize,
@@ -12,25 +39,70 @@ pub const Value = extern struct {
undefined: void,
},
- pub const Kind = enum(u8) {
- uninit = 0,
- object = 1,
- string = 2,
- int = 3,
- uint = 4,
- float = 5,
- undefined = 6,
+ pub fn from(value: anytype) Value {
+ const T = @TypeOf(value);
+
+ return switch (T) {
+ *Object => .{
+ .kind = .object,
+ .value = .{ .object = 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 = {} },
+ },
+ else => str: {
+ if (comptime isString(T)) {
+ break :str .{
+ .kind = .string,
+ .value = .{
+ .string = .{
+ .ptr = @ptrCast(value),
+ .len = value.len,
+ },
+ },
+ };
+ } else {
+ @compileError(std.fmt.comptimePrint(
+ "cannot auto convert type '{}'.",
+ .{T},
+ ));
+ }
+ },
+ };
+ }
+
+ pub const Kind = enum(u8) {
+ uninit = 0,
+ object = 1,
+ string = 2,
+ int = 3,
+ uint = 4,
+ float = 5,
+ undefined = 6,
- pub fn asType(comptime self: Kind) type {
- return switch (self) {
- .object => return u32,
- .string => return []const u8,
- .int => return isize,
- .uint => return usize,
- .float => f64,
- .undefined => return void,
- .uninit => @compileError("uninit has no associated type."),
- };
- }
- };
+ 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,
+ .uninit => @compileError("uninit has no associated type."),
+ };
+ }
+ };
};