Skip to content

Commit

Permalink
fix(value): ObjMap has unmanaged hashmap
Browse files Browse the repository at this point in the history
No need to embark the allocator since it's always the gc allocator
  • Loading branch information
giann committed Jun 13, 2024
1 parent b445c3c commit bdbcc02
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
9 changes: 7 additions & 2 deletions src/Ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,14 @@ fn binaryValue(self: Self, node: Node.Index, gc: *GarbageCollector) !?Value {
},
)).toValue();
} else {
var new_map = try left_map.?.map.clone();
var new_map = try left_map.?.map.clone(gc.allocator);
var it = right_map.?.map.iterator();
while (it.next()) |entry| {
try new_map.put(entry.key_ptr.*, entry.value_ptr.*);
try new_map.put(
gc.allocator,
entry.key_ptr.*,
entry.value_ptr.*,
);
}

return (try gc.allocateObject(
Expand Down Expand Up @@ -1161,6 +1165,7 @@ pub fn toValue(self: Self, node: Node.Index, gc: *GarbageCollector) Error!Value

for (components.entries) |entry| {
try map.map.put(
gc.allocator,
try self.toValue(entry.key, gc),
try self.toValue(entry.value, gc),
);
Expand Down
8 changes: 5 additions & 3 deletions src/builtin/map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub fn clone(ctx: *NativeCtx) c_int {
ctx.vm.panic("Out of memory");
unreachable;
};
new_map.map.deinit();
new_map.map = self.map.clone() catch {
new_map.map.deinit(ctx.vm.gc.allocator);
new_map.map = self.map.clone(ctx.vm.gc.allocator) catch {
ctx.vm.panic("Out of memory");
unreachable;
};
Expand Down Expand Up @@ -131,7 +131,9 @@ pub fn map(ctx: *NativeCtx) c_int {
const self = ObjMap.cast(ctx.vm.peek(1).obj()).?;
const closure = ObjClosure.cast(ctx.vm.peek(0).obj()).?;

const mapped_type = closure.function.type_def.resolved_type.?.Function.return_type.resolved_type.?.ObjectInstance.resolved_type.?.Object;
const mapped_type = closure.function.type_def.resolved_type.?.Function
.return_type.resolved_type.?.ObjectInstance
.resolved_type.?.Object;

var new_map: *ObjMap = ctx.vm.gc.allocateObject(
ObjMap,
Expand Down
8 changes: 6 additions & 2 deletions src/buzz_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,14 @@ export fn bz_mapConcat(vm: *VM, map: Value, other_map: Value) Value {
const left: *ObjMap = ObjMap.cast(map.obj()).?;
const right: *ObjMap = ObjMap.cast(other_map.obj()).?;

var new_map = left.map.clone() catch @panic("Could not concatenate maps");
var new_map = left.map.clone(vm.gc.allocator) catch @panic("Could not concatenate maps");
var it = right.map.iterator();
while (it.next()) |entry| {
new_map.put(entry.key_ptr.*, entry.value_ptr.*) catch @panic("Could not concatenate maps");
new_map.put(
vm.gc.allocator,
entry.key_ptr.*,
entry.value_ptr.*,
) catch @panic("Could not concatenate maps");
}

return (vm.gc.allocateObject(ObjMap, ObjMap{
Expand Down
16 changes: 11 additions & 5 deletions src/obj.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ pub const Obj = struct {
) catch return error.OutOfMemory;

serialized_range.map.put(
vm.gc.allocator,
(vm.gc.copyString("low") catch return error.OutOfMemory).toValue(),
Value.fromInteger(range.low),
) catch return error.OutOfMemory;

serialized_range.map.put(
vm.gc.allocator,
(vm.gc.copyString("high") catch return error.OutOfMemory).toValue(),
Value.fromInteger(range.high),
) catch return error.OutOfMemory;
Expand Down Expand Up @@ -2910,14 +2912,14 @@ pub const ObjMap = struct {
type_def: *ObjTypeDef,

// We need an ArrayHashMap for `next`
map: std.AutoArrayHashMap(Value, Value),
map: std.AutoArrayHashMapUnmanaged(Value, Value),

methods: []?*ObjNative,

pub fn init(allocator: Allocator, type_def: *ObjTypeDef) !Self {
const self = Self{
.type_def = type_def,
.map = std.AutoArrayHashMap(Value, Value).init(allocator),
.map = std.AutoArrayHashMapUnmanaged(Value, Value){},
.methods = try allocator.alloc(
?*ObjNative,
Self.members.len,
Expand All @@ -2932,7 +2934,11 @@ pub const ObjMap = struct {
}

pub fn set(self: *Self, gc: *GarbageCollector, key: Value, value: Value) !void {
try self.map.put(key, value);
try self.map.put(
gc.allocator,
key,
value,
);
try gc.markObjDirty(&self.obj);
}

Expand Down Expand Up @@ -3022,7 +3028,7 @@ pub const ObjMap = struct {
}

pub fn deinit(self: *Self, allocator: Allocator) void {
self.map.deinit();
self.map.deinit(allocator);
allocator.free(self.methods);
}

Expand Down Expand Up @@ -4947,7 +4953,7 @@ pub fn cloneObject(obj: *Obj, vm: *VM) !Value {
ObjMap,
.{
.type_def = map.type_def,
.map = try map.map.clone(),
.map = try map.map.clone(vm.gc.allocator),
.methods = map.methods,
},
)).toValue();
Expand Down
8 changes: 6 additions & 2 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3559,13 +3559,17 @@ pub const VM = struct {
const right: *ObjMap = self.pop().obj().access(ObjMap, .Map, self.gc).?;
const left: *ObjMap = self.pop().obj().access(ObjMap, .Map, self.gc).?;

var new_map = left.map.clone() catch {
var new_map = left.map.clone(self.gc.allocator) catch {
self.panic("Out of memory");
unreachable;
};
var it = right.map.iterator();
while (it.next()) |entry| {
new_map.put(entry.key_ptr.*, entry.value_ptr.*) catch {
new_map.put(
self.gc.allocator,
entry.key_ptr.*,
entry.value_ptr.*,
) catch {
self.panic("Out of memory");
unreachable;
};
Expand Down

0 comments on commit bdbcc02

Please sign in to comment.