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 });
}
}

View file

@ -32,6 +32,10 @@ const usage =
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const env_map = init.env_map;
const cwd_path = try std.process.getCwdAlloc(arena);
try env_map.put("CLICOLOR_FORCE", "1");
var args_it = try init.minimal.args.iterateAllocator(arena);
if (!args_it.skip()) fatal("missing argv[0]", .{});
@ -97,12 +101,13 @@ pub fn main(init: std.process.Init) !void {
out,
code,
tmp_dir_path,
try Dir.path.relative(arena, tmp_dir_path, zig_path),
try Dir.path.relative(arena, tmp_dir_path, input_path),
try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, zig_path),
try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, input_path),
if (opt_zig_lib_dir) |zig_lib_dir|
try Dir.path.relative(arena, tmp_dir_path, zig_lib_dir)
try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, zig_lib_dir)
else
null,
env_map,
);
try out_file_writer.end();
@ -121,10 +126,8 @@ fn printOutput(
input_path: []const u8,
/// Relative to `tmp_dir_path`.
opt_zig_lib_dir: ?[]const u8,
env_map: *const process.Environ.Map,
) !void {
var env_map = try process.getEnvMap(arena);
try env_map.put("CLICOLOR_FORCE", "1");
const host = try std.zig.system.resolveTargetQuery(io, .{});
const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
const print = std.debug.print;
@ -196,7 +199,7 @@ fn printOutput(
const result = try process.run(arena, io, .{
.argv = build_args.items,
.cwd = tmp_dir_path,
.env_map = &env_map,
.env_map = env_map,
.max_output_bytes = max_doc_file_size,
});
switch (result.term) {
@ -218,7 +221,7 @@ fn printOutput(
try shell_out.writeAll(colored_stderr);
break :code_block;
}
const exec_result = run(arena, io, &env_map, tmp_dir_path, build_args.items) catch
const exec_result = run(arena, io, env_map, tmp_dir_path, build_args.items) catch
fatal("example failed to compile", .{});
if (code.verbose_cimport) {
@ -251,7 +254,7 @@ fn printOutput(
const result = if (expected_outcome == .fail) blk: {
const result = try process.run(arena, io, .{
.argv = run_args,
.env_map = &env_map,
.env_map = env_map,
.cwd = tmp_dir_path,
.max_output_bytes = max_doc_file_size,
});
@ -268,7 +271,7 @@ fn printOutput(
}
break :blk result;
} else blk: {
break :blk run(arena, io, &env_map, tmp_dir_path, run_args) catch
break :blk run(arena, io, env_map, tmp_dir_path, run_args) catch
fatal("example crashed", .{});
};
@ -337,7 +340,7 @@ fn printOutput(
}
}
const result = run(arena, io, &env_map, tmp_dir_path, test_args.items) catch
const result = run(arena, io, env_map, tmp_dir_path, test_args.items) catch
fatal("test failed", .{});
const escaped_stderr = try escapeHtml(arena, result.stderr);
const escaped_stdout = try escapeHtml(arena, result.stdout);
@ -370,7 +373,7 @@ fn printOutput(
}
const result = try process.run(arena, io, .{
.argv = test_args.items,
.env_map = &env_map,
.env_map = env_map,
.cwd = tmp_dir_path,
.max_output_bytes = max_doc_file_size,
});
@ -426,7 +429,7 @@ fn printOutput(
const result = try process.run(arena, io, .{
.argv = test_args.items,
.env_map = &env_map,
.env_map = env_map,
.cwd = tmp_dir_path,
.max_output_bytes = max_doc_file_size,
});
@ -502,7 +505,7 @@ fn printOutput(
if (maybe_error_match) |error_match| {
const result = try process.run(arena, io, .{
.argv = build_args.items,
.env_map = &env_map,
.env_map = env_map,
.cwd = tmp_dir_path,
.max_output_bytes = max_doc_file_size,
});
@ -528,7 +531,7 @@ fn printOutput(
const colored_stderr = try termColor(arena, escaped_stderr);
try shell_out.print("\n{s} ", .{colored_stderr});
} else {
_ = run(arena, io, &env_map, tmp_dir_path, build_args.items) catch fatal("example failed to compile", .{});
_ = run(arena, io, env_map, tmp_dir_path, build_args.items) catch fatal("example failed to compile", .{});
}
try shell_out.writeAll("\n");
},
@ -587,7 +590,7 @@ fn printOutput(
try test_args.append(option);
try shell_out.print("{s} ", .{option});
}
const result = run(arena, io, &env_map, tmp_dir_path, test_args.items) catch fatal("test failed", .{});
const result = run(arena, io, env_map, tmp_dir_path, test_args.items) catch fatal("test failed", .{});
const escaped_stderr = try escapeHtml(arena, result.stderr);
const escaped_stdout = try escapeHtml(arena, result.stdout);
try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
@ -1120,7 +1123,7 @@ fn in(slice: []const u8, number: u8) bool {
fn run(
allocator: Allocator,
io: Io,
env_map: *process.Environ.Map,
env_map: *const process.Environ.Map,
cwd: []const u8,
args: []const []const u8,
) !process.RunResult {
@ -1138,6 +1141,11 @@ fn run(
return error.ChildExitError;
}
},
.signal => |sig| {
std.debug.print("{s}\nThe following command terminated with signal {t}:\n", .{ result.stderr, sig });
dumpArgs(args);
return error.ChildCrashed;
},
else => {
std.debug.print("{s}\nThe following command crashed:\n", .{result.stderr});
dumpArgs(args);