-
Assuming I have a FFI function called in C it is declared as: int complex_args(
MY_STRUCT *self, /* generic pointer, maybe NULL */ /* 1st */
const char *cont_arg, /* const arg */ /* 2nd */
int (*callback)(void*), /* Callback function */ /* 3rd */
void *cb_arg, /* 1st argument to callback */ /* 4th */
int16_t flags, /* Some flags using a fixed integer */ /* 5th */
void **output /* some output */ /* 6th */
); The examples provided in https://docs.exaloop.io/codon/interoperability/cpp and https://docs.exaloop.io/codon/language/ffi are not sufficient. I was writing some code to interface a library and a lot of questions comes to my mind. How can I call this from codon? Some example that comes to my mind: # export to be called back from C code (Is this correct?)
@export
def cb(arg1: cobj) -> Int[32]: # How to define callback parameters / return type?
pass
LIBRARY="/path/to/my/library.so"
# Is this declaration correct?
# There is any type for default C integer (compiler dependent, 64bits GCC it is 4 bytes) ?
from C import LIBRARY.complex_args(cobj, cobj, cobj, cobj, Int[16], cobj) -> Int[32]
# Params
output = None
flags = Int[16]( FLAG_1 | FLAG_2 | FLAG_3 )
C_NULL = Ptr[int]()
my_data = Dict[int, str]()
# How to pass C_NULL (first arg)? Could it be 0x0 ?
# How to pass a constant arg (2nd arg)?
# How to pass callback (3rd arg)?
# How to pass the callback arg (4th arg)?
# How to pass a 16 bit integer (5th arg)? How to correct pass a integer/float/double/long long/unsigned/etc?
# How to pass am output param it (6th arg)? is safe to write on it?
complex_args(C_NULL, "arg2".c_str(), __ptr__(cb), 0x0, flags, __ptr__(my_data), flags, __ptr__(__ptr__(output))) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @iuridiniz -- your code is almost correct; here's what these arguments would be in Codon:
It looks like your Now for your other questions:
Please let me know if this makes sense and if you run into any issues. |
Beta Was this translation helpful? Give feedback.
Hi @iuridiniz -- your code is almost correct; here's what these arguments would be in Codon:
MY_STRUCT *self
: Can just becobj
, which represents a generic (void *
/char *
) pointer in Cconst char *cont_arg
:Ptr[byte]
in Codon orcobj
if you don't care about the pointer's typeint (*callback)(void*)
: This can also becobj
, and you can cast it to the correct type and call it as inFunction[[cobj], i32](callback)(arg)
void *cb_arg
: Againcobj
int16_t
:i16
in Codon (orInt[16]
; it's the same)void **output
:Ptr[cobj]
, or justcobj
if you don't care about the pointer's typeIt looks like your
from C import
is missing the lastcobj
? i.e. there should be 6 arguments but your code has 5.Now f…