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
179
180
181
182
183
184
185
186
187
188
|
const builtin = @import("builtin");
const std = @import("std");
const gc = @import("../gc.zig");
const value = @import("../value.zig");
const Alloc = std.mem.Allocator;
const Value = value.Value;
/// Pointer to header for an array of various element types and sizes.
///
/// The low 48 bits are either the length (element count, not buffer size) of
/// the array contents that follow immediately, or a pointer to the header of
/// another array whose memory is shared with this one, or a null pointer that
/// has special meaning; see below.
///
/// NOTE: For strings, the length is in fact the size in bytes of the buffer.
/// To get the "length" of a string according to other definitions of length,
/// such as count of Unicode Scalar Values, or count of Grapheme Clusters,
/// different encoding-specific string APIs must be used.
///
/// If this is a pointer (`is_ptr` is set) it means it's a re-interpretation of
/// the contents of the array that is being pointed to. (But see below.)
///
/// If this is a slice (`is_slice` is set) then two more u64 values follow this
/// one, marking the start and end of the slice of the pointed-to array that
/// this one represents. (But see next paragraph.)
///
/// If this is a pointer or slice, but the pointer value is null, then another
/// u64 follows this one immediately, and points directly to a memory buffer
/// (not array head) whose contents are used. In this case, if it's a slice,
/// the start and end u64 values come after that pointer instead.
///
/// If this header encodes a non-slice, non-array, direct buffer pointer, then
/// there is no length information, so the count of elements is unknown and
/// bounds checks cannot be performed.
///
/// Having `is_slice` set implies `is_ptr` being set. Having `is_slice` set
/// while `is_ptr` is unset is an undefined state.
///
/// Only one layer of indirection is allowed; a pointer or slice must not point
/// to another pointer or slice header. Given that the pointer points to the
/// header of another array, rather than its contents, it's always possible to
/// reach the original array and create another pointer to or slice of it, so
/// additional layers of indirection are never needed. Alternatively, if the
/// array pointer is null, then the buffer pointer can be reused to create
/// another array with the same buffer pointer and different type info.
///
/// Other remaining bits provide information about element type and size.
pub const ArrayPtr = *align(@alignOf(value.Zptr)) ArrayHeader;
pub const ArrayHeader = packed struct(u64) {
len_or_ptr: u48,
is_slice: bool = false,
is_ptr: bool = false,
type: enum(u2) { int, float, value, string },
info: packed union {
int: packed struct(u12) {
size: u10,
signed: bool,
endian: Endian = .native,
},
float: packed struct(u12) {
size: u10,
_: bool,
endian: Endian = .native,
},
value: packed struct(u12) {
weak: bool,
_: u11,
},
string: packed struct(u12) {
encoding: enum(u11) {
utf8,
utf16,
utf24,
utf32,
} = .utf8,
endian: Endian = .native,
},
},
fn bufU64(self: *@This()) [*]u64 {
return @ptrCast(self);
}
fn bufContent(self: *@This()) [*]u8 {
std.debug.assert(!self.is_ptr);
return @ptrCast(self.bufU64() + 1);
}
fn bufPointer(self: *@This()) [*]u8 {
std.debug.assert(self.is_ptr);
std.debug.assert(self.len_or_ptr == 0);
return @ptrFromInt(self.bufU64()[1]);
}
fn eltSize(self: *@This()) u16 {
std.debug.assert(!self.is_ptr);
return switch (self.type) {
.string => 1,
else => @panic("not implemented"),
};
}
fn size(self: *@This()) usize {
std.debug.assert(!self.is_ptr);
return self.len_or_ptr * self.eltSize();
}
fn arrPointer(self: *@This()) ?ArrayPtr {
std.debug.assert(self.is_ptr);
const p = self.len_or_ptr;
return if (p != 0) @ptrFromInt(p) else null;
}
fn sliceInfo(self: *@This()) [2]u64 {
std.debug.assert(self.is_slice);
const ptr = self.len_or_ptr;
const buf = self.bufU64();
if (ptr != 0) {
return .{ buf[1], buf[2] };
} else {
return .{ buf[2], buf[3] };
}
}
pub fn bufU8(self: *@This()) [*]u8 {
if (self.is_ptr) {
if (self.arrPointer()) |a| {
return a.bufContent();
} else {
return self.bufPointer();
}
} else {
return self.bufContent();
}
}
pub fn str(self: *@This()) []const u8 {
if (self.is_slice) {
const buf = self.bufU8();
const start, const end = self.sliceInfo();
return buf[start..end];
}
var arr = self;
if (self.is_ptr) {
arr = self.arrPointer() orelse {
@panic("Called str() on array with direct buffer pointer.");
};
}
return arr.bufContent()[0..arr.size()];
}
};
const Endian = enum(u1) {
little,
big,
const native: Endian = switch (builtin.target.cpu.arch.endian()) {
.little => .little,
.big => .big,
};
};
pub fn newString(alloc: Alloc, s: []const u8) !Value {
std.debug.assert(s.len <= std.math.maxInt(u48));
const algn = std.mem.Alignment.of(ArrayPtr);
const size = @sizeOf(ArrayHeader) + s.len;
const ptr = try alloc.alignedAlloc(u8, algn, size);
const arr: ArrayPtr = @ptrCast(ptr);
arr.* = .{
.len_or_ptr = @intCast(s.len),
.type = .string,
.info = .{ .string = .{} },
};
const buf = arr.bufContent();
@memcpy(buf[0..s.len], s);
return value.ptr.pack(arr, .array);
}
pub fn check(v: Value) ?ArrayPtr {
if (v.getPtr(.array)) |p| {
return @ptrCast(p);
}
return null;
}
|