Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit exit from main #15299

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/crystal/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ end

{% if flag?(:win32) %}
require "./system/win32/wmain"
{% end %}

{% if flag?(:wasi) %}
{% elsif flag?(:wasi) %}
require "./system/wasi/main"
{% else %}
require "./system/unix/main"
{% end %}
11 changes: 11 additions & 0 deletions src/crystal/system/unix/main.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "c/stdlib"

# Prefer explicit exit over returning the status, so we are free to resume the
# main thread's fiber on any thread, without occuring a weird behavior where
# another thread returns from main when the caller might expect the main thread
# to be the one returning.

fun main(argc : Int32, argv : UInt8**) : Int32
status = Crystal.main(argc, argv)
LibC.exit(status)
end
3 changes: 2 additions & 1 deletion src/crystal/system/wasi/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fun _start
LibWasi.proc_exit(status) if status != 0
end

# `__main_argc_argv` is called by wasi-libc's `__main_void` with the program arguments.
# `__main_argc_argv` is called by wasi-libc's `__main_void` with the program
# arguments.
fun __main_argc_argv(argc : Int32, argv : UInt8**) : Int32
main(argc, argv)
end
22 changes: 11 additions & 11 deletions src/crystal/system/win32/wmain.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ require "c/stdlib"
lib LibCrystalMain
end

# The actual entry point for Windows executables. This is necessary because
# *argv* (and Win32's `GetCommandLineA`) mistranslate non-ASCII characters to
# Windows-1252, so `PROGRAM_NAME` and `ARGV` would be garbled; to avoid that, we
# use this Windows-exclusive entry point which contains the correctly encoded
# UTF-16 *argv*, convert it to UTF-8, and then forward it to the original
# `main`.
# The actual entry point for Windows executables.
#
# The different main functions in `src/crystal/main.cr` need not be aware that
# such an alternate entry point exists, nor that the original command line was
# not UTF-8. Thus all other aspects of program initialization still occur there,
# and uses of those main functions continue to work across platforms.
# This is necessary because *argv* (and Win32's `GetCommandLineA`) mistranslate
# non-ASCII characters to Windows-1252, so `PROGRAM_NAME` and `ARGV` would be
# garbled; to avoid that, we use this Windows-exclusive entry point which
# contains the correctly encoded UTF-16 *argv*, convert it to UTF-8, and then
# forward it to the original `main`.
#
# NOTE: we cannot use anything from the standard library here, including the GC.
fun wmain(argc : Int32, argv : UInt16**) : Int32
Expand All @@ -46,5 +42,9 @@ fun wmain(argc : Int32, argv : UInt16**) : Int32
end
LibC.free(utf8_argv)

status
# prefer explicit exit over returning the status, so we are free to resume the
# main thread's fiber on any thread, without occuring a weird behavior where
# another thread returns from main when the caller might expect the main
# thread to be the one returning.
LibC.exit(status)
end
Loading