Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart/Flutter ffi (Foreign Function Interface) native callbacks eg: sqlite3_exec

Hello I am using dart:ffi to build an interface with my native c/c++ library. and I needed a way to get a callback from c to dart as an example in sqlite:

int sqlite3_exec(
    sqlite3*,                                  /* An open database */
    const char *sql,                           /* SQL to be evaluated */
    int (*callback)(void*,int,char**,char**),  /* Callback function */
    void *,                                    /* 1st argument to callback */
    char **errmsg                              /* Error msg written here */
);

the third parameter in sqlite3_exec is function pointer to a callback. so if I called this function in dart using ffi I need to pass a function pointer: and in dart:ffi Pointer class there is a function named fromFunction witch accepts a dart static function and an exceptionalReturn; but just by calling this function to get the function pointer of a dart managed function: a (sigterm) is raised and the dart code no long work in the process.

So My Question: Is there any way to get a native callback in dart, as in Python, c#, ..

Extra: Is there any way to include dartino in a flutter project, since this ForeignDartFunction covers what I need.

like image 502
Ala'a Al Hallaq Avatar asked May 01 '20 11:05

Ala'a Al Hallaq


1 Answers

I got an example to work. Hopefully you can adapt this to your case.

Example C function

EXTERNC int32_t foo(
                    int32_t bar,
                    int32_t (*callback)(void*, int32_t)
                    ) {
    return callback(nullptr, bar);
}

Dart code

First the typedefs. We need two for the native function foo and one for the Dart callback.

typedef example_foo = Int32 Function(
    Int32 bar, Pointer<NativeFunction<example_callback>>);
typedef ExampleFoo = int Function(
    int bar, Pointer<NativeFunction<example_callback>>);

typedef example_callback = Int32 Function(Pointer<Void>, Int32);

and the code for the callback

  static int callback(Pointer<Void> ptr, int i) {
    print('in callback i=$i');
    return i + 1;
  }

and the lookup

  ExampleFoo nativeFoo =
    nativeLib.lookup<NativeFunction<example_foo>>('foo').asFunction();

and, finally, use it like this:

  int foo(int i) {
    return nativeFoo(
      i,
      Pointer.fromFunction<example_callback>(callback, except),
    );
  }

as expected, foo(123) prints flutter: in callback i=123 and returns 124

like image 119
Richard Heap Avatar answered Nov 07 '22 10:11

Richard Heap