Skip to content

Commit

Permalink
fix(value): List uses ArrayListUnmanaged
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Jun 13, 2024
1 parent a33d311 commit 5cb6b20
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
11 changes: 7 additions & 4 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/builtin/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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");
Expand All @@ -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;
};
Expand Down
12 changes: 6 additions & 6 deletions src/builtin/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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();
Expand All @@ -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;
};
Expand Down Expand Up @@ -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());
Expand Down
13 changes: 7 additions & 6 deletions src/builtin/pattern.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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.*..]);
},
}

Expand Down
6 changes: 3 additions & 3 deletions src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 13 additions & 6 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
);
}
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit 5cb6b20

Please sign in to comment.