Home

Awesome

npm install -g ffi-generate

Generate FFI Bindings

ffi-generate -f /path/to/myLibrary/header.h -l libmyLibrary

Will parse the given filename and print to standard out the resulting javascript suitable for use as a module.

It may be necessary to pass additional flags to libclang so it can better parse the header (i.e. include paths). To pass options directly to libclang use -- so ffi-generate-node knows to stop parsing arguments, the rest will be passed to libclang without modification.

ffi-generate -f /usr/include/ImageMagick/wand/MagickWand.h -l libMagickWand -m wand -p Magick -- $(Magick-config --cflags)

Generate FFI Bindings Programatically

var exec = require('child_process').exec;
var path = require('path');
var fs = require('fs');
var jsb = require('beautifyjs');
var generate = require('lib/generateffi').generate;

exec('llvm-config --includedir', function (fail, out, err) {
  var includedir = out.replace(/\s+$/, '');
  var result = exports.generate({
    filename: path.join(includedir, 'clang-c', 'Index.h'),
    library: 'libclang',
    prefix: 'clang_', 
    includes: [includedir],
  });

  if (result.unmapped.length > 0) {
    console.log('----- UNMAPPED FUNCTIONS -----');
    console.log(result.unmapped);
    console.log('----- UNMAPPED FUNCTIONS -----');
  }

  fs.writeFileSync(path.join(__dirname, 'dynamic_clang.js'), jsb.js_beautify(result.serialized));
  var dynamic_clang = require(path.join(__dirname, 'dynamic_clang'));
  var ver = dynamic_clang.libclang.clang_getClangVersion();
  console.log(dynamic_clang.libclang.clang_getCString(ver));
  dynamic_clang.libclang.clang_disposeString(ver)
});

Input to the generate method

The result from generate is an object that has two properties