Skip to content

Commit

Permalink
Added promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Apr 9, 2016
1 parent f0547a7 commit d8950a3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/_foreign_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var assert = require('assert')
, debug = require('debug')('ffi:_ForeignFunction')
, ref = require('ref')
, Promise = require('any-promise')
, bindings = require('./bindings')
, POINTER_SIZE = ref.sizeof.pointer
, FFI_ARG_SIZE = bindings.FFI_ARG_SIZE
Expand Down Expand Up @@ -117,6 +118,24 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
})
}

/**
* A promise based wrapper around .async
*/

proxy.promise = function () {
var args = [].splice.call(arguments, 0)
return new Promise(function (resolve, reject) {
args.push(function (err, res) {
if (err) {
return reject(err)
} else {
return resolve(res)
}
})
proxy.async.apply(undefined, args)
})
}

return proxy
}
module.exports = ForeignFunction
5 changes: 4 additions & 1 deletion lib/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ function Library (libfile, funcs, lib) {
, fopts = info[2]
, abi = fopts && fopts.abi
, async = fopts && fopts.async
, promise = fopts && fopts.promise
, varargs = fopts && fopts.varargs

if (varargs) {
lib[func] = VariadicForeignFunction(fptr, resultType, paramTypes, abi)
} else {
var ff = ForeignFunction(fptr, resultType, paramTypes, abi)
lib[func] = async ? ff.async : ff
if (async) lib[func] = ff.async
else if (promise) lib[func] = ff.promise
else lib[func] = ff
}
})

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
},
"main": "./lib/ffi",
"dependencies": {
"any-promise": "^1.1.0",
"bindings": "~1.2.0",
"debug": "2",
"nan": "2",
"ref": "1",
"ref-struct": "1",
"nan": "2"
"ref-struct": "1"
},
"devDependencies": {
"fs-extra": "^0.23.1",
Expand Down
26 changes: 26 additions & 0 deletions test/foreign_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,30 @@ describe('ForeignFunction', function () {

})

describe('promise', function () {

it('should call the static "abs" bindings asynchronously', function () {
var _abs = bindings.abs
var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])
assert.equal('function', typeof abs.async)

return abs.promise(-1234).then(function (res) {
assert.equal(res, 1234)
})
})

it('should invoke the callback with an Error with a meaningful message when type\'s `set()` throws', function () {
var _abs = bindings.abs
var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])

return abs.promise(1111111111111111111111).catch(function (err) {
return err;
}).then(function (err) {
assert(err instanceof Error)
assert(/error setting argument 0/.test(err.message))
})
})

})

})
14 changes: 14 additions & 0 deletions test/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,18 @@ describe('Library', function () {

})

describe('promise', function () {

it('should call a function asynchronously and return a promise', function () {
var lib = process.platform == 'win32' ? 'msvcrt' : 'libm'
var libm = new Library(lib, {
'ceil': [ 'double', [ 'double' ], { promise: true } ]
})
return libm.ceil(1.1).then(function (res) {
assert.equal(res, 2)
})
})

})

})

0 comments on commit d8950a3

Please sign in to comment.