Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can ref-array be used in ffi Callback? #10

Open
zerbobo opened this issue Dec 9, 2015 · 2 comments
Open

Can ref-array be used in ffi Callback? #10

zerbobo opened this issue Dec 9, 2015 · 2 comments

Comments

@zerbobo
Copy link

zerbobo commented Dec 9, 2015

I have C code:

typedef void (*CallbackFunction)(int values[], int num);

int onMessage(CallbackFunction cb)
{
    int nums[] = {1,2,3,4,5};
    cb(nums, 5);
    return 0;
}

And js code:

var ffi = require('ffi');
var ref = require('ref');
var refArray = require('ref-array');
var lib = ffi.Library('my_lib', {
    'onMessage', ['int', [ffi.Function('void', [refArray(ref.types.int), ref.types.int])]]
});

lib.onMessage(ffi.Callback('void', [refArray(ref.types.int), ref.types.int], function(values, num){
      for(var i = 0; i < num; i++) {
        console.log(values[i]);
      }
}));

The js run result is:

undefined
undefined
undefined
undefined
undefined

So It doesn't work?

@zerbobo zerbobo changed the title Can ref-array used in ffi Callback? Can ref-array be used in ffi Callback? Dec 9, 2015
@hitman401
Copy link

I am facing a similar problem too. Any help on this issue is much appreciated. Is there a workaround for this issue?

@hitman401
Copy link

@zerbobo, I managed to get this one right.

const arrayPointer = ref._reinterpret(values, num);
const actualArray = new IntArrayType(arrayPointer);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants