diff options
Diffstat (limited to 'src/zisp/value/array.zig')
| -rw-r--r-- | src/zisp/value/array.zig | 32 |
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); |
