authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-02 18:57:43-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-04 00:27:08-08:00
log77087f6f316e16cf374339750f7a5446c18d000d
tree162b04d466012cefaf6acaf5857987474beea25f
parent4afed3e9ef83724bdfddd1d06b7727acda55e642

langref: update to new main API


5 files changed, 35 insertions(+), 50 deletions(-)

doc/langref.html.in+1-2
...@@ -7027,8 +7027,7 @@ WebAssembly.instantiate(typedArray, {...@@ -7027,8 +7027,7 @@ WebAssembly.instantiate(typedArray, {
7027The result is 3{#end_shell_samp#}7027The result is 3{#end_shell_samp#}
7028 {#header_close#}7028 {#header_close#}
7029 {#header_open|WASI#}7029 {#header_open|WASI#}
7030 <p>Zig's support for WebAssembly System Interface (WASI) is under active development.7030 <p>Zig standard library has first-class support for WebAssembly System Interface.</p>
7031 Example of using the standard library and reading command line arguments:</p>
7032 {#code|wasi_args.zig#}7031 {#code|wasi_args.zig#}
70337032
7034 {#shell_samp#}$ wasmtime wasi_args.wasm 123 hello7033 {#shell_samp#}$ wasmtime wasi_args.wasm 123 hello
doc/langref/hello.zig+2-12
...@@ -1,17 +1,7 @@...@@ -1,17 +1,7 @@
1const std = @import("std");1const std = @import("std");
22
3// See https://github.com/ziglang/zig/issues/245103pub fn main(init: std.process.Init) !void {
4// for the plan to simplify this code.4 try std.Io.File.stdout().writeStreamingAll(init.io, "Hello, World!\n");
5pub fn main() !void {
6 var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
7 defer _ = debug_allocator.deinit();
8 const gpa = debug_allocator.allocator();
9
10 var threaded: std.Io.Threaded = .init(gpa, .{});
11 defer threaded.deinit();
12 const io = threaded.io();
13
14 try std.Io.File.stdout().writeStreamingAll(io, "Hello, World!\n");
15}5}
166
17// exe=succeed7// exe=succeed
doc/langref/wasi_args.zig+4-8
...@@ -1,13 +1,9 @@...@@ -1,13 +1,9 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() !void {3pub fn main(init: std.process.Init) !void {
4 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;4 const args = try init.minimal.args.toSlice(init.arena.allocator());
5 const gpa = general_purpose_allocator.allocator();5 for (0.., args) |i, arg| {
6 const args = try std.process.argsAlloc(gpa);6 std.debug.print("{d}: {s}\n", .{ i, arg });
7 defer std.process.argsFree(gpa, args);
8
9 for (args, 0..) |arg, i| {
10 std.debug.print("{}: {s}\n", .{ i, arg });
11 }7 }
12}8}
139
doc/langref/wasi_preopens.zig+3-11
...@@ -1,18 +1,10 @@...@@ -1,18 +1,10 @@
1const std = @import("std");1const std = @import("std");
2const fs = std.fs;
32
4pub fn main() !void {3pub fn main(init: std.process.Init) !void {
5 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;4 const preopens = try std.fs.wasi.preopensAlloc(init.arena.allocator());
6 const gpa = general_purpose_allocator.allocator();
7
8 var arena_instance = std.heap.ArenaAllocator.init(gpa);
9 defer arena_instance.deinit();
10 const arena = arena_instance.allocator();
11
12 const preopens = try fs.wasi.preopensAlloc(arena);
135
14 for (preopens.names, 0..) |preopen, i| {6 for (preopens.names, 0..) |preopen, i| {
15 std.debug.print("{}: {s}\n", .{ i, preopen });7 std.debug.print("{d}: {s}\n", .{ i, preopen });
16 }8 }
17}9}
1810
tools/doctest.zig+25-17
...@@ -32,6 +32,10 @@ const usage =...@@ -32,6 +32,10 @@ const usage =
32pub fn main(init: std.process.Init) !void {32pub fn main(init: std.process.Init) !void {
33 const arena = init.arena.allocator();33 const arena = init.arena.allocator();
34 const io = init.io;34 const io = init.io;
35 const env_map = init.env_map;
36 const cwd_path = try std.process.getCwdAlloc(arena);
37
38 try env_map.put("CLICOLOR_FORCE", "1");
3539
36 var args_it = try init.minimal.args.iterateAllocator(arena);40 var args_it = try init.minimal.args.iterateAllocator(arena);
37 if (!args_it.skip()) fatal("missing argv[0]", .{});41 if (!args_it.skip()) fatal("missing argv[0]", .{});
...@@ -97,12 +101,13 @@ pub fn main(init: std.process.Init) !void {...@@ -97,12 +101,13 @@ pub fn main(init: std.process.Init) !void {
97 out,101 out,
98 code,102 code,
99 tmp_dir_path,103 tmp_dir_path,
100 try Dir.path.relative(arena, tmp_dir_path, zig_path),104 try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, zig_path),
101 try Dir.path.relative(arena, tmp_dir_path, input_path),105 try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, input_path),
102 if (opt_zig_lib_dir) |zig_lib_dir|106 if (opt_zig_lib_dir) |zig_lib_dir|
103 try Dir.path.relative(arena, tmp_dir_path, zig_lib_dir)107 try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, zig_lib_dir)
104 else108 else
105 null,109 null,
110 env_map,
106 );111 );
107112
108 try out_file_writer.end();113 try out_file_writer.end();
...@@ -121,10 +126,8 @@ fn printOutput(...@@ -121,10 +126,8 @@ fn printOutput(
121 input_path: []const u8,126 input_path: []const u8,
122 /// Relative to `tmp_dir_path`.127 /// Relative to `tmp_dir_path`.
123 opt_zig_lib_dir: ?[]const u8,128 opt_zig_lib_dir: ?[]const u8,
129 env_map: *const process.Environ.Map,
124) !void {130) !void {
125 var env_map = try process.getEnvMap(arena);
126 try env_map.put("CLICOLOR_FORCE", "1");
127
128 const host = try std.zig.system.resolveTargetQuery(io, .{});131 const host = try std.zig.system.resolveTargetQuery(io, .{});
129 const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);132 const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
130 const print = std.debug.print;133 const print = std.debug.print;
...@@ -196,7 +199,7 @@ fn printOutput(...@@ -196,7 +199,7 @@ fn printOutput(
196 const result = try process.run(arena, io, .{199 const result = try process.run(arena, io, .{
197 .argv = build_args.items,200 .argv = build_args.items,
198 .cwd = tmp_dir_path,201 .cwd = tmp_dir_path,
199 .env_map = &env_map,202 .env_map = env_map,
200 .max_output_bytes = max_doc_file_size,203 .max_output_bytes = max_doc_file_size,
201 });204 });
202 switch (result.term) {205 switch (result.term) {
...@@ -218,7 +221,7 @@ fn printOutput(...@@ -218,7 +221,7 @@ fn printOutput(
218 try shell_out.writeAll(colored_stderr);221 try shell_out.writeAll(colored_stderr);
219 break :code_block;222 break :code_block;
220 }223 }
221 const exec_result = run(arena, io, &env_map, tmp_dir_path, build_args.items) catch224 const exec_result = run(arena, io, env_map, tmp_dir_path, build_args.items) catch
222 fatal("example failed to compile", .{});225 fatal("example failed to compile", .{});
223226
224 if (code.verbose_cimport) {227 if (code.verbose_cimport) {
...@@ -251,7 +254,7 @@ fn printOutput(...@@ -251,7 +254,7 @@ fn printOutput(
251 const result = if (expected_outcome == .fail) blk: {254 const result = if (expected_outcome == .fail) blk: {
252 const result = try process.run(arena, io, .{255 const result = try process.run(arena, io, .{
253 .argv = run_args,256 .argv = run_args,
254 .env_map = &env_map,257 .env_map = env_map,
255 .cwd = tmp_dir_path,258 .cwd = tmp_dir_path,
256 .max_output_bytes = max_doc_file_size,259 .max_output_bytes = max_doc_file_size,
257 });260 });
...@@ -268,7 +271,7 @@ fn printOutput(...@@ -268,7 +271,7 @@ fn printOutput(
268 }271 }
269 break :blk result;272 break :blk result;
270 } else blk: {273 } else blk: {
271 break :blk run(arena, io, &env_map, tmp_dir_path, run_args) catch274 break :blk run(arena, io, env_map, tmp_dir_path, run_args) catch
272 fatal("example crashed", .{});275 fatal("example crashed", .{});
273 };276 };
274277
...@@ -337,7 +340,7 @@ fn printOutput(...@@ -337,7 +340,7 @@ fn printOutput(
337 }340 }
338 }341 }
339342
340 const result = run(arena, io, &env_map, tmp_dir_path, test_args.items) catch343 const result = run(arena, io, env_map, tmp_dir_path, test_args.items) catch
341 fatal("test failed", .{});344 fatal("test failed", .{});
342 const escaped_stderr = try escapeHtml(arena, result.stderr);345 const escaped_stderr = try escapeHtml(arena, result.stderr);
343 const escaped_stdout = try escapeHtml(arena, result.stdout);346 const escaped_stdout = try escapeHtml(arena, result.stdout);
...@@ -370,7 +373,7 @@ fn printOutput(...@@ -370,7 +373,7 @@ fn printOutput(
370 }373 }
371 const result = try process.run(arena, io, .{374 const result = try process.run(arena, io, .{
372 .argv = test_args.items,375 .argv = test_args.items,
373 .env_map = &env_map,376 .env_map = env_map,
374 .cwd = tmp_dir_path,377 .cwd = tmp_dir_path,
375 .max_output_bytes = max_doc_file_size,378 .max_output_bytes = max_doc_file_size,
376 });379 });
...@@ -426,7 +429,7 @@ fn printOutput(...@@ -426,7 +429,7 @@ fn printOutput(
426429
427 const result = try process.run(arena, io, .{430 const result = try process.run(arena, io, .{
428 .argv = test_args.items,431 .argv = test_args.items,
429 .env_map = &env_map,432 .env_map = env_map,
430 .cwd = tmp_dir_path,433 .cwd = tmp_dir_path,
431 .max_output_bytes = max_doc_file_size,434 .max_output_bytes = max_doc_file_size,
432 });435 });
...@@ -502,7 +505,7 @@ fn printOutput(...@@ -502,7 +505,7 @@ fn printOutput(
502 if (maybe_error_match) |error_match| {505 if (maybe_error_match) |error_match| {
503 const result = try process.run(arena, io, .{506 const result = try process.run(arena, io, .{
504 .argv = build_args.items,507 .argv = build_args.items,
505 .env_map = &env_map,508 .env_map = env_map,
506 .cwd = tmp_dir_path,509 .cwd = tmp_dir_path,
507 .max_output_bytes = max_doc_file_size,510 .max_output_bytes = max_doc_file_size,
508 });511 });
...@@ -528,7 +531,7 @@ fn printOutput(...@@ -528,7 +531,7 @@ fn printOutput(
528 const colored_stderr = try termColor(arena, escaped_stderr);531 const colored_stderr = try termColor(arena, escaped_stderr);
529 try shell_out.print("\n{s} ", .{colored_stderr});532 try shell_out.print("\n{s} ", .{colored_stderr});
530 } else {533 } else {
531 _ = run(arena, io, &env_map, tmp_dir_path, build_args.items) catch fatal("example failed to compile", .{});534 _ = run(arena, io, env_map, tmp_dir_path, build_args.items) catch fatal("example failed to compile", .{});
532 }535 }
533 try shell_out.writeAll("\n");536 try shell_out.writeAll("\n");
534 },537 },
...@@ -587,7 +590,7 @@ fn printOutput(...@@ -587,7 +590,7 @@ fn printOutput(
587 try test_args.append(option);590 try test_args.append(option);
588 try shell_out.print("{s} ", .{option});591 try shell_out.print("{s} ", .{option});
589 }592 }
590 const result = run(arena, io, &env_map, tmp_dir_path, test_args.items) catch fatal("test failed", .{});593 const result = run(arena, io, env_map, tmp_dir_path, test_args.items) catch fatal("test failed", .{});
591 const escaped_stderr = try escapeHtml(arena, result.stderr);594 const escaped_stderr = try escapeHtml(arena, result.stderr);
592 const escaped_stdout = try escapeHtml(arena, result.stdout);595 const escaped_stdout = try escapeHtml(arena, result.stdout);
593 try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });596 try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
...@@ -1120,7 +1123,7 @@ fn in(slice: []const u8, number: u8) bool {...@@ -1120,7 +1123,7 @@ fn in(slice: []const u8, number: u8) bool {
1120fn run(1123fn run(
1121 allocator: Allocator,1124 allocator: Allocator,
1122 io: Io,1125 io: Io,
1123 env_map: *process.Environ.Map,1126 env_map: *const process.Environ.Map,
1124 cwd: []const u8,1127 cwd: []const u8,
1125 args: []const []const u8,1128 args: []const []const u8,
1126) !process.RunResult {1129) !process.RunResult {
...@@ -1138,6 +1141,11 @@ fn run(...@@ -1138,6 +1141,11 @@ fn run(
1138 return error.ChildExitError;1141 return error.ChildExitError;
1139 }1142 }
1140 },1143 },
1144 .signal => |sig| {
1145 std.debug.print("{s}\nThe following command terminated with signal {t}:\n", .{ result.stderr, sig });
1146 dumpArgs(args);
1147 return error.ChildCrashed;
1148 },
1141 else => {1149 else => {
1142 std.debug.print("{s}\nThe following command crashed:\n", .{result.stderr});1150 std.debug.print("{s}\nThe following command crashed:\n", .{result.stderr});
1143 dumpArgs(args);1151 dumpArgs(args);