From 925727af18b9f51ed6814b5cfdb4e96e61fe9b74 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 15:28:54 -0500 Subject: [PATCH 01/11] Update build.zig to 15.1 --- build.zig | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/build.zig b/build.zig index 4b179f1..8129bb2 100644 --- a/build.zig +++ b/build.zig @@ -103,11 +103,14 @@ pub fn build(b: *Build) void { .options = options, }); - const lib_bytebox: *Compile = b.addStaticLibrary(.{ + const lib_bytebox: *Compile = b.addLibrary(.{ .name = "bytebox", - .root_source_file = b.path("src/cffi.zig"), - .target = target, - .optimize = optimize, + .linkage = .static, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/cffi.zig"), + .target = target, + .optimize = optimize, + }), }); lib_bytebox.root_module.addImport(stable_array_import.name, stable_array_import.module); lib_bytebox.root_module.addOptions("config", options); @@ -116,9 +119,11 @@ pub fn build(b: *Build) void { // Unit tests const unit_tests: *Compile = b.addTest(.{ - .root_source_file = b.path("src/tests.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/tests.zig"), + .target = target, + .optimize = optimize, + }), }); unit_tests.root_module.addImport(stable_array_import.name, stable_array_import.module); unit_tests.root_module.addOptions("config", options); @@ -161,11 +166,10 @@ pub fn build(b: *Build) void { // Cffi test const cffi_test_step = b.step("test-cffi", "Run cffi test"); - const cffi_build = b.addExecutable(.{ - .name = "test-cffi", + const cffi_build = b.addExecutable(.{ .name = "test-cffi", .root_module = b.createModule(.{ .target = target, .optimize = optimize, - }); + }) }); cffi_build.addCSourceFile(.{ .file = b.path("test/cffi/main.c"), }); @@ -188,12 +192,14 @@ pub fn build(b: *Build) void { all_tests_step.dependOn(cffi_test_step); } -fn buildExeWithRunStep(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Mode, imports: []const Import, opts: ExeOpts) *Build.Step { +fn buildExeWithRunStep(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, imports: []const Import, opts: ExeOpts) *Build.Step { const exe: *Compile = b.addExecutable(.{ .name = opts.exe_name, - .root_source_file = b.path(opts.root_src), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path(opts.root_src), + .target = target, + .optimize = optimize, + }), }); for (imports) |import| { @@ -251,9 +257,11 @@ fn buildWasmExe(b: *Build, filepath: []const u8, comptime arch: WasmArch) WasmBu var exe = b.addExecutable(.{ .name = filename_no_extension, - .root_source_file = b.path(filepath), - .target = b.resolveTargetQuery(target_query), - .optimize = .ReleaseSmall, + .root_module = b.createModule(.{ + .root_source_file = b.path(filepath), + .target = b.resolveTargetQuery(target_query), + .optimize = .ReleaseSmall, + }), }); exe.rdynamic = true; exe.entry = .disabled; From d03b8f950b9e80d1154d3c86999f4ae3d39dde3f Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 21:41:21 -0500 Subject: [PATCH 02/11] Change std.ArrayList to std.array_list.Managed --- bench/samples/json.zig | 2 +- run/main.zig | 18 +++--- src/cffi.zig | 2 +- src/definition.zig | 136 ++++++++++++++++++++--------------------- src/instance.zig | 70 ++++++++++----------- src/vm_register.zig | 70 ++++++++++----------- src/vm_stack.zig | 24 ++++---- src/wasi.zig | 14 ++--- test/wasm/main.zig | 24 ++++---- 9 files changed, 180 insertions(+), 180 deletions(-) diff --git a/bench/samples/json.zig b/bench/samples/json.zig index 1b23959..2962846 100644 --- a/bench/samples/json.zig +++ b/bench/samples/json.zig @@ -108,7 +108,7 @@ fn generate(n: usize, allocator: std.mem.Allocator) AllTables { var rng = prng.random(); // generate some json text - var json = std.ArrayList(u8).init(allocator); + var json = std.array_list.Managed(u8).init(allocator); { const products = allocator.alloc(AllTables.Product, n) catch @panic("OOM"); for (products) |*entry| { diff --git a/run/main.zig b/run/main.zig index 097db6e..f1d8cb7 100644 --- a/run/main.zig +++ b/run/main.zig @@ -45,7 +45,7 @@ fn getArgSafe(index: usize, args: []const []const u8) ?[]const u8 { return if (index < args.len) args[index] else null; } -fn parseCmdOpts(args: []const [:0]const u8, env_buffer: *std.ArrayList([]const u8), dir_buffer: *std.ArrayList([]const u8)) CmdOpts { +fn parseCmdOpts(args: []const [:0]const u8, env_buffer: *std.array_list.Managed([]const u8), dir_buffer: *std.array_list.Managed([]const u8)) CmdOpts { var opts = CmdOpts{}; if (args.len < 2) { @@ -191,11 +191,11 @@ pub fn main() !void { const args: []const [:0]u8 = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args); - var env_buffer = std.ArrayList([]const u8).init(allocator); + var env_buffer = std.array_list.Managed([]const u8).init(allocator); defer env_buffer.deinit(); try env_buffer.ensureTotalCapacity(4096); // 4096 vars should be enough for most insane script file scenarios. - var dir_buffer = std.ArrayList([]const u8).init(allocator); + var dir_buffer = std.array_list.Managed([]const u8).init(allocator); defer dir_buffer.deinit(); try dir_buffer.ensureTotalCapacity(4096); @@ -247,7 +247,7 @@ pub fn main() !void { }; if (opts.print_dump) { - var strbuf = std.ArrayList(u8).init(allocator); + var strbuf = std.array_list.Managed(u8).init(allocator); try strbuf.ensureTotalCapacity(1024 * 16); try module_def.dump(strbuf.writer()); log.info("{s}", .{strbuf.items}); @@ -289,7 +289,7 @@ pub fn main() !void { const num_params: usize = invoke_args.len; if (func_export.params.len != num_params) { - var strbuf = std.ArrayList(u8).init(allocator); + var strbuf = std.array_list.Managed(u8).init(allocator); defer strbuf.deinit(); try writeSignature(&strbuf, &func_export); std.log.err("Specified {} params but expected {}. The signature of '{s}' is:\n{s}", .{ @@ -303,7 +303,7 @@ pub fn main() !void { std.debug.assert(invoke_args.len == num_params); - var params = std.ArrayList(bytebox.Val).init(allocator); + var params = std.array_list.Managed(bytebox.Val).init(allocator); defer params.deinit(); try params.resize(invoke_args.len); for (func_export.params, 0..) |valtype, i| { @@ -352,7 +352,7 @@ pub fn main() !void { } } - var returns = std.ArrayList(bytebox.Val).init(allocator); + var returns = std.array_list.Managed(bytebox.Val).init(allocator); try returns.resize(func_export.returns.len); module_instance.invoke(func_handle, params.items.ptr, returns.items.ptr, .{}) catch |e| { @@ -363,7 +363,7 @@ pub fn main() !void { }; { - var strbuf = std.ArrayList(u8).init(allocator); + var strbuf = std.array_list.Managed(u8).init(allocator); defer strbuf.deinit(); const writer = strbuf.writer(); @@ -389,7 +389,7 @@ pub fn main() !void { } } -fn writeSignature(strbuf: *std.ArrayList(u8), info: *const bytebox.FunctionExport) !void { +fn writeSignature(strbuf: *std.array_list.Managed(u8), info: *const bytebox.FunctionExport) !void { const writer = strbuf.writer(); if (info.params.len == 0) { try std.fmt.format(writer, " params: none\n", .{}); diff --git a/src/cffi.zig b/src/cffi.zig index 0f04219..015f810 100644 --- a/src/cffi.zig +++ b/src/cffi.zig @@ -380,7 +380,7 @@ export fn bb_module_instance_instantiate(module: ?*ModuleInstance, c_opts: CModu const packages: []?*const ModuleImportPackage = c_opts.packages.?[0..c_opts.num_packages]; const allocator = cffi_gpa.allocator(); - var flat_packages = std.ArrayList(ModuleImportPackage).init(allocator); + var flat_packages = std.array_list.Managed(ModuleImportPackage).init(allocator); defer flat_packages.deinit(); flat_packages.ensureTotalCapacityPrecise(packages.len) catch return CError.OutOfMemory; diff --git a/src/definition.zig b/src/definition.zig index df3047d..4d1e856 100644 --- a/src/definition.zig +++ b/src/definition.zig @@ -550,7 +550,7 @@ pub const ConstantExpression = union(ConstantExpressionType) { }; pub const FunctionTypeDefinition = struct { - types: std.ArrayList(ValType), // TODO replace this with offsets into a single array in the ModuleDefinition + types: std.array_list.Managed(ValType), // TODO replace this with offsets into a single array in the ModuleDefinition num_params: u32, pub fn getParams(self: *const FunctionTypeDefinition) []const ValType { @@ -703,8 +703,8 @@ pub const ElementDefinition = struct { mode: ElementMode, reftype: ValType, offset: ?ConstantExpression, - elems_value: std.ArrayList(Val), - elems_expr: std.ArrayList(ConstantExpression), + elems_value: std.array_list.Managed(Val), + elems_expr: std.array_list.Managed(ConstantExpression), }; pub const DataMode = enum { @@ -713,7 +713,7 @@ pub const DataMode = enum { }; pub const DataDefinition = struct { - bytes: std.ArrayList(u8), + bytes: std.array_list.Managed(u8), memory_index: ?u32, offset: ?ConstantExpression, mode: DataMode, @@ -748,7 +748,7 @@ pub const DataDefinition = struct { } const num_bytes = try decodeLEB128(u32, reader); - var bytes = std.ArrayList(u8).init(allocator); + var bytes = std.array_list.Managed(u8).init(allocator); try bytes.resize(num_bytes); const num_read = try readBytes(reader, bytes.items); if (num_read != num_bytes) { @@ -1077,7 +1077,7 @@ pub const Instruction = struct { .Branch_Table => { const table_length = try decodeLEB128(u32, reader); - var label_ids = std.ArrayList(u32).init(module.allocator); + var label_ids = std.array_list.Managed(u32).init(module.allocator); defer label_ids.deinit(); try label_ids.ensureTotalCapacity(table_length); @@ -1408,7 +1408,7 @@ pub const Instruction = struct { const CustomSection = struct { name: []const u8, - data: std.ArrayList(u8), + data: std.array_list.Managed(u8), }; pub const NameCustomSection = struct { @@ -1517,8 +1517,8 @@ const ModuleValidator = struct { // Note that we use a nullable ValType here to map to the "Unknown" value type as described in the wasm spec // validation algorithm: https://webassembly.github.io/spec/core/appendix/algorithm.html - type_stack: std.ArrayList(?ValType), - control_stack: std.ArrayList(ControlFrame), + type_stack: std.array_list.Managed(?ValType), + control_stack: std.array_list.Managed(ControlFrame), control_types: StableArray(ValType), log: Logger, @@ -1527,8 +1527,8 @@ const ModuleValidator = struct { fn init(allocator: std.mem.Allocator, log: Logger) ModuleValidator { return ModuleValidator{ - .type_stack = std.ArrayList(?ValType).init(allocator), - .control_stack = std.ArrayList(ControlFrame).init(allocator), + .type_stack = std.array_list.Managed(?ValType).init(allocator), + .control_stack = std.array_list.Managed(ControlFrame).init(allocator), .control_types = StableArray(ValType).init(1 * 1024 * 1024), .log = log, }; @@ -2749,48 +2749,48 @@ pub const ModuleDefinitionOpts = struct { pub const ModuleDefinition = struct { const Code = struct { - locals: std.ArrayList(ValType), - instructions: std.ArrayList(Instruction), - validation_immediates: std.ArrayList(ValidationImmediates), + locals: std.array_list.Managed(ValType), + instructions: std.array_list.Managed(Instruction), + validation_immediates: std.array_list.Managed(ValidationImmediates), wasm_address_to_instruction_index: std.AutoHashMap(u32, u32), // Instruction.immediate indexes these arrays depending on the opcode - branch_table_immediates: std.ArrayList(BranchTableImmediates), - branch_table_ids_immediates: std.ArrayList(u32), - v128_immediates: std.ArrayList(v128), - memory_offset_and_lane_immediates: std.ArrayList(MemoryOffsetAndLaneImmediates), - vec_shuffle_16_immediates: std.ArrayList([16]u8), + branch_table_immediates: std.array_list.Managed(BranchTableImmediates), + branch_table_ids_immediates: std.array_list.Managed(u32), + v128_immediates: std.array_list.Managed(v128), + memory_offset_and_lane_immediates: std.array_list.Managed(MemoryOffsetAndLaneImmediates), + vec_shuffle_16_immediates: std.array_list.Managed([16]u8), }; const Imports = struct { - functions: std.ArrayList(FunctionImportDefinition), - tables: std.ArrayList(TableImportDefinition), - memories: std.ArrayList(MemoryImportDefinition), - globals: std.ArrayList(GlobalImportDefinition), + functions: std.array_list.Managed(FunctionImportDefinition), + tables: std.array_list.Managed(TableImportDefinition), + memories: std.array_list.Managed(MemoryImportDefinition), + globals: std.array_list.Managed(GlobalImportDefinition), }; const Exports = struct { - functions: std.ArrayList(ExportDefinition), - tables: std.ArrayList(ExportDefinition), - memories: std.ArrayList(ExportDefinition), - globals: std.ArrayList(ExportDefinition), + functions: std.array_list.Managed(ExportDefinition), + tables: std.array_list.Managed(ExportDefinition), + memories: std.array_list.Managed(ExportDefinition), + globals: std.array_list.Managed(ExportDefinition), }; allocator: std.mem.Allocator, code: Code, - types: std.ArrayList(FunctionTypeDefinition), + types: std.array_list.Managed(FunctionTypeDefinition), imports: Imports, - functions: std.ArrayList(FunctionDefinition), - globals: std.ArrayList(GlobalDefinition), - tables: std.ArrayList(TableDefinition), - memories: std.ArrayList(MemoryDefinition), - elements: std.ArrayList(ElementDefinition), + functions: std.array_list.Managed(FunctionDefinition), + globals: std.array_list.Managed(GlobalDefinition), + tables: std.array_list.Managed(TableDefinition), + memories: std.array_list.Managed(MemoryDefinition), + elements: std.array_list.Managed(ElementDefinition), exports: Exports, - datas: std.ArrayList(DataDefinition), - custom_sections: std.ArrayList(CustomSection), + datas: std.array_list.Managed(DataDefinition), + custom_sections: std.array_list.Managed(CustomSection), name_section: NameCustomSection, @@ -2806,37 +2806,37 @@ pub const ModuleDefinition = struct { def.* = ModuleDefinition{ .allocator = allocator, .code = Code{ - .instructions = std.ArrayList(Instruction).init(allocator), - .validation_immediates = std.ArrayList(ValidationImmediates).init(allocator), - .locals = std.ArrayList(ValType).init(allocator), + .instructions = std.array_list.Managed(Instruction).init(allocator), + .validation_immediates = std.array_list.Managed(ValidationImmediates).init(allocator), + .locals = std.array_list.Managed(ValType).init(allocator), .wasm_address_to_instruction_index = std.AutoHashMap(u32, u32).init(allocator), - .branch_table_immediates = std.ArrayList(BranchTableImmediates).init(allocator), - .branch_table_ids_immediates = std.ArrayList(u32).init(allocator), - .v128_immediates = std.ArrayList(v128).init(allocator), - .memory_offset_and_lane_immediates = std.ArrayList(MemoryOffsetAndLaneImmediates).init(allocator), - .vec_shuffle_16_immediates = std.ArrayList([16]u8).init(allocator), + .branch_table_immediates = std.array_list.Managed(BranchTableImmediates).init(allocator), + .branch_table_ids_immediates = std.array_list.Managed(u32).init(allocator), + .v128_immediates = std.array_list.Managed(v128).init(allocator), + .memory_offset_and_lane_immediates = std.array_list.Managed(MemoryOffsetAndLaneImmediates).init(allocator), + .vec_shuffle_16_immediates = std.array_list.Managed([16]u8).init(allocator), }, - .types = std.ArrayList(FunctionTypeDefinition).init(allocator), + .types = std.array_list.Managed(FunctionTypeDefinition).init(allocator), .imports = Imports{ - .functions = std.ArrayList(FunctionImportDefinition).init(allocator), - .tables = std.ArrayList(TableImportDefinition).init(allocator), - .memories = std.ArrayList(MemoryImportDefinition).init(allocator), - .globals = std.ArrayList(GlobalImportDefinition).init(allocator), - }, - .functions = std.ArrayList(FunctionDefinition).init(allocator), - .globals = std.ArrayList(GlobalDefinition).init(allocator), - .tables = std.ArrayList(TableDefinition).init(allocator), - .memories = std.ArrayList(MemoryDefinition).init(allocator), - .elements = std.ArrayList(ElementDefinition).init(allocator), + .functions = std.array_list.Managed(FunctionImportDefinition).init(allocator), + .tables = std.array_list.Managed(TableImportDefinition).init(allocator), + .memories = std.array_list.Managed(MemoryImportDefinition).init(allocator), + .globals = std.array_list.Managed(GlobalImportDefinition).init(allocator), + }, + .functions = std.array_list.Managed(FunctionDefinition).init(allocator), + .globals = std.array_list.Managed(GlobalDefinition).init(allocator), + .tables = std.array_list.Managed(TableDefinition).init(allocator), + .memories = std.array_list.Managed(MemoryDefinition).init(allocator), + .elements = std.array_list.Managed(ElementDefinition).init(allocator), .exports = Exports{ - .functions = std.ArrayList(ExportDefinition).init(allocator), - .tables = std.ArrayList(ExportDefinition).init(allocator), - .memories = std.ArrayList(ExportDefinition).init(allocator), - .globals = std.ArrayList(ExportDefinition).init(allocator), + .functions = std.array_list.Managed(ExportDefinition).init(allocator), + .tables = std.array_list.Managed(ExportDefinition).init(allocator), + .memories = std.array_list.Managed(ExportDefinition).init(allocator), + .globals = std.array_list.Managed(ExportDefinition).init(allocator), }, - .datas = std.ArrayList(DataDefinition).init(allocator), - .custom_sections = std.ArrayList(CustomSection).init(allocator), + .datas = std.array_list.Managed(DataDefinition).init(allocator), + .custom_sections = std.array_list.Managed(CustomSection).init(allocator), .name_section = NameCustomSection.init(allocator), .log = if (opts.log) |log| log else Logger.empty(), .debug_name = try allocator.dupe(u8, opts.debug_name), @@ -2920,7 +2920,7 @@ pub const ModuleDefinition = struct { var section = CustomSection{ .name = name, - .data = std.ArrayList(u8).init(allocator), + .data = std.array_list.Managed(u8).init(allocator), }; const name_length: usize = stream.pos - section_start_pos; @@ -2951,7 +2951,7 @@ pub const ModuleDefinition = struct { const num_params = try decodeLEB128(u32, reader); - var func = FunctionTypeDefinition{ .num_params = num_params, .types = std.ArrayList(ValType).init(allocator) }; + var func = FunctionTypeDefinition{ .num_params = num_params, .types = std.array_list.Managed(ValType).init(allocator) }; errdefer func.types.deinit(); var params_left = num_params; @@ -3256,7 +3256,7 @@ pub const ModuleDefinition = struct { return expr; } - fn readElemsVal(elems: *std.ArrayList(Val), valtype: ValType, _reader: anytype, _module: *const ModuleDefinition) !void { + fn readElemsVal(elems: *std.array_list.Managed(Val), valtype: ValType, _reader: anytype, _module: *const ModuleDefinition) !void { const num_elems = try decodeLEB128(u32, _reader); try elems.ensureTotalCapacity(num_elems); @@ -3270,7 +3270,7 @@ pub const ModuleDefinition = struct { } } - fn readElemsExpr(elems: *std.ArrayList(ConstantExpression), _reader: anytype, _module: *const ModuleDefinition, expected_reftype: ValType) !void { + fn readElemsExpr(elems: *std.array_list.Managed(ConstantExpression), _reader: anytype, _module: *const ModuleDefinition, expected_reftype: ValType) !void { const num_elems = try decodeLEB128(u32, _reader); try elems.ensureTotalCapacity(num_elems); @@ -3302,8 +3302,8 @@ pub const ModuleDefinition = struct { .reftype = ValType.FuncRef, .table_index = 0, .offset = null, - .elems_value = std.ArrayList(Val).init(allocator), - .elems_expr = std.ArrayList(ConstantExpression).init(allocator), + .elems_value = std.array_list.Managed(Val).init(allocator), + .elems_expr = std.array_list.Managed(ConstantExpression).init(allocator), }; errdefer def.elems_value.deinit(); errdefer def.elems_expr.deinit(); @@ -3362,7 +3362,7 @@ pub const ModuleDefinition = struct { begin_index: u32, opcode: Opcode, }; - var block_stack = std.ArrayList(BlockData).init(allocator); + var block_stack = std.array_list.Managed(BlockData).init(allocator); defer block_stack.deinit(); var if_to_else_offsets = std.AutoHashMap(u32, u32).init(allocator); @@ -3385,7 +3385,7 @@ pub const ModuleDefinition = struct { valtype: ValType, count: u32, }; - var local_types_scratch = std.ArrayList(TypeCount).init(allocator); + var local_types_scratch = std.array_list.Managed(TypeCount).init(allocator); defer local_types_scratch.deinit(); var code_index: u32 = 0; diff --git a/src/instance.zig b/src/instance.zig index 54480ed..f59a0f5 100644 --- a/src/instance.zig +++ b/src/instance.zig @@ -140,7 +140,7 @@ pub const GlobalInstance = struct { }; pub const TableInstance = struct { - refs: std.ArrayList(Val), // should only be reftypes + refs: std.array_list.Managed(Val), // should only be reftypes reftype: ValType, limits: Limits, @@ -150,7 +150,7 @@ pub const TableInstance = struct { try verifyLimitsAreInstantiable(limits); var table = TableInstance{ - .refs = std.ArrayList(Val).init(allocator), + .refs = std.array_list.Managed(Val).init(allocator), .reftype = reftype, .limits = limits, }; @@ -361,7 +361,7 @@ pub const MemoryInstance = struct { }; pub const ElementInstance = struct { - refs: std.ArrayList(Val), + refs: std.array_list.Managed(Val), reftype: ValType, }; @@ -430,7 +430,7 @@ pub const FunctionImport = struct { switch (copy.data) { .Host => |*data| { var func_type_def = FunctionTypeDefinition{ - .types = std.ArrayList(ValType).init(allocator), + .types = std.array_list.Managed(ValType).init(allocator), .num_params = data.func_type_def.num_params, }; try func_type_def.types.appendSlice(data.func_type_def.types.items); @@ -525,10 +525,10 @@ pub const ModuleImportPackage = struct { name: []const u8, instance: ?*ModuleInstance, userdata: ?*anyopaque, - functions: std.ArrayList(FunctionImport), - tables: std.ArrayList(TableImport), - memories: std.ArrayList(MemoryImport), - globals: std.ArrayList(GlobalImport), + functions: std.array_list.Managed(FunctionImport), + tables: std.array_list.Managed(TableImport), + memories: std.array_list.Managed(MemoryImport), + globals: std.array_list.Managed(GlobalImport), allocator: std.mem.Allocator, pub fn init(name: []const u8, instance: ?*ModuleInstance, userdata: ?*anyopaque, allocator: std.mem.Allocator) std.mem.Allocator.Error!ModuleImportPackage { @@ -536,10 +536,10 @@ pub const ModuleImportPackage = struct { .name = try allocator.dupe(u8, name), .instance = instance, .userdata = userdata, - .functions = std.ArrayList(FunctionImport).init(allocator), - .tables = std.ArrayList(TableImport).init(allocator), - .memories = std.ArrayList(MemoryImport).init(allocator), - .globals = std.ArrayList(GlobalImport).init(allocator), + .functions = std.array_list.Managed(FunctionImport).init(allocator), + .tables = std.array_list.Managed(TableImport).init(allocator), + .memories = std.array_list.Managed(MemoryImport).init(allocator), + .globals = std.array_list.Managed(GlobalImport).init(allocator), .allocator = allocator, }; } @@ -547,7 +547,7 @@ pub const ModuleImportPackage = struct { pub fn addHostFunction(self: *ModuleImportPackage, name: []const u8, param_types: []const ValType, return_types: []const ValType, callback: HostFunctionCallback, userdata: ?*anyopaque) std.mem.Allocator.Error!void { std.debug.assert(self.instance == null); // cannot add host functions to an imports that is intended to be bound to a module instance - var type_list = std.ArrayList(ValType).init(self.allocator); + var type_list = std.array_list.Managed(ValType).init(self.allocator); try type_list.appendSlice(param_types); try type_list.appendSlice(return_types); @@ -596,30 +596,30 @@ pub const ModuleImportPackage = struct { }; pub const Store = struct { - tables: std.ArrayList(TableInstance), - memories: std.ArrayList(MemoryInstance), - globals: std.ArrayList(GlobalInstance), - elements: std.ArrayList(ElementInstance), + tables: std.array_list.Managed(TableInstance), + memories: std.array_list.Managed(MemoryInstance), + globals: std.array_list.Managed(GlobalInstance), + elements: std.array_list.Managed(ElementInstance), imports: struct { - functions: std.ArrayList(FunctionImport), - tables: std.ArrayList(TableImport), - memories: std.ArrayList(MemoryImport), - globals: std.ArrayList(GlobalImport), + functions: std.array_list.Managed(FunctionImport), + tables: std.array_list.Managed(TableImport), + memories: std.array_list.Managed(MemoryImport), + globals: std.array_list.Managed(GlobalImport), }, allocator: std.mem.Allocator, fn init(allocator: std.mem.Allocator) Store { const store = Store{ .imports = .{ - .functions = std.ArrayList(FunctionImport).init(allocator), - .tables = std.ArrayList(TableImport).init(allocator), - .memories = std.ArrayList(MemoryImport).init(allocator), - .globals = std.ArrayList(GlobalImport).init(allocator), + .functions = std.array_list.Managed(FunctionImport).init(allocator), + .tables = std.array_list.Managed(TableImport).init(allocator), + .memories = std.array_list.Managed(MemoryImport).init(allocator), + .globals = std.array_list.Managed(GlobalImport).init(allocator), }, - .tables = std.ArrayList(TableInstance).init(allocator), - .memories = std.ArrayList(MemoryInstance).init(allocator), - .globals = std.ArrayList(GlobalInstance).init(allocator), - .elements = std.ArrayList(ElementInstance).init(allocator), + .tables = std.array_list.Managed(TableInstance).init(allocator), + .memories = std.array_list.Managed(MemoryInstance).init(allocator), + .globals = std.array_list.Managed(GlobalInstance).init(allocator), + .elements = std.array_list.Managed(ElementInstance).init(allocator), .allocator = allocator, }; @@ -728,7 +728,7 @@ pub const VM = struct { const ResumeInvokeFn = *const fn (vm: *VM, module: *ModuleInstance, returns: []Val, opts: ResumeInvokeOpts) TrapError!void; const StepFn = *const fn (vm: *VM, module: *ModuleInstance, returns: []Val) TrapError!void; const SetDebugTrapFn = *const fn (vm: *VM, module: *ModuleInstance, wasm_address: u32, mode: DebugTrapInstructionMode) AllocError!bool; - const FormatBacktraceFn = *const fn (vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.ArrayList(u8); + const FormatBacktraceFn = *const fn (vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.array_list.Managed(u8); const FindFuncTypeDefFn = *const fn (vm: *VM, module: *ModuleInstance, func_index: usize) *const FunctionTypeDefinition; const ResolveFuncRefFn = *const fn (vm: *VM, ref: FuncRef) FuncRef; @@ -755,8 +755,8 @@ pub const VM = struct { var mem = try allocator.alloc(u8, total_alloc_size); - var vm: *VM = @as(*VM, @alignCast(@ptrCast(mem.ptr))); - const impl: *T = @as(*T, @alignCast(@ptrCast(mem[vm_alloc_size..].ptr))); + var vm: *VM = @as(*VM, @ptrCast(@alignCast(mem.ptr))); + const impl: *T = @as(*T, @ptrCast(@alignCast(mem[vm_alloc_size..].ptr))); vm.deinit_fn = T.deinit; vm.instantiate_fn = T.instantiate; @@ -804,7 +804,7 @@ pub const VM = struct { return try vm.set_debug_trap_fn(vm, module, wasm_address, mode); } - pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.ArrayList(u8) { + pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.array_list.Managed(u8) { return vm.format_backtrace_fn(vm, indent, allocator); } @@ -1096,7 +1096,7 @@ pub const ModuleInstance = struct { try store.elements.ensureTotalCapacity(module_def.elements.items.len); for (module_def.elements.items) |*def_elem| { var elem = ElementInstance{ - .refs = std.ArrayList(Val).init(allocator), + .refs = std.array_list.Managed(Val).init(allocator), .reftype = def_elem.reftype, }; @@ -1324,7 +1324,7 @@ pub const ModuleInstance = struct { } /// Caller owns returned memory and must free via allocator.free() - pub fn formatBacktrace(self: *ModuleInstance, indent: u8, allocator: std.mem.Allocator) anyerror!std.ArrayList(u8) { + pub fn formatBacktrace(self: *ModuleInstance, indent: u8, allocator: std.mem.Allocator) anyerror!std.array_list.Managed(u8) { return self.vm.format_backtrace_fn(self.vm, indent, allocator); } diff --git a/src/vm_register.zig b/src/vm_register.zig index 7d9fd48..21441c9 100644 --- a/src/vm_register.zig +++ b/src/vm_register.zig @@ -224,7 +224,7 @@ const IRNode = struct { } // a node that has no out edges to instructions with side effects or control flow - fn isIsland(node: *IRNode, unvisited: *std.ArrayList(*IRNode)) AllocError!bool { + fn isIsland(node: *IRNode, unvisited: *std.array_list.Managed(*IRNode)) AllocError!bool { if (node.opcode == .Return) { return false; } @@ -257,12 +257,12 @@ const RegisterSlots = struct { prev: ?u32, }; - slots: std.ArrayList(Slot), + slots: std.array_list.Managed(Slot), last_free: ?u32, fn init(allocator: std.mem.Allocator) RegisterSlots { return RegisterSlots{ - .slots = std.ArrayList(Slot).init(allocator), + .slots = std.array_list.Managed(Slot).init(allocator), .last_free = null, }; } @@ -333,7 +333,7 @@ const IRFunction = struct { var slots = RegisterSlots.init(allocator); defer slots.deinit(); - var visit_queue = std.ArrayList(*IRNode).init(allocator); + var visit_queue = std.array_list.Managed(*IRNode).init(allocator); defer visit_queue.deinit(); try visit_queue.append(func.ir_root); @@ -368,7 +368,7 @@ const IRFunction = struct { } } - fn codegen(func: *IRFunction, instructions: *std.ArrayList(RegInstruction), module_def: ModuleDefinition, allocator: std.mem.Allocator) AllocError!void { + fn codegen(func: *IRFunction, instructions: *std.array_list.Managed(RegInstruction), module_def: ModuleDefinition, allocator: std.mem.Allocator) AllocError!void { // walk the graph in breadth-first order // when a node is visited, emit its instruction @@ -376,7 +376,7 @@ const IRFunction = struct { const start_instruction_offset = instructions.items.len; - var visit_queue = std.ArrayList(*IRNode).init(allocator); + var visit_queue = std.array_list.Managed(*IRNode).init(allocator); defer visit_queue.deinit(); try visit_queue.append(func.ir_root); @@ -420,14 +420,14 @@ const IRFunction = struct { } fn dumpVizGraph(func: IRFunction, path: []u8, module_def: ModuleDefinition, allocator: std.mem.Allocator) !void { - var graph_txt = std.ArrayList(u8).init(allocator); + var graph_txt = std.array_list.Managed(u8).init(allocator); defer graph_txt.deinit(); try graph_txt.ensureTotalCapacity(1024 * 16); var writer = graph_txt.writer(); _ = try writer.write("digraph {\n"); - var nodes = std.ArrayList(*const IRNode).init(allocator); + var nodes = std.array_list.Managed(*const IRNode).init(allocator); defer nodes.deinit(); try nodes.ensureTotalCapacity(1024); nodes.appendAssumeCapacity(func.ir_root); @@ -494,9 +494,9 @@ const ModuleIR = struct { phi_nodes: []*IRNode, }; - nodes: std.ArrayList(*IRNode), - blocks: std.ArrayList(Block), - phi_nodes: std.ArrayList(*IRNode), + nodes: std.array_list.Managed(*IRNode), + blocks: std.array_list.Managed(Block), + phi_nodes: std.array_list.Managed(*IRNode), // const ContinuationType = enum { // .Normal, @@ -505,9 +505,9 @@ const ModuleIR = struct { fn init(allocator: std.mem.Allocator) BlockStack { return BlockStack{ - .nodes = std.ArrayList(*IRNode).init(allocator), - .blocks = std.ArrayList(Block).init(allocator), - .phi_nodes = std.ArrayList(*IRNode).init(allocator), + .nodes = std.array_list.Managed(*IRNode).init(allocator), + .blocks = std.array_list.Managed(Block).init(allocator), + .phi_nodes = std.array_list.Managed(*IRNode).init(allocator), }; } @@ -571,19 +571,19 @@ const ModuleIR = struct { allocator: std.mem.Allocator, - // all_nodes: std.ArrayList(*IRNode), + // all_nodes: std.array_list.Managed(*IRNode), blocks: BlockStack, // This stack is a record of the nodes to push values onto the stack. If an instruction would push // multiple values onto the stack, it would be in this list as many times as values it pushed. Note // that we don't have to do any type checking here because the module has already been validated. - value_stack: std.ArrayList(*IRNode), + value_stack: std.array_list.Managed(*IRNode), // records the current block continuation - // label_continuations: std.ArrayList(u32), + // label_continuations: std.array_list.Managed(u32), - pending_continuation_edges: std.ArrayList(PendingContinuationEdge), + pending_continuation_edges: std.array_list.Managed(PendingContinuationEdge), // when hitting an unconditional control transfer, we need to mark the rest of the stack values as unreachable just like in validation is_unreachable: bool, @@ -592,27 +592,27 @@ const ModuleIR = struct { // we need a way to represent what's in the locals slot as an SSA node. This array lets us do that. We also // reuse the Local_Get instructions to indicate the "initial value" of the slot. Since our IRNode only stores // indices to instructions, we'll just lazily set these when they're fetched for the first time. - locals: std.ArrayList(?*IRNode), + locals: std.array_list.Managed(?*IRNode), // Lets us collapse multiple const IR nodes with the same type/value into a single one unique_constants: UniqueValueToIRNodeMap, - scratch_node_list_1: std.ArrayList(*IRNode), - scratch_node_list_2: std.ArrayList(*IRNode), + scratch_node_list_1: std.array_list.Managed(*IRNode), + scratch_node_list_2: std.array_list.Managed(*IRNode), fn init(allocator: std.mem.Allocator) IntermediateCompileData { return IntermediateCompileData{ .allocator = allocator, - // .all_nodes = std.ArrayList(*IRNode).init(allocator), + // .all_nodes = std.array_list.Managed(*IRNode).init(allocator), .blocks = BlockStack.init(allocator), - .value_stack = std.ArrayList(*IRNode).init(allocator), - // .label_continuations = std.ArrayList(u32).init(allocator), - .pending_continuation_edges = std.ArrayList(PendingContinuationEdge).init(allocator), + .value_stack = std.array_list.Managed(*IRNode).init(allocator), + // .label_continuations = std.array_list.Managed(u32).init(allocator), + .pending_continuation_edges = std.array_list.Managed(PendingContinuationEdge).init(allocator), .is_unreachable = false, - .locals = std.ArrayList(?*IRNode).init(allocator), + .locals = std.array_list.Managed(?*IRNode).init(allocator), .unique_constants = UniqueValueToIRNodeMap.init(allocator), - .scratch_node_list_1 = std.ArrayList(*IRNode).init(allocator), - .scratch_node_list_2 = std.ArrayList(*IRNode).init(allocator), + .scratch_node_list_1 = std.array_list.Managed(*IRNode).init(allocator), + .scratch_node_list_2 = std.array_list.Managed(*IRNode).init(allocator), }; } @@ -713,16 +713,16 @@ const ModuleIR = struct { allocator: std.mem.Allocator, module_def: *const ModuleDefinition, - functions: std.ArrayList(IRFunction), + functions: std.array_list.Managed(IRFunction), ir: StableArray(IRNode), - // instructions: std.ArrayList(RegInstruction), + // instructions: std.array_list.Managed(RegInstruction), fn init(allocator: std.mem.Allocator, module_def: *const ModuleDefinition) ModuleIR { return ModuleIR{ .allocator = allocator, .module_def = module_def, - .functions = std.ArrayList(IRFunction).init(allocator), + .functions = std.array_list.Managed(IRFunction).init(allocator), .ir = StableArray(IRNode).init(1024 * 1024 * 8), }; } @@ -826,7 +826,7 @@ const ModuleIR = struct { try compile_data.blocks.pushBlock(instruction.immediate.Block.continuation); // TODO record the kind of block so we know this is a loop? }, .If => { - var phi_nodes: *std.ArrayList(*IRNode) = &compile_data.scratch_node_list_1; + var phi_nodes: *std.array_list.Managed(*IRNode) = &compile_data.scratch_node_list_1; defer compile_data.scratch_node_list_1.clearRetainingCapacity(); std.debug.assert(phi_nodes.items.len == 0); @@ -873,7 +873,7 @@ const ModuleIR = struct { // At the end of every block, we ensure all nodes with side effects are still in the graph. Order matters // since mutations to the Store or control flow changes must happen in the order of the original instructions. { - var nodes_with_side_effects: *std.ArrayList(*IRNode) = &compile_data.scratch_node_list_1; + var nodes_with_side_effects: *std.array_list.Managed(*IRNode) = &compile_data.scratch_node_list_1; defer nodes_with_side_effects.clearRetainingCapacity(); const current_block_nodes: []*IRNode = compile_data.blocks.currentBlockNodes(); @@ -916,7 +916,7 @@ const ModuleIR = struct { try compile_data.popPushValueStackNodes(node.?, 1, 0); - // var continuation_edges: std.ArrayList(*IRNode).init(allocator); + // var continuation_edges: std.array_list.Managed(*IRNode).init(allocator); // defer continuation_edges.deinit(); const immediates: *const BranchTableImmediates = &mir.module_def.code.branch_table.items[instruction.immediate.Index]; @@ -1160,7 +1160,7 @@ pub const RegisterVM = struct { unreachable; } - pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.ArrayList(u8) { + pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.array_list.Managed(u8) { _ = vm; _ = indent; _ = allocator; diff --git a/src/vm_stack.zig b/src/vm_stack.zig index ee4d011..5c8f343 100644 --- a/src/vm_stack.zig +++ b/src/vm_stack.zig @@ -3293,7 +3293,7 @@ pub const StackVM = struct { }; const DebugState = struct { - trapped_opcodes: std.ArrayList(TrappedOpcode), + trapped_opcodes: std.array_list.Managed(TrappedOpcode), pc: u32 = 0, trap_counter: u32 = 0, // used for trapping on step is_invoking: bool = false, @@ -3317,22 +3317,22 @@ pub const StackVM = struct { } else void; stack: Stack, - instructions: std.ArrayList(Instruction), - functions: std.ArrayList(FunctionInstance), - host_function_import_data: std.ArrayList(HostFunctionData), + instructions: std.array_list.Managed(Instruction), + functions: std.array_list.Managed(FunctionInstance), + host_function_import_data: std.array_list.Managed(HostFunctionData), debug_state: ?DebugState, meter_state: MeterState, pub fn fromVM(vm: *VM) *StackVM { - return @as(*StackVM, @alignCast(@ptrCast(vm.impl))); + return @as(*StackVM, @ptrCast(@alignCast(vm.impl))); } pub fn init(vm: *VM) void { var self: *StackVM = fromVM(vm); self.stack = Stack.init(vm.allocator); - self.instructions = std.ArrayList(Instruction).init(vm.allocator); - self.functions = std.ArrayList(FunctionInstance).init(vm.allocator); - self.host_function_import_data = std.ArrayList(HostFunctionData).init(vm.allocator); + self.instructions = std.array_list.Managed(Instruction).init(vm.allocator); + self.functions = std.array_list.Managed(FunctionInstance).init(vm.allocator); + self.host_function_import_data = std.array_list.Managed(HostFunctionData).init(vm.allocator); self.debug_state = null; } @@ -3354,7 +3354,7 @@ pub const StackVM = struct { if (opts.enable_debug) { self.debug_state = DebugState{ .pc = 0, - .trapped_opcodes = std.ArrayList(TrappedOpcode).init(vm.allocator), + .trapped_opcodes = std.array_list.Managed(TrappedOpcode).init(vm.allocator), }; } @@ -3370,7 +3370,7 @@ pub const StackVM = struct { // vm keeps a copy of the instructions to mutate some of them try self.instructions.appendSlice(module.module_def.code.instructions.items); - var locals_remap: std.ArrayList(u32) = .init(vm.allocator); + var locals_remap: std.array_list.Managed(u32) = .init(vm.allocator); defer locals_remap.deinit(); try locals_remap.ensureTotalCapacity(1024); @@ -3679,10 +3679,10 @@ pub const StackVM = struct { return false; } - pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.ArrayList(u8) { + pub fn formatBacktrace(vm: *VM, indent: u8, allocator: std.mem.Allocator) anyerror!std.array_list.Managed(u8) { var self: *StackVM = fromVM(vm); - var buffer = std.ArrayList(u8).init(allocator); + var buffer = std.array_list.Managed(u8).init(allocator); try buffer.ensureTotalCapacity(512); var writer = buffer.writer(); diff --git a/src/wasi.zig b/src/wasi.zig index 20a0d6d..ec0c4fb 100644 --- a/src/wasi.zig +++ b/src/wasi.zig @@ -16,7 +16,7 @@ const WasiContext = struct { rights: WasiRights, is_preopen: bool, open_handles: u32 = 1, - dir_entries: std.ArrayList(WasiDirEntry), + dir_entries: std.array_list.Managed(WasiDirEntry), }; cwd: []const u8, @@ -26,8 +26,8 @@ const WasiContext = struct { // having a master table with a side table of wasi file descriptors lets us map multiple wasi fds into the same // master entry and avoid duplicating OS handles, which has proved buggy on win32 - fd_table: std.ArrayList(FdInfo), - fd_table_freelist: std.ArrayList(u32), + fd_table: std.array_list.Managed(FdInfo), + fd_table_freelist: std.array_list.Managed(u32), fd_wasi_table: std.AutoHashMap(u32, u32), // fd_wasi -> fd_table index fd_path_lookup: std.StringHashMap(u32), // path_absolute -> fd_table index @@ -38,8 +38,8 @@ const WasiContext = struct { fn init(opts: *const WasiOpts, allocator: std.mem.Allocator) !WasiContext { var context = WasiContext{ .cwd = "", - .fd_table = std.ArrayList(FdInfo).init(allocator), - .fd_table_freelist = std.ArrayList(u32).init(allocator), + .fd_table = std.array_list.Managed(FdInfo).init(allocator), + .fd_table_freelist = std.array_list.Managed(u32).init(allocator), .fd_wasi_table = std.AutoHashMap(u32, u32).init(allocator), .fd_path_lookup = std.StringHashMap(u32).init(allocator), .strings = StringPool.init(1024 * 1024 * 4, allocator), // 4MB for absolute paths @@ -77,7 +77,7 @@ const WasiContext = struct { const path_stdout = try context.strings.put("stdout"); const path_stderr = try context.strings.put("stderr"); - const empty_dir_entries = std.ArrayList(WasiDirEntry).init(allocator); + const empty_dir_entries = std.array_list.Managed(WasiDirEntry).init(allocator); try context.fd_table.ensureTotalCapacity(3 + context.dirs.len); context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.io.getStdIn().handle, .path_absolute = path_stdin, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); @@ -243,7 +243,7 @@ const WasiContext = struct { info.rights = rights; info.is_preopen = is_preopen; info.open_handles = 1; - info.dir_entries = std.ArrayList(WasiDirEntry).init(self.allocator); + info.dir_entries = std.array_list.Managed(WasiDirEntry).init(self.allocator); self.fd_wasi_table.put(fd_wasi, fd_table_index) catch |err| { errno.* = Errno.translateError(err); diff --git a/test/wasm/main.zig b/test/wasm/main.zig index 81738bf..83a975b 100644 --- a/test/wasm/main.zig +++ b/test/wasm/main.zig @@ -47,7 +47,7 @@ const Action = struct { type: ActionType, module: []const u8, field: []const u8, - args: std.ArrayList(LaneTypedVal), + args: std.array_list.Managed(LaneTypedVal), }; const BadModuleError = struct { @@ -68,7 +68,7 @@ const CommandRegister = struct { const CommandAssertReturn = struct { action: Action, - expected_returns: ?std.ArrayList(LaneTypedVal), + expected_returns: ?std.array_list.Managed(LaneTypedVal), }; const CommandAssertTrap = struct { @@ -335,8 +335,8 @@ const LaneTypedVal = struct { v: TaggedVal, lane_type: V128LaneType, // only valid when v contains a V128 - fn toValArrayList(typed: []const LaneTypedVal, allocator: std.mem.Allocator) !std.ArrayList(Val) { - var vals = std.ArrayList(Val).init(allocator); + fn toValArrayList(typed: []const LaneTypedVal, allocator: std.mem.Allocator) !std.array_list.Managed(Val) { + var vals = std.array_list.Managed(Val).init(allocator); try vals.ensureTotalCapacityPrecise(typed.len); for (typed) |v| { try vals.append(v.v.val); @@ -444,7 +444,7 @@ fn isSameError(err: anyerror, err_string: []const u8) bool { }; } -fn parseCommands(json_path: []const u8, allocator: std.mem.Allocator) !std.ArrayList(Command) { +fn parseCommands(json_path: []const u8, allocator: std.mem.Allocator) !std.array_list.Managed(Command) { const Helpers = struct { fn parseAction(json_action: *std.json.Value, fallback_module: []const u8, _allocator: std.mem.Allocator) !Action { const json_type = json_action.object.getPtr("type").?; @@ -460,7 +460,7 @@ fn parseCommands(json_path: []const u8, allocator: std.mem.Allocator) !std.Array const json_field = json_action.object.getPtr("field").?; const json_args_or_null = json_action.object.getPtr("args"); - var args = std.ArrayList(LaneTypedVal).init(_allocator); + var args = std.array_list.Managed(LaneTypedVal).init(_allocator); if (json_args_or_null) |json_args| { for (json_args.array.items) |item| { const val: LaneTypedVal = try parseLaneTypedVal(item.object); @@ -500,7 +500,7 @@ fn parseCommands(json_path: []const u8, allocator: std.mem.Allocator) !std.Array var fallback_module: []const u8 = ""; defer allocator.free(fallback_module); - var commands = std.ArrayList(Command).init(allocator); + var commands = std.array_list.Managed(Command).init(allocator); const json_commands = parsed.value.object.getPtr("commands").?; for (json_commands.array.items) |json_command| { @@ -546,10 +546,10 @@ fn parseCommands(json_path: []const u8, allocator: std.mem.Allocator) !std.Array const action = try Helpers.parseAction(json_action, fallback_module, allocator); - var expected_returns_or_null: ?std.ArrayList(LaneTypedVal) = null; + var expected_returns_or_null: ?std.array_list.Managed(LaneTypedVal) = null; const json_expected_or_null = json_command.object.getPtr("expected"); if (json_expected_or_null) |json_expected| { - var expected_returns = std.ArrayList(LaneTypedVal).init(allocator); + var expected_returns = std.array_list.Managed(LaneTypedVal).init(allocator); for (json_expected.array.items) |item| { try expected_returns.append(try parseLaneTypedVal(item.object)); } @@ -751,7 +751,7 @@ fn makeSpectestImports(allocator: std.mem.Allocator) !bytebox.ModuleImportPackag fn run(allocator: std.mem.Allocator, suite_path: []const u8, opts: *const TestOpts) !bool { var did_fail_any_test: bool = false; - var commands: std.ArrayList(Command) = try parseCommands(suite_path, allocator); + var commands: std.array_list.Managed(Command) = try parseCommands(suite_path, allocator); defer { for (commands.items) |*command| { command.deinit(allocator); @@ -783,7 +783,7 @@ fn run(allocator: std.mem.Allocator, suite_path: []const u8, opts: *const TestOp name_to_module.ensureTotalCapacity(256) catch unreachable; // NOTE this shares the same copies of the import arrays, since the modules must share instances - var imports = std.ArrayList(bytebox.ModuleImportPackage).init(allocator); + var imports = std.array_list.Managed(bytebox.ModuleImportPackage).init(allocator); defer { const spectest_imports = imports.items[0]; for (spectest_imports.tables.items) |*item| { @@ -1027,7 +1027,7 @@ fn run(allocator: std.mem.Allocator, suite_path: []const u8, opts: *const TestOp } const num_expected_returns = if (c.expected_returns) |returns| returns.items.len else 0; - var returns_placeholder = std.ArrayList(bytebox.Val).init(allocator); + var returns_placeholder = std.array_list.Managed(bytebox.Val).init(allocator); defer returns_placeholder.deinit(); try returns_placeholder.resize(num_expected_returns); From 99a801ada806ef5fc86d23e6c199bafdb209b134 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 16:01:16 -0500 Subject: [PATCH 03/11] Switch from std.io to std.fs.File for stdin/stdout/stderr --- src/wasi.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wasi.zig b/src/wasi.zig index ec0c4fb..a863410 100644 --- a/src/wasi.zig +++ b/src/wasi.zig @@ -80,9 +80,9 @@ const WasiContext = struct { const empty_dir_entries = std.array_list.Managed(WasiDirEntry).init(allocator); try context.fd_table.ensureTotalCapacity(3 + context.dirs.len); - context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.io.getStdIn().handle, .path_absolute = path_stdin, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); - context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.io.getStdOut().handle, .path_absolute = path_stdout, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); - context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.io.getStdErr().handle, .path_absolute = path_stderr, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); + context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.fs.File.stdin().handle, .path_absolute = path_stdin, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); + context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.fs.File.stdout().handle, .path_absolute = path_stdout, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); + context.fd_table.appendAssumeCapacity(FdInfo{ .fd = std.fs.File.stderr().handle, .path_absolute = path_stderr, .rights = .{}, .is_preopen = true, .dir_entries = empty_dir_entries }); try context.fd_wasi_table.put(0, 0); try context.fd_wasi_table.put(1, 1); try context.fd_wasi_table.put(2, 2); From 7fd2962d244533504a7308138b67fb75472bb0af Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 16:41:08 -0500 Subject: [PATCH 04/11] Fix lossy conversion from int to float errors https://ziglang.org/download/0.15.1/release-notes.html#Error-on-Lossy-Coercion-from-Int-to-Float --- src/stack_ops.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stack_ops.zig b/src/stack_ops.zig index 1330915..e5cdef8 100644 --- a/src/stack_ops.zig +++ b/src/stack_ops.zig @@ -2914,7 +2914,7 @@ fn truncateTo(comptime T: type, value: anytype) TrapError!T { return error.TrapIntegerOverflow; } } else { - if (truncated >= std.math.maxInt(T)) { + if (truncated >= @as(@TypeOf(truncated), @floatFromInt(std.math.maxInt(T)))) { return error.TrapIntegerOverflow; } } @@ -2948,7 +2948,7 @@ fn saturatedTruncateTo(comptime T: type, value: anytype) T { return std.math.maxInt(T); } } else { - if (truncated >= std.math.maxInt(T)) { + if (truncated >= @as(@TypeOf(truncated), @floatFromInt(std.math.maxInt(T)))) { return std.math.maxInt(T); } } From b1d11ecc7c7c2d4661779fd8cd7e996778a93b15 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 19:48:04 -0500 Subject: [PATCH 05/11] Use new writer interface for logging --- src/common.zig | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/common.zig b/src/common.zig index cc33a5b..7f46d37 100644 --- a/src/common.zig +++ b/src/common.zig @@ -28,13 +28,21 @@ pub const Logger = struct { fn defaultLog(level: LogLevel, text: [:0]const u8) void { var fd = switch (level) { - .Info => std.io.getStdOut(), - .Error => std.io.getStdErr(), + .Info => std.fs.File.stdout(), + .Error => std.fs.File.stderr(), }; - var writer = fd.writer(); - nosuspend writer.writeAll(text) catch |e| { + + var buffer: [1024]u8 = undefined; + var writer = fd.writer(&buffer); + const w: *std.io.Writer = &writer.interface; + + nosuspend w.writeAll(text) catch |e| { std.debug.print("Failed logging due to error: {}\n", .{e}); }; + + nosuspend w.flush() catch |e| { + std.debug.print("Failed flushing log due to error: {}\n", .{e}); + }; } pub fn info(self: Logger, comptime format: []const u8, args: anytype) void { From b4023162d25714603533f1a3db483f36be14421a Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 21:43:20 -0500 Subject: [PATCH 06/11] Use new names for leb functions --- src/definition.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/definition.zig b/src/definition.zig index 4d1e856..a0ac6e2 100644 --- a/src/definition.zig +++ b/src/definition.zig @@ -120,7 +120,7 @@ fn readBytes(reader: anytype, bytes: []u8) MalformedError!usize { fn decodeLEB128(comptime T: type, reader: anytype) MalformedError!T { if (@typeInfo(T).int.signedness == .signed) { - return std.leb.readILEB128(T, reader) catch |e| { + return std.leb.readIleb128(T, reader) catch |e| { if (e == error.Overflow) { return error.MalformedLEB128; } else { @@ -128,7 +128,7 @@ fn decodeLEB128(comptime T: type, reader: anytype) MalformedError!T { } }; } else { - return std.leb.readULEB128(T, reader) catch |e| { + return std.leb.readUleb128(T, reader) catch |e| { if (e == error.Overflow) { return error.MalformedLEB128; } else { From 05616dd74e579548239b3caa416b4c1e0f50d8c5 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 19:57:20 -0500 Subject: [PATCH 07/11] Rename C calling conv variant --- src/cffi.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cffi.zig b/src/cffi.zig index 015f810..5840e48 100644 --- a/src/cffi.zig +++ b/src/cffi.zig @@ -44,7 +44,7 @@ const CModuleDefinitionInitOpts = extern struct { debug_name: ?[*:0]u8, }; -const CHostFunction = *const fn (userdata: ?*anyopaque, module: *ModuleInstance, params: [*]const Val, returns: [*]Val) callconv(.C) void; +const CHostFunction = *const fn (userdata: ?*anyopaque, module: *ModuleInstance, params: [*]const Val, returns: [*]Val) callconv(.c) void; const CWasmMemoryConfig = extern struct { resize: ?core.WasmMemoryResizeFunction, @@ -258,7 +258,7 @@ const HostFunc = extern struct { }; fn trampoline(userdata: ?*anyopaque, module: *core.ModuleInstance, params: [*]const Val, returns: [*]Val) error{}!void { - const host: *HostFunc = @alignCast(@ptrCast(userdata)); + const host: *HostFunc = @ptrCast(@alignCast(userdata)); @call(.auto, host.callback, .{ host.userdata, module, params, returns }); } From 07e2ce6587f3379852e7c73d322eee70a5c2fb31 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 20:33:19 -0500 Subject: [PATCH 08/11] Update zig version in CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 137ef4a..4b271e7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - name: Setup Zig uses: mlugg/setup-zig@v1 with: - version: 0.14.0 + version: 0.15.1 - name: Setup Python uses: actions/setup-python@v4 From b949402de742bbf0bfd43097a8887346f1b92c13 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Sat, 23 Aug 2025 21:44:17 -0500 Subject: [PATCH 09/11] Temporarily update zig-stable-array --- build.zig | 2 +- build.zig.zon | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 8129bb2..5848a9f 100644 --- a/build.zig +++ b/build.zig @@ -53,7 +53,7 @@ pub fn build(b: *Build) void { options.addOption(bool, "enable_debug_trap", enable_debug_trap); options.addOption(StackVmKind, "vm_kind", vm_kind); - const stable_array = b.dependency("stable_array", .{ + const stable_array = b.dependency("zig-stable-array", .{ .target = target, .optimize = optimize, }); diff --git a/build.zig.zon b/build.zig.zon index 3c29ed6..d146c60 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,9 +3,9 @@ .version = "0.0.1", .fingerprint = 0x5a2a0eadb1367749, .dependencies = .{ - .stable_array = .{ - .url = "https://github.com/rdunnington/zig-stable-array/archive/f8940119afd6864702f543befa76d558a1a23ec5.tar.gz", - .hash = "stable_array-0.1.0-3ihgvUVbAADlJAqXJOXZ2xEIHUY5-32i8d6fk7hraKju", + .@"zig-stable-array" = .{ + .url = "git+https://github.com/isaacvando/zig-stable-array#3b8b675b32d31d371dbefed9496312c8f3bba98d", + .hash = "stable_array-0.1.0-3ihgvVxbAACET5MoiUn2T5ENunG_da_X3kGbji-f4QTF", }, }, .minimum_zig_version = "0.13.0", From c7532773ae02131e947ee56783b2e7f393c8f6e9 Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Mon, 25 Aug 2025 07:55:49 -0500 Subject: [PATCH 10/11] update zig version in CI --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b271e7..e2b39b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: - name: Setup Zig uses: mlugg/setup-zig@v1 with: - version: 0.15.1 + version: 0.15.0 - name: Setup Python uses: actions/setup-python@v4 From c8c452c4c8b4e5050ffa658993c44581878c576c Mon Sep 17 00:00:00 2001 From: Isaac Van Doren Date: Mon, 25 Aug 2025 09:43:28 -0500 Subject: [PATCH 11/11] Update zig stable array --- build.zig.zon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.zig.zon b/build.zig.zon index d146c60..09a1bd9 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,7 +4,7 @@ .fingerprint = 0x5a2a0eadb1367749, .dependencies = .{ .@"zig-stable-array" = .{ - .url = "git+https://github.com/isaacvando/zig-stable-array#3b8b675b32d31d371dbefed9496312c8f3bba98d", + .url = "git+https://github.com/rdunnington/zig-stable-array#9e4f089bd3abf127eafd307ecf9796455871becc", .hash = "stable_array-0.1.0-3ihgvVxbAACET5MoiUn2T5ENunG_da_X3kGbji-f4QTF", }, },