node-ffi

Node.js Foreign Function Interface

  • 所有者: node-ffi/node-ffi
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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 (FP) 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.

主要指标

概览
名称与所有者node-ffi/node-ffi
主编程语言JavaScript
编程语言Python (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2009-12-05 17:23:17
推送于2023-07-15 04:15:33
最后一次提交2019-01-25 10:27:00
发布数43
最新版本名称2.3.0 (发布于 )
第一版名称v0.3.1 (发布于 )
用户参与
星数4.3k
关注者数101
派生数411
提交数838
已启用问题?
问题数562
打开的问题数230
拉请求数25
打开的拉请求数17
关闭的拉请求数41
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?