[MODS]: Make sure only runtime and parser need to be defer freed

This commit is contained in:
luccie 2025-08-10 23:28:57 +02:00
parent caed24d178
commit 7836ebfcd3
4 changed files with 24 additions and 7 deletions

View file

@ -81,6 +81,8 @@ pub fn deinit(self: Parser) void {
self.allocator.free(t.returns);
}
self.allocator.free(self.types);
self.allocator.free(self.globalValues);
self.allocator.free(self.globalTypes);
}
pub fn module(self: *Parser) vm.Module {

View file

@ -48,6 +48,8 @@ indices: []Index,
// TODO: this could be a byte array and v128.const and i8x16.shuffle could live here too
select_valtypes: []vm.Valtype,
br_table_vectors: []u32,
pub fn print(self: IR, writer: anytype) !void {
for (self.opcodes, 0..) |op, i| {
try writer.print("{x:3} {s}", .{ i, @tagName(op) });
@ -860,12 +862,12 @@ pub fn parse(parser: *Parser) !IR {
.allocator = parser.allocator,
};
try state.parseFunction();
std.debug.print("Br length: {d}\n", .{state.branches.items.len});
if (state.branches.items.len != 0) return Parser.Error.unresolved_branch;
return .{
.opcodes = try state.opcodes.toOwnedSlice(state.allocator),
.indices = try state.indices.toOwnedSlice(state.allocator),
.select_valtypes = &.{},
.br_table_vectors = state.br_table_vectors.items
};
}
@ -883,6 +885,7 @@ pub fn parseGlobalExpr(parser: *Parser) !IR {
.opcodes = try state.opcodes.toOwnedSlice(state.allocator),
.indices = try state.indices.toOwnedSlice(state.allocator),
.select_valtypes = &.{},
.br_table_vectors = state.br_table_vectors.items
};
}
@ -900,5 +903,6 @@ pub fn parseSingleExpr(parser: *Parser) !IR {
.opcodes = try state.opcodes.toOwnedSlice(state.allocator),
.indices = try state.indices.toOwnedSlice(state.allocator),
.select_valtypes = &.{},
.br_table_vectors = state.br_table_vectors.items
};
}

View file

@ -60,10 +60,7 @@ pub const Module = struct {
elems: [][]u32,
pub fn deinit(self: Module, allocator: Allocator) void {
// self.exports.deinit(allocator);
for (self.functions) |f| {
allocator.free(f.func_type.parameters);
allocator.free(f.func_type.returns);
switch (f.typ) {
.internal => {
allocator.free(f.typ.internal.ir.opcodes);
@ -75,6 +72,12 @@ pub const Module = struct {
}
}
allocator.free(self.functions);
allocator.free(self.data);
allocator.free(self.tables);
for (self.elems) |elem| {
allocator.free(elem);
}
allocator.free(self.elems);
}
};
@ -116,6 +119,8 @@ pub const Runtime = struct {
pub fn deinit(self: *Runtime, allocator: Allocator) void {
self.stack.deinit();
self.module.deinit(allocator);
self.global_runtime.deinit();
allocator.free(self.memory);
}
@ -139,7 +144,15 @@ pub const Runtime = struct {
continue;
}
},
.br_table => @panic("UNIMPLEMENTED"),
.br_table => {
const idx = self.stack.pop().?.i32;
if (idx < index.indirect.y){
frame.program_counter = frame.code.br_table_vectors[index.indirect.x + @as(u32, @intCast(idx))];
} else {
frame.program_counter = frame.code.br_table_vectors[index.indirect.y];
}
continue;
},
.@"return" => break :loop,
// TODO: Move this to callExternal
.call => {

View file

@ -24,7 +24,6 @@ var resources: ecs.Resources = undefined;
fn init_mods() void {
var global_runtime = mods.GlobalRuntime.init(allocator);
defer global_runtime.deinit();
const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm");
// const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm");
@ -36,7 +35,6 @@ fn init_mods() void {
std.debug.panic("[ERROR]: error {any} at byte {x}(0x{x})\n", .{ err, parser.byte_idx, parser.bytes[parser.byte_idx] });
};
const module = parser.module();
defer module.deinit(allocator);
for (0..parser.globalTypes.len) |i| {
global_runtime.addGlobal(@intCast(i), parser.globalTypes[i], parser.globalValues[i]) catch @panic("Failed to add runtime global");