Skip to content

Instantly share code, notes, and snippets.

@jackdbd
Last active January 23, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackdbd/c6de94bd30a77554276a63d7a1def39f to your computer and use it in GitHub Desktop.
Save jackdbd/c6de94bd30a77554276a63d7a1def39f to your computer and use it in GitHub Desktop.
Zig program running on wasmtime (WASI runtime)
//! Example of using the zig standard library and reading command line arguments.
//! https://ziglang.org/documentation/master/#WASI
//! See also the talk Zig loves WASI! by Jakub Konka
//! https://www.youtube.com/watch?v=g_Degmqfo4Q
/// In order to run this example you will need a WebAssembly runtime such as wasmtime.
/// https://wasmtime.dev/
/// 1. compile the wasm module with the zig compiler
/// zig build-exe hello_wasi.zig -target wasm32-wasi
/// 2. run the .wasm module with wasmtime and pass the command line arguments
/// wasmtime hello_wasi.wasm 123 hello
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("{}: {}\n", .{ i, arg });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment