Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

foreign_function: better error messages when a type's "set()" functio…

…n throws

Part of #78.
  • Loading branch information...
commit 12c70902cc4448405189520619da5b342c8b882c 1 parent 2a4ea60
@TooTallNate TooTallNate authored
Showing with 42 additions and 11 deletions.
  1. +22 −11 lib/_foreign_function.js
  2. +20 −0 test/foreign_function.js
View
33 lib/_foreign_function.js
@@ -42,11 +42,17 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
, argsList = new Buffer(argsArraySize)
// write arguments to storage areas
- for (var i = 0; i < numArgs; i++) {
- var argType = argTypes[i]
- , val = arguments[i]
- var valPtr = ref.alloc(argType, val)
- argsList.writePointer(valPtr, i * POINTER_SIZE)
+ var i, argType, val, valPtr
+ try {
+ for (i = 0; i < numArgs; i++) {
+ argType = argTypes[i]
+ val = arguments[i]
+ valPtr = ref.alloc(argType, val)
+ argsList.writePointer(valPtr, i * POINTER_SIZE)
+ }
+ } catch (e) {
+ e.message = 'error setting argument ' + i + ' - ' + e.message
+ throw e
}
// invoke the `ffi_call()` function
@@ -80,12 +86,17 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
var argsList = new Buffer(argsArraySize)
// write arguments to storage areas
- for (var i = 0; i < numArgs; i++) {
- var argType = argTypes[i]
- var val = arguments[i]
-
- var valPtr = ref.alloc(argType, val)
- argsList.writePointer(valPtr, i * POINTER_SIZE)
+ var i, argType, val, valPtr
+ try {
+ for (i = 0; i < numArgs; i++) {
+ argType = argTypes[i]
+ val = arguments[i]
+ valPtr = ref.alloc(argType, val)
+ argsList.writePointer(valPtr, i * POINTER_SIZE)
+ }
+ } catch (e) {
+ e.message = 'error setting argument ' + i + ' - ' + e.message
+ return process.nextTick(callback.bind(null, e));
}
// invoke the `ffi_call()` function asynchronously
View
20 test/foreign_function.js
@@ -28,6 +28,14 @@ describe('ForeignFunction', function () {
assert.equal(1234, abs(-1234))
})
+ it('should throw an Error with a meaningful message when type\'s `set()` throws', function () {
+ var _abs = bindings.abs
+ var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])
+ assert.throws(function () {
+ abs('a string?!?!')
+ }, /error setting argument 0/)
+ })
+
it('should call the static "atoi" bindings', function () {
var _atoi = bindings.atoi
var atoi = ffi.ForeignFunction(_atoi, 'int', [ 'string' ])
@@ -163,6 +171,18 @@ describe('ForeignFunction', function () {
})
})
+ it('should invoke the callback with an Error with a meaningful message when type\'s `set()` throws', function (done) {
+ var _abs = bindings.abs
+ var abs = ffi.ForeignFunction(_abs, 'int', [ 'int' ])
+
+ abs.async('a string!?!?', function (err, res) {
+ assert(err)
+ assert(/error setting argument 0/.test(err.message))
+ assert.equal('undefined', typeof res)
+ done()
+ });
+ })
+
})
})
Please sign in to comment.
Something went wrong with that request. Please try again.