From 286e316ac5d47a7dbf4cfb5063de8a67c4025432 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 27 Aug 2022 01:13:48 +0800 Subject: [PATCH] feat: add wasi01 --- wasi01/README.md | 9 +++++++++ wasi01/build.zig | 34 ++++++++++++++++++++++++++++++++++ wasi01/justfile | 6 ++++++ wasi01/src/main.zig | 13 +++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 wasi01/README.md create mode 100644 wasi01/build.zig create mode 100644 wasi01/justfile create mode 100644 wasi01/src/main.zig diff --git a/wasi01/README.md b/wasi01/README.md new file mode 100644 index 0000000..e7cec13 --- /dev/null +++ b/wasi01/README.md @@ -0,0 +1,9 @@ + +```console +$ wasmtime main.wasm hello world +0: main.wasm +1: hello +2: world +``` + + diff --git a/wasi01/build.zig b/wasi01/build.zig new file mode 100644 index 0000000..21a2892 --- /dev/null +++ b/wasi01/build.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("wasi01", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + const exe_tests = b.addTest("src/main.zig"); + exe_tests.setTarget(target); + exe_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run unit tests"); + test_step.dependOn(&exe_tests.step); +} diff --git a/wasi01/justfile b/wasi01/justfile new file mode 100644 index 0000000..d22bd4e --- /dev/null +++ b/wasi01/justfile @@ -0,0 +1,6 @@ +_: + @just --list + +build: + zig build-exe src/main.zig -target wasm32-wasi + diff --git a/wasi01/src/main.zig b/wasi01/src/main.zig new file mode 100644 index 0000000..28a4754 --- /dev/null +++ b/wasi01/src/main.zig @@ -0,0 +1,13 @@ +const std = @import("std"); + +pub fn main() !void { + var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; + const gpa = general_purpose_allocator.allocator(); + const args = try std.process.argsAlloc(gpa); + defer std.process.argsFree(gpa, args); + + for (args) |arg, i| { + std.debug.print("{}: {s}\n", .{ i, arg }); + } +} +