From 79f4d5ae23c44924f413f9d7a1113d2280611ec5 Mon Sep 17 00:00:00 2001 From: Taylan Kammer Date: Sun, 31 May 2026 01:02:58 +0200 Subject: Make the code compile. --- src/zisp/value/array.zig | 14 +++++++------- src/zisp/value/istr.zig | 23 +++++++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src/zisp/value') diff --git a/src/zisp/value/array.zig b/src/zisp/value/array.zig index b2faf94..6e37013 100644 --- a/src/zisp/value/array.zig +++ b/src/zisp/value/array.zig @@ -94,13 +94,13 @@ pub const Array = *align(8) packed struct(u64) { fn bufContent(self: *@This()) [*]u8 { std.debug.assert(!self.is_ptr); - return @ptrCast(self.bufU64 + 1); + 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 @ptrCast(self.bufU64[1]); + return @ptrFromInt(self.bufU64()[1]); } fn eltSize(self: *@This()) u16 { @@ -116,7 +116,7 @@ pub const Array = *align(8) packed struct(u64) { fn arrPointer(self: *@This()) ?Array { std.debug.assert(self.is_ptr); const p = self.len_or_ptr; - return if (p != 0) @ptrCast(p) else null; + return if (p != 0) @ptrFromInt(p) else null; } fn sliceInfo(self: *@This()) [2]u64 { @@ -124,13 +124,13 @@ pub const Array = *align(8) packed struct(u64) { const ptr = self.len_or_ptr; const buf = self.bufU64(); if (ptr != 0) { - return buf[1..2]; + return .{ buf[1], buf[2] }; } else { - return buf[2..3]; + return .{ buf[2], buf[3] }; } } - pub fn buf(self: *@This()) [*]u8 { + pub fn bufU8(self: *@This()) [*]u8 { if (self.is_ptr) { if (self.arrPointer()) |a| { return a.bufContent(); @@ -144,7 +144,7 @@ pub const Array = *align(8) packed struct(u64) { pub fn str(self: *@This()) []u8 { if (self.is_slice) { - const buf = self.buf(); + const buf = self.bufU8(); const start, const end = self.sliceInfo(); return buf[start..end]; } diff --git a/src/zisp/value/istr.zig b/src/zisp/value/istr.zig index dec3b09..8f70725 100644 --- a/src/zisp/value/istr.zig +++ b/src/zisp/value/istr.zig @@ -3,6 +3,8 @@ const std = @import("std"); const gc = @import("../gc.zig"); const value = @import("../value.zig"); +const Alloc = std.mem.Allocator; + const Value = value.Value; // Zig API @@ -22,25 +24,26 @@ pub const Istr = *align(8) packed union { return @ptrCast(self); } - pub fn new(alloc: *Allocator, hash: u64, s: []const u8) Istr { - const istr: Istr = @ptrCast(alloc.alloc(u8, 8 + s.len)); - istr.hash = hash; - istr.putStr(s); - return istr; - } - pub fn putStr(self: *@This(), s: []const u8) void { std.debug.assert(s.len <= 255); - @memcpy(istr.bufU8()[8 .. 8 + s.len], s); + @memcpy(self.bufU8()[8 .. 8 + s.len], s); } pub fn str(self: *@This()) []const u8 { const buf = self.bufU8(); - const len = self.meta.len; - return buf[8 .. 8 + len]; + return buf[8 .. 8 + self.meta.len]; } }; +pub fn new(alloc: Alloc, hash: u64, s: []const u8) !Istr { + const aln = std.mem.Alignment.of(Istr); + const ptr = try alloc.alignedAlloc(u8, aln, 8 + s.len); + const istr: Istr = @ptrCast(ptr); + istr.hash = hash; + istr.putStr(s); + return istr; +} + pub fn check(v: Value) ?Istr { if (v.getPtr(.istr)) |p| { return @ptrCast(p); -- cgit v1.2.3