Reputation: 889
Is it possible, using ffi, to pass a pointer to the of dart function into a C module (i.e., to the * .so library) and access this dart function directly from the * .so library and use it by means call back?
Upvotes: 3
Views: 2045
Reputation: 625
This is now possible using the NativeCallable class.
Here is an example
Upvotes: 3
Reputation: 889
Thank you very much for the information. This is what I need.
dart:
int myPlus(int a, int b) {
print("dart.myPlus.a=$a,b=$b. $a+$b => ${a + b}");
return a + b;
}
main() {
...
ffi.Pointer<ffi.NativeFunction<NativeIntptrBinOp>>
pointer = ffi.Pointer.fromFunction(myPlus, 0);
print(pointer);
ffi.Pointer<ffi.NativeFunction<NativeApplyTo42And74Type>>
p17 = dylib.lookup("ApplyTo42And74");
ApplyTo42And74Type applyTo42And74 = p17.asFunction();
int result = applyTo42And74(pointer);
print("result => $result");
}
c:
#include <stdio.h>
#include <stdint.h>
typedef intptr_t (*IntptrBinOp)(intptr_t a, intptr_t b);
intptr_t ApplyTo42And74(IntptrBinOp binop) {
printf("ApplyTo42And74()\n");
intptr_t retval = binop(42, 74);
printf("returning %lu\n", retval);
return retval;
}
result:
Pointer<NativeFunction<(IntPtr, IntPtr) => IntPtr>>: address=0x7fb4acd98000
ApplyTo42And74()
dart.myPlus.a=42,b=74. 42+74 => 116
returning 116
result => 116
Upvotes: 6