const std = @import("std"); const gc = @import("../gc.zig"); const value = @import("../value.zig"); const PtrTag = value.PtrTag; const Value = value.Value; const Zptr = value.Zptr; // Zig API pub fn check(v: Value) bool { return v.isPtrAny(); } pub fn assert(v: Value) void { if (!check(v)) { v.dump(); @panic("not pointer"); } } pub fn checkWeak(v: Value) bool { return v.isPtrProps(0b110, 0b001); } pub fn assertWeak(v: Value) void { if (!checkWeak(v)) { v.dump(); @panic("not weak pointer"); } } pub fn _pack(ptr: Zptr, tag: PtrTag, is_weak: bool) Value { const ptr_val: usize = @intFromPtr(ptr); std.debug.assert(ptr_val < std.math.maxInt(u48)); const tagged: u48 = @intCast(ptr_val | @intFromEnum(tag)); return .{ .ptr = .{ .tagged_value = tagged, .is_weak = is_weak } }; } pub fn pack(ptr: Zptr, tag: PtrTag) Value { return _pack(ptr, tag, false); } pub fn packWeak(ptr: Zptr, tag: PtrTag) Value { return _pack(ptr, tag, true); } pub fn setWeakNull(v: *Value) void { assertWeak(v.*); v.ptr.tagged_value = 0; } pub fn isWeakNull(v: Value) bool { assertWeak(v); return v.ptr.tagged_value == 0; } pub fn unpack(v: Value) struct { Zptr, PtrTag } { assert(v); const tagged = v.ptr.tagged_value; const ptr_val: u48 = tagged & ~@as(u48, 7); const tag_val: u3 = @intCast(tagged & 7); const ptr: Zptr = @ptrFromInt(ptr_val); const tag: PtrTag = @enumFromInt(tag_val); return .{ ptr, tag }; } // Zisp API pub fn makeWeak(v: Value) Value { assert(v); var copy = v; copy.ptr.is_weak = true; return copy; } pub fn predWeak(v: Value) Value { return value.boole.pack(checkWeak(v)); } pub fn predWeakNull(v: Value) Value { return value.boole.pack(isWeakNull(v)); } pub fn getWeak(v: Value) Value { if (isWeakNull(v)) { return value.f; } else { var copy = v; copy.ptr.is_weak = false; return copy; } }