Search code examples
javascriptcnode.jsnode-ffi

node ffi struct variable array


I'm using node-ffi and I have a structure that needs to be passed. A few of the variables in the structure are arrays. How do I define this in the JavaScript wrapper?

C header:

typedef struct
{
    unsigned char myUChar;
    float fmyFloat;
    float arrayFloat[ARRAY_SIZE];
} sMyStruct;

JavaScript wrapper:

var ffi = require('ffi');
var Struct require('ref-struct');

var sMyStruct = Struct({
    'myUChar': 'uchar',
    'fmyFloat': 'float',
    'arrayFloat[ARRAY_SIZE]': 'float'  // Would this work??
});

Is there a tutorial which indicates all of the variables that can be passed into node-ffi anyone can point me to? (For example can I actually pass 'uchar'?)


Solution

  • Turns out the correct way to declare the array in a struct is actually now:

    var Struct require('ref-struct');
    var ArrayType require('ref-array');
    
    var sMyStruct = Struct({
        'arrayFloat': ArrayType('float', 10) // array of floats, length 10
    });
    

    I still need to test this a bit more thoroughly, and as far as a list of types goes I found a decent one here: https://www.npmjs.com/package/node-ffi