2

I am using Node-ffi to write a Node bindings for MITIE. But I got problem,

The argument of a function is char**: An array of NULL terminated C strings, like this:

int run (char** tokens)
{
    try
    {
        std::vector<std::string> words;
        for (unsigned long i = 0; tokens[i]; ++i)
            words.push_back(tokens[i]);

        return 1;
    }
    catch(...)
    {
        return 0;
    }
}

And this is what I did use ffi:

const ffi = require('ffi');
const ArrayType = require('ref-array');

const StringArray = ArrayType('string')

const test = ffi.Library('test', {
  'run': [ 'int', [StringArray] ]
});

test.run(['a', 'b']);

But I got: Segmentation fault: 11.

I uploaded the sample code to this repo.

And in this repo you also can see I have wrote a Python bindings by ctypes, It runs well.

Here is my operating environment:

1 Answer 1

2

You have to explicitly terminate token array with NULL:

const ffi = require('ffi');
const ArrayType = require('ref-array');
const ref = require('ref');

const StringArray = ArrayType('string')

const test = ffi.Library('test', {
  'run': [ 'int', [StringArray] ]
});

console.log(test.run(['a', 'b', ref.NULL])); // -> 2
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! It's helpful!But there was a lot of work I needed to do, so I decided to use Node C++ Addon to write a bindings wrap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.