-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Crystal.main #4998
Merged
Merged
Add Crystal.main #4998
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
lib LibCrystalMain | ||
@[Raises] | ||
fun __crystal_main(argc : Int32, argv : UInt8**) | ||
end | ||
|
||
# :nodoc: | ||
def _crystal_main(argc : Int32, argv : UInt8**) | ||
# TODO: remove this method and embed this inside | ||
# Crystal.main. A bug in Crystal 0.23.1 prevents invoking | ||
# __crystal_main from anywhere except the top level. | ||
LibCrystalMain.__crystal_main(argc, argv) | ||
end | ||
|
||
module Crystal | ||
# Defines the main routine run by normal Crystal programs: | ||
# | ||
# - Initializes the GC | ||
# - Invokes the given *block* | ||
# - Handles unhandled exceptions | ||
# - Invokes `at_exit` handlers | ||
# - Flushes `STDOUT` and `STDERR` | ||
# | ||
# This method can be invoked if you need to define a custom | ||
# main (as in C main) function, doing all the above steps. | ||
# | ||
# For example: | ||
# | ||
# ``` | ||
# fun main(argc : Int32, argv : UInt8**) : Int32 | ||
# Crystal.main do | ||
# time = Time.now | ||
# Crystal.main_user_code(argc, argv) | ||
# puts "Time to execute program: #{Time.now - time}" | ||
# end | ||
# end | ||
# ``` | ||
# | ||
# Note that the above is really just an example, almost the | ||
# same can be accomplished with `at_exit`. But in some cases | ||
# redefinition of C's main is needed. | ||
def self.main(&block) | ||
GC.init | ||
|
||
status = | ||
begin | ||
yield | ||
0 | ||
rescue ex | ||
1 | ||
end | ||
|
||
AtExitHandlers.run status | ||
ex.inspect_with_backtrace STDERR if ex | ||
STDOUT.flush | ||
STDERR.flush | ||
status | ||
end | ||
|
||
# Main method run by all Crystal programs at startup. | ||
# | ||
# This setups up the GC, invokes your program, rescuing | ||
# any handled exception, and then runs `at_exit` handlers. | ||
# | ||
# This method is automatically invoked for you, so you | ||
# don't need to invoke it. | ||
# | ||
# However, if you need to define a special main C function, | ||
# you can redefine main and invoke `Crystal.main` from it: | ||
# | ||
# ``` | ||
# fun main(argc : Int32, argv : UInt8**) : Int32 | ||
# # some setup before Crystal main | ||
# Crystal.main(argc, argv) | ||
# # some cleanup logic after Crystal main | ||
# end | ||
# ``` | ||
# | ||
# The `Crystal.main` can also be passed as a callback: | ||
# | ||
# ``` | ||
# fun main(argc : Int32, argv : UInt8**) : Int32 | ||
# LibFoo.init_foo_and_invoke_main(argc, argv, ->Crystal.main) | ||
# end | ||
# ``` | ||
# | ||
# Note that before `Crystal.main` is invoked the GC | ||
# is not setup yet, so nothing that allocates memory | ||
# in Crystal (like `new` for classes) can be used. | ||
def self.main(argc : Int32, argv : UInt8**) | ||
main do | ||
main_user_code(argc, argv) | ||
end | ||
end | ||
|
||
# Executes the main user code. This normally is executed | ||
# after initializing the GC and before executing `at_exit` handlers. | ||
# | ||
# You should never invoke this method unless you need to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to raise early if this is executed twice? Seems like a bit of an edge case but might be worth protecting. This could be another PR though. |
||
# redefine C's main function. See `Crystal.main` for | ||
# more details. | ||
def self.main_user_code(argc : Int32, argv : UInt8**) | ||
_crystal_main(argc, argv) | ||
end | ||
end | ||
|
||
# Main function that acts as C's main function. | ||
# Invokes `Crystal.main`. | ||
# | ||
# Can be redefined. See `Crystal.main` for examples. | ||
fun main(argc : Int32, argv : UInt8**) : Int32 | ||
Crystal.main(argc, argv) | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
->Crystal.main
be->Crystal.main(Int32, UInt8**)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a lib call the compiler figures out the argument types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know that, cushtie!