summaryrefslogtreecommitdiff
path: root/src/mods/vm.zig
diff options
context:
space:
mode:
authorErnesto Lanchares <elancha98@proton.me>2025-04-04 18:34:58 +0200
committerErnesto Lanchares <elancha98@proton.me>2025-04-04 18:34:58 +0200
commit25e51f9aead0d1de6d6a9a9660d9363ef145316e (patch)
tree7d882523495f255792e661b8a68f9903c6bee2c1 /src/mods/vm.zig
parent28420f53b06c7be0090ebbd4f242b11f976d6f64 (diff)
Modified how exports work and fixed memory leaks.
Now exports are already defined by the mods api (something like preinit, init, preframe, postframe, deinit should be enough functions). At the moment we only support preinit function.
Diffstat (limited to 'src/mods/vm.zig')
-rw-r--r--src/mods/vm.zig51
1 files changed, 35 insertions, 16 deletions
diff --git a/src/mods/vm.zig b/src/mods/vm.zig
index a636cb3..03e370c 100644
--- a/src/mods/vm.zig
+++ b/src/mods/vm.zig
@@ -24,22 +24,37 @@ pub const Functype = struct {
allocator.free(self.returns);
}
};
-pub const Function = struct { func_type: Functype, typ: union(enum) {
- internal: struct {
- locals: []Valtype,
- ir: IR,
- },
- external: void,
-} };
+pub const Function = struct {
+ func_type: Functype,
+ typ: union(enum) {
+ internal: struct {
+ locals: []Valtype,
+ ir: IR,
+ },
+ external: void,
+ }
+};
+
+pub const ExportFunction = enum {
+ preinit,
+};
+pub const Exports = struct {
+ preinit: ?u32 = null,
+};
+comptime {
+ std.debug.assert(@typeInfo(ExportFunction).@"enum".fields.len == @typeInfo(Exports).@"struct".fields.len );
+}
+
pub const Module = struct {
memory: Memory,
functions: []Function,
- exports: std.StringHashMapUnmanaged(u32),
+ exports: Exports,
- fn deinit(self: *Module, allocator: Allocator) void {
- self.exports.deinit(allocator);
+ pub fn deinit(self: Module, allocator: Allocator) void {
+ // self.exports.deinit(allocator);
for (self.functions) |f| {
+ std.debug.print("Freeing function parameters at {*}\n", .{f.func_type.parameters.ptr});
allocator.free(f.func_type.parameters);
allocator.free(f.func_type.returns);
switch (f.typ) {
@@ -877,12 +892,16 @@ pub const Runtime = struct {
}
// TODO: Do name resolution at parseTime
- pub fn callExternal(self: *Runtime, allocator: Allocator, name: []const u8, parameters: []Value) !void {
- if (self.module.exports.get(name)) |function| {
- try self.call(allocator, function, parameters);
- } else {
- std.debug.panic("Function `{s}` not avaliable", .{name});
- }
+ pub fn callExternal(self: *Runtime, allocator: Allocator, name: ExportFunction, parameters: []Value) !void {
+ switch (name) {
+ .preinit => {
+ if (self.module.exports.preinit) |func| {
+ try self.call(allocator, func, parameters);
+ } else {
+ std.debug.panic("Function preinit unavailable\n", .{});
+ }
+ },
+ }
}
pub fn call(self: *Runtime, allocator: Allocator, function: usize, parameters: []Value) AllocationError!void {