-
-
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 VaList API #5103
Add VaList API #5103
Conversation
`aarch64`, `arm` and `freebsd` and `openbsd` are not tested. Because I don't know how to run Crystal on these environment (and I don't have aarch64 and arm machine.)
Wouldn't it be possible to have the |
@Papierkorb It is impossible currently because |
@makenowjust Do you mean if I know a C library using a function pointer to a variadic function, or if I know how to use |
@Papierkorb I said about C lib. |
But as you mentioned, implementing a struct VaList
def next : Void* # Magic
def next_i32 : Int32 # And conversion functions for convenience
next.value.to_i32
end
end However I may be overlooking something. Edit: I don't know of a C library right of the bat. |
@Papierkorb It is not so simple on x86-64 call convention. See https://github.com/MakeNowJust/crystal/blob/13cc38fb2f17b660f1a04333969f78df351f38e5/src/lib_c/x86_64-macosx-darwin/c/stdarg.cr for example. We should use LLVM intrinsics to use |
Absolutely, otherwise this sounds like a nightmare to support for all ABIs. |
I was about to say that this could simplify JNI bindings but no: supporting
Yes. |
And I totally misunderstood, and further don't understand much the need. Do you have examples of C libraries that callback to Crystal with variadic arguments? |
@ysbaddaden For example in stdlib, Line 23 in 2637f83
|
|
||
def self.open | ||
ap = uninitialized LibC::VaList | ||
Intrinsics.va_start pointerof(ap) |
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.
Can you use out ap
here?
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.
No because va_start
needs Void*
but ap
must be VaList
.
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.
I see, makes sense.
src/va_list.cr
Outdated
@@ -0,0 +1,20 @@ | |||
require "c/stdarg" | |||
|
|||
class VaList |
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.
Can this be a struct
?
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.
Thank you.
Do you plan to add |
@bew |
Make the |
Perhaps |
@makenowjust This is excellent, thank you! Do you think you can fix the TODO here? Lines 22 to 34 in 2637f83
That way we can see whether VaList is correctly implemented and useful. I think you can try that out by parsing an XML with errors, the error has a format and arguments that are usable with C's But this is not strictly necessary in this PR, so you can leave it for later, or for someone else to implement it. But without using VaList it's a bit hard to know if it's implemented correctly. |
@asterite I think it is better too. I'll try to fix this. But I repeat:
To fix this is more important, but it is hard for me to prepare such a os/architecture environment to test. Please help me. |
Will it fix/close #214 also? |
No |
Is there anyone else who can review this 😿 |
Just for future reference: https://llvm.org/docs/LangRef.html#va-arg-instruction
|
Now we can compile and run this code:
Added API is
VaList.open
only.VaList.open
needs a block and it passesVaList
pointer to this block. It is combination ofva_start
andva_end
. And this PR doesn't provideva_arg
like method because I think it is not needed for Crystal.NOTE:
aarch64
,arm
andfreebsd
andopenbsd
are not tested. Because I don't know how to run Crystal on these environment (and I don't haveaarch64
andarm
machine.) And specs forVaList
are missing. How do I write this?@ysbaddaden Please help me!