blob: ef12f79542f54b993fbba68ea8fb32440739b6ba (
plain)
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
|
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;
}
}
|