4.0.3 • Published 3 years ago

ffi-napi v4.0.3

Weekly downloads
15,945
License
MIT
Repository
github
Last release
3 years ago

node-ffi-napi

Node.js Foreign Function Interface for N-API

Greenkeeper badge

NPM Version NPM Downloads Build Status Coverage Status Dependency Status

node-ffi-napi 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-napi 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.

WARNING: The original API of node-ffi is left mostly untouched in the N-API wrapper. However, the API did not have very well-defined properties in the context of garbage collection and multi-threaded execution. It is recommended to avoid any multi-threading usage of this library if possible.

Example

var ffi = require('ffi-napi');

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-napi; it does not need to be installed on your system.
  • The current version is tested to run on Node 6 and above.

Installation

Make sure you've installed all the necessary build tools for your platform, then invoke:

$ npm install ffi-napi

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-napi:

$ git clone git://github.com/node-ffi-napi/node-ffi-napi.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 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.

@hyperledger/vcxagent-coresdl2-gfx-napisdl2-image-napisdl2-ttf-napinode-sdl2-napiwiretestnodejs-telegramtelegram-clientlegalcert-dike-wrapper-jsmsplibarcface-idcard-proarcsoft-face-proarcsoft-idcardveri-proautoit-wrapperstring-art-backendvosk-api-unofficialtimefibers-active-winmusig-js-test2xvbacomjscobhanjscgozenitytelefire-panel-connectjavonet.nodejs.sdk@adc-ops/autocore-js@infinitebrahmanuniverse/nolb-ffacbrlib@everything-registry/sub-chunk-1661@yeisonkirax/ibm-mq@zuler_platform/todesk-sdk@znetstar/lock-screena42069boi@xyzrr/active-winaccel-sort@daevm/aries-cheqd-node@daevm/aries-framework-nodebasedirs@axetroy/whatchangedbassaudiobassaudio-updatedbassaudio-updated-lightbassaudiolibrary@azurity/acrylic@crubn/aries-framework-nodeauto-deploy-cliclj-protocolcompass-electroncobhan@initia/builder.jselectgetelectron-angular-nativeenet-jsenigmakeygenenn-pplb@chinaums/node-sm4asdasdasdggg@cch0/labeler@apexob/rc-withdraw@apexob/vc-withdraw@dopl-technologies/telerobotic-sdk@futpib/tglibeasy-voicemeeter-remote@gmarcon83/ibmmq-no-installclwconsole-menu.jsffi-adapterfdlib-win-x64fdlib-win-x86gmssl-nodejscustom-net-keepalivecvr100-napifiskaly-sdk-nodefmodex@metaverse-systems/the-seed-utilshaskill-mytestharaka-nonofficialhifond-napihermes-protocol@kibcode/tonlib-nodecrt310-napicrt580-napi@larriereguichet/vosk@laser-dac/easylase@laser-dac/helios@laser-dac/laserdock@laser-dac/beyond@labjack/labjack-nodejs@labjack/ljm-ffid5c-dll-libd5c-electron-sdk@jonathanblade/omronjscrypto-mpcgenerate-ctrl-c-eventdecardt6-napigcigci-js@devshack/gphoto2-driver@glander/ph-ackbms-renderer@daydreamer1984/datacenterblizzardry
4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

3.1.0

3 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.5.0

4 years ago

2.4.7

4 years ago

2.4.6

4 years ago

2.4.5

5 years ago

2.4.4

5 years ago

2.4.3

6 years ago

2.4.2

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago