authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-30 18:39:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-30 18:39:04-07:00
logacf2e8fe6484a48cef76c20368ff06fe9d7b264e
treeb1deefdf85b727308494b86bf9165f8cca630328
parentd606811f555e6906d1f8189dd2bf123cf98faf91

fix start code for WebAssembly


3 files changed, 24 insertions(+), 19 deletions(-)

lib/std/start.zig+22-17
......@@ -65,9 +65,16 @@ comptime {
6565 }
6666 } else if (native_os == .uefi) {
6767 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
68 } else if (native_arch.isWasm()) {
69 const wasm_start_sym = if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start";
70 if (!@hasDecl(root, wasm_start_sym)) @export(wasm_start, .{ .name = wasm_start_sym });
68 } else if (native_os == .wasi) {
69 const wasm_start_sym = switch (builtin.wasi_exec_model) {
70 .reactor => "_initialize",
71 .command => "_start",
72 };
73 if (!@hasDecl(root, wasm_start_sym)) {
74 @export(wasi_start, .{ .name = wasm_start_sym });
75 }
76 } else if (native_arch.isWasm() and native_os == .freestanding) {
77 if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
7178 } else if (native_os != .other and native_os != .freestanding) {
7279 if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
7380 }
......@@ -136,20 +143,18 @@ fn _DllMainCRTStartup(
136143 return std.os.windows.TRUE;
137144}
138145
139fn wasm_start() callconv(.C) void {
140 // The entrypoint is marked inline because for some reason LLVM in release mode fails to inline it,
141 // and we want fewer call frames in stack traces.
142 switch (native_os) {
143 .freestanding => {
144 _ = @call(.{ .modifier = .always_inline }, callMain, .{});
145 },
146 .wasi => {
147 switch (builtin.wasi_exec_model) {
148 .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
149 .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
150 }
151 },
152 else => @compileError("unsupported OS"),
146fn wasm_freestanding_start() callconv(.C) void {
147 // This is marked inline because for some reason LLVM in
148 // release mode fails to inline it, and we want fewer call frames in stack traces.
149 _ = @call(.{ .modifier = .always_inline }, callMain, .{});
150}
151
152fn wasi_start() callconv(.C) void {
153 // The function call is marked inline because for some reason LLVM in
154 // release mode fails to inline it, and we want fewer call frames in stack traces.
155 switch (builtin.wasi_exec_model) {
156 .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
157 .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
153158 }
154159}
155160
test/stage2/darwin.zig+1-1
......@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
1414 {
1515 var case = ctx.exe("hello world with updates", target);
1616 case.addError("", &[_][]const u8{
17 ":86:9: error: struct 'test_case.test_case' has no member named 'main'",
17 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
1818 });
1919
2020 // Incorrect return type
test/stage2/test.zig+1-1
......@@ -24,7 +24,7 @@ pub fn addCases(ctx: *TestContext) !void {
2424 var case = ctx.exe("hello world with updates", linux_x64);
2525
2626 case.addError("", &[_][]const u8{
27 ":86:9: error: struct 'test_case.test_case' has no member named 'main'",
27 ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
2828 });
2929
3030 // Incorrect return type