callback.js
8.1 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
var assert = require('assert')
, ref = require('ref')
, ffi = require('../')
, int = ref.types.int
, bindings = require('bindings')({ module_root: __dirname, bindings: 'ffi_tests' })
describe('Callback', function () {
afterEach(gc)
it('should create a C function pointer from a JS function', function () {
var callback = ffi.Callback('void', [ ], function (val) { })
assert(Buffer.isBuffer(callback))
})
it('should be invokable by an ffi\'d ForeignFunction', function () {
var funcPtr = ffi.Callback(int, [ int ], Math.abs)
var func = ffi.ForeignFunction(funcPtr, int, [ int ])
assert.equal(1234, func(-1234))
})
it('should work with a "void" return type', function () {
var funcPtr = ffi.Callback('void', [ ], function (val) { })
var func = ffi.ForeignFunction(funcPtr, 'void', [ ])
assert.strictEqual(null, func())
})
it('should not call "set()" of a pointer type', function () {
var voidType = Object.create(ref.types.void)
voidType.get = function () {
throw new Error('"get()" should not be called')
}
voidType.set = function () {
throw new Error('"set()" should not be called')
}
var voidPtr = ref.refType(voidType)
var called = false
var cb = ffi.Callback(voidPtr, [ voidPtr ], function (ptr) {
called = true
assert.equal(0, ptr.address())
return ptr
})
var fn = ffi.ForeignFunction(cb, voidPtr, [ voidPtr ])
assert(!called)
var nul = fn(ref.NULL)
assert(called)
assert(Buffer.isBuffer(nul))
assert.equal(0, nul.address())
})
it('should throw an Error when invoked through a ForeignFunction and throws', function () {
var cb = ffi.Callback('void', [ ], function () {
throw new Error('callback threw')
})
var fn = ffi.ForeignFunction(cb, 'void', [ ])
assert.throws(function () {
fn()
}, /callback threw/)
})
it('should throw an Error with a meaningful message when a type\'s "set()" throws', function () {
var cb = ffi.Callback('int', [ ], function () {
// Changed, because returning string is not failing because of this
// https://github.com/iojs/io.js/issues/1161
return 1111111111111111111111
})
var fn = ffi.ForeignFunction(cb, 'int', [ ])
assert.throws(function () {
fn()
}, /error setting return value/)
})
it('should throw an Error when invoked after the callback gets garbage collected', function () {
var cb = ffi.Callback('void', [ ], function () { })
// register the callback function
bindings.set_cb(cb)
// should be ok
bindings.call_cb()
cb = null // KILL!!
gc()
// should throw an Error synchronously
assert.throws(function () {
bindings.call_cb()
}, /callback has been garbage collected/)
})
/**
* We should make sure that callbacks or errors gets propagated back to node's main thread
* when it called on a non libuv native thread.
* See: https://github.com/node-ffi/node-ffi/issues/199
*/
it("should propagate callbacks and errors back from native threads", function(done) {
var invokeCount = 0
var cb = ffi.Callback('void', [ ], function () {
invokeCount++
})
var kill = (function (cb) {
// register the callback function
bindings.set_cb(cb)
return function () {
var c = cb
cb = null // kill
c = null // kill!!!
}
})(cb)
// destroy the outer "cb". now "kill()" holds the "cb" reference
cb = null
// invoke the callback a couple times
assert.equal(0, invokeCount)
bindings.call_cb_from_thread()
bindings.call_cb_from_thread()
setTimeout(function () {
assert.equal(2, invokeCount)
gc() // ensure the outer "cb" Buffer is collected
process.nextTick(finish)
}, 100)
function finish () {
kill()
gc() // now ensure the inner "cb" Buffer is collected
// should throw an Error asynchronously!,
// because the callback has been garbage collected.
// hijack the "uncaughtException" event for this test
var listeners = process.listeners('uncaughtException').slice()
process.removeAllListeners('uncaughtException')
process.once('uncaughtException', function (e) {
var err
try {
assert(/ffi/.test(e.message))
} catch (ae) {
err = ae
}
done(err)
listeners.forEach(function (fn) {
process.on('uncaughtException', fn)
})
})
bindings.call_cb_from_thread()
}
})
describe('async', function () {
it('should be invokable asynchronously by an ffi\'d ForeignFunction', function (done) {
var funcPtr = ffi.Callback(int, [ int ], Math.abs)
var func = ffi.ForeignFunction(funcPtr, int, [ int ])
func.async(-9999, function (err, res) {
assert.equal(null, err)
assert.equal(9999, res)
done()
})
})
/**
* See https://github.com/rbranson/node-ffi/issues/153.
*/
it('multiple callback invocations from uv thread pool should be properly synchronized', function (done) {
this.timeout(10000)
var iterations = 30000
var cb = ffi.Callback('string', [ 'string' ], function (val) {
if (val === "ping" && --iterations > 0) {
return "pong"
}
return "end"
})
var pingPongFn = ffi.ForeignFunction(bindings.play_ping_pong, 'void', [ 'pointer' ])
pingPongFn.async(cb, function (err, ret) {
assert.equal(iterations, 0)
done()
})
})
/**
* See https://github.com/rbranson/node-ffi/issues/72.
* This is a tough issue. If we pass the ffi_closure Buffer to some foreign
* C function, we really don't know *when* it's safe to dispose of the Buffer,
* so it's left up to the developer.
*
* In this case, we wrap the responsibility in a simple "kill()" function
* that, when called, destroys of its references to the ffi_closure Buffer.
*/
it('should work being invoked multiple times', function (done) {
var invokeCount = 0
var cb = ffi.Callback('void', [ ], function () {
invokeCount++
})
var kill = (function (cb) {
// register the callback function
bindings.set_cb(cb)
return function () {
var c = cb
cb = null // kill
c = null // kill!!!
}
})(cb)
// destroy the outer "cb". now "kill()" holds the "cb" reference
cb = null
// invoke the callback a couple times
assert.equal(0, invokeCount)
bindings.call_cb()
assert.equal(1, invokeCount)
bindings.call_cb()
assert.equal(2, invokeCount)
setTimeout(function () {
// invoke it once more for shits and giggles
bindings.call_cb()
assert.equal(3, invokeCount)
gc() // ensure the outer "cb" Buffer is collected
process.nextTick(finish)
}, 25)
function finish () {
bindings.call_cb()
assert.equal(4, invokeCount)
kill()
gc() // now ensure the inner "cb" Buffer is collected
// should throw an Error synchronously
try {
bindings.call_cb()
assert(false) // shouldn't get here
} catch (e) {
assert(/ffi/.test(e.message))
}
done()
}
})
it('should throw an Error when invoked after the callback gets garbage collected', function (done) {
var cb = ffi.Callback('void', [ ], function () { })
// register the callback function
bindings.set_cb(cb)
// should be ok
bindings.call_cb()
// hijack the "uncaughtException" event for this test
var listeners = process.listeners('uncaughtException').slice()
process.removeAllListeners('uncaughtException')
process.once('uncaughtException', function (e) {
var err
try {
assert(/ffi/.test(e.message))
} catch (ae) {
err = ae
}
done(err)
listeners.forEach(function (fn) {
process.on('uncaughtException', fn)
})
})
cb = null // KILL!!
gc()
// should generate an "uncaughtException" asynchronously
bindings.call_cb_async()
})
})
})