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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
zig-cache
.zig-cache
zig-pkg
zig-out
test/testrunner/bin
79 changes: 43 additions & 36 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
const Build = @import("std").Build;
const std = @import("std");
const Build = std.Build;

pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const zware_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
});

try b.modules.put(b.dupe("zware"), zware_module);

const main_mod = b.addModule("zware", .{
const zware = b.addModule("zware", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const lib = b.addLibrary(.{
.name = "zware",
.root_module = main_mod,
.root_module = zware,
});
b.installArtifact(lib);

const main_tests = b.addTest(.{
.root_module = main_mod,
.root_module = zware,
.use_llvm = true,
});

Expand All @@ -41,7 +37,7 @@ pub fn build(b: *Build) !void {
}),
.use_llvm = true,
});
testrunner.root_module.addImport("zware", zware_module);
testrunner.root_module.addImport("zware", zware);

const testsuite_dep = b.dependency("testsuite", .{});

Expand All @@ -53,6 +49,7 @@ pub fn build(b: *Build) !void {
const json_file = run_wast2json.addOutputFileArg(b.fmt("{s}.json", .{test_name}));

const run_test = b.addRunArtifact(testrunner);
run_test.setName(b.fmt("run test-{s}", .{test_name}));
run_test.addFileArg(json_file);
run_test.cwd = json_file.dirname();
const step = b.step(b.fmt("test-{s}", .{test_name}), b.fmt("Run the '{s}' test", .{test_name}));
Expand All @@ -74,14 +71,12 @@ pub fn build(b: *Build) !void {
}),
.use_llvm = true,
});
exe.root_module.addImport("zware", zware_module);
exe.root_module.addImport("zware", zware);
const install = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install.step);
const run = b.addRunArtifact(exe);
run.step.dependOn(&install.step);
if (b.args) |args| {
run.addArgs(args);
}
passthroughArgs(b, run);
b.step("run", "Run the cmdline runner zware-run").dependOn(&run.step);
}

Expand All @@ -94,14 +89,12 @@ pub fn build(b: *Build) !void {
.optimize = optimize,
}),
});
exe.root_module.addImport("zware", zware_module);
exe.root_module.addImport("zware", zware);
const install = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install.step);
const run = b.addRunArtifact(exe);
run.step.dependOn(&install.step);
if (b.args) |args| {
run.addArgs(args);
}
passthroughArgs(b, run);
b.step("gen", "Run the cmdline runner zware-gen").dependOn(&run.step);
}
}
Expand All @@ -127,35 +120,38 @@ fn addWast2Json(b: *Build) *Build.Step.Compile {
.SIZEOF_SIZE_T = @sizeOf(usize),
});

const wabt_lib = b.addLibrary(.{
.name = "wabt",
.root_module = b.createModule(.{
.target = b.graph.host,
.optimize = .Debug,
}),
const wabt_mod = b.createModule(.{
.target = b.graph.host,
.optimize = .Debug,
.link_libcpp = true,
});
wabt_lib.addConfigHeader(wabt_config_h);
wabt_lib.addIncludePath(wabt_dep.path("include"));
wabt_lib.addCSourceFiles(.{
wabt_mod.addConfigHeader(wabt_config_h);
wabt_mod.addIncludePath(wabt_dep.path("include"));
wabt_mod.addCSourceFiles(.{
.root = wabt_dep.path("."),
.files = &wabt_files,
});
wabt_lib.linkLibCpp();

const wast2json = b.addExecutable(.{
.name = "wast2json",
.root_module = b.createModule(.{
.target = b.graph.host,
}),
const wabt_lib = b.addLibrary(.{
.name = "wabt",
.root_module = wabt_mod,
});

const wast2json = b.createModule(.{
.target = b.graph.host,
.link_libcpp = true,
});

wast2json.addConfigHeader(wabt_config_h);
wast2json.addIncludePath(wabt_dep.path("include"));
wast2json.addCSourceFile(.{
.file = wabt_dep.path("src/tools/wast2json.cc"),
});
wast2json.linkLibCpp();
wast2json.linkLibrary(wabt_lib);
return wast2json;
return b.addExecutable(.{
.name = "wast2json",
.root_module = wast2json,
});
}

const test_names = [_][]const u8{
Expand Down Expand Up @@ -282,3 +278,14 @@ const wabt_files = [_][]const u8{
"src/wast-lexer.cc",
"src/wast-parser.cc",
};

// zig 0.17.0 and 0.16.0 compatible args passthrough function
inline fn passthroughArgs(b: *Build, run: *Build.Step.Run) void {
if (comptime @import("builtin").zig_version.order(std.SemanticVersion.parse("0.16.0") catch unreachable) == .gt) {
run.addPassthruArgs();
} else {
if (b.args) |args| {
for (args) |arg| run.addArg(arg);
}
}
}
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.name = .zware,
.version = "0.0.1",
.fingerprint = 0xea6cb8e2e9e30e64,
.minimum_zig_version = "0.15.2",
.minimum_zig_version = "0.16.0",
.dependencies = .{
.wabt = .{
.url = "https://github.com/WebAssembly/wabt/archive/39f85a791cbbad91a253a851841a29777efdc2cd.tar.gz",
Expand Down
18 changes: 14 additions & 4 deletions examples/fib/build.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Build = @import("std").Build;
const std = @import("std");
const Build = std.Build;

pub fn build(b: *Build) void {
const target = b.standardTargetOptions(.{});
Expand All @@ -20,10 +21,19 @@ pub fn build(b: *Build) void {

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
passthroughArgs(b, run_cmd);

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

// zig 0.17.0 and 0.16.0 compatible args passthrough function
inline fn passthroughArgs(b: *Build, run: *Build.Step.Run) void {
if (comptime @import("builtin").zig_version.order(std.SemanticVersion.parse("0.16.0") catch unreachable) == .gt) {
run.addPassthruArgs();
} else {
if (b.args) |args| {
for (args) |arg| run.addArg(arg);
}
}
}
7 changes: 2 additions & 5 deletions examples/fib/src/fib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ const zware = @import("zware");
const Store = zware.Store;
const Module = zware.Module;
const Instance = zware.Instance;
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
var gpa = GeneralPurposeAllocator(.{}){};

pub fn main() !void {
defer _ = gpa.deinit();
const alloc = gpa.allocator();
pub fn main(init: std.process.Init) !void {
const alloc = init.gpa;

const bytes = @embedFile("fib.wasm");

Expand Down
20 changes: 10 additions & 10 deletions src/instance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub const Instance = struct {

.wasi_preopens = .empty,
.wasi_args = .empty,
.wasi_env = .{},
.wasi_env = .empty,
};
}

Expand Down Expand Up @@ -323,9 +323,9 @@ pub const Instance = struct {

const function = try self.getFunc(funcidx);

var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = [_]VirtualMachine.Frame{undefined} ** options.frame_stack_size;
var label_stack: [options.label_stack_size]VirtualMachine.Label = [_]VirtualMachine.Label{undefined} ** options.label_stack_size;
var op_stack: [options.operand_stack_size]u64 = [_]u64{0} ** options.operand_stack_size;
var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = @splat(undefined);
var label_stack: [options.label_stack_size]VirtualMachine.Label = @splat(undefined);
var op_stack: [options.operand_stack_size]u64 = @splat(0);

switch (function.subtype) {
.function => |f| {
Expand Down Expand Up @@ -386,9 +386,9 @@ pub const Instance = struct {
pub fn invokeStart(self: *Instance, index: u32, comptime options: VirtualMachineOptions) !void {
const function = try self.getFunc(index);

var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = [_]VirtualMachine.Frame{undefined} ** options.frame_stack_size;
var label_stack: [options.label_stack_size]VirtualMachine.Label = [_]VirtualMachine.Label{undefined} ** options.label_stack_size;
var op_stack: [options.operand_stack_size]u64 = [_]u64{0} ** options.operand_stack_size;
var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = @splat(undefined);
var label_stack: [options.label_stack_size]VirtualMachine.Label = @splat(undefined);
var op_stack: [options.operand_stack_size]u64 = @splat(0);

switch (function.subtype) {
.function => |f| {
Expand Down Expand Up @@ -427,9 +427,9 @@ pub const Instance = struct {
}

pub fn invokeExpression(self: *Instance, start: usize, comptime Result: type, comptime options: VirtualMachineOptions) !Result {
var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = [_]VirtualMachine.Frame{undefined} ** options.frame_stack_size;
var label_stack: [options.label_stack_size]VirtualMachine.Label = [_]VirtualMachine.Label{undefined} ** options.label_stack_size;
var op_stack: [options.operand_stack_size]u64 = [_]u64{0} ** options.operand_stack_size;
var frame_stack: [options.frame_stack_size]VirtualMachine.Frame = @splat(undefined);
var label_stack: [options.label_stack_size]VirtualMachine.Label = @splat(undefined);
var op_stack: [options.operand_stack_size]u64 = @splat(0);

var vm = VirtualMachine.init(op_stack[0..], frame_stack[0..], label_stack[0..], self);

Expand Down
6 changes: 3 additions & 3 deletions src/instance/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2682,9 +2682,9 @@ pub const WasmError = error{
const testing = std.testing;

test "operand push / pop test" {
var op_stack: [6]u64 = [_]u64{0} ** 6;
var frame_stack_mem: [1024]VirtualMachine.Frame = [_]VirtualMachine.Frame{undefined} ** 1024;
var label_stack_mem: [1024]VirtualMachine.Label = [_]VirtualMachine.Label{undefined} ** 1024;
var op_stack: [6]u64 = @splat(0);
var frame_stack_mem: [1024]VirtualMachine.Frame = @splat(undefined);
var label_stack_mem: [1024]VirtualMachine.Label = @splat(undefined);

var inst: Instance = undefined;

Expand Down
Loading
Loading