I have a C API which takes a callback and examines the integer return value of that callback to decide what to do: a return value of 0 means stop doing stuff, any other value means continue to block and process stuff. I have exposed this callback to mJS but my problem is that the integer return value seen by the C code is always coming out as 1. What am I doing wrong? How can I get the correct integer return value in my C code?
Here’s how I am coding it:
// The callback is of this form
int cb(void *, void *);
int (*pgStoredCb)(void *, void *) = NULL;
void *pgStoredCbParams = NULL
void *pgStoredCbUserData = NULL
// Set up the callback
void setCb(int(*pCb) (void *, void *), void *pCbParams, void *pCbUserData) {
pgStoredCb = pCb;
pgStoredCbParams = pCbParams;
pgStoredCbUserData = pCbUserData;
}
// Use the callback
void eventHasOccurred{} {
if (pgStoredCb != NULL) {
if (pgStoredCb(pgStoredCbParams, pgStoredCbUserData)) {
printf("Callback returned true.\n");
}
}
}
…and in mJS it is used like this:
let cb = function(pParam) {
print("Callback has been called with \"", pParam, "\" and is returning 0.\n");
return 0;
};
let cbParam = "Hello World!";
let cbSet = ffi('void setCb(int(*) (void *, userdata), void *pCbParams, userdata');
cbSet(cb, cbParam, null);