-
Notifications
You must be signed in to change notification settings - Fork 43
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
Support variadic functions #497
Comments
Some high level doubts-
|
Yes. One question is whether it's easier to support the literal C strings representing the types or whether we should go for the Dart types representing the C types. E.g.
Yes. Varargs can be structs/unions. They cannot be be int8/int16/float, that is undefined behavior in C.
Yeah, why not.
Probably a warning and not emit it, it's unlikely that you want to call the variadic function without any variadic arguments. (But technically you can, e.g. |
The dart type, since then we can do something like this ffi.Int Which allows the user to specify any type. Although, we may need to remove validations on these types, since we don't know the types in custom_lib, or dart:ffi may add a new type which would require an update to ffigen, or a struct being generated is renamed by user or by us due to collision. (We can still check the library prefix - ffi is predefined and custom_lib must be part of the config's library import) |
We'll also need to handle this, and all the dart:ffi types since they have different C and Dart types (int/Int32) |
But, do we always know how to get back to the C type then? Because we will also need to generate the C type in the signature. (If it turns out to be hard to generate the C type from the Dart type, and the Dart type from the C type, then we could consider writing a C file with the C types and parsing it with libclang. But that would be hard with imported types again.) |
Wouldn't it be enough to handle all the basic C native types which are part of dart:ffi (Int, Int32, Int64, etc) |
Users can use arbitrary types right? AbiSpecificIntegers, Structs, Unions, Pointers to everything. |
Yeah, maybe I'm forgetting something? 🤔 Isn't the C and Dart type same except for the native types. |
We'll be adding support for variadic functions to
dart:ffi
:One interesting thing to think about is how this would work for bindings generation from
.h
files: It wouldn't know what specific var-args signature to use when generating method(s) for a vararg method. Either it could generate a lookup of pointer and require users tofp.asFunction
or one would need to specify in the configuration.Originally posted by @mkustermann in dart-lang/sdk#38578 (comment)
Generating the function pointer would always require a cast, because a
Pointer<NativeFunction<X Function(X, VarArgs(X, X))>>
can't have the right signature. So it should probably be aPointer<Void>
if we go for that option. Which would result in:However, that would not allow us to ever use variadic functions with static linking (
@FfiNative
) through ffigen.Moreover, that requires the user to both write out the C type and Dart type of both the non-variadic arguments and variadic arguments.
So, I believe it would be better to specify the variadic arguments wanted in the config. That way the user only has to write out the C types, and only for the variadic arguments (not for the arguments before the
...
).Also, since Dart doesn't have overloading, we would need to invent names if we want a function more than once. Adding a number is a bad idea, because when adding a variadic args config it might renumber, maybe we can default to appending the variadic argument types? Or default to appending the variadic argument types if there's more than one config?
Related issue:
@mannprerak2 @mkustermann @Sunbreak WDYT? Any alternative ideas?
The text was updated successfully, but these errors were encountered: