Skip to content
Draft
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
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@v2
with:
version: 0.15.1
version: 0.16.0

- name: Setup Python
uses: actions/setup-python@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.zig-cache
zig-out
zig-pkg
test/wasm/wasm-generated
*.wasm
*.wasm.o
Expand Down
12 changes: 5 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const std = @import("std");
const CrossTarget = std.zig.CrossTarget;

const Build = std.Build;
const Module = Build.Module;
const Import = Module.Import;
Expand Down Expand Up @@ -63,7 +61,7 @@ pub fn build(b: *Build) void {
options.addOption(bool, "enable_wasi", enable_wasi);
options.addOption(StackVmKind, "vm_kind", vm_kind);

const stable_array = b.dependency("zig-stable-array", .{
const stable_array = b.dependency("stable_array", .{
.target = target,
.optimize = optimize,
});
Expand Down Expand Up @@ -185,15 +183,15 @@ pub fn build(b: *Build) void {
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
.use_llvm = use_llvm,
});
cffi_test.addCSourceFile(.{
cffi_test.root_module.addCSourceFile(.{
.file = b.path("test/cffi/main.c"),
});
cffi_test.addIncludePath(b.path("src/bytebox.h"));
cffi_test.linkLibC();
cffi_test.linkLibrary(lib_bytebox);
cffi_test.root_module.addIncludePath(b.path("src/bytebox.h"));
cffi_test.root_module.linkLibrary(lib_bytebox);

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

Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
.version = "0.0.1",
.fingerprint = 0x5a2a0eadb1367749,
.dependencies = .{
.@"zig-stable-array" = .{
.url = "git+https://github.com/rdunnington/zig-stable-array#9e4f089bd3abf127eafd307ecf9796455871becc",
.hash = "stable_array-0.1.0-3ihgvVxbAACET5MoiUn2T5ENunG_da_X3kGbji-f4QTF",
.stable_array = .{
.url = "git+https://github.com/lukewilliamboswell/zig-stable-array.git#727b74bc223ddcedcf9201ec0952e57963649efd",
.hash = "stable_array-0.1.0-3ihgvTFbAAAduZIruCYsxVwc2nWci42weBHshok2iRhM",
},
},
.minimum_zig_version = "0.13.0",
.minimum_zig_version = "0.16.0",
.paths = .{
"src",
"test/mem64",
Expand Down
77 changes: 40 additions & 37 deletions run/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,17 @@ fn printHelp(args: []const []const u8) void {
log.info(usage_string, .{args[0]});
}

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator: std.mem.Allocator = gpa.allocator();

const args: []const [:0]u8 = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);
pub fn main(process_init: std.process.Init) !void {
const allocator: std.mem.Allocator = process_init.gpa;

var args_list = std.ArrayList([:0]const u8).empty;
defer args_list.deinit(allocator);
var args_iter = std.process.Args.Iterator.init(process_init.minimal.args);
defer args_iter.deinit();
while (args_iter.next()) |arg| {
try args_list.append(allocator, arg);
}
const args: []const [:0]const u8 = args_list.items;

var env_buffer = std.array_list.Managed([]const u8).init(allocator);
defer env_buffer.deinit();
Expand Down Expand Up @@ -227,8 +232,7 @@ pub fn main() !void {

std.debug.assert(opts.filename != null);

var cwd = std.fs.cwd();
const wasm_data: []u8 = cwd.readFileAlloc(allocator, opts.filename.?, 1024 * 1024 * 128) catch |e| {
const wasm_data: []u8 = std.Io.Dir.cwd().readFileAlloc(process_init.io, opts.filename.?, allocator, .limited(1024 * 1024 * 128)) catch |e| {
std.log.err("Failed to read file '{s}' into memory: {}", .{ opts.filename.?, e });
return RunErrors.IoError;
};
Expand All @@ -247,10 +251,10 @@ pub fn main() !void {
};

if (opts.print_dump) {
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});
var aw = try std.Io.Writer.Allocating.initCapacity(allocator, 1024 * 16);
defer aw.deinit();
try module_def.dump(&aw.writer);
log.info("{s}", .{aw.writer.buffered()});
return;
}

Expand Down Expand Up @@ -301,14 +305,14 @@ pub fn main() !void {

const num_params: usize = invoke_args.len;
if (func_export.params.len != num_params) {
var strbuf = std.array_list.Managed(u8).init(allocator);
defer strbuf.deinit();
try writeSignature(&strbuf, &func_export);
var aw = std.Io.Writer.Allocating.init(allocator);
defer aw.deinit();
try writeSignature(&aw.writer, &func_export);
std.log.err("Specified {} params but expected {}. The signature of '{s}' is:\n{s}", .{
num_params,
func_export.params.len,
invoke_funcname,
strbuf.items,
aw.writer.buffered(),
});
return RunErrors.FunctionParamMismatch;
}
Expand Down Expand Up @@ -375,51 +379,50 @@ pub fn main() !void {
};

{
var strbuf = std.array_list.Managed(u8).init(allocator);
defer strbuf.deinit();
const writer = strbuf.writer();
var aw = std.Io.Writer.Allocating.init(allocator);
defer aw.deinit();
const writer = &aw.writer;

if (returns.items.len > 0) {
const return_types = func_export.returns;
try std.fmt.format(writer, "return:\n", .{});
try writer.print("return:\n", .{});
for (returns.items, 0..) |_, i| {
switch (return_types[i]) {
.I32 => try std.fmt.format(writer, " {} (i32)\n", .{returns.items[i].I32}),
.I64 => try std.fmt.format(writer, " {} (i64)\n", .{returns.items[i].I64}),
.F32 => try std.fmt.format(writer, " {} (f32)\n", .{returns.items[i].F32}),
.F64 => try std.fmt.format(writer, " {} (f64)\n", .{returns.items[i].F64}),
.I32 => try writer.print(" {} (i32)\n", .{returns.items[i].I32}),
.I64 => try writer.print(" {} (i64)\n", .{returns.items[i].I64}),
.F32 => try writer.print(" {} (f32)\n", .{returns.items[i].F32}),
.F64 => try writer.print(" {} (f64)\n", .{returns.items[i].F64}),
.V128 => unreachable, // TODO support
.FuncRef => try std.fmt.format(writer, " (funcref)\n", .{}),
.ExternRef => try std.fmt.format(writer, " (externref)\n", .{}),
.FuncRef => try writer.print(" (funcref)\n", .{}),
.ExternRef => try writer.print(" (externref)\n", .{}),
}
}
try std.fmt.format(writer, "\n", .{});
try writer.print("\n", .{});
}
if (strbuf.items.len > 0) {
log.info("{s}\n", .{strbuf.items});
if (aw.writer.end > 0) {
log.info("{s}\n", .{aw.writer.buffered()});
}
}
}

fn writeSignature(strbuf: *std.array_list.Managed(u8), info: *const bytebox.FunctionExport) !void {
const writer = strbuf.writer();
fn writeSignature(writer: *std.Io.Writer, info: *const bytebox.FunctionExport) !void {
if (info.params.len == 0) {
try std.fmt.format(writer, " params: none\n", .{});
try writer.print(" params: none\n", .{});
} else {
try std.fmt.format(writer, " params:\n", .{});
try writer.print(" params:\n", .{});
for (info.params) |valtype| {
const name: []const u8 = valtypeToString(valtype);
try std.fmt.format(writer, " {s}\n", .{name});
try writer.print(" {s}\n", .{name});
}
}

if (info.returns.len == 0) {
try std.fmt.format(writer, " returns: none\n", .{});
try writer.print(" returns: none\n", .{});
} else {
try std.fmt.format(writer, " returns:\n", .{});
try writer.print(" returns:\n", .{});
for (info.returns) |valtype| {
const name: []const u8 = valtypeToString(valtype);
try std.fmt.format(writer, " {s}\n", .{name});
try writer.print(" {s}\n", .{name});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cffi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const CGlobalExport = extern struct {

const INVALID_FUNC_INDEX = std.math.maxInt(u32);

var cffi_gpa = std.heap.GeneralPurposeAllocator(.{}){};
var cffi_gpa = std.heap.DebugAllocator(.{}){};

// const CAllocator = struct {
// const AllocError = std.mem.Allocator.Error;
Expand Down
73 changes: 25 additions & 48 deletions src/common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,8 @@ pub const Logger = struct {
};
}

fn defaultLog(level: LogLevel, text: [:0]const u8) void {
var fd = switch (level) {
.Info => std.fs.File.stdout(),
.Error => std.fs.File.stderr(),
};

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});
};
fn defaultLog(_: LogLevel, text: [:0]const u8) void {
std.debug.print("{s}", .{text});
}

pub fn info(self: Logger, comptime format: []const u8, args: anytype) void {
Expand Down Expand Up @@ -79,7 +64,15 @@ pub const ScratchAllocator = struct {
}

pub fn allocator(self: *ScratchAllocator) std.mem.Allocator {
return std.mem.Allocator.init(self, alloc, resize, free);
return .{
.ptr = self,
.vtable = &.{
.alloc = @ptrCast(&alloc),
.resize = @ptrCast(&resize),
.remap = std.mem.Allocator.noRemap,
.free = @ptrCast(&free),
},
};
}

pub fn reset(self: *ScratchAllocator) void {
Expand All @@ -89,50 +82,34 @@ pub const ScratchAllocator = struct {
fn alloc(
self: *ScratchAllocator,
len: usize,
ptr_align: u29,
len_align: u29,
ret_addr: usize,
) std.mem.Allocator.Error![]u8 {
_ = ret_addr;
_ = len_align;

alignment: std.mem.Alignment,
_: usize,
) ?[*]u8 {
const alloc_size = len;
const offset_begin = std.mem.alignForward(self.buffer.items.len, ptr_align);
const offset_begin = std.mem.alignForward(usize, self.buffer.items.len, alignment.toByteUnits());
const offset_end = offset_begin + alloc_size;
self.buffer.resize(offset_end) catch {
return std.mem.Allocator.Error.OutOfMemory;
return null;
};
return self.buffer.items[offset_begin..offset_end];
return self.buffer.items[offset_begin..offset_end].ptr;
}

fn resize(
self: *ScratchAllocator,
_: *ScratchAllocator,
old_mem: []u8,
old_align: u29,
_: std.mem.Alignment,
new_size: usize,
len_align: u29,
ret_addr: usize,
) ?usize {
_ = self;
_ = old_align;
_ = ret_addr;

if (new_size > old_mem.len) {
return null;
}
const aligned_size: usize = if (len_align == 0) new_size else std.mem.alignForward(new_size, len_align);
return aligned_size;
_: usize,
) bool {
return new_size <= old_mem.len;
}

fn free(
self: *ScratchAllocator,
old_mem: []u8,
old_align: u29,
ret_addr: usize,
_: []u8,
_: std.mem.Alignment,
_: usize,
) void {
_ = self;
_ = old_mem;
_ = old_align;
_ = ret_addr;
}
};
Loading
Loading