Mqx
Mqx3d ago

FFI callback with multiple parameters

Hey could someone please help me with this interface definition:
MODULE_API auto getDevices(
void (*callback_fn)(
const char *name,
const char *path,
const char *manufacturer,
const char *serial_number
),
ErrorCallbackT error_callback = nullptr
) -> int;
const getDevices : Deno.ForeignFunction = {
parameters: [
'function',
'function'
],
result: 'i32',
nonblocking: true
}
MODULE_API auto getDevices(
void (*callback_fn)(
const char *name,
const char *path,
const char *manufacturer,
const char *serial_number
),
ErrorCallbackT error_callback = nullptr
) -> int;
const getDevices : Deno.ForeignFunction = {
parameters: [
'function',
'function'
],
result: 'i32',
nonblocking: true
}
const {symbols} = Deno.dlopen('', {
getDevices
})

await symbols.getDevices(
(
name,
path,
manufacturer,
serial_number
) => {
devices.push({
name,
path,
manufacturer,
serial_number
});
},
(error) => {console.log(error);}
)

console.log(devices);
const {symbols} = Deno.dlopen('', {
getDevices
})

await symbols.getDevices(
(
name,
path,
manufacturer,
serial_number
) => {
devices.push({
name,
path,
manufacturer,
serial_number
});
},
(error) => {console.log(error);}
)

console.log(devices);
I would like to know on how to properly define my FFI interface for that kind of C++ function. I want to understand how this works correctly, because I think my current approach is completely wrong 😅 I want to transmit kind of an „object“ from C++ to TypeScript. And I read somewhere that this is the preferred method, using a callback function. Thanks 😊
2 Replies
AapoAlas
AapoAlas2d ago
Right, you'll need to use the Deno.UnsafeCallback API to create the callbacks
Mqx
MqxOP2d ago
👍🏻 will try. Thanks

Did you find this page helpful?