ReviewOS

also looking at this

zig-utils/zig-js

fix(compat): stop describing ArrayList's layout, and read it instead

#876
Merged glennmichael123 wants to merge fix/zig-1963-arraylist-compat into main
5 files +88 -42
src/vm.zigmodified+18-13
Changes to src/vm.zig
@@ -214,22 +214,30 @@ const OperandStack = struct {
214214 self.items[self.items.len - 1] = item;
215215 }
216216
217 /// The stack as the `ArrayListUnmanaged` it is a view of, so the list can
218 /// grow and free the buffer on the stack's behalf.
219 ///
220 /// Built with `initBuffer` rather than a struct literal. A literal names
221 /// every field, and Zig 0.17.0-dev.1963 added `pointer_stability` to the
222 /// list — so a literal that was complete on 1818 is a compile error on
223 /// 1963. `initBuffer` fills whatever fields the current std has, and
224 /// `items.len` is the only thing this type adds on top.
225 fn asList(self: *const OperandStack) std.ArrayListUnmanaged(Value) {
226 var list: std.ArrayListUnmanaged(Value) = .initBuffer(self.items.ptr[0..self.capacity]);
227 list.items.len = self.items.len;
228 return list;
229 }
230
217231 pub fn ensureTotalCapacity(self: *OperandStack, allocator: std.mem.Allocator, new_capacity: usize) std.mem.Allocator.Error!void {
218232 if (new_capacity <= self.capacity) return;
219 var list: std.ArrayListUnmanaged(Value) = .{
220 .items = self.items,
221 .capacity = self.capacity,
222 };
233 var list = self.asList();
223234 try list.ensureTotalCapacity(allocator, new_capacity);
224235 self.items = list.items;
225236 self.capacity = list.capacity;
226237 }
227238
228239 noinline fn grow(self: *OperandStack, allocator: std.mem.Allocator) std.mem.Allocator.Error!void {
229 var list: std.ArrayListUnmanaged(Value) = .{
230 .items = self.items,
231 .capacity = self.capacity,
232 };
240 var list = self.asList();
233241 try list.ensureTotalCapacity(allocator, self.items.len + 1);
234242 self.items = list.items;
235243 self.capacity = list.capacity;
@@ -252,10 +260,7 @@ const OperandStack = struct {
252260 }
253261
254262 pub fn deinit(self: *OperandStack, allocator: std.mem.Allocator) void {
255 var list: std.ArrayListUnmanaged(Value) = .{
256 .items = self.items,
257 .capacity = self.capacity,
258 };
263 var list = self.asList();
259264 list.deinit(allocator);
260265 self.* = .empty;
261266 }
@@ -10044,7 +10049,7 @@ fn runChunk(
1004410049 try vm.notifyDebuggerException(false);
1004510050 return error.Throw;
1004610051 },
10047 .throw_not_a_reference => return vm.throwNotAReference(@enumFromInt(inst.a)),
10052 .throw_not_a_reference => return vm.throwNotAReference(@fromBackingInt(@intCast(inst.a))),
1004810053 .push_handler, .push_handler_catch, .push_handler_outer => {
1004910054 const outer = inst.op == .push_handler_outer;
1005010055 if (outer and (exec.environment_depth == 0 or vm.env.parent == null)) return error.OutOfMemory;