Currently the best place for examples of ffi.Function usage would be in the
test files:
https://github.com/node-ffi/node-ffi/blob/master/test/function.js#L31-L42

As for your case:

 1) I believe that you need to just use "void" rather than
ref.refType(ref.types.void) for the logFunctionPtr return value, since it
looks like the C declaration is defined that way.
 2) When invoking context_set_logfunc(), you pass the JS function directly
where the `logfunc` argument goes. So probably something like:

myLib.context_set_logfunc(context.ref(), function(context, loglevel, file,
line, fn, message, userData) {
  // do something with the arguments!
}, null);

Hope that helps. Cheers!

On Mon, Jul 13, 2015 at 12:30 PM, Christian Guimaraes <
[email protected]> wrote:

> Hi!
>
> I'm trying to wrap a C library using node-ffi and I have some doubts
> regarding function pointers.
>
> The C library has a following callback function:
>
> // typedef void (*logfunc_t) (context_t *context, loglevel_t loglevel,
> const char *file, unsigned int line, const char *function, const char
> *message, void *userdata);
> var logFunctionPtr = ffi.Function( ref.refType(ref.types.void), [
> contextTPtr, "int", "string", "int", "string", "string",
> ref.refType(ref.types.void) ]);
>
> And below the FFI definition that I did:
>
> var myLib = ffi.Library("mylib", {
>   // dc_status_t -> type int
>   // dc_status_t context_set_logfunc (context_t *context, logfunc_t
> logfunc, void *userdata);
>   "context_set_logfunc": [ "int", [ contextTPtr, logFunctionPtr,
> ref.refType(ref.types.void) ] ]
> });
>
> 1. Is it the code above ok?
>
> 2. How should I pass the callback (as parameter, in red) to the function
> call? I'm trying this:
>
> myLib.context_set_logfunc(context.ref(), ref.alloc(logFunctionPtr), null);
>
> I'm having trouble trying to find some node-ffi examples where/how a
> callback function is defined and used?
>
> Thanks.
>
> --
> Christian GuimarĂ£es
>
>
>  --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/b2181056-1089-49be-838f-3bfef504235b%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/b2181056-1089-49be-838f-3bfef504235b%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAN5MXoeSRy-JYGwSN_J_ShAfZp%2BqwKcAuorksFXp-fMXsZg8XA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to