From 5cb6b20137544299220a87e7c97ec08ad8020bf3 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 13 Jun 2024 10:47:04 +0200 Subject: [PATCH] fix(value): List uses ArrayListUnmanaged --- src/Ast.zig | 11 +++++++---- src/builtin/list.zig | 8 ++++---- src/builtin/map.zig | 12 ++++++------ src/builtin/pattern.zig | 13 +++++++------ src/buzz_api.zig | 6 +++--- src/obj.zig | 19 +++++++++++++------ src/vm.zig | 7 ++++--- 7 files changed, 44 insertions(+), 32 deletions(-) diff --git a/src/Ast.zig b/src/Ast.zig index a09eb7fe..072e62f3 100644 --- a/src/Ast.zig +++ b/src/Ast.zig @@ -989,9 +989,9 @@ fn binaryValue(self: Self, node: Node.Index, gc: *GarbageCollector) !?Value { } else if (right_integer) |ri| { return Value.fromInteger(ri +% left_integer.?); } else if (right_list) |rl| { - var new_list = std.ArrayList(Value).init(gc.allocator); - try new_list.appendSlice(left_list.?.items.items); - try new_list.appendSlice(rl.items.items); + var new_list = std.ArrayListUnmanaged(Value){}; + try new_list.appendSlice(gc.allocator, left_list.?.items.items); + try new_list.appendSlice(gc.allocator, rl.items.items); return (try gc.allocateObject( obj.ObjList, @@ -1147,7 +1147,10 @@ pub fn toValue(self: Self, node: Node.Index, gc: *GarbageCollector) Error!Value ); for (components.items) |item| { - try list.items.append(try self.toValue(item, gc)); + try list.items.append( + gc.allocator, + try self.toValue(item, gc), + ); } break :list list.toValue(); diff --git a/src/builtin/list.zig b/src/builtin/list.zig index c988094d..e8f34754 100644 --- a/src/builtin/list.zig +++ b/src/builtin/list.zig @@ -75,7 +75,7 @@ pub fn reverse(ctx: *NativeCtx) c_int { unreachable; }; - new_list.items.appendSlice(list.items.items) catch { + new_list.items.appendSlice(ctx.vm.gc.allocator, list.items.items) catch { ctx.vm.panic("Out of memory"); unreachable; }; @@ -192,7 +192,7 @@ pub fn clone(ctx: *NativeCtx) c_int { ctx.vm.panic("Out of memory"); unreachable; }; - new_list.items.appendSlice(self.items.items) catch { + new_list.items.appendSlice(ctx.vm.gc.allocator, self.items.items) catch { ctx.vm.panic("Out of memory"); unreachable; }; @@ -268,7 +268,7 @@ pub fn sub(ctx: *NativeCtx) c_int { ctx.vm.panic("Out of memory"); unreachable; }, - .items = std.ArrayList(Value).init(ctx.vm.gc.allocator), + .items = std.ArrayListUnmanaged(Value){}, }, ) catch { ctx.vm.panic("Out of memory"); @@ -277,7 +277,7 @@ pub fn sub(ctx: *NativeCtx) c_int { ctx.vm.push(list.toValue()); - list.items.appendSlice(substr) catch { + list.items.appendSlice(ctx.vm.gc.allocator, substr) catch { ctx.vm.panic("Out of memory"); unreachable; }; diff --git a/src/builtin/map.zig b/src/builtin/map.zig index 5479f675..d6e7ecf5 100644 --- a/src/builtin/map.zig +++ b/src/builtin/map.zig @@ -337,9 +337,9 @@ pub fn keys(ctx: *NativeCtx) c_int { const self: *ObjMap = ObjMap.cast(ctx.vm.peek(0).obj()).?; const map_keys = self.map.keys(); - var result = std.ArrayList(Value).init(ctx.vm.gc.allocator); + var result = std.ArrayListUnmanaged(Value){}; for (map_keys) |key| { - result.append(key) catch { + result.append(ctx.vm.gc.allocator, key) catch { ctx.vm.panic("Out of memory"); unreachable; }; @@ -375,7 +375,7 @@ pub fn keys(ctx: *NativeCtx) c_int { unreachable; }; - list.items.deinit(); + list.items.deinit(ctx.vm.gc.allocator); list.items = result; _ = ctx.vm.pop(); @@ -388,8 +388,8 @@ pub fn values(ctx: *NativeCtx) c_int { const self: *ObjMap = ObjMap.cast(ctx.vm.peek(0).obj()).?; const map_values: []Value = self.map.values(); - var result = std.ArrayList(Value).init(ctx.vm.gc.allocator); - result.appendSlice(map_values) catch { + var result = std.ArrayListUnmanaged(Value){}; + result.appendSlice(ctx.vm.gc.allocator, map_values) catch { ctx.vm.panic("Out of memory"); unreachable; }; @@ -421,7 +421,7 @@ pub fn values(ctx: *NativeCtx) c_int { unreachable; }; - list.items.deinit(); + list.items.deinit(ctx.vm.gc.allocator); list.items = result; ctx.vm.push(list.toValue()); diff --git a/src/builtin/pattern.zig b/src/builtin/pattern.zig index 150c2c2a..badf1a60 100644 --- a/src/builtin/pattern.zig +++ b/src/builtin/pattern.zig @@ -101,6 +101,7 @@ fn rawMatch(self: *ObjPattern, vm: *VM, subject: *ObjString, offset: *usize) !?* var i: usize = 0; while (i < rc) : (i += 1) { try results.?.items.append( + vm.gc.allocator, (try vm.gc.copyString( subject.string[@intCast(output_vector[2 * i])..@intCast(output_vector[2 * i + 1])], )).toValue(), @@ -133,7 +134,7 @@ fn rawMatchAll(self: *ObjPattern, vm: *VM, subject: *ObjString) !?*ObjList { vm.push(results.?.toValue()); } - try results.?.items.append(matches.toValue()); + try results.?.items.append(vm.gc.allocator, matches.toValue()); } else { if (results != null) { _ = vm.pop(); @@ -155,8 +156,8 @@ fn rawReplace(self: *ObjPattern, vm: *VM, subject: *ObjString, replacement: *Obj return subject; } - var result = std.ArrayList(u8).init(vm.gc.allocator); - defer result.deinit(); + var result = std.ArrayListUnmanaged(u8){}; + defer result.deinit(vm.gc.allocator); var match_data = self.pattern.createMatchData(null) orelse { vm.panic("Out of memory"); @@ -185,9 +186,9 @@ fn rawReplace(self: *ObjPattern, vm: *VM, subject: *ObjString, replacement: *Obj offset.* = @as(usize, @intCast(output_vector[1])); - try result.appendSlice(subject.string[0..@as(usize, @intCast(output_vector[0]))]); - try result.appendSlice(replacement.string); - try result.appendSlice(subject.string[offset.*..]); + try result.appendSlice(vm.gc.allocator, subject.string[0..@as(usize, @intCast(output_vector[0]))]); + try result.appendSlice(vm.gc.allocator, replacement.string); + try result.appendSlice(vm.gc.allocator, subject.string[offset.*..]); }, } diff --git a/src/buzz_api.zig b/src/buzz_api.zig index bc4253de..1040dc30 100644 --- a/src/buzz_api.zig +++ b/src/buzz_api.zig @@ -578,9 +578,9 @@ export fn bz_listConcat(vm: *VM, list: Value, other_list: Value) Value { const left: *ObjList = ObjList.cast(list.obj()).?; const right: *ObjList = ObjList.cast(other_list.obj()).?; - var new_list = std.ArrayList(Value).init(vm.gc.allocator); - new_list.appendSlice(left.items.items) catch @panic("Could not concatenate lists"); - new_list.appendSlice(right.items.items) catch @panic("Could not concatenate lists"); + var new_list = std.ArrayListUnmanaged(Value){}; + new_list.appendSlice(vm.gc.allocator, left.items.items) catch @panic("Could not concatenate lists"); + new_list.appendSlice(vm.gc.allocator, right.items.items) catch @panic("Could not concatenate lists"); return (vm.gc.allocateObject( ObjList, diff --git a/src/obj.zig b/src/obj.zig index f4ce1965..48194323 100644 --- a/src/obj.zig +++ b/src/obj.zig @@ -1827,13 +1827,13 @@ pub const ObjList = struct { type_def: *ObjTypeDef, /// List items - items: std.ArrayList(Value), + items: std.ArrayListUnmanaged(Value), methods: []?*ObjNative, pub fn init(allocator: Allocator, type_def: *ObjTypeDef) !Self { const self = Self{ - .items = std.ArrayList(Value).init(allocator), + .items = std.ArrayListUnmanaged(Value){}, .type_def = type_def, .methods = try allocator.alloc( ?*ObjNative, @@ -1862,7 +1862,7 @@ pub const ObjList = struct { } pub fn deinit(self: *Self, allocator: Allocator) void { - self.items.deinit(); + self.items.deinit(allocator); allocator.free(self.methods); } @@ -1943,12 +1943,19 @@ pub const ObjList = struct { } pub fn rawAppend(self: *Self, gc: *GarbageCollector, value: Value) !void { - try self.items.append(value); + try self.items.append( + gc.allocator, + value, + ); try gc.markObjDirty(&self.obj); } pub fn rawInsert(self: *Self, gc: *GarbageCollector, index: usize, value: Value) !void { - try self.items.insert(index, value); + try self.items.insert( + gc.allocator, + index, + value, + ); try gc.markObjDirty(&self.obj); } @@ -4940,7 +4947,7 @@ pub fn cloneObject(obj: *Obj, vm: *VM) !Value { ObjList, .{ .type_def = list.type_def, - .items = try list.items.clone(), + .items = try list.items.clone(vm.gc.allocator), .methods = list.methods, }, )).toValue(); diff --git a/src/vm.zig b/src/vm.zig index 23671c8d..7cfa159a 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -517,6 +517,7 @@ pub const VM = struct { } try arg_list.items.append( + self.gc.allocator, Value.fromObj((try self.gc.copyString(std.mem.sliceTo(arg, 0))).toObj()), ); } @@ -3520,12 +3521,12 @@ pub const VM = struct { const right: *ObjList = self.pop().obj().access(ObjList, .List, self.gc).?; const left: *ObjList = self.pop().obj().access(ObjList, .List, self.gc).?; - var new_list = std.ArrayList(Value).init(self.gc.allocator); - new_list.appendSlice(left.items.items) catch { + var new_list = std.ArrayListUnmanaged(Value){}; + new_list.appendSlice(self.gc.allocator, left.items.items) catch { self.panic("Out of memory"); unreachable; }; - new_list.appendSlice(right.items.items) catch { + new_list.appendSlice(self.gc.allocator, right.items.items) catch { self.panic("Out of memory"); unreachable; };