Last active
January 23, 2021 14:45
-
-
Save jackdbd/c6de94bd30a77554276a63d7a1def39f to your computer and use it in GitHub Desktop.
Zig program running on wasmtime (WASI runtime)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! 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