Skip to content

How to handle variable number of arguments? #238

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

Closed
arcticfox1919 opened this issue May 31, 2022 · 5 comments
Closed

How to handle variable number of arguments? #238

arcticfox1919 opened this issue May 31, 2022 · 5 comments
Labels
package:ffigen waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi

Comments

@arcticfox1919
Copy link

The C library function I want to bind has some variable number of parameters, ffigen doesn't seem to handle this situation very well, is there any way?

for example:

int func(int, ... ) {}
@dcharkes dcharkes added the waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi label May 31, 2022
@dcharkes
Copy link
Collaborator

This is not yet supported in dart:ffi dart-lang/sdk#38578, so package:ffigen cannot generate something to support this yet.

@kingwill101
Copy link

@dcharkes any update on this, now that dart-lang/sdk#38578 has been completed?

@kingwill101
Copy link

I see that 79e845f was done but it seems to not work.

int fcntl (int __fd, int __cmd, ...);
  int fcntl(
    int __fd,
    int __cmd,
  ) {
    return _fcntl(
      __fd,
      __cmd,
    );
  }


  late final _fcntlPtr =
      _lookup<ffi.NativeFunction<ffi.Int Function(ffi.Int, ffi.Int)>>('fcntl');
  late final _fcntl = _fcntlPtr.asFunction<int Function(int, int)>();

@dcharkes
Copy link
Collaborator

If you write bindings yourself, you need to use VarArgs somewhere: https://api.dart.dev/stable/3.4.4/dart-ffi/VarArgs-class.html

If you want FFIgen to generate bindings, you need to configure FFIgen to do that.

functions:
  variadic-arguments:
    myfunc:
      // Native C types are supported
      - [int, unsigned char, long*, float**]
      // Common C typedefs (stddef.h) are supported too
      - [uint8_t, intptr_t, size_t, wchar_t*]
      // Structs/Unions/Typedefs from generated code or a library import can be referred too.
      - [MyStruct*, my_custom_lib.CustomUnion]

Since the feature is implemented, and was tracked by #497, I'll close this as duplicate issue.

Please file a new issue if you're having issues with VarArgs or FFIgen variadic-arguments.

@kingwill101
Copy link

kingwill101 commented Oct 27, 2024

@dcharkes is there a way to do variadic arguments that are functions? trying to generate libcurl bindings

CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);

and a version of the function accepts a

static size_t curl_callback(char *data, size_t size, size_t nmemb, void *clientp)
 functions:
    variadic-arguments:
      curl_easy_setopt:
        - [ 'long' ]
        - [ 'char*' ]
        - [ 'curl_off_t' ]
        - [ 'size_t curl_callback(char *data, size_t size, size_t nmemb, void *clientp)' ] <- this is what i want to somehow represent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package:ffigen waiting-on-dart-ffi Issues which are blocked by lacking suppport in dart:ffi
Projects
None yet
Development

No branches or pull requests

4 participants