Array of structure as OUT parameter from C to JS using node-ffi, ref-array and ref-struct

586 Views Asked by At

I am using node-ffi for integration of JS with C library. I figured out ways to pass in complex structures as IN params and get single structure as OUT param. But, I could not successfully get an array of structures from C and iterate over them in JS. I have the following C Structure and API.

typedef struct _st {
uint32_t index;
uint8_t size;
uint8_t* data;
} ST;

int getData(uint8_t len, Input* arrInputs, ST* structArray) ; // Fills structArray with dynamic # of struct objects (calloc'ed on heap)

I have emulated it on the JS side and invoking the C API as follows

var ST = StructType({
index : 'uint32',
size : 'uint8',
data : 'string'
})
var stArray = ArrayType(ST);

var Clib = ffi.Library('./CLib1', {
'getData':[ 'int', ['uint8', InputArray, stArray] ]
});
var arrData = ref.alloc(ST)
var res = Clib.getData(3, arrInputs, arrData);

I could print and check that the values are filled properly within the out parameter inside C. But I am not able to get the values printed on the JS side. It either fails with Seg fault or undefined.

Any suggestions would be of great help!

0

There are 0 best solutions below