Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
submodules: 'true'

- name: Setup Zig
uses: mlugg/setup-zig@v1
uses: mlugg/setup-zig@v2
with:
version: 0.14.0
version: 0.15.1

- name: Setup Python
uses: actions/setup-python@v4
Expand Down
7 changes: 4 additions & 3 deletions bench/samples/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ 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_writer: std.Io.Writer.Allocating = .init(allocator);
defer json_writer.deinit();
{
const products = allocator.alloc(AllTables.Product, n) catch @panic("OOM");
for (products) |*entry| {
Expand Down Expand Up @@ -152,11 +153,11 @@ fn generate(n: usize, allocator: std.mem.Allocator) AllTables {
.stores = stores,
};

std.json.stringify(tables, .{}, json.writer()) catch @panic("can't serialize json");
std.json.Stringify.value(tables, .{}, &json_writer.writer) catch @panic("can't serialize json");
}

// parse the json text into a native type
const parsed = std.json.parseFromSlice(AllTables, allocator, json.items, .{}) catch @panic("bad json");
const parsed = std.json.parseFromSlice(AllTables, allocator, json_writer.written(), .{}) catch @panic("bad json");
return parsed.value;
}

Expand Down
68 changes: 44 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const WasmBuild = struct {
install: *Step,
};

// At the time of this writing, zig's stage2 codegen backend can't handle tailcalls, so we'll default to using LLVM for simplicity
const use_llvm = true;

pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
Expand All @@ -53,7 +56,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 +106,15 @@ 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,
}),
.use_llvm = use_llvm,
});
lib_bytebox.root_module.addImport(stable_array_import.name, stable_array_import.module);
lib_bytebox.root_module.addOptions("config", options);
Expand All @@ -116,9 +123,12 @@ 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,
}),
.use_llvm = use_llvm,
});
unit_tests.root_module.addImport(stable_array_import.name, stable_array_import.module);
unit_tests.root_module.addOptions("config", options);
Expand Down Expand Up @@ -160,23 +170,27 @@ pub fn build(b: *Build) void {
});

// Cffi test
const cffi_test_step = b.step("test-cffi", "Run cffi test");
const cffi_build = b.addExecutable(.{
const cffi_test = b.addExecutable(.{
.name = "test-cffi",
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
.use_llvm = use_llvm,
});
cffi_build.addCSourceFile(.{
cffi_test.addCSourceFile(.{
.file = b.path("test/cffi/main.c"),
});
cffi_build.addIncludePath(b.path("src/bytebox.h"));
cffi_build.linkLibC();
cffi_build.linkLibrary(lib_bytebox);
cffi_test.addIncludePath(b.path("src/bytebox.h"));
cffi_test.linkLibC();
cffi_test.linkLibrary(lib_bytebox);

const ffi_guest: WasmBuild = buildWasmExe(b, "test/cffi/module.zig", .wasm32);

const cffi_run_step = b.addRunArtifact(cffi_build);
const cffi_run_step = b.addRunArtifact(cffi_test);
cffi_run_step.addFileArg(ffi_guest.compile.getEmittedBin());

const cffi_test_step = b.step("test-cffi", "Run cffi test");
cffi_test_step.dependOn(&cffi_run_step.step);

// All tests
Expand All @@ -188,12 +202,15 @@ 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,
}),
.use_llvm = use_llvm,
});

for (imports) |import| {
Expand Down Expand Up @@ -251,9 +268,12 @@ 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,
}),
.use_llvm = use_llvm,
});
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
18 changes: 9 additions & 9 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));

@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 Expand Up @@ -629,7 +629,7 @@ comptime {
}

// Zig's own stack-probe routine (available only on x86 and x86_64)
fn zig_probe_stack() callconv(.Naked) void {
fn zig_probe_stack() callconv(.naked) void {
@setRuntimeSafety(false);

// Versions of the Linux kernel before 5.1 treat any access below SP as
Expand Down Expand Up @@ -823,11 +823,11 @@ fn win_probe_stack_adjust_sp() void {
// ___chkstk (__alloca) | yes | yes |
// ___chkstk_ms | no | no |

fn _chkstk() callconv(.Naked) void {
fn _chkstk() callconv(.naked) void {
@setRuntimeSafety(false);
@call(.always_inline, win_probe_stack_adjust_sp, .{});
}
fn __chkstk() callconv(.Naked) void {
fn __chkstk() callconv(.naked) void {
@setRuntimeSafety(false);
if (comptime builtin.cpu.arch.isAARCH64()) {
@call(.always_inline, win_probe_stack_only, .{});
Expand All @@ -837,15 +837,15 @@ fn __chkstk() callconv(.Naked) void {
else => unreachable,
}
}
fn ___chkstk() callconv(.Naked) void {
fn ___chkstk() callconv(.naked) void {
@setRuntimeSafety(false);
@call(.always_inline, win_probe_stack_adjust_sp, .{});
}
fn __chkstk_ms() callconv(.Naked) void {
fn __chkstk_ms() callconv(.naked) void {
@setRuntimeSafety(false);
@call(.always_inline, win_probe_stack_only, .{});
}
fn ___chkstk_ms() callconv(.Naked) void {
fn ___chkstk_ms() callconv(.naked) void {
@setRuntimeSafety(false);
@call(.always_inline, win_probe_stack_only, .{});
}
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