TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

buffer dereference issue in node.js using ffi & ref #48

Closed tonyguo1987 closed 8 years ago

tonyguo1987 commented 8 years ago

I have an issue in dereferencing a buffer using ffi and ref in Node.js. I created javascript wrapper using ffi to call dll functions. What I am doing below is passing a different types of pointer to pointer to dll, allocate memory in dll, and assign values to these memory. Then read these values back in javascript.

Here is my C++ functions:

#ifdef __cplusplus
extern "C" {
#endif
_declspec(dllexport) void testFloatPtr(float *resultPtr){
    resultPtr[0] = 1.5;
}

_declspec(dllexport) void testFloatPtrPtr(float **resultPtrPtr){
    float *resultPtr = *resultPtrPtr = new float[3];
    resultPtr[0] = 1.1;
    resultPtr[1] = 10.1;
    resultPtr[2] = 100.1;
}

_declspec(dllexport) void testIntPtrPtr(int **resultPtrPtr){
    int *resultPtr = *resultPtrPtr = new int[3];
    resultPtr[0] = 2;
    resultPtr[1] = 3;
    resultPtr[2] = 4;
}

_declspec(dllexport) void testCharPtrPtr(char **resultPtrPtr){
    char *resultPtr = *resultPtrPtr = new char[3];
    resultPtr[0] = 5;
    resultPtr[1] = 6;
    resultPtr[2] = 7;
}
#ifdef __cplusplus
}
#endif

Here is my javacript application:

var ref = require('ref');
var ffi = require('ffi');

var mylib = ffi.Library('debug', {
    'testFloatPtr' : ['void', ['float*']],
    'testFloatPtrPtr':['void', ['float**']],
    'testIntPtrPtr' : ['void', ['int**']],
    'testCharPtrPtr': ['void', ['char**']]
});

var ptr1 = ref.alloc('float');
mylib.testFloatPtr(ptr1);
var result1 = ref.deref(ptr1);
console.log("result1: ", result1);

var ptrptr2 = ref.alloc('float*');
mylib.testFloatPtrPtr(ptrptr2);
var ptr2 = ref.deref(ptrptr2);
var result2 = ref.reinterpret(ptr2, 3, 0);
console.log("result2[0]: ", result2[0]);
console.log("result2[1]: ", result2[1]);
console.log("result2[2]: ", result2[2]);

var ptrptr3 = ref.alloc('int*');
mylib.testIntPtrPtr(ptrptr3);
var ptr3 = ref.deref(ptrptr3);
var result3 = ref.reinterpret(ptr3, 3, 0);
console.log("result3[0]: ", result3[0]);
console.log("result3[1]: ", result3[1]);
console.log("result3[2]: ", result3[2]);

var ptrptr4 = ref.alloc('char*');
mylib.testCharPtrPtr(ptrptr4);
var ptr4 = ref.deref(ptrptr4);
var result4 = ref.reinterpret(ptr4, 3, 0);
console.log("result4[0]: ", result4[0]);
console.log("result4[1]: ", result4[1]);
console.log("result4[2]: ", result4[2]);

And here is my console output:

result1:  1.5
result2[0]:  205
result2[1]:  204
result2[2]:  140
result3[0]:  2
result3[1]:  0
result3[2]:  0
result4[0]:  5
result4[1]:  6
result4[2]:  7

result1 and result4 are OK but result2 and result3 are wrong. It seems that somethings is wrong with dereferencing the buffer with type 'float' and 'int'. However, it works for 'char**'.

Could you help me with it? Thank you!

tonyguo1987 commented 8 years ago

Problem solved! Please see https://github.com/node-ffi/node-ffi/issues/281.