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, {
70277027The result is 3{#end_shell_samp#}
70287028 {#header_close#}
70297029 {#header_open|WASI#}
7030 <p>Zig's support for WebAssembly System Interface (WASI) is under active development.
7031 Example of using the standard library and reading command line arguments:</p>
7030 <p>Zig standard library has first-class support for WebAssembly System Interface.</p>
70327031 {#code|wasi_args.zig#}
70337032
70347033 {#shell_samp#}$ wasmtime wasi_args.wasm 123 hello
doc/langref/hello.zig+2-12
......@@ -1,17 +1,7 @@
11const std = @import("std");
22
3// See https://github.com/ziglang/zig/issues/24510
4// for the plan to simplify this code.
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");
3pub fn main(init: std.process.Init) !void {
4 try std.Io.File.stdout().writeStreamingAll(init.io, "Hello, World!\n");
155}
166
177// exe=succeed
doc/langref/wasi_args.zig+4-8
......@@ -1,13 +1,9 @@
11const std = @import("std");
22
3pub fn main() !void {
4 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
5 const gpa = general_purpose_allocator.allocator();
6 const args = try std.process.argsAlloc(gpa);
7 defer std.process.argsFree(gpa, args);
8
9 for (args, 0..) |arg, i| {
10 std.debug.print("{}: {s}\n", .{ i, arg });
3pub fn main(init: std.process.Init) !void {
4 const args = try init.minimal.args.toSlice(init.arena.allocator());
5 for (0.., args) |i, arg| {
6 std.debug.print("{d}: {s}\n", .{ i, arg });
117 }
128}
139
doc/langref/wasi_preopens.zig+3-11
......@@ -1,18 +1,10 @@
11const std = @import("std");
2const fs = std.fs;
32
4pub fn main() !void {
5 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
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);
3pub fn main(init: std.process.Init) !void {
4 const preopens = try std.fs.wasi.preopensAlloc(init.arena.allocator());
135
146 for (preopens.names, 0..) |preopen, i| {
15 std.debug.print("{}: {s}\n", .{ i, preopen });
7 std.debug.print("{d}: {s}\n", .{ i, preopen });
168 }
179}
1810
tools/doctest.zig+25-17
......@@ -32,6 +32,10 @@ const usage =
3232pub fn main(init: std.process.Init) !void {
3333 const arena = init.arena.allocator();
3434 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
3640 var args_it = try init.minimal.args.iterateAllocator(arena);
3741 if (!args_it.skip()) fatal("missing argv[0]", .{});
......@@ -97,12 +101,13 @@ pub fn main(init: std.process.Init) !void {
97101 out,
98102 code,
99103 tmp_dir_path,
100 try Dir.path.relative(arena, tmp_dir_path, zig_path),
101 try Dir.path.relative(arena, tmp_dir_path, input_path),
104 try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, zig_path),
105 try Dir.path.relative(arena, cwd_path, env_map, tmp_dir_path, input_path),
102106 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)
104108 else
105109 null,
110 env_map,
106111 );
107112
108113 try out_file_writer.end();
......@@ -121,10 +126,8 @@ fn printOutput(
121126 input_path: []const u8,
122127 /// Relative to `tmp_dir_path`.
123128 opt_zig_lib_dir: ?[]const u8,
129 env_map: *const process.Environ.Map,
124130) !void {
125 var env_map = try process.getEnvMap(arena);
126 try env_map.put("CLICOLOR_FORCE", "1");
127
128131 const host = try std.zig.system.resolveTargetQuery(io, .{});
129132 const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
130133 const print = std.debug.print;
......@@ -196,7 +199,7 @@ fn printOutput(
196199 const result = try process.run(arena, io, .{
197200 .argv = build_args.items,
198201 .cwd = tmp_dir_path,
199 .env_map = &env_map,
202 .env_map = env_map,
200203 .max_output_bytes = max_doc_file_size,
201204 });
202205 switch (result.term) {
......@@ -218,7 +221,7 @@ fn printOutput(
218221 try shell_out.writeAll(colored_stderr);
219222 break :code_block;
220223 }
221 const exec_result = run(arena, io, &env_map, tmp_dir_path, build_args.items) catch
224 const exec_result = run(arena, io, env_map, tmp_dir_path, build_args.items) catch
222225 fatal("example failed to compile", .{});
223226
224227 if (code.verbose_cimport) {
......@@ -251,7 +254,7 @@ fn printOutput(
251254 const result = if (expected_outcome == .fail) blk: {
252255 const result = try process.run(arena, io, .{
253256 .argv = run_args,
254 .env_map = &env_map,
257 .env_map = env_map,
255258 .cwd = tmp_dir_path,
256259 .max_output_bytes = max_doc_file_size,
257260 });
......@@ -268,7 +271,7 @@ fn printOutput(
268271 }
269272 break :blk result;
270273 } else blk: {
271 break :blk run(arena, io, &env_map, tmp_dir_path, run_args) catch
274 break :blk run(arena, io, env_map, tmp_dir_path, run_args) catch
272275 fatal("example crashed", .{});
273276 };
274277
......@@ -337,7 +340,7 @@ fn printOutput(
337340 }
338341 }
339342
340 const result = run(arena, io, &env_map, tmp_dir_path, test_args.items) catch
343 const result = run(arena, io, env_map, tmp_dir_path, test_args.items) catch
341344 fatal("test failed", .{});
342345 const escaped_stderr = try escapeHtml(arena, result.stderr);
343346 const escaped_stdout = try escapeHtml(arena, result.stdout);
......@@ -370,7 +373,7 @@ fn printOutput(
370373 }
371374 const result = try process.run(arena, io, .{
372375 .argv = test_args.items,
373 .env_map = &env_map,
376 .env_map = env_map,
374377 .cwd = tmp_dir_path,
375378 .max_output_bytes = max_doc_file_size,
376379 });
......@@ -426,7 +429,7 @@ fn printOutput(
426429
427430 const result = try process.run(arena, io, .{
428431 .argv = test_args.items,
429 .env_map = &env_map,
432 .env_map = env_map,
430433 .cwd = tmp_dir_path,
431434 .max_output_bytes = max_doc_file_size,
432435 });
......@@ -502,7 +505,7 @@ fn printOutput(
502505 if (maybe_error_match) |error_match| {
503506 const result = try process.run(arena, io, .{
504507 .argv = build_args.items,
505 .env_map = &env_map,
508 .env_map = env_map,
506509 .cwd = tmp_dir_path,
507510 .max_output_bytes = max_doc_file_size,
508511 });
......@@ -528,7 +531,7 @@ fn printOutput(
528531 const colored_stderr = try termColor(arena, escaped_stderr);
529532 try shell_out.print("\n{s} ", .{colored_stderr});
530533 } 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", .{});
532535 }
533536 try shell_out.writeAll("\n");
534537 },
......@@ -587,7 +590,7 @@ fn printOutput(
587590 try test_args.append(option);
588591 try shell_out.print("{s} ", .{option});
589592 }
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", .{});
591594 const escaped_stderr = try escapeHtml(arena, result.stderr);
592595 const escaped_stdout = try escapeHtml(arena, result.stdout);
593596 try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
......@@ -1120,7 +1123,7 @@ fn in(slice: []const u8, number: u8) bool {
11201123fn run(
11211124 allocator: Allocator,
11221125 io: Io,
1123 env_map: *process.Environ.Map,
1126 env_map: *const process.Environ.Map,
11241127 cwd: []const u8,
11251128 args: []const []const u8,
11261129) !process.RunResult {
......@@ -1138,6 +1141,11 @@ fn run(
11381141 return error.ChildExitError;
11391142 }
11401143 },
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 },
11411149 else => {
11421150 std.debug.print("{s}\nThe following command crashed:\n", .{result.stderr});
11431151 dumpArgs(args);