function.js
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var assert = require('assert')
, ref = require('ref')
, ffi = require('../')
, bindings = require('bindings')({ module_root: __dirname, bindings: 'ffi_tests' })
describe('Function "type"', function () {
afterEach(gc)
it('should be a function', function () {
assert.equal('function', typeof ffi.Function)
})
var voidFn = ffi.Function('void', [])
it('should return a "type" object when invoked with a return type and array of arguments types', function () {
assert(voidFn)
assert.equal('function', typeof voidFn.get)
assert.equal('function', typeof voidFn.set)
})
it('should be accepted as a return "type" to a ForeignFunction', function () {
var fn = ffi.ForeignFunction(ref.NULL, voidFn, [])
})
it('should be accepted as an argument "type" to a ForeignFunction', function () {
var fn = ffi.ForeignFunction(ref.NULL, 'void', [ voidFn ])
})
it('should work as expected using the "callback_func" static bindings', function () {
var fn = ffi.Function('int', [ 'int' ])
var callback_func = ffi.ForeignFunction(bindings.callback_func, fn, [ fn ])
var abs = callback_func(Math.abs)
assert.equal('function', typeof abs)
assert.equal(Math.abs(-5), abs(-5))
assert.equal(Math.abs(-9), abs(-9))
assert.equal(Math.abs(-69), abs(-69))
assert.equal(Math.abs(3), abs(3))
})
})