Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak fixes, code reorg, plus a very WIP register VM #28

Merged
merged 12 commits into from
Mar 16, 2024
2 changes: 1 addition & 1 deletion bench/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn run(allocator: std.mem.Allocator, benchmark: Benchmark) !void {
defer module_def.deinit();
try module_def.decode(wasm_data);

var module_instance = bytebox.ModuleInstance.init(&module_def, allocator);
var module_instance = try bytebox.ModuleInstance.init(&module_def, allocator);
defer module_instance.deinit();
try module_instance.instantiate(.{});

Expand Down
4 changes: 2 additions & 2 deletions run/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub fn main() !void {
return;
}

var module_instance = bytebox.ModuleInstance.init(&module_def, allocator);
var module_instance = try bytebox.ModuleInstance.init(&module_def, allocator);
defer module_instance.deinit();

var imports_wasi: bytebox.ModuleImportPackage = try wasi.initImports(.{
Expand Down Expand Up @@ -353,7 +353,7 @@ pub fn main() !void {
var returns = std.ArrayList(bytebox.Val).init(allocator);
try returns.resize(func_export.returns.len);

module_instance.invoke(func_handle, params.items, returns.items, .{}) catch |e| {
module_instance.invoke(func_handle, params.items.ptr, returns.items.ptr, .{}) catch |e| {
var backtrace = module_instance.formatBacktrace(1, allocator) catch unreachable;
std.log.err("Caught {} during function invoke. Backtrace:\n{s}\n", .{ e, backtrace.items });
backtrace.deinit();
Expand Down
15 changes: 10 additions & 5 deletions src/cffi.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const std = @import("std");
const core = @import("core.zig");
const StableArray = @import("zig-stable-array/stable_array.zig").StableArray;
const AllocError = std.mem.Allocator.Error;

const core = @import("core.zig");
const ValType = core.ValType;
const Val = core.Val;
const ModuleDefinition = core.ModuleDefinition;
const ModuleInstance = core.ModuleInstance;
const ModuleImportPackage = core.ModuleImportPackage;

const StableArray = @import("zig-stable-array/stable_array.zig").StableArray;

// C interface
const CSlice = extern struct {
data: ?[*]u8,
Expand Down Expand Up @@ -348,7 +350,10 @@ export fn bb_module_instance_init(module_definition: ?*ModuleDefinition) ?*Modul
module = allocator.create(core.ModuleInstance) catch null;

if (module) |m| {
m.* = core.ModuleInstance.init(module_definition.?, allocator);
m.* = core.ModuleInstance.init(module_definition.?, allocator) catch {
// TODO log out of memory?
return null;
};
}
}

Expand Down Expand Up @@ -462,14 +467,14 @@ export fn bb_module_instance_invoke(module: ?*ModuleInstance, c_handle: CFuncHan
.type = @as(core.FunctionHandleType, @enumFromInt(c_handle.type)),
};

const invoke_opts = core.ModuleInstance.InvokeOpts{
const invoke_opts = core.InvokeOpts{
.trap_on_start = opts.trap_on_start,
};

var params_slice: []const Val = if (params != null) params.?[0..num_params] else &[_]Val{};
var returns_slice: []Val = if (returns != null) returns.?[0..num_returns] else &[_]Val{};

if (module.?.invoke(handle, params_slice, returns_slice, invoke_opts)) {
if (module.?.invoke(handle, params_slice.ptr, returns_slice.ptr, invoke_opts)) {
return CError.Ok;
} else |err| {
return translateError(err);
Expand Down
5 changes: 4 additions & 1 deletion src/common.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Lowest layer of the codebase, that contains types and code used in higher layers

const std = @import("std");
const StableArray = @import("zig-stable-array/stable_array.zig").StableArray;

pub const StableArray = @import("zig-stable-array/stable_array.zig").StableArray;

pub fn decodeLEB128(comptime T: type, reader: anytype) !T {
if (@typeInfo(T).Int.signedness == .signed) {
Expand Down
Loading
Loading