-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathfunction.cc
More file actions
698 lines (536 loc) · 22.4 KB
/
function.cc
File metadata and controls
698 lines (536 loc) · 22.4 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include <string.h>
#include <girffi.h>
#include "boxed.h"
#include "callback.h"
#include "debug.h"
#include "error.h"
#include "function.h"
#include "gobject.h"
#include "macros.h"
#include "type.h"
#include "value.h"
using v8::Array;
using v8::TypedArray;
using v8::External;
using v8::Function;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Persistent;
using v8::String;
using v8::Value;
using Nan::New;
using Nan::WeakCallbackType;
namespace GNodeJS {
static void FillArgument(GIArgInfo *arg_info, GIArgument *argument, Local<Value> value) {
GITypeInfo type_info;
bool may_be_null = g_arg_info_may_be_null (arg_info);
g_arg_info_load_type (arg_info, &type_info);
V8ToGIArgument(&type_info, argument, value, may_be_null);
}
static int GetV8ArrayLength (Local<Value> value) {
if (value->IsArray())
return Local<Array>::Cast (TO_OBJECT (value))->Length();
else if (value->IsString())
return TO_STRING (value)->Length();
else if (value->IsTypedArray())
return Local<TypedArray>::Cast (TO_OBJECT (value))->Length();
else if (value->IsNull() || value->IsUndefined())
return 0;
ERROR("Could not determine array length for value %s",
*Nan::Utf8String(TO_STRING (value)));
}
static void* AllocateArgument (GIBaseInfo *arg_info) {
GITypeInfo arg_type;
g_arg_info_load_type(arg_info, &arg_type);
g_assert(g_type_info_get_tag(&arg_type) == GI_TYPE_TAG_INTERFACE);
GIBaseInfo* base_info = g_type_info_get_interface (&arg_type);
size_t size = Boxed::GetSize (base_info);
void* pointer = g_malloc0(size);
g_base_info_unref(base_info);
return pointer;
}
static bool IsMethod (GIBaseInfo *info) {
auto flags = g_function_info_get_flags (info);
return ((flags & GI_FUNCTION_IS_METHOD) != 0 &&
(flags & GI_FUNCTION_IS_CONSTRUCTOR) == 0);
}
static bool ShouldSkipReturn(GIBaseInfo *info, GITypeInfo *return_type) {
return g_type_info_get_tag(return_type) == GI_TYPE_TAG_VOID
|| g_callable_info_skip_return(info) == TRUE;
}
static inline bool IsDirectionOut (GIDirection direction) {
return (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT);
}
static inline bool IsDirectionIn (GIDirection direction) {
return (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT);
}
static inline bool IsPointerType(GITypeInfo *type_info) {
auto tag = g_type_info_get_tag (type_info);
if (tag != GI_TYPE_TAG_INTERFACE)
return false;
auto interface_info = g_type_info_get_interface (type_info);
auto interface_type = g_base_info_get_type (interface_info);
bool isPointer =
interface_type != GI_INFO_TYPE_ENUM &&
interface_type != GI_INFO_TYPE_FLAGS;
g_base_info_unref(interface_info);
return isPointer;
}
bool IsDestroyNotify (GIBaseInfo *info) {
return strcmp(g_base_info_get_name(info), "DestroyNotify") == 0
&& strcmp(g_base_info_get_namespace(info), "GLib") == 0;
}
/**
* The constructor just stores the GIBaseInfo ref. The rest of the
* initialization is done in FunctionInfo::Init, lazily.
*/
FunctionInfo::FunctionInfo (GIBaseInfo* gi_info) {
info = g_base_info_ref (gi_info);
call_parameters = nullptr;
}
FunctionInfo::~FunctionInfo () {
g_base_info_unref (info);
if (call_parameters != nullptr) {
g_function_invoker_destroy (&invoker);
delete[] call_parameters;
}
}
/**
* Initializes the parameters metadata (number, directionality, type) and caches it
*/
bool FunctionInfo::Init() {
if (call_parameters != nullptr)
return true;
g_function_info_prep_invoker (info, &invoker, NULL);
is_method = IsMethod(info);
can_throw = g_callable_info_can_throw_gerror (info);
n_callable_args = g_callable_info_get_n_args (info);
n_total_args = n_callable_args;
n_out_args = 0;
n_in_args = 0;
if (is_method)
n_total_args++;
if (can_throw)
n_total_args++;
call_parameters = new Parameter[n_callable_args]();
/*
* Examine load parameter types and count arguments
*/
for (int i = 0; i < n_callable_args; i++) {
GIArgInfo arg_info;
GITypeInfo type_info;
g_callable_info_load_arg ((GICallableInfo *) info, i, &arg_info);
g_arg_info_load_type (&arg_info, &type_info);
bool may_be_null = g_arg_info_may_be_null (&arg_info);
GIDirection direction = g_arg_info_get_direction (&arg_info);
GITypeTag tag = g_type_info_get_tag(&type_info);
call_parameters[i].direction = direction;
if (call_parameters[i].type == ParameterType::kSKIP)
continue;
// If there is an array length, this is an array
int length_i = g_type_info_get_array_length (&type_info);
if (tag == GI_TYPE_TAG_ARRAY && length_i >= 0) {
call_parameters[i].type = ParameterType::kARRAY;
call_parameters[length_i].type = ParameterType::kSKIP;
// If array length came before, we need to remove it from args count
if (IsDirectionIn(call_parameters[length_i].direction) && length_i < i)
n_in_args--;
if (IsDirectionOut(call_parameters[length_i].direction) && length_i < i)
n_out_args--;
} else if (tag == GI_TYPE_TAG_INTERFACE) {
GIBaseInfo* interface_info = g_type_info_get_interface(&type_info);
GIInfoType interface_type = g_base_info_get_type(interface_info);
if (interface_type == GI_INFO_TYPE_CALLBACK) {
if (IsDestroyNotify(interface_info)) {
/* Skip GDestroyNotify if they appear before the respective callback */
call_parameters[i].type = ParameterType::kSKIP;
} else {
call_parameters[i].type = ParameterType::kCALLBACK;
int destroy_i = g_arg_info_get_destroy(&arg_info);
int closure_i = g_arg_info_get_closure(&arg_info);
if (destroy_i >= 0 && closure_i < 0) {
Throw::UnsupportedCallback (info);
g_base_info_unref(interface_info);
return false;
}
if (destroy_i >= 0 && destroy_i < n_callable_args)
call_parameters[destroy_i].type = ParameterType::kSKIP;
if (closure_i >= 0 && closure_i < n_callable_args)
call_parameters[closure_i].type = ParameterType::kSKIP;
if (destroy_i < i) {
if (IsDirectionIn(call_parameters[destroy_i].direction))
n_in_args--;
if (IsDirectionOut(call_parameters[destroy_i].direction))
n_out_args--;
}
if (closure_i < i) {
if (IsDirectionIn(call_parameters[closure_i].direction))
n_in_args--;
if (IsDirectionOut(call_parameters[closure_i].direction))
n_out_args--;
}
}
}
g_base_info_unref(interface_info);
}
if (IsDirectionIn(call_parameters[i].direction) && !may_be_null)
n_in_args++;
if (IsDirectionOut(call_parameters[i].direction))
n_out_args++;
}
/*
* Examine return type
*/
GITypeInfo return_type;
g_callable_info_load_return_type(info, &return_type);
int return_length_i = g_type_info_get_array_length(&return_type);
if (return_length_i != -1)
n_out_args--;
if (!ShouldSkipReturn(info, &return_type))
n_out_args++;
return true;
}
/**
* Type checks the JS arguments, throwing an error.
* @returns true if types match
*/
bool FunctionInfo::TypeCheck (const Nan::FunctionCallbackInfo<Value> &arguments) {
if (arguments.Length() < n_in_args) {
Throw::NotEnoughArguments(n_in_args, arguments.Length());
return false;
}
/*
* Type check every IN-argument that is not skipped
*/
for (int in_arg = 0, i = 0; i < n_callable_args; i++) {
Parameter ¶m = call_parameters[i];
if (param.type == ParameterType::kSKIP)
continue;
GIArgInfo arg_info;
g_callable_info_load_arg (info, i, &arg_info);
GIDirection direction = g_arg_info_get_direction (&arg_info);
if (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT) {
GITypeInfo type_info;
g_arg_info_load_type (&arg_info, &type_info);
bool may_be_null = g_arg_info_may_be_null (&arg_info);
if (!CanConvertV8ToGIArgument(&type_info, arguments[in_arg], may_be_null)) {
Throw::InvalidType(&arg_info, &type_info, arguments[in_arg]);
return false;
}
in_arg++;
}
}
return true;
}
/**
* Calls a C function from JS arguments
* @param func the function info
* @param info JS call informations
* @param return_value (out, nullable) the C return value
* @param error (out, nullable) the C error - if null, can throw a JS error
* @returns the JS return value, if @return_value is null
*/
Local<Value> FunctionCall (
FunctionInfo *func,
const Nan::FunctionCallbackInfo<Value> &info,
GIArgument *return_value,
GError **error
) {
Local<Value> jsReturnValue;
GIBaseInfo *gi_info = func->info; // do-not-free
bool use_return_value = return_value != NULL;
bool use_error = error != NULL;
if (!func->Init())
return jsReturnValue;
if (!func->TypeCheck(info))
return jsReturnValue;
/*
* First, add arguments for the instance if it's a method,
* and for error, if it can throw
*/
#ifndef __linux__
GIArgument *total_arg_values = new GIArgument[func->n_total_args]();
#else
GIArgument total_arg_values[func->n_total_args];
#endif
GIArgument *callable_arg_values;
GError *error_stack = nullptr;
if (func->is_method) {
GIBaseInfo *container = g_base_info_get_container (gi_info);
V8ToGIArgumentInterface(container, &total_arg_values[0], info.This());
callable_arg_values = &total_arg_values[1];
} else {
callable_arg_values = &total_arg_values[0];
}
if (func->can_throw)
callable_arg_values[func->n_callable_args].v_pointer = error != NULL ? error : &error_stack;
/*
* Second, allocate OUT-arguments and fill IN-arguments
*/
for (int in_arg = 0, i = 0; i < func->n_callable_args; i++) {
Parameter& param = func->call_parameters[i];
if (param.type == ParameterType::kSKIP)
continue;
GIArgInfo arg_info;
GITypeInfo type_info;
g_callable_info_load_arg (gi_info, i, &arg_info);
g_arg_info_load_type (&arg_info, &type_info);
GIDirection direction = g_arg_info_get_direction (&arg_info);
if (param.type == ParameterType::kARRAY) {
GIArgInfo array_length_arg;
GITypeInfo array_length_type;
int length_i = g_type_info_get_array_length (&type_info);
g_callable_info_load_arg(gi_info, length_i, &array_length_arg);
g_arg_info_load_type (&array_length_arg, &array_length_type);
Parameter& len_param = func->call_parameters[length_i];
if (len_param.direction == GI_DIRECTION_IN) {
param.length = GetV8ArrayLength(info[in_arg]);
callable_arg_values[length_i].v_long = param.length;
}
else if (len_param.direction == GI_DIRECTION_INOUT) {
len_param.data.v_long = GetV8ArrayLength(info[in_arg]);
callable_arg_values[length_i].v_pointer = &len_param.data;
}
else if (direction == GI_DIRECTION_OUT) {
len_param.data = {};
callable_arg_values[length_i].v_pointer = &len_param.data;
}
}
else if (param.type == ParameterType::kCALLBACK) {
Callback *callback;
ffi_closure *closure;
if (info[in_arg]->IsNullOrUndefined()) {
closure = nullptr;
callback = nullptr;
} else {
GICallableInfo *callback_info = g_type_info_get_interface (&type_info);
GIScopeType scope_type = g_arg_info_get_scope(&arg_info);
callback = new Callback(info[in_arg].As<Function>(), callback_info, scope_type);
closure = callback->closure;
g_base_info_unref (callback_info);
}
int destroy_i = g_arg_info_get_destroy(&arg_info);
int closure_i = g_arg_info_get_closure(&arg_info);
if (destroy_i >= 0) {
g_assert (func->call_parameters[destroy_i].type == ParameterType::kSKIP);
callable_arg_values[destroy_i].v_pointer = callback ? (void*) Callback::DestroyNotify : NULL;
}
if (closure_i >= 0) {
g_assert (func->call_parameters[closure_i].type == ParameterType::kSKIP);
callable_arg_values[closure_i].v_pointer = callback;
}
callable_arg_values[i].v_pointer = closure;
func->call_parameters[i].data.v_pointer = callback;
}
if (direction == GI_DIRECTION_OUT) {
if (g_arg_info_is_caller_allocates (&arg_info)) {
callable_arg_values[i].v_pointer = AllocateArgument(&arg_info);
} else /* callee will allocate */ {
param.data = {};
callable_arg_values[i].v_pointer = ¶m.data;
}
}
else /* (direction == GI_DIRECTION_IN || direction == GI_DIRECTION_INOUT) */ {
// Callback GIArgument is filled above, for the rest...
if (param.type != ParameterType::kCALLBACK) {
// FIXME(handle failure here)
FillArgument(&arg_info, &callable_arg_values[i], info[in_arg]);
// Add a level of indirection for INOUT arguments
if (direction == GI_DIRECTION_INOUT) {
param.data = {};
param.data.v_pointer = callable_arg_values[i].v_pointer;
callable_arg_values[i].v_pointer = ¶m.data;
}
}
in_arg++;
}
}
/*
* Third, make the actual ffi_call
*/
#ifndef __linux__
void **ffi_args = new void*[func->n_total_args]();
#else
void *ffi_args[func->n_total_args];
#endif
for (int i = 0; i < func->n_total_args; i++)
ffi_args[i] = (void *)&total_arg_values[i];
GIArgument return_value_stack = {0};
ffi_call (&func->invoker.cif, FFI_FN (func->invoker.native_address),
use_return_value ? return_value : &return_value_stack, ffi_args);
#ifndef __linux__
delete[] ffi_args;
#endif
/*
* Fourth, convert the return value & OUT-arguments back to JS
*/
GITypeInfo return_type;
g_callable_info_load_return_type(gi_info, &return_type);
GITransfer return_transfer = g_callable_info_get_caller_owns(gi_info);
bool didThrow = error ? *error != NULL : error_stack != NULL;
// Return the value or throw the error, if any occured
if (didThrow) {
jsReturnValue = Nan::Undefined();
if (!use_error) {
Nan::ThrowError(error_stack->message);
g_error_free(error_stack);
}
/*
* Fifth, free the return value and arguments
*/
if (!use_return_value)
FreeGIArgument(&return_type, &return_value_stack, return_transfer);
} else if (!use_return_value) {
// Value transferred to jsReturnValue
jsReturnValue = func->JsReturnValue (
info.This(),
&return_type,
&return_value_stack,
callable_arg_values,
return_transfer);
} else {
// Value returned in return_value
jsReturnValue = Nan::Undefined();
}
for (int i = 0; i < func->n_callable_args; i++) {
GIArgInfo arg_info = {};
GITypeInfo arg_type;
GIArgument arg_value = callable_arg_values[i];
Parameter ¶m = func->call_parameters[i];
g_callable_info_load_arg ((GICallableInfo *) gi_info, i, &arg_info);
g_arg_info_load_type (&arg_info, &arg_type);
GIDirection direction = g_arg_info_get_direction (&arg_info);
GITransfer transfer = g_arg_info_get_ownership_transfer (&arg_info);
if (param.type == ParameterType::kARRAY) {
if (direction == GI_DIRECTION_INOUT || direction == GI_DIRECTION_OUT)
FreeGIArgumentArray (&arg_type, (GIArgument*)arg_value.v_pointer, transfer, direction, param.length);
else
FreeGIArgumentArray (&arg_type, &arg_value, transfer, direction, param.length);
}
else if (param.type == ParameterType::kCALLBACK) {
Callback *callback = static_cast<Callback*>(func->call_parameters[i].data.v_pointer);
g_assert(direction == GI_DIRECTION_IN);
if (callback && callback->scope_type == GI_SCOPE_TYPE_CALL) {
delete callback;
}
}
else {
if (direction == GI_DIRECTION_INOUT || (direction == GI_DIRECTION_OUT && !g_arg_info_is_caller_allocates (&arg_info)))
FreeGIArgument (&arg_type, (GIArgument*)arg_value.v_pointer, transfer, direction);
else
FreeGIArgument (&arg_type, &arg_value, transfer, direction);
}
}
#ifndef __linux__
delete[] total_arg_values;
#endif
return jsReturnValue;
}
/**
* Creates the JS return value from the C arguments list
* @returns the JS return value
*/
Local<Value> FunctionInfo::JsReturnValue (
Local<Value> self,
GITypeInfo* return_type,
GIArgument* return_value,
GIArgument* callable_arg_values,
GITransfer return_transfer) {
Local<Value> jsReturnValue;
int jsReturnIndex = 0;
if (n_out_args > 1)
jsReturnValue = Nan::New<Array>();
#define ADD_RETURN(value) if (n_out_args > 1) \
Nan::Set(TO_OBJECT (jsReturnValue), jsReturnIndex++, (value)); \
else \
jsReturnValue = (value);
int return_length_i = g_type_info_get_array_length(return_type);
if (!ShouldSkipReturn(info, return_type)) {
long length = -1;
if (return_length_i >= 0) {
GIArgInfo length_info;
g_callable_info_load_arg (info, return_length_i, &length_info);
GITypeInfo length_type;
g_arg_info_load_type (&length_info, &length_type);
length =
GIArgumentToLength(
&length_type,
&callable_arg_values[return_length_i],
IsDirectionOut(call_parameters[return_length_i].direction));
}
// When a method returns the instance itself, skip the conversion and just return the
// existent wrapper
bool isReturningSelf = is_method && PointerFromWrapper(self) == return_value->v_pointer;
ADD_RETURN (isReturningSelf ? self :
GIArgumentToV8 (
return_type, return_value, length,
return_transfer == GI_TRANSFER_EVERYTHING ? kTransfer : kNone))
}
for (int i = 0; i < n_callable_args; i++) {
if (return_length_i == i)
continue;
GIArgInfo arg_info = {};
GITypeInfo arg_type;
GIArgument arg_value = callable_arg_values[i];
Parameter ¶m = call_parameters[i];
g_callable_info_load_arg (info, i, &arg_info);
g_arg_info_load_type (&arg_info, &arg_type);
GIDirection direction = g_arg_info_get_direction (&arg_info);
if (direction == GI_DIRECTION_OUT || direction == GI_DIRECTION_INOUT) {
if (param.type == ParameterType::kARRAY) {
int length_i = g_type_info_get_array_length(&arg_type);
GIArgInfo length_arg;
g_callable_info_load_arg (info, length_i, &length_arg);
GITypeInfo length_type;
g_arg_info_load_type (&length_arg, &length_type);
GIDirection length_direction = g_arg_info_get_direction(&length_arg);
param.length =
GIArgumentToLength(
&length_type,
&callable_arg_values[length_i],
IsDirectionOut(length_direction));
Local<Value> result = ArrayToV8(&arg_type, *(void**)arg_value.v_pointer, param.length);
ADD_RETURN (result)
} else if (param.type == ParameterType::kNORMAL) {
GITransfer transfer = g_arg_info_get_ownership_transfer(&arg_info);
ResourceOwnership ownership = transfer == GI_TRANSFER_EVERYTHING ? kTransfer : kNone;
if (IsPointerType(&arg_type) && g_arg_info_is_caller_allocates(&arg_info)) {
void *pointer = &arg_value.v_pointer;
ADD_RETURN (GIArgumentToV8(&arg_type, (GIArgument*) pointer, -1, ownership))
}
else {
ADD_RETURN (GIArgumentToV8(&arg_type, (GIArgument*) arg_value.v_pointer, -1, ownership))
}
}
}
}
#undef ADD_RETURN
return jsReturnValue;
}
Local<Function> MakeFunction(GIBaseInfo *info) {
FunctionInfo *func = new FunctionInfo(info);
auto external = New<External>(func);
auto name = UTF8(g_function_info_get_symbol (info));
auto tpl = New<FunctionTemplate>(FunctionInvoker, external);
tpl->SetLength(g_callable_info_get_n_args (info));
auto fn = Nan::GetFunction (tpl).ToLocalChecked();
fn->SetName(name);
func->persistent = new Nan::Persistent<FunctionTemplate>(tpl);
func->persistent->SetWeak(func, FunctionDestroyed, WeakCallbackType::kParameter);
return fn;
}
void FunctionInvoker(const Nan::FunctionCallbackInfo<Value> &info) {
FunctionInfo *func = (FunctionInfo *) External::Cast (*info.Data ())->Value ();
Local<Value> jsReturnValue = FunctionCall (func, info);
if (!jsReturnValue.IsEmpty()) {
RETURN (jsReturnValue);
}
Callback::AsyncFree();
}
void FunctionDestroyed(const Nan::WeakCallbackInfo<FunctionInfo> &data) {
FunctionInfo *func = data.GetParameter ();
delete func->persistent;
delete func;
}
};