I have a set of C functions which take a pointer to a buffer and a size for that buffer, e.g:
int getString1(char *pBuffer, int size);
The functions all return an integer which indicates success (== 0
) or error (!= 0
) and place a standard C string into pBuffer
, making sure it is terminated within size
bytes with a null
byte.
I would like to map these into JS functions which return an object containing error
(an integer) and value
(a JS string), something like:
function getGenericString(fnName) {
// Create buffer here
let cCall = ffi(fnName);
return {
error: // something like cCall(buffer, lengthOfBuffer),
value: // convert buffer into JS string somehow here
};
}
In mJS, how do I create the necessary fixed-length buffer to pass into the C function and what mJS function do I use to turn the null-terminated string that buffer contains into a true JS string?