summaryrefslogtreecommitdiff
path: root/src/zisp/value/array.zig
diff options
context:
space:
mode:
authorTaylan Kammer <taylan.kammer@gmail.com>2026-06-04 16:37:16 +0200
committerTaylan Kammer <taylan.kammer@gmail.com>2026-06-04 16:37:16 +0200
commit8de05de165cc7e7f900fb2320a58262ac8d0be5f (patch)
tree7c914020c41dd56953a5407989c0373a2883af9b /src/zisp/value/array.zig
parent1a0a55692988fe8e22ebb397a70b60424d653a5e (diff)
We can read and print back various strings now.
Diffstat (limited to 'src/zisp/value/array.zig')
-rw-r--r--src/zisp/value/array.zig32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig
index ef2bcd6..67a5f37 100644
--- a/src/zisp/value/array.zig
+++ b/src/zisp/value/array.zig
@@ -4,6 +4,8 @@ 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.
@@ -46,12 +48,12 @@ const Value = value.Value;
/// 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(8) ArrayHeader;
+pub const ArrayPtr = *align(@alignOf(value.Zptr)) ArrayHeader;
pub const ArrayHeader = packed struct(u64) {
len_or_ptr: u48,
- is_slice: bool,
- is_ptr: bool,
+ is_slice: bool = false,
+ is_ptr: bool = false,
type: enum(u2) { int, float, value, string },
info: packed union {
int: packed struct(u12) {
@@ -69,13 +71,12 @@ pub const ArrayHeader = packed struct(u64) {
_: u11,
},
string: packed struct(u12) {
- encoding: enum(u10) {
+ encoding: enum(u11) {
utf8,
utf16,
utf24,
utf32,
} = .utf8,
- interned: bool = false,
endian: Endian = .native,
},
},
@@ -97,7 +98,10 @@ pub const ArrayHeader = packed struct(u64) {
fn eltSize(self: *@This()) u16 {
std.debug.assert(!self.is_ptr);
- @panic("");
+ return switch (self.type) {
+ .string => 1,
+ else => @panic("not implemented"),
+ };
}
fn size(self: *@This()) usize {
@@ -160,6 +164,22 @@ const Endian = enum(u1) {
};
};
+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);