-
-
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
Expose alt_name for redefine_main #4798
Conversation
Previously, Crystal owned the main function, then called Allegro's al_run_main with a function pointer to a Crystal method, but this would cause segfaults on Fiber switches. Now, we make Allegro own the main function, and pass Crystal's main function to it instead, which works as expected. We are keeping a slightly modified copy of redefine_main until crystal-lang/crystal#4798 is merged.
I'm actually a little confused here, what are the two names for in a fun with body and why do you need both? |
I think the issue is that Maybe there's a more elegant way to solve this? |
I think the solution is pretty elegant, but we'd need at least some basic documentation for this, otherwise this will be an obscure feature and no one will know about it. |
The use case is pretty obscure already. Allegro requires main to invoke their main, so main needs to be redefined. We can document it, but very few bindings will actually need this. 99.999% of the apps don't need to redefine main. |
I beg to differ here. If the feature is worth adding, it is worth documenting. The fact that it is obscure and rarely used actually is an argument in favor of documenting: I can get away with not documenting how IMO, we've got two options: either we accept it with documentation or we reject it. To be clear, by documentation I mean simply something along the lines of what @Fryguy put in the description of this PR, with a little more context, in the autogenerated docs of |
Sounds good. @Fryguy Care to add some doc comments? |
Actually, I'm thinking this has a much simpler solution. Right now fun main = main(...)
end The name in the left side is the name used to invoke that function from Crystal. The name on the right is is the name to generate for C. But, the name on the left side is never invoked in Crystal code. So we can always call it In fact, |
Fixed by #4998. |
This changes
redefine_main
to expose the alternate name as a parameter. The purpose of this is to allow an author to create the crystal main function without literally naming itmain
.In my case, as described in #4793, I am writing a binding to Allegro. Allegro requires that you pass your "main" function to theirs as a callback. So, I need to pass Crystal's main function to Allegro's main function handler, which means I need to get a reference to it. With the change in this PR, I can avoid copy-pasting all of redefine_main just to avoid the
fun main =
part.I had debated renaming
redefine_main
todefine_main
, and writing a separateredefine_main
that would calldefine_main
, but I couldn't seem to embed one macro in another without getting aunterminated macro
error.Closes #4793