Skip to content
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v1
with:
version: 0.14.0
version: 0.15.0

- name: Setup Python
uses: actions/setup-python@v4
Expand Down
2 changes: 1 addition & 1 deletion bench/samples/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
44 changes: 26 additions & 18 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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"),
});
Expand All @@ -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| {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -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/rdunnington/zig-stable-array#9e4f089bd3abf127eafd307ecf9796455871becc",
.hash = "stable_array-0.1.0-3ihgvVxbAACET5MoiUn2T5ENunG_da_X3kGbji-f4QTF",
},
},
.minimum_zig_version = "0.13.0",
Expand Down
18 changes: 9 additions & 9 deletions run/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -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}", .{
Expand All @@ -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| {
Expand Down Expand Up @@ -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| {
Expand All @@ -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();

Expand All @@ -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", .{});
Expand Down
6 changes: 3 additions & 3 deletions src/cffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done by zig fmt


@call(.auto, host.callback, .{ host.userdata, module, params, returns });
}
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading
Loading