Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 669
pancake Avatar asked May 28 '13 02:05

pancake


People also ask

What is node FFI?

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.

What file extension is used for a program that can be executed with node?

json file extension. The . node extension assumes that the file is a compiled binary, not a text file containing JavaScript.

Is Nodejs faster than C?

c to use a double instead of a long in the loop, the time C took was even longer! Not trying to start a flame war, but why is Node. JS (116 ms.) so much faster than native C (198 ms.)

Is node built on C?

What benefits are you getting? Node. js is written in C++. C and C++ are different languages, with different strengths and weaknesses.


1 Answers

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.

like image 102
konsumer Avatar answered Oct 04 '22 09:10

konsumer