2

I am trying to call CPP function from NodeJS

typedef struct foo {

    uint8_t     *data;
    int         dataSize;

} foo ;

foo *ExtractBar(const int16_t *bar);

I have tried nbind library in NodeJS. but getting unbound type error.

uint16 = Int16Array.from('12345');
uint16.fill(0);
lib.ExtractBar(uint16);

I have also tried ffi-napi npm module. but from that I was getting segmentation fault error. can anyone help me please on how to pass correct argument from NodeJS code. I am not sure how to create similar structure in NodeJS-

const int16_t *bar
Mohit Sahu
  • 177
  • 2
  • 17

1 Answers1

1

Usually you have to have a reference to a variable as JavaScript is a garbage collected language.

Specifically to nbind, have a look at this and this. You want to pass from NodeJS an buffer of uint16 so you could use nbind::Buffer.

siniarskimar
  • 66
  • 1
  • 3
  • Hi, I tried nbind::Buffer --- int length = buf.length(); unsigned char *data = buf.data(); const int16_t *p = (int16_t *)data; lib.ExtractBar(p); but not it is giving me segmentation fault error. – Mohit Sahu Jun 06 '21 at 18:58