Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

mJS, FFI, how to pass array of double values

Is it possible to pass an array of doubles through the mJS FFI layer to a C-function that takes an a array of doubles as argument?

C-function signature:

double *calc_something(double *vals, int size);

in mJS api_*.js would like to do something like:

let ParentObj = {
  ...
  calc_some:  ffi('void *calc_something(void *, int)'),
  ...
  calcSomething: function(data) {
    let rslts = this.calc_some(data, data.length);
    return { "rslt0":rslts[0], "rslt1":rslts[1] };
  }

};

in client code (init.js)

    let data = [5.51, 8.43, 6.77, ......];
    let calcs = ParentObj.calcSomething(data);
    print('data:  ',JSON.stringify(data));
    print('calcs:  ', JSON.stringify(calcs));

My attempts to do this yield

MJS error: failed to call FFIed function: actual arg #0 is not a ptr

Comments

  • No, at the moment this is not supported.

  • pedalPusher68pedalPusher68 Boston
    edited January 11

    ok - after asking that, I realized it's probably hard to achieve coming from the JS side of the code (loose typing, etc.). Instead, I came up with the approach of creating a struct as an intermediate object that contains an array of double(s), etc. and use the 'opaque pointer reference' approach to operate on its contents with the C side of the code; i.e. hang on to a ref. to the pointer on the JS side and pass it to functions in the C layer.

    Thanks.

Sign In or Register to comment.