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; const max = std.math.maxInt; // 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(comptime tag: PtrTag, ptr: tag.ptype(), 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(comptime tag: PtrTag, ptr: tag.ptype()) Value { return _pack(tag, ptr, false); } pub fn packWeak(comptime tag: PtrTag, ptr: tag.ptype()) Value { return _pack(tag, ptr, 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); return v.getPtrAny() orelse unreachable; } // 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; } }