Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ffi

node-ffi5.5kMIT2.3.0TypeScript support: definitely-typed

A foreign function interface (FFI) for Node.js

foreign, function, interface, ffi, libffi, binding, c

readme

node-ffi

Node.js Foreign Function Interface

Build Status Build Status

node-ffi is a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.

It also simplifies the augmentation of node.js with C code as it takes care of handling the translation of types across JavaScript and C, which can add reams of boilerplate code to your otherwise simple C. See the example/factorial for an example of this use case.

WARNING: node-ffi assumes you know what you're doing. You can pretty easily create situations where you will segfault the interpreter and unless you've got C debugger skills, you probably won't know what's going on.

Example

var ffi = require('ffi');

var libm = ffi.Library('libm', {
  'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2

// You can also access just functions in the current process by passing a null
var current = ffi.Library(null, {
  'atoi': [ 'int', [ 'string' ] ]
});
current.atoi('1234'); // 1234

For a more detailed introduction, see the node-ffi tutorial page.

Requirements

  • Linux, OS X, Windows, or Solaris.
  • libffi comes bundled with node-ffi; it does not need to be installed on your system.
  • The current version is tested to run on node v0.6, v0.8, v0.9 and v0.10.

Installation

Make sure you've installed all the necessary build tools for your platform, then invoke:

$ npm install ffi

Source Install / Manual Compilation

To compile from source it's easiest to use node-gyp:

$ npm install -g node-gyp

Now you can compile node-ffi:

$ git clone git://github.com/node-ffi/node-ffi.git
$ cd node-ffi
$ node-gyp rebuild

Types

The types that you specify in function declarations correspond to ref's types system. So see its docs for a reference if you are unfamiliar.

V8 and 64-bit Types

Internally, V8 stores integers that will fit into a 32-bit space in a 32-bit integer, and those that fall outside of this get put into double-precision floating point numbers. This is problematic because FP numbers are imprecise. To get around this, the methods in node-ffi that deal with 64-bit integers return strings and can accept strings as parameters.

Call Overhead

There is non-trivial overhead associated with FFI calls. Comparing a hard-coded binding version of strtoul() to an FFI version of strtoul() shows that the native hard-coded binding is orders of magnitude faster. So don't just use the C version of a function just because it's faster. There's a significant cost in FFI calls, so make them worth it.

License

MIT License. See the LICENSE file.

changelog

2.2.0 / 2016-10-29

2.1.0 / 2016-08-03

  • [a66fb8b282] - rename History.md to CHANGELOG.md (Nathan Rajlich)
  • [424d6b2278] - test node v6 with CI (Nathan Rajlich)
  • [37dc33f10d] - Move errno method implement to C++ side (Lee, SungUk)
  • [f0547a7535] - test: use full URL to issue (Nathan Rajlich)
  • [819c664605] - appveyor, travis: test node v5.1 (Nathan Rajlich)
  • [b6e8dba046] - remove benchmark files (Nathan Rajlich)
  • [f5e445be91] - test: load Foundation first instead (Nathan Rajlich)
  • [529ea78029] - travis: remove iojs v3 (Nathan Rajlich)
  • [c81ab1ed1e] - test: load Cocoa lib for Obj-C tests (Nathan Rajlich)
  • [829d7dac02] - travis: attempt to test "osx" (Nathan Rajlich)
  • [979da99892] - test: fix hardcoded strtoul() bindings (Nathan Rajlich)
  • [9cc558632c] - test: fix comment (Nathan Rajlich)
  • [3d673ca2a1] - test: attempt to fix test 169 on Linux (Nathan Rajlich)
  • [c2e5996d9d] - test: remove .only() (Nathan Rajlich)
  • [1187b80f7b] - test: add case for allowing Buffer backing store for "string" FFI argument (Nathan Rajlich)
  • [3b09d1ac09] - test: remove semis (Nathan Rajlich)
  • [74e29a17d0] - test: whitespace fixes (Nathan Rajlich)
  • [6551d4ab5b] - appveyor: test node v4.1 (Nathan Rajlich)
  • [c0b64413fe] - travis: test node v4.1 (Nathan Rajlich)
  • [730bd4a92f] - travis: drop "iojs-" prefix from version names (Nathan Rajlich)
  • [0324f3be9c] - test node v0.4 (Nathan Rajlich)
  • [f3e393bb55] - remove node v0.8 from testing matrices (Nathan Rajlich)

2.0.0 / 2015-09-04

  • update to "nan" v2, adds io.js v3 support
  • replace "dlfcn-win32" with "simple-dlfcn-win32" (uses MIT license rather than LGPL, #226, @mcnameej)
  • remove compiled binary file from libffi deps dir (#229, @fredericgermain)
  • fix dynamic linking when locale is not English (#224, @unbornchikken)
  • appveyor: test v0.8, io.js v2.5 and v3
  • travis: test v0.8, and iojs v2.5 and v3
  • package: add "license" field
  • package: add Gábor to LICENSE and "contributors"
  • package: move TooTallNate to "contributors" array

1.3.2 / 2015-07-31

  • package: made the nan dependency stricter (#217, @feldgendler)
  • package: reflect the fact that the build fails for node <= 0.8 (#196, @addaleax)

1.3.1 / 2015-04-16

  • test: use assert.throws() for Obj-C test cases
  • test: add case for #199 that covers callback and error propagation on non-libuv thread
  • HandleScope issue fix for iojs v1.7+
  • use Windows' native thread API, rather than libuv

1.3.0 / 2015-03-22

  • add appveyor.yml file for Windows testing
  • add support for io.js >= v1.1.0 and node.js v0.12.x via nan
  • avoid VS build error LNK2005
  • package: allow any "debug" v2
  • package: update github URLs for new repo location
  • travis: don't test node v0.6, test v0.12
  • now using libuv's pthread impl on Windows, removed pthreads-win32 dep
  • dlfcn-win32 dep updated to fix process global symbols on Windows
  • README: add appveyor build badge
  • README: use SVG appveyor badge

1.2.7 / 2014-07-06

  • test: add test case for race condition in #153
  • factorial: fix Windows build instructions
  • example: turn factorial readme to Markdown
  • example: add Windows libfactorial.dll compile command
  • package: remove "expect.js" dev dependency
  • test: remove final expect.js usage
  • jshintrc: enable "laxbreak"
  • travis: remove IRC notifications from Travis
  • test: properly re-add Mocha's uncaught listeners
  • test: add a try/catch test after the callback is GC'd
  • src: fix race condition when callback is invoked from thread pool (@nikmikov, #154)
  • change Node.js versions used on Travis CI for testing (@Mithgol, #151)
  • use SVG to display Travis CI build testing status (@Mithgol, #149)

1.2.6 / 2013-10-08

  • just a minor documentation typo fix (Jason May, #126)
  • example: fix "factorial" example on Windows (#127)
  • package: add "keywords" section
  • callback: store a reference to the CIF struct on the ffi closure Buffer instance (#125)

1.2.5 / 2013-04-06

  • type: make detecting "long" and "ulong" ffi_types work
  • travis: don't test node v0.7.x, test node v0.10.x

1.2.4 / 2013-02-18

  • FreeBSD 32-bit support (Dave Osborne)
  • libffi: don't build libffi as a "thin" archive (CentOS 5 support, #110)

1.2.3 / 2012-12-20

  • FreeBSD 64-bit support (Dave Osborne)

1.2.2 / 2012-12-15

  • fix nasty bug in async FFI'd function on node v0.9.x

1.2.1 / 2012-12-15

  • add node >= v0.9.4 support

1.2.0 / 2012-10-13

  • type: full support for "ref-array" arguments and return types
  • type: add basic support for basic ref types without a ffi_type prop set
  • don't call the "ref()" function on passed in arguments
  • libffi: fix unused variable warnings
  • add Function "type" for functions/callbacks that accept/return C Functions
  • dynamic_library: use RTLD_LAZY by default
  • export all the RTLD_* symbols from the native binding
  • foreign_function: better error messages when a type's "set()" function throws
  • callback: make catching callbacks that throw JS exceptions work as expected
  • callback: more meaningful error message when a type's "set()" function throws
  • callback: fix pointer return values

1.1.3 / 2012-09-25

  • callback: use IsEmpty() instead of an explicit NULL check
  • test: use "bindings" to load the bindings for the variadic tests
  • ffi: use HandleScope in WrapPointer() (fixes ffi calls in a tight loop, see #74)
  • test: fix typo in test name
  • libffi: disable the C4267 implicit conversion warnings on Windows
  • libffi: remove "as.bat" from the gyp file

1.1.2 / 2012-09-16

  • callback: throw an Error if the callback function has been garbage collected
  • test: 100% tests passing on Windows!

1.1.1 / 2012-09-16

  • libffi: define "FFI_MMAP_EXEC_WRIT" on OS X (#71)
  • added a new test case that calls a callback function directly (#72)

1.1.0 / 2012-09-11

  • properly "gyp-ify" libffi
    • added "libffi.gyp"
    • no more "hacks" in binding.gyp
    • no need for MozillaBuild on Windows anymore!

1.0.7 / 2012-08-03

  • export FFI_FIRST_ABI
  • export abi_enum values for ARM processors (100% tests passing on Raspberry Pi!)

1.0.6 / 2012-07-22

  • VariadicForeignFunction: apply a tweak to prevent false positives on ffi id's

1.0.5 / 2012-07-22

  • DynamicLibrary: use 'string' instead of "char *"
  • DynamicLibrary: set the "name" property of the returned Buffer when get() is called
  • test: add some "DynamicLibrary" tests
  • VariadicForeignFunction: quick hack fix for the key caching name collision

1.0.4 / 2012-07-12

  • exit early when not compiling from within a MozillaBuild window on Windows

1.0.3 / 2012-07-9

  • refactor the README
  • fix deprecation warning for using the Utf8String type (renamed to CString)
  • remove circular require() calls (Justin Freitag)
  • use the node-gyp --directory flag for npm test command

1.0.2 / 2012-06-20

  • Fix Windows build (32-bit at least). Fixes #51.

1.0.1 / 2012-06-13

  • Refactor the variadic function generator to allow for an overridden "returnType"

1.0.0 / 2012-05-31

  • Add a VariadicForeignFunction function for vararg C functions
  • Various cleanup
  • Don't export the native bindings (ffi.Bindings is gone)
  • Use the ref() function when available, then fall back to ref.alloc()
  • Add a few more tests

1.0.0-alpha1 / 2012-05-29

  • Readme improvements
  • Node >= v0.7.9 compatability

1.0.0-alpha / 2012-05-25

  • Alpha release of v1.0.0

< 1.0.0

  • Prehistoric: see git log