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:
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