Commit acf2e8fe64

Andrew Kelley <andrew@ziglang.org>
2021-07-01 03:39:04
fix start code for WebAssembly
1 parent d606811
Changed files (3)
lib
test
lib/std/start.zig
@@ -65,9 +65,16 @@ comptime {
                 }
             } else if (native_os == .uefi) {
                 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
-            } else if (native_arch.isWasm()) {
-                const wasm_start_sym = if (builtin.wasi_exec_model == .reactor) "_initialize" else "_start";
-                if (!@hasDecl(root, wasm_start_sym)) @export(wasm_start, .{ .name = wasm_start_sym });
+            } else if (native_os == .wasi) {
+                const wasm_start_sym = switch (builtin.wasi_exec_model) {
+                    .reactor => "_initialize",
+                    .command => "_start",
+                };
+                if (!@hasDecl(root, wasm_start_sym)) {
+                    @export(wasi_start, .{ .name = wasm_start_sym });
+                }
+            } else if (native_arch.isWasm() and native_os == .freestanding) {
+                if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
             } else if (native_os != .other and native_os != .freestanding) {
                 if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
             }
@@ -136,20 +143,18 @@ fn _DllMainCRTStartup(
     return std.os.windows.TRUE;
 }
 
-fn wasm_start() callconv(.C) void {
-    // The entrypoint is marked inline because for some reason LLVM in release mode fails to inline it,
-    // and we want fewer call frames in stack traces.
-    switch (native_os) {
-        .freestanding => {
-            _ = @call(.{ .modifier = .always_inline }, callMain, .{});
-        },
-        .wasi => {
-            switch (builtin.wasi_exec_model) {
-                .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
-                .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
-            }
-        },
-        else => @compileError("unsupported OS"),
+fn wasm_freestanding_start() callconv(.C) void {
+    // This is marked inline because for some reason LLVM in
+    // release mode fails to inline it, and we want fewer call frames in stack traces.
+    _ = @call(.{ .modifier = .always_inline }, callMain, .{});
+}
+
+fn wasi_start() callconv(.C) void {
+    // The function call is marked inline because for some reason LLVM in
+    // release mode fails to inline it, and we want fewer call frames in stack traces.
+    switch (builtin.wasi_exec_model) {
+        .reactor => _ = @call(.{ .modifier = .always_inline }, callMain, .{}),
+        .command => std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{})),
     }
 }
 
test/stage2/darwin.zig
@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
         {
             var case = ctx.exe("hello world with updates", target);
             case.addError("", &[_][]const u8{
-                ":86:9: error: struct 'test_case.test_case' has no member named 'main'",
+                ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
             });
 
             // Incorrect return type
test/stage2/test.zig
@@ -24,7 +24,7 @@ pub fn addCases(ctx: *TestContext) !void {
         var case = ctx.exe("hello world with updates", linux_x64);
 
         case.addError("", &[_][]const u8{
-            ":86:9: error: struct 'test_case.test_case' has no member named 'main'",
+            ":93:9: error: struct 'test_case.test_case' has no member named 'main'",
         });
 
         // Incorrect return type