@aabuhijleh/ffi 中文文档教程

发布于 5年前 浏览 24 项目主页 更新于 3年前

node-ffi

Node.js Foreign Function Interface

构建状态Build Status

node-ffi 是一个 Node.js 插件,用于加载和调用动态库,使用 纯JavaScript。 它可用于创建与本机库的绑定,而无需 编写任何 C++ 代码。

它还通过 C 代码简化了 node.js 的扩充,因为它负责 处理跨 JavaScript 和 C 的类型转换,这可以增加大量 将样板代码添加到其他简单的 C 中。请参阅 example/factorial 有关此用例的示例。

警告:node-ffi 假定您知道自己在做什么。 你可以很容易地 创造你将对解释器进行段错误的情况,除非你有 C 调试器技能,您可能不知道发生了什么。

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

有关更详细的介绍,请参阅 node-ffi 教程页面

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

确保你已经安装了所有必要的构建 适用于您平台的工具, 然后调用:

$ npm install ffi

Source Install / Manual Compilation

从源代码编译它最容易使用 node-gyp

$ npm install -g node-gyp

现在可以编译node-ffi

$ git clone git://github.com/node-ffi/node-ffi.git
$ cd node-ffi
$ node-gyp rebuild

Types

您在函数声明中指定的类型对应于 ref 的类型 系统。 所以查看它的文档 如果您不熟悉,请参考。

V8 and 64-bit Types

在内部,V8 将适合 32 位空间的整数存储在 32 位 整数,超出此范围的将被放入双精度 浮点数 (FP)。 这是有问题的,因为 FP 数字不精确。 为了解决这个问题,node-ffi 中处理 64 位整数的方法返回 字符串并且可以接受字符串作为参数。

Call Overhead

与 FFI 调用相关的开销不小。 比较硬编码 strtoul() 的绑定版本到 strtoul() 的 FFI 版本表明 本机硬编码绑定要快几个数量级。 所以不要只使用 C 版本的函数只是因为它更快。 有一个显着的成本 FFI 电话,所以让他们值得。

License

麻省理工学院许可证。 请参阅 LICENSE 文件。

[v1apichanges]:https://github.com/node-ffi/node-ffi/wiki/API-changes-from-v0.x-to-v1.x

node-ffi

Node.js Foreign Function Interface

Build StatusBuild 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.

[v1apichanges]: https://github.com/node-ffi/node-ffi/wiki/API-changes-from-v0.x-to-v1.x

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文