node-ffi vs. node extension for accessing existing C++ functionality

pancake picture pancake · May 28, 2013 · Viewed 14.8k times · Source

I've got some existing C++ code that does numerical processing within a stand-alone C++ application. I now want to use that code within a new node.js application.

Researching how to access C++ code from node.js, two options come up:

  1. Write a node.js extension
  2. Use node-ffi

node-ffi seems like a good option to access existing libraries, but am I right thinking if I use node-ffi I would have to write a C wrapper to make my C++ accessible? (This was the only way I could get a simple test case to work on Windows with Visual Studio).

For my case where my source code is already in C++, not C, what are the considerations in choosing between the two options above?

Answer

konsumer picture konsumer · Nov 16, 2013

FFI works with dynamic C libraries. This means that you have to expose your dynamic library externally. In C++, you do this with extern "C", like this:

#ifdef __cplusplus
extern "C" {
#endif

int foo (int param){
  int ret = 0;
  // do C++ things
  return ret;
}

int bar(){
  int ret = 0;
  // do C++ things
  return ret;
}

#ifdef __cplusplus
}
#endif

This will make your C++ function available to C-things, as a dynamic library method.

Here is how you wrap this in javascript, after you have compiled your C++ lib as libmylibrary.dll/.so:

var ffi = require('ffi');

var mylibrary = ffi.Library('libmylibrary', {
  "foo": [ "int", ["int"] ],
  "bar": [ "int", [] ]
});

There are lots cooler things that you can do. Check it out, here

If this is a node library, just put your methods in module.exports. Here is a full example of a wrap of the above C++ code, with synchronous & asynchronous methods:

var ffi = require('ffi');

var mylibrary = ffi.Library('libmylibrary', {
  "foo": [ "int", ["int"] ],
  "bar": [ "int", [] ]
});

module.exports = {
  fooSync : mylibrary.foo,
  foo: mylibrary.foo.async,
  barSync : mylibrary.bar,
  bar: mylibrary.bar.async
};

I haven't used node-ffi-generate, but it looks pretty cool for generating these sort of wrappers for you.

If I saved this file as mylibrary.js, I could use it like this:

var mylib = require('./mylibrary.js');

var num = mylib.fooSync(1);

// or

mylib.foo(1, function(er, num){

});

As for the question of "is it better?" I think so, for most things. If you make your methods extern C, they will work in just about every other language, some of which also have FFI, so you would write the simple equivalent of the above in whatever your target language is. This means very little code to maintain, other than the basic "load C++ lib", and "mess around with it's signature to feel right for language X". It's not specific to node. Another bonus is for common shared libraries (like sqlite, given in tutorial example.) You maybe don't care exactly what version they have, or want to wrap it with more C++ code, that needs to be compiled to use it. Using FFI, you can wrap a pre-compiled/installed lib with just javascript.