I have a C function which takes a callback and which I need to access from JS. Because of the restrictions in the number of parameters that can be passed from JS to C I need an intermediate local callback inside my JS API to do some processing on the parameters before the “user” callback is actually called. I am using the Revealing Module pattern to hide this local callback so that it does not appear in the API (as that would be confusing for the user). However, I cannot get the privileged (this.
) bit of the construction to work, it gives me a parser error. Below is my code, where the place that the parser error occurs is indicated in a comment.
Can anyone tell me what I’m doing wrong?
let test = (function () {
let publicApi = {};
let userCallback = null;
let localCallback = function(parameter) {
if (userCallback !== null) {
// Some local processing on parameter goes here
userCallback(parameter);
userCallback = null;
}
};
publicApi.start = function (callback) {
this.userCallback = callback;
// Parser error occurs on the line below, where argument 0 (this.localCallback) results in the
// error "MJS Print: actual arg #0 is not a function, but undefined"
ffi(void myCFunction(void (*) (void *, userdata), userdata)')(this.localCallback, null);
};
return publicApi;
})();
let myCallback = function(parameter) {
print("myCallback() has been called with parameter ", parameter, ".\n");
};
let testObject = test;
testObject.start(myCallback);