I'm trying to wrap an existing C library using node-ffi but I can't seem to read the returned result. The situation is a little awkward because the function returns the C struct
typedef struct {
  int size;
  void *ptr;
} datum;
and the length field of the node-buffer representing datum.ptr is 0.
Here's the entire C code that shows the issue.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
        void *ptr;
        int size;
} datum;
datum get() {
        int r = 23;
        datum *p = malloc(sizeof(*p));
        if(!p) {
                printf("malloc failed\n");
                exit(1);
        }
        p->ptr = malloc(sizeof(r));
        if(!p->ptr) {
                printf("malloc failed\n");
                exit(1);
        }
        p->size = sizeof(r);
        memcpy(p->ptr, &r, sizeof(r));
        printf("p->size = %d\n", p->size);
        printf("p->ptr = %p\n", p->ptr);
        return *p;
}
And here's the javascript:
#!/opt/local/bin/node
var ref = require("ref");
var ffi = require("ffi");
var util = require("util");
var Struct = require('ref-struct');
var datum = Struct({
        'ptr':  'pointer',
        'size': 'int'
});     
var wrapper = ffi.Library('test.dylib', {
        "get":  [ datum, [ ] ] ,
});             
let result = wrapper.get();
console.log(result);
console.log(result.ptr);
console.log("result.ptr.length = " + result.ptr.length);
console.log(result.ptr.readIntLE(0, 4, true));
console.log(result.ptr.readIntBE(0, 4, true));
console.log(result.ptr.readUInt8(0, true));
console.log(result.ptr.readUInt8(1, true));
console.log(result.ptr.readUInt8(2, true));
console.log(result.ptr.readUInt8(3, true));
And here's the output:
p->size = 4
p->ptr = 0x103b02e20
{ ptr: <Buffer@0x103b02e20 >,
  size: 4,
  'ref.buffer': <Buffer@0x1030549e8 20 2e b0 03 01 00 00 00 04 00 00 00 00 00 00 00> }
<Buffer@0x103b02e20 >
result.ptr.length = 0
NaN
NaN
undefined
undefined
undefined
undefined
Notice that p->ptr and the buffer both say 0x103b02e20 so I think the data is there, I just can't read it out of the buffer for some reason.
Anyone have any ideas what I am missing?
 
    