• Stars
    star
    131
  • Rank 275,867 (Top 6 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate ffi bindings from header files

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.

  • f -- required -- The header file you wish to parse
  • l -- required -- The library FFI will use to dlopen
  • m -- optional -- The module name underwhich functions will be stored (uses library name otherwise)
  • p -- optional -- Only include functions whose name starts with the provided prefix
  • you can specify multiple -p on the command line to get multiple prefixes
  • x -- optional -- Restrict to only functions declared in the given header file
  • s -- optional -- Use StrictType type wrapper (experimental)
  • L -- optional -- If libclang.{so,dylib} is in a non-standard path use this which will rerun the process with [DY]LD_LIBRARY_PATH set

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

  • opts.filename -- required -- the full path to the header source file to parse
  • opts.library -- required -- the library ffi should use to dlopen
  • opts.module -- optional -- the name of the module that will be exported (otherwise uses library name)
  • opts.prefix -- optional -- restrict imported functions to a given prefix
  • opts.includes -- optional -- a set of directory paths to aid type expansion
  • opts.compiler_args -- optional -- a set of clang command line options passed to the parser
  • opts.single_file -- optional -- restricts functions to only those defined in the header file
    • this does not restrict dependent types

The result from generate is an object that has two properties

  • serialized - a string representation of the bindings suitable for writing to file
  • unmapped - a set of functions that failed to map -- most likely from failure to map a type to ffi type.
  • each element is an object with following properties
  • position - -1 means the return type, otherwise the argument
  • arg - name of the type that failed to map
  • name - the name of the function that failed
  • decl - the signature of the function that failed

More Repositories

1

node-dns

Replacement dns module in pure javascript for node.js
JavaScript
585
star
2

airprint-generate

Automatically generate AirPrint Avahi service files for CUPS printers
Python
405
star
3

linode-python

Linode API Python Bindings
Python
163
star
4

node-vlc

Node.js bindings for vlc
JavaScript
75
star
5

lldb-v8

v8 utility scripts for lldb
Python
73
star
6

native-dns-packet

DNS parsing and writing in javascript
JavaScript
58
star
7

node-libclang

nodejs bindings for libclang
JavaScript
52
star
8

node-addon-layer

A thin C API to create node binary addons with, inspired by JSAPI
C++
38
star
9

javascript-rrd

Pure javascript RRD file manipulation implementation. Fork of the original project for use with node.js
JavaScript
35
star
10

node-imagick

Native bindings to ImageMagick
C
21
star
11

node-buffercursor

A simple way to traverse a nodejs Buffer while keeping track of position
JavaScript
20
star
12

dnsbalance

Generic DNS server to dynamically distribute requests
JavaScript
17
star
13

native-dns-cache

Node.js DNS Caching
JavaScript
15
star
14

node-purple

nodejs libpurple ffi bindings
JavaScript
15
star
15

node-binaryheap

Basic binary heap tree implementation for node.js using linked lists
JavaScript
13
star
16

node-setflags

Set v8 flags at runtime
JavaScript
10
star
17

git-dht

Git Decentralized and Distributed
JavaScript
9
star
18

llmdb

mdb for lldb -- modular shell pipelines for debugging
Python
9
star
19

node-lstream

Small linestream transform
JavaScript
9
star
20

node-ircws

simple irc websocket proxy with webirc support
JavaScript
8
star
21

zpyzpr

Zippy Zipper -- Generic Python Multi-Threaded Compression Frontend
Python
6
star
22

irssi_proxy_websocket

irssi proxy module with a websocket listener
JavaScript
6
star
23

node-path-platform

Transitionary package for those not yet on v0.12 of Node.js
JavaScript
4
star
24

node-libgit2

libgit2 wrapping for node
JavaScript
4
star
25

vm_profile_guest

Profile Arbitrary VM Guests with DTrace in KVM or Hypervisor.framework
Python
4
star
26

jrq

C
4
star
27

node-struct

Implementation of Python's struct module in javascript with Buffers
JavaScript
4
star
28

linode-javascript

Linode API Javascript Bindings
JavaScript
3
star
29

node-addon-layer-test

A small module to test the addon-layer
C
3
star
30

mlang

Write Jobs for Joyent Manta in Node.js
JavaScript
3
star
31

nimbusmeus

Node.js Personal Media Streamer
JavaScript
3
star
32

node-nf

Node Filter -- use node for one liners like you would with perl
JavaScript
3
star
33

node-trace-func

trace arbitrary javascript function
JavaScript
3
star
34

linode-chrome

Linode Chrome extension that supports multiple API keys
3
star
35

multi-stage-linuxkit

LinuxKit in pieces, stage 1 kernel+initrd, stage 2 squshfs "from somewhere"
Makefile
3
star
36

chrome-nodeci

Chrome Extension for Node.js CI integration
JavaScript
2
star
37

podspec2linuxkit

Create immutable machine images from Kubernetes Manifests
Go
2
star
38

web220

Web browser based virtual terminal client
C++
2
star
39

ref-strict

Strict typing for node-ffi and ref
JavaScript
2
star
40

round-up-storage-claim

Mutating Webhook Example to Round Up PersistentVolumeClaims
JavaScript
1
star
41

twuuenc

Twitter UUENCODE
Python
1
star
42

node-gcore

A module to induce core files from inside a node process
C++
1
star
43

node-httptop

A small wrapper around DTrace to see popular routes and their latencies
JavaScript
1
star
44

immich-client-swift

Swift
1
star