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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
const std = @import("std");
const Value = @import("../value.zig").Value;
// Zig API
pub fn check(v: Value) bool {
return v.isPacked() and v.ptr.is_ptr;
}
pub fn assert(v: Value) void {
if (!check(v)) {
v.dump();
@panic("not a pointer");
}
}
// Foreign Pointers
pub fn checkForeign(v: Value) bool {
return check(v) and v.ptr.foreign;
}
pub fn assertForeign(v: Value) void {
if (!checkForeign(v)) {
v.dump();
@panic("not foreign pointer");
}
}
pub fn packForeign(int: u50) Value {
return .{ .fptr = .{int} };
}
pub fn unpackForeign(v: Value) u64 {
assertForeign(v);
return v.ptr.value.foreign;
}
// Zisp Pointers
pub fn checkZisp(v: Value) bool {
return check(v) and !v.ptr.foreign;
}
pub fn assertZisp(v: Value) void {
if (!checkZisp(v)) {
v.dump();
@panic("not zisp pointer");
}
}
pub fn checkWeak(v: Value) bool {
return checkZisp(v) and v.ptr.weak;
}
pub fn assertWeak(v: Value) void {
if (!checkWeak(v)) {
v.dump();
@panic("not weak zisp pointer");
}
}
pub fn checkNormal(v: Value) bool {
return checkZisp(v) and !v.ptr.weak;
}
pub fn assertNormal(v: Value) void {
if (!checkNormal(v)) {
v.dump();
@panic("not normal zisp pointer");
}
}
pub fn packZisp(ptr: *anyopaque, tag: Tag, weak: bool) Value {
return .{ .ptr = .{
.value = tagPtr(ptr, tag),
.weak = weak,
} };
}
pub fn pack(ptr: *anyopaque, tag: Tag) Value {
return packZisp(ptr, tag, false);
}
pub fn packWeak(ptr: *anyopaque, tag: Tag) Value {
return packZisp(ptr, tag, true);
}
// Unpacks weak as well; no need for a separate fn.
pub fn unpack(v: Value) PtrAndTag {
assertZisp(v);
return untagPtr(v.ptr.value);
}
// Weak pointers may be null.
pub fn isNull(v: Value) bool {
assertWeak(v);
const ptr, _ = untagPtr(v.ptr.value);
return @intFromPtr(ptr) == 0;
}
pub fn tagPtr(ptr: *anyopaque, tag: Tag) u49 {
const int: u64 = @intFromPtr(ptr);
const untagged: u49 = @truncate(int);
return untagged << 1 | @intFromEnum(tag);
}
pub const PtrAndTag = struct { *anyopaque, Tag };
pub fn untagPtr(tagged: u49) PtrAndTag {
const untagged: u49 = tagged >> 1 & 0xfffffffffff0;
const ptr: *anyopaque = @ptrFromInt(untagged);
const int: u4 = @truncate(tagged);
const tag: Tag = @enumFromInt(int);
return .{ ptr, tag };
}
pub const Tag = enum(u4) {
/// 0. Strings / Symbols
string,
/// 1. Bignums / Ratnums
number,
/// 2. Pairs ([2]Value)
pair,
/// 3. Vector, bytevector, etc.
array,
/// 4. Ordered hash table
table,
/// 5. String buffer
text,
/// 6. Class, interface, etc.
role,
/// 7. Instance, basically
actor,
/// 8. I/O Port
port,
/// 9. Procedure
proc,
/// 10. Continuation
cont,
/// Other
other = 15,
};
// Zisp API
pub fn predForeign(v: Value) Value {
return Value.boole.pack(checkForeign(v));
}
pub fn makeWeak(v: Value) Value {
assertNormal(v);
var copy = v;
copy.ptr.weak = true;
return copy;
}
pub fn predWeak(v: Value) Value {
const isWeak = checkWeak(v);
return Value.boole.pack(isWeak);
}
pub fn predWeakNull(v: Value) Value {
assertWeak(v);
return Value.boole.pack(v.ptr.weak);
}
pub fn getWeak(v: Value) Value {
assertWeak(v);
if (isNull(v)) {
return Value.boole.pack(false);
} else {
var copy = v;
copy.ptr.weak = false;
return copy;
}
}
|