Skip to content

Commit

Permalink
foreign_function: better error messages when a type's "set()" functio…
Browse files Browse the repository at this point in the history
…n throws

Part of #78.
  • Loading branch information
TooTallNate committed Oct 5, 2012
1 parent 2a4ea60 commit 12c7090
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/_foreign_function.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
, argsList = new Buffer(argsArraySize) , argsList = new Buffer(argsArraySize)


// write arguments to storage areas // write arguments to storage areas
for (var i = 0; i < numArgs; i++) { var i, argType, val, valPtr
var argType = argTypes[i] try {
, val = arguments[i] for (i = 0; i < numArgs; i++) {
var valPtr = ref.alloc(argType, val) argType = argTypes[i]
argsList.writePointer(valPtr, i * POINTER_SIZE) 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 // invoke the `ffi_call()` function
Expand Down Expand Up @@ -80,12 +86,17 @@ function ForeignFunction (cif, funcPtr, returnType, argTypes) {
var argsList = new Buffer(argsArraySize) var argsList = new Buffer(argsArraySize)


// write arguments to storage areas // write arguments to storage areas
for (var i = 0; i < numArgs; i++) { var i, argType, val, valPtr
var argType = argTypes[i] try {
var val = arguments[i] for (i = 0; i < numArgs; i++) {

argType = argTypes[i]
var valPtr = ref.alloc(argType, val) val = arguments[i]
argsList.writePointer(valPtr, i * POINTER_SIZE) 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 // invoke the `ffi_call()` function asynchronously
Expand Down
20 changes: 20 additions & 0 deletions test/foreign_function.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ describe('ForeignFunction', function () {
assert.equal(1234, abs(-1234)) 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 () { it('should call the static "atoi" bindings', function () {
var _atoi = bindings.atoi var _atoi = bindings.atoi
var atoi = ffi.ForeignFunction(_atoi, 'int', [ 'string' ]) var atoi = ffi.ForeignFunction(_atoi, 'int', [ 'string' ])
Expand Down Expand Up @@ -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()
});
})

}) })


}) })

0 comments on commit 12c7090

Please sign in to comment.