ffi_tests.cc
8.2 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "v8.h"
#include "node.h"
#include "node_buffer.h"
#include <nan.h>
#ifdef WIN32
#include <process.h>
#else
#include <pthread.h>
#endif // WIN32
using namespace v8;
using namespace node;
/*
* Exported function with C naming and calling conventions.
* Used by dynamic_library.js to test symbol lookup.
* Never actually called.
*/
extern "C"
int
NODE_MODULE_EXPORT
ExportedFunction(int value)
{
return value * 2;
}
namespace {
/*
* Test struct definition used in the test harness functions below.
*/
typedef struct box {
int width;
int height;
} _box;
/*
* Accepts a struct by value, and returns a struct by value.
*/
box double_box(box input) {
box rtn;
// modify the input box, ensure on the JS side that it's not altered
input.width *= 2;
input.height *= 2;
rtn.width = input.width;
rtn.height = input.height;
return rtn;
}
/*
* Accepts a box struct pointer, and returns a struct by value.
*/
box double_box_ptr(box *input) {
box rtn;
// modify the input box, ensure on the JS side that IT IS altered
input->width *= 2;
input->height *= 2;
rtn.width = input->width;
rtn.height = input->height;
return rtn;
}
/*
* Accepts a struct by value, and returns an int.
*/
int area_box(box input) {
return input.width * input.height;
}
/*
* Accepts a box pointer and returns an int.
*/
int area_box_ptr(box *input) {
return input->width * input->height;
}
/*
* Creates a box and returns it by value.
*/
box create_box(int width, int height) {
box rtn = { width, height };
return rtn;
}
/*
* Creates a box that has the sum of the width and height for its own values.
*/
box add_boxes(box boxes[], int num) {
box rtn = { 0, 0 };
box cur;
for (int i = 0; i < num; i++) {
cur = boxes[i];
rtn.width += cur.width;
rtn.height += cur.height;
}
return rtn;
}
/*
* Reads "ints" from the "input" array until -1 is found.
* Returns the number of elements in the array.
*/
int *int_array(int *input) {
int *array = input;
while (*array != -1){
*array = *array * 2;
array++;
}
return input;
}
/*
* Tests for passing a Struct that contains Arrays inside of it.
*/
struct arst {
int num;
double array[20];
};
struct arst array_in_struct (struct arst input) {
struct arst rtn;
rtn.num = input.num * 2;
for (int i = 0; i < 20; i++) {
rtn.array[i] = input.array[i] * 3.14;
}
return rtn;
}
/*
* Tests for C function pointers.
*/
typedef int (*my_callback)(int);
my_callback callback_func (my_callback cb) {
return cb;
}
/*
* Hard-coded `strtoul` binding, for the benchmarks.
*
* args[0] - the string number to convert to a real Number
* args[1] - a "buffer" instance to write into (the "endptr")
* args[2] - the base (0 means autodetect)
*/
NAN_METHOD(Strtoul) {
Nan::HandleScope();
int base;
char **endptr;
Nan::Utf8String buf(info[0]);
Local<Value> endptr_arg = info[1];
endptr = (char **)Buffer::Data(endptr_arg.As<Object>());
base = info[2]->Int32Value();
unsigned long val = strtoul(*buf, endptr, base);
info.GetReturnValue().Set(Nan::New<Integer>((uint32_t)val));
}
// experiments for #72
typedef void (*cb)(void);
static cb callback = NULL;
NAN_METHOD(SetCb) {
Nan::HandleScope();
char *buf = Buffer::Data(info[0].As<Object>());
callback = (cb)buf;
info.GetReturnValue().SetUndefined();
}
NAN_METHOD(CallCb) {
Nan::HandleScope();
if (callback == NULL) {
return Nan::ThrowError("you must call \"set_cb()\" first");
} else {
callback();
}
info.GetReturnValue().SetUndefined();
}
// Invoke callback from a native (non libuv) thread:
#ifdef WIN32
void invoke_callback(void* args) {
#else
void* invoke_callback(void* args) {
#endif // WIN32
cb c = callback;
if (c != NULL) {
c();
}
#ifndef WIN32
return NULL;
#endif // WIN32
}
NAN_METHOD(CallCbFromThread) {
Nan::HandleScope();
if (callback == NULL) {
return Nan::ThrowError("you must call \"set_cb()\" first");
}
else {
#ifdef WIN32
_beginthread(&invoke_callback, 0, NULL);
#else
pthread_t thread;
pthread_create(&thread, NULL, &invoke_callback, NULL);
#endif // WIN32
}
info.GetReturnValue().SetUndefined();
}
void AsyncCbCall(uv_work_t *req) {
cb c = (cb)req->data;
c();
}
void FinishAsyncCbCall(uv_work_t *req) {
// nothing
delete req;
}
NAN_METHOD(CallCbAsync) {
Nan::HandleScope();
if (callback == NULL) {
return Nan::ThrowError("you must call \"set_cb()\" first");
} else {
uv_work_t *req = new uv_work_t;
req->data = (void *)callback;
uv_queue_work(uv_default_loop(), req, AsyncCbCall, (uv_after_work_cb)FinishAsyncCbCall);
}
info.GetReturnValue().SetUndefined();
}
// Race condition in threaded callback invocation testing
// https://github.com/node-ffi/node-ffi/issues/153
void play_ping_pong (const char* (*callback) (const char*)) {
const char * response;
do {
response = callback("ping");
} while (strcmp(response, "pong") == 0);
}
// https://github.com/node-ffi/node-ffi/issues/169
int test_169(char* dst, int len) {
const char src[] = "sample str\0";
strncpy(dst, src, len);
return fmin(len, strlen(src));
}
// https://github.com/TooTallNate/ref/issues/56
struct Obj56 {
bool traceMode;
};
int test_ref_56(struct Obj56 *obj) {
return obj->traceMode ? 1 : 0;
}
void wrap_pointer_cb(char *data, void *hint) {
}
inline Local<Value> WrapPointer(char *ptr, size_t length) {
Nan::EscapableHandleScope scope;
return scope.Escape(Nan::NewBuffer(ptr, length, wrap_pointer_cb, NULL).ToLocalChecked());
}
inline Local<Value> WrapPointer(char *ptr) {
return WrapPointer(ptr, 0);
}
void Initialize(Handle<Object> target) {
Nan::HandleScope();
#if WIN32
// initialize "floating point support" on Windows?!?!
// (this is some serious bullshit...)
// http://support.microsoft.com/kb/37507
float x = 2.3f;
#endif
// atoi and abs here for testing purposes
target->Set(Nan::New<String>("atoi").ToLocalChecked(), WrapPointer((char *)atoi));
// Windows has multiple `abs` signatures, so we need to manually disambiguate
int (*absPtr)(int)(abs);
target->Set(Nan::New<String>("abs").ToLocalChecked(), WrapPointer((char *)absPtr));
// sprintf pointer; used in the varadic tests
target->Set(Nan::New<String>("sprintf").ToLocalChecked(), WrapPointer((char *)sprintf));
// hard-coded `strtoul` binding, for the benchmarks
Nan::Set(target, Nan::New<String>("strtoul").ToLocalChecked(),
Nan::New<FunctionTemplate>(Strtoul)->GetFunction());
Nan::Set(target, Nan::New<String>("set_cb").ToLocalChecked(),
Nan::New<FunctionTemplate>(SetCb)->GetFunction());
Nan::Set(target, Nan::New<String>("call_cb").ToLocalChecked(),
Nan::New<FunctionTemplate>(CallCb)->GetFunction());
Nan::Set(target, Nan::New<String>("call_cb_from_thread").ToLocalChecked(),
Nan::New<FunctionTemplate>(CallCbFromThread)->GetFunction());
Nan::Set(target, Nan::New<String>("call_cb_async").ToLocalChecked(),
Nan::New<FunctionTemplate>(CallCbAsync)->GetFunction());
// also need to test these custom functions
target->Set(Nan::New<String>("double_box").ToLocalChecked(), WrapPointer((char *)double_box));
target->Set(Nan::New<String>("double_box_ptr").ToLocalChecked(), WrapPointer((char *)double_box_ptr));
target->Set(Nan::New<String>("area_box").ToLocalChecked(), WrapPointer((char *)area_box));
target->Set(Nan::New<String>("area_box_ptr").ToLocalChecked(), WrapPointer((char *)area_box_ptr));
target->Set(Nan::New<String>("create_box").ToLocalChecked(), WrapPointer((char *)create_box));
target->Set(Nan::New<String>("add_boxes").ToLocalChecked(), WrapPointer((char *)add_boxes));
target->Set(Nan::New<String>("int_array").ToLocalChecked(), WrapPointer((char *)int_array));
target->Set(Nan::New<String>("array_in_struct").ToLocalChecked(), WrapPointer((char *)array_in_struct));
target->Set(Nan::New<String>("callback_func").ToLocalChecked(), WrapPointer((char *)callback_func));
target->Set(Nan::New<String>("play_ping_pong").ToLocalChecked(), WrapPointer((char *)play_ping_pong));
target->Set(Nan::New<String>("test_169").ToLocalChecked(), WrapPointer((char *)test_169));
target->Set(Nan::New<String>("test_ref_56").ToLocalChecked(), WrapPointer((char *)test_ref_56));
}
} // anonymous namespace
NODE_MODULE(ffi_tests, Initialize);