Problem with mjs ffi parse error

If you are asking a question, please follow this template:

  1. My goal is: [describe your goal]
    to link a C function to MJS

  2. My actions are: [describe your actions - code, commands, etc]
    let rxAcc = ‘’; // Accumulated Rx data
    let start;
    let finish;
    let getMsg=ffi(‘int get_msg(char *,int,int *,int *)’);
    let ret=getMsg(rxAcc, rxAcc.length, &start, &finish);

C routine: int get_msg(char *buffer, int len, int *start, int *finish)

  1. The result I see is: [show the result - log, etc]
    MJS error: parse error at line 92: [&start, &f]

  2. My expectation & question is: [describe your expectation and your question]
    getting the values for ret, start and finish returned to the MJS code

C/C++ interoperability

With the current C function signature (int get_msg(char *buffer, int len, int *start, int *finish)) you have to ffi it like this

let getMsg = ffi('int get_msg(char *, int, void *, void *)');

In the mJS code use Sys.malloc to allocate start and finish, pass the pointers to your function,
create a Dataview for each pointer, get the value and finally the pointers.

It’s possible to use another approach if you replace the start and finish parameters with a pointer to a structure. In this case Converting structs to objects is to be used.

1 Like

excuse my ignorance, but how do I go about “create a Dataview for each pointer”?

I see this ‘assuming’ in the MJS document [],
how do I get it into my MJS code assuming it exists in my C code?

// Assuming s is a foreign pointer to an instance of my_struct, obtained elsewhere.
let sd = ffi(‘void *get_my_struct_descr(void)’)();
let o = s2o(s, sd);
print(o.a, o.b);

Create and free the structure in C and ffi them.

struct data *create_data(){
void free_data(struct data *data){
1 Like

I want to do the same.
but instead of using the Sys.malloc, can I simply declare a fix var buffer in JS and pass it the C function ?