diff options
Diffstat (limited to 'src/root.zig')
-rw-r--r-- | src/root.zig | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/src/root.zig b/src/root.zig index 4e278e3..5ea9dc9 100644 --- a/src/root.zig +++ b/src/root.zig @@ -200,20 +200,21 @@ pub fn wrap(x: anytype) !Value { } }, }, - .@"struct" => |info| y: { + .@"struct" => |info| janet.wrapStruct(y: { const kv = try structWithMethods(T, info.fields.len); inline for (info.fields) |field| { const k = keyword(field.name); const v = @field(x, field.name); janet.structPut(kv, k, try wrap(v)); } - break :y janet.wrapStruct(janet.structEnd(kv)); - }, + break :y janet.structEnd(kv); + }), .@"union" => |info| if (info.tag_type != null) switch (x) { - inline else => |v, tag| janet.wrapTuple(y: { + inline else => |v, tag| janet.wrapStruct(y: { + const kv = try structWithMethods(T, 1); const k = keyword(@tagName(tag)); - const tuple = [_]Value{ k, try wrap(v) }; - break :y janet.tupleN(&tuple, tuple.len); + janet.structPut(kv, k, try wrap(v)); + break :y janet.structEnd(kv); }), } else @compileError("can't wrap untagged union"), .vector => |info| try wrap(@as([info.len]info.child, x)), @@ -303,10 +304,10 @@ pub fn unwrap(T: type, x: Value) !T { } else dest; }, .@"union" => |info| if (info.tag_type != null) y: { - const head = janet.tupleHead(janet.unwrapTuple(x)); - const k, const v = tupleData(2, head).*; + const st = janet.unwrapStruct(x); break :y inline for (info.fields) |field| { - if (equal(keyword(field.name), k)) + const v = janet.structGet(st, keyword(field.name)); + if (!isNil(v)) break @unionInit(T, field.name, try unwrap(field.type, v)); } else error.UnionTagNotFound; } else @compileError("can't wrap untagged union"), |