Skip to content

[Discussion] FFI - require('ffi') - libffi low-level binding

This is the big boy for the ffi label, adding a new public core module: require('ffi'). This is what I consider to be the final piece of the puzzle (other pieces being https://github.com/nodejs/io.js/pull/1750, https://github.com/nodejs/io.js/pull/1759, and https://github.com/nodejs/io.js/pull/1762).

I am the maintainer for the npm package, and while the APIs don't quite line up, I've always felt that the node-ffi API was too high level (doing things like dynamic library loading, and type boxing/unboxing), and what I have done in this PR is more along the lines of the direction I've wanted to take it.

So this is a good opportunity to get the low-level API correct/useful so that higher level facilities can be built on top (i.e. ctypes, ref's Type system, etc.) without needing to compile a native addon (this part is HUGE, especially considering the annoying V8 API changes in latest versions). Ideally we can abstract any V8 API changes away via this module and the npm userspace can contain code for bindings to C libs using the FFI api, which won't change often.

Most of the diff here is just adding a gypified libffi to the deps dir. That's not so useful, so see this more useful diff for the integration code: https://github.com/nodejs/io.js/compare/1ebc059...TooTallNate:idea/libffi-bindings

A few things which I'd still like to figure out (and would like a comment on if anybody has a particular preference):

  • We need to compile some native code for the tests (i.e. to expose static function pointer Buffers to pass to ffi.call(), to invoke a C function pointer to test the ffi.Closure stuff). We could add a new process.binding('ffi_tests') internal binding that would only be loaded during the tests. Or another possibility would be for these ffi tests to rely on the dlopen module, and we can piggyback off of the shared library that gets loaded for the dlopen tests. Either way sounds good to me.
  • The async ffi.callAsync() function is not yet implemented. Working on that still though considering https://github.com/nodejs/hardware/issues/9. 81f9b884
  • Docs still needed as well.
  • A helper for creating "struct" ffi_type instances.
  • A "variadic" CIF interface (right now ffi.CIF only expects to describe fixed-length functions, but there is a variadic alternative: we just need a parameter to specify the number of fixed arguments vs. variable arguments).

Merge request reports

Loading