summaryrefslogtreecommitdiff
path: root/src/zisp/value
diff options
context:
space:
mode:
Diffstat (limited to 'src/zisp/value')
-rw-r--r--src/zisp/value/array.zig32
-rw-r--r--src/zisp/value/istr.zig15
-rw-r--r--src/zisp/value/sstr.zig8
3 files changed, 39 insertions, 16 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);
diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig
index cadf849..504ffe7 100644
--- a/src/zisp/value/istr.zig
+++ b/src/zisp/value/istr.zig
@@ -13,7 +13,7 @@ const Value = value.Value;
///
/// First 64 bits are a cached hash, of which the lowest 8 bits are actually the
/// length of the string: u64hash = (real_u64hash << 8) | u8length
-pub const IstrPtr = *align(8) IstrHeader;
+pub const IstrPtr = *align(@alignOf(value.Zptr)) IstrHeader;
const IstrHeader = packed union {
hash: u64,
@@ -29,20 +29,23 @@ const IstrHeader = packed union {
pub fn putStr(self: *@This(), s: []const u8) void {
std.debug.assert(s.len <= 255);
const buf = self.bufU8();
- @memcpy(buf[8 .. 8 + s.len], s);
+ const start = @sizeOf(IstrHeader);
+ @memcpy(buf[start .. start + s.len], s);
}
pub fn str(self: *@This()) []const u8 {
const buf = self.bufU8();
- return buf[8 .. 8 + self.meta.len];
+ const start = @sizeOf(IstrHeader);
+ return buf[start .. start + self.meta.len];
}
};
pub fn new(alloc: Alloc, hash: u64, s: []const u8) !IstrPtr {
- const aln = std.mem.Alignment.of(IstrPtr);
- const ptr = try alloc.alignedAlloc(u8, aln, 8 + s.len);
+ const algn = std.mem.Alignment.of(IstrPtr);
+ const size = @sizeOf(IstrHeader) + s.len;
+ const ptr = try alloc.alignedAlloc(u8, algn, size);
const istr: IstrPtr = @ptrCast(ptr);
- istr.hash = hash;
+ istr.* = .{ .hash = hash };
istr.putStr(s);
return istr;
}
diff --git a/src/zisp/value/sstr.zig b/src/zisp/value/sstr.zig
index a91bfa2..8435800 100644
--- a/src/zisp/value/sstr.zig
+++ b/src/zisp/value/sstr.zig
@@ -38,11 +38,11 @@ pub fn pack(s: []const u8) Value {
return .{ .sstr = .{ .string = @bitCast(buf) } };
}
-pub fn unpack(v: Value) ShortString {
- assert(v);
- const buf: [6]u8 = @bitCast(v.sstr.string);
+pub fn unpack(v: *const Value) []const u8 {
+ assert(v.*);
+ const buf = v.bytes();
const len = value.sstrLen(v.bits);
- return .{ .buf = buf, .len = len };
+ return buf[0..len];
}
// No Zisp API for sstr specifically, since it's a string. See string.zig.