diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-10-31 11:41:39 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-10-31 11:41:39 -0400 |
| commit | d3d3e4e374e47b275dd3e0483634852b2d0a56d8 (patch) | |
| tree | 31336c781d33854a0734006e9b0076d625886f31 /lib/std/special/start.zig | |
| parent | 788848e123c056b6204f7555a0118377aa4bd8e1 (diff) | |
| download | zig-d3d3e4e374e47b275dd3e0483634852b2d0a56d8.tar.gz zig-d3d3e4e374e47b275dd3e0483634852b2d0a56d8.zip | |
startup code sets up event loop if I/O mode is declared evented
Diffstat (limited to 'lib/std/special/start.zig')
| -rw-r--r-- | lib/std/special/start.zig | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/lib/std/special/start.zig b/lib/std/special/start.zig index bb28646fd3..cdf09606a9 100644 --- a/lib/std/special/start.zig +++ b/lib/std/special/start.zig @@ -35,7 +35,9 @@ comptime { } extern fn wasm_freestanding_start() void { - _ = callMain(); + // 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. + _ = @inlineCall(callMain); } extern fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) usize { @@ -63,7 +65,9 @@ extern fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) u nakedcc fn _start() noreturn { if (builtin.os == builtin.Os.wasi) { - std.os.wasi.proc_exit(callMain()); + // 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. + std.os.wasi.proc_exit(@inlineCall(callMain)); } switch (builtin.arch) { @@ -110,7 +114,7 @@ extern fn WinMainCRTStartup() noreturn { std.debug.maybeEnableSegfaultHandler(); - std.os.windows.kernel32.ExitProcess(callMain()); + std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain()); } // TODO https://github.com/ziglang/zig/issues/265 @@ -170,7 +174,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { std.debug.maybeEnableSegfaultHandler(); - return callMain(); + return initEventLoopAndCallMain(); } extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 { @@ -185,7 +189,32 @@ const bad_main_ret = "expected return type of main to be 'void', '!void', 'noret // 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. -inline fn callMain() u8 { +inline fn initEventLoopAndCallMain() u8 { + if (std.event.Loop.instance) |loop| { + loop.init() catch |err| { + std.debug.warn("error: {}\n", @errorName(err)); + if (@errorReturnTrace()) |trace| { + std.debug.dumpStackTrace(trace.*); + } + return 1; + }; + defer loop.deinit(); + + var result: u8 = undefined; + var frame: @Frame(callMain) = undefined; + _ = @asyncCall(&frame, &result, callMain); + loop.run(); + return result; + } else { + // 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. + return @inlineCall(callMain); + } +} + +// This is not marked inline because it is called with @asyncCall when +// there is an event loop. +fn callMain() u8 { switch (@typeInfo(@typeOf(root.main).ReturnType)) { .NoReturn => { root.main(); |
