-
Notifications
You must be signed in to change notification settings - Fork 415
API changes from v0.x to v1.x
Most of the API's in node-ffi
changed in v1.x
to be more concise.
The main change is that the old Pointer
class has been completely removed, in favor of using official Node Buffer
objects everywhere. The Pointer
class had some additional functionality over vanilla Buffers, so ref
was written to iron out those differences.
ref
introduces a "type" system, which allows you to classify the data inside a Buffer as an int
, or char *
, etc. So now node-ffi
uses these "type" objects when defining FFI functions:
var ref = require('ref')
var ffi = require('node-ffi')
// the "int" type
var int = ref.types.int
// create an FFI'd "abs(3)" function
var absPtr = ffi.DynamicLibrary().get('abs')
var abs = ffi.ForeignFunction(absPtr, int, [ int ])
// and invoke!
abs(-1) // ← 1
abs(-123) // ← 123
The API for invoking asynchronous ForeignFunction instances has changed. Previously, you'd have to specify whether a ForeignFunction instance was "async"
during instantiation, but now you can decide at call-time. The "regular" function invokation (i.e. func()
) is the "sync" version, and calling func.async()
with a callback function is the "async" version.
Additionally, before an async invokation would return an EventEmitter
instance that would emit "success" when the FFI'd function completed. This was unnecessary, and now you simply pass the .async()
function a callback function as the final argument. The callback follows Node's the traditional err, res
argument signature.
var func = new ffi.ForeignFunction(funcPtr, 'int', [ 'int' ], true)
func(-5).on('success', function (res) {
console.log('result:', res)
})
var func = ffi.ForeignFunction(funcPtr, 'int', [ 'int' ]) // no need to specify "async" here...
func(-5) // "sync" version
func.async(-5, function (err, res) {
if (err) throw err
console.log('result:', res)
})
More coming soon!