langref: update to new main API

This commit is contained in:
Andrew Kelley 2026-01-02 18:57:43 -08:00
parent 4afed3e9ef
commit 77087f6f31
5 changed files with 35 additions and 50 deletions

View file

@ -7027,8 +7027,7 @@ WebAssembly.instantiate(typedArray, {
The result is 3{#end_shell_samp#}
{#header_close#}
{#header_open|WASI#}
<p>Zig's support for WebAssembly System Interface (WASI) is under active development.
Example of using the standard library and reading command line arguments:</p>
<p>Zig standard library has first-class support for WebAssembly System Interface.</p>
{#code|wasi_args.zig#}
{#shell_samp#}$ wasmtime wasi_args.wasm 123 hello

View file

@ -1,17 +1,7 @@
const std = @import("std");
// See https://github.com/ziglang/zig/issues/24510
// for the plan to simplify this code.
pub fn main() !void {
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
defer _ = debug_allocator.deinit();
const gpa = debug_allocator.allocator();
var threaded: std.Io.Threaded = .init(gpa, .{});
defer threaded.deinit();
const io = threaded.io();
try std.Io.File.stdout().writeStreamingAll(io, "Hello, World!\n");
pub fn main(init: std.process.Init) !void {
try std.Io.File.stdout().writeStreamingAll(init.io, "Hello, World!\n");
}
// exe=succeed

View file

@ -1,13 +1,9 @@
const std = @import("std");
pub fn main() !void {
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
const gpa = general_purpose_allocator.allocator();
const args = try std.process.argsAlloc(gpa);
defer std.process.argsFree(gpa, args);
for (args, 0..) |arg, i| {
std.debug.print("{}: {s}\n", .{ i, arg });
pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
for (0.., args) |i, arg| {
std.debug.print("{d}: {s}\n", .{ i, arg });
}
}

View file

@ -1,18 +1,10 @@
const std = @import("std");
const fs = std.fs;
pub fn main() !void {
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
const gpa = general_purpose_allocator.allocator();
var arena_instance = std.heap.ArenaAllocator.init(gpa);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
const preopens = try fs.wasi.preopensAlloc(arena);
pub fn main(init: std.process.Init) !void {
const preopens = try std.fs.wasi.preopensAlloc(init.arena.allocator());
for (preopens.names, 0..) |preopen, i| {
std.debug.print("{}: {s}\n", .{ i, preopen });
std.debug.print("{d}: {s}\n", .{ i, preopen });
}
}