Compare commits

..

No commits in common. "37942599490dd4aec2d4e9db26e26753bf393b0a" and "155645a308844ee4ac18c0602209f23a44c7898b" have entirely different histories.

2 changed files with 35 additions and 46 deletions

View File

@ -52,6 +52,17 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
entry.value = value;
}
///
/// Fetches the value referenced by `index` in `self`, returning it.
///
pub fn fetch(self: *Self, index: Index) Value {
const entry = &self.table[index];
debug.assert(entry.* == .value);
return entry.value;
}
///
/// Deinitializes `self` and sets it to an invalid state, freeing all memory allocated by `allocator`.
///
@ -70,17 +81,6 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
self.free_index = 0;
}
///
/// Fetches the value referenced by `index` in `self`, returning it.
///
pub fn fetch(self: *Self, index: Index) Value {
const entry = &self.table[index];
debug.assert(entry.* == .value);
return entry.value;
}
///
/// Attempts to grow the internal buffer of `self` by `growth_amount` using `allocator`.
///
@ -147,20 +147,12 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
debug.assert(entry.* == .free_index);
self.count += 1;
self.free_index = entry.free_index;
entry.* = .{.value = value};
return entry_index;
}
///
/// Returns `true` if `self` contains no values, otherwise `false`.
///
pub fn is_empty(self: Self) bool {
return self.count == 0;
}
///
/// Removes the value referenced by `index` from `self`.
///
@ -169,7 +161,6 @@ pub fn Map(comptime index_int: std.builtin.Type.Int, comptime Value: type) type
debug.assert(entry.* == .value);
self.count -= 1;
entry.* = .{.free_index = self.free_index};
self.free_index = index;
}

View File

@ -170,26 +170,46 @@ pub fn check(self: *Self, condition: bool, failure_message: []const u8) !void {
}
pub fn deinit(self: *Self) void {
self.object_release(self.global_object);
self.discard(.{.object = self.global_object});
{
var interned_iterable = InternTable.Iterable{.hashed_map = &self.interned};
while (interned_iterable.next()) |entry| {
self.object_release(entry.value);
self.discard(.{.object = entry.value});
}
}
self.interned.deinit(self.allocator);
self.values.deinit(self.allocator);
self.calls.deinit(self.allocator);
coral.debug.assert(self.heap.is_empty());
self.heap.deinit(self.allocator);
}
pub fn discard(self: *Self, val: types.Val) void {
switch (val) {
.object => |object| self.object_release(object),
.object => |object| {
var data = self.heap.fetch(object);
coral.debug.assert(data.ref_count != 0);
data.ref_count -= 1;
if (data.ref_count == 0) {
data.state.info.deinitializer(.{
.env = self,
.obj = val.as_ref(),
});
// TODO: Free individual key-value pairs of fields
data.state.fields.deinit(self.allocator);
coral.io.deallocate(self.allocator, data.state.userdata);
self.heap.remove(object);
} else {
self.heap.assign(object, data);
}
},
else => {},
}
}
@ -396,28 +416,6 @@ pub fn new_string(self: *Self, data: []const u8) coral.io.AllocationError!types.
});
}
pub fn object_release(self: *Self, object: types.Object) void {
var data = self.heap.fetch(object);
coral.debug.assert(data.ref_count != 0);
data.ref_count -= 1;
if (data.ref_count == 0) {
data.state.info.deinitializer(.{
.env = self,
.obj = .{.object = object},
});
// TODO: Free individual key-value pairs of fields
data.state.fields.deinit(self.allocator);
coral.io.deallocate(self.allocator, data.state.userdata);
self.heap.remove(object);
} else {
self.heap.assign(object, data);
}
}
pub fn set_global(self: *Self, global_name: []const u8, value: types.Ref) coral.io.AllocationError!void {
try self.globals.assign(self.allocator, global_name, value);
}