-
Notifications
You must be signed in to change notification settings - Fork 157
/
node_loader_impl.cpp
1918 lines (1307 loc) · 48 KB
/
node_loader_impl.cpp
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Loader Library by Parra Studios
* Copyright (C) 2016 - 2019 Vicente Eduardo Ferrer Garcia <vic798@gmail.com>
*
* A plugin for loading nodejs code at run-time into a process.
*
*/
#if defined(WIN32) || defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <io.h>
# ifndef dup
# define dup _dup
# endif
# ifndef dup2
# define dup2 _dup2
# endif
# ifndef STDIN_FILENO
# define STDIN_FILENO _fileno(stdin)
# endif
# ifndef STDOUT_FILENO
# define STDOUT_FILENO _fileno(stdout)
# endif
# ifndef STDERR_FILENO
# define STDERR_FILENO _fileno(stderr)
# endif
#else
# include <unistd.h>
#endif
#include <node_loader/node_loader_impl.h>
#include <loader/loader_impl.h>
#include <reflect/reflect_type.h>
#include <reflect/reflect_function.h>
#include <reflect/reflect_scope.h>
#include <reflect/reflect_context.h>
/* TODO: Make logs thread safe */
#include <log/log.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <new>
#include <string>
#include <fstream>
#include <streambuf>
#include <libplatform/libplatform.h>
#include <v8.h> /* version: 6.2.414.50 */
#ifdef ENABLE_DEBUGGER_SUPPORT
# include <v8-debug.h>
#endif /* ENALBLE_DEBUGGER_SUPPORT */
#include <uv.h>
#include <node.h>
#include <node_api.h>
/* TODO:
To solve the deadlock we have to make MetaCall fork tolerant.
The problem is that when Linux makes a fork it uses fork-one strategy, this means
that only the caller thread is cloned, the others are not, so the NodeJS thread pool
does not survive. When the thread pool tries to continue it blocks the whole application.
To solve this, we have to:
- Change all mutex and conditions by a binary sempahore.
- Move all calls to MetaCall in async functions to outside of the async methods, passing result data in
the async data structure (async_object.data) because MetaCall is not thread safe and it can induce
other threads to overwrite data improperly.
- Make a detour to function fork. Intercept the function fork to shutdown all runtimes in the parent
and then re-initialize all of them in parent and child after the fork. pthread_atfork is not sufficient
because it is a bug on the POSIX standard (too many limitations are related to this technique).
*/
/* TODO: (2.0)
Detour method is not valid because of NodeJS cannot be reinitialized, platform pointer already initialized in CHECK macro
*/
typedef struct loader_impl_node_type
{
napi_env env;
napi_ref global_ref;
napi_ref function_table_object_ref;
uv_thread_t thread_id;
uv_loop_t * thread_loop;
uv_async_t async_initialize;
uv_async_t async_load_from_file;
uv_async_t async_clear;
uv_async_t async_discover;
uv_async_t async_func_call;
uv_async_t async_func_destroy;
uv_async_t async_destroy;
int result;
} * loader_impl_node;
typedef struct loader_impl_node_function_type
{
loader_impl_node node_impl;
napi_ref func_ref;
napi_value * argv;
} * loader_impl_node_function;
typedef struct loader_impl_async_load_from_file_type
{
loader_impl_node node_impl;
const loader_naming_path * paths;
size_t size;
napi_ref handle_ref;
} * loader_impl_async_load_from_file;
typedef struct loader_impl_async_clear_type
{
loader_impl_node node_impl;
napi_ref handle_ref;
} * loader_impl_async_clear;
typedef struct loader_impl_async_discover_type
{
loader_impl_node node_impl;
napi_ref handle_ref;
context ctx;
} * loader_impl_async_discover;
typedef struct loader_impl_async_func_call_type
{
loader_impl_node node_impl;
function func;
loader_impl_node_function node_func;
void ** args;
function_return ret;
} * loader_impl_async_func_call;
typedef struct loader_impl_async_func_destroy_type
{
loader_impl_node node_impl;
loader_impl_node_function node_func;
} * loader_impl_async_func_destroy;
static uv_mutex_t node_impl_mutex;
static uv_cond_t node_impl_cond;
/* Function */
int function_node_interface_create(function func, function_impl impl);
function_return function_node_interface_invoke(function func, function_impl impl, function_args args);
void function_node_interface_destroy(function func, function_impl impl);
function_interface function_node_singleton(void);
/* Async */
void node_loader_impl_async_initialize(uv_async_t * async);
void node_loader_impl_async_func_call(uv_async_t * async);
void node_loader_impl_async_func_destroy(uv_async_t * async);
void node_loader_impl_async_load_from_file(uv_async_t * async);
void node_loader_impl_async_clear(uv_async_t * async);
void node_loader_impl_async_discover(uv_async_t * async);
void node_loader_impl_async_destroy(uv_async_t * async);
/* Loader */
void * node_loader_impl_register(void * node_impl_ptr, void * env_ptr, void * function_table_object_ptr);
void node_loader_impl_thread(void * data);
void node_loader_impl_walk(uv_handle_t * handle, void * data);
/* Standard Input Output descriptors */
static int stdin_copy, stdout_copy, stderr_copy;
int function_node_interface_create(function func, function_impl impl)
{
loader_impl_node_function node_func = (loader_impl_node_function)impl;
signature s = function_signature(func);
const size_t args_size = signature_count(s);
node_func->argv = static_cast<napi_value *>(malloc(sizeof(napi_value) * args_size));
return (node_func->argv == NULL);
}
function_return function_node_interface_invoke(function func, function_impl impl, function_args args)
{
loader_impl_node_function node_func = (loader_impl_node_function)impl;
if (node_func != NULL)
{
loader_impl_node node_impl = node_func->node_impl;
struct loader_impl_async_func_call_type async_data =
{
node_impl,
func,
node_func,
static_cast<void **>(args),
NULL
};
uv_mutex_lock(&node_impl_mutex);
node_impl->async_func_call.data = static_cast<void *>(&async_data);
/* Execute function call async callback */
uv_async_send(&node_impl->async_func_call);
/* Wait until function is called */
uv_cond_wait(&node_impl_cond, &node_impl_mutex);
uv_mutex_unlock(&node_impl_mutex);
return async_data.ret;
}
return NULL;
}
void function_node_interface_destroy(function func, function_impl impl)
{
loader_impl_node_function node_func = (loader_impl_node_function)impl;
(void)func;
if (node_func != NULL)
{
loader_impl_node node_impl = node_func->node_impl;
struct loader_impl_async_func_destroy_type async_data =
{
node_impl,
node_func
};
uv_mutex_lock(&node_impl_mutex);
node_impl->async_func_destroy.data = static_cast<void *>(&async_data);
/* Execute function destroy async callback */
uv_async_send(&node_impl->async_func_destroy);
/* Wait until function is destroyed */
uv_cond_wait(&node_impl_cond, &node_impl_mutex);
uv_mutex_unlock(&node_impl_mutex);
free(node_func->argv);
free(node_func);
}
}
function_interface function_node_singleton()
{
static struct function_interface_type node_function_interface =
{
&function_node_interface_create,
&function_node_interface_invoke,
&function_node_interface_destroy
};
return &node_function_interface;
}
void node_loader_impl_async_initialize(uv_async_t * async)
{
loader_impl_node node_impl;
/* Lock node implementation mutex */
uv_mutex_lock(&node_impl_mutex);
node_impl = *(static_cast<loader_impl_node *>(async->data));
(void)node_impl;
/* Signal start condition */
uv_cond_signal(&node_impl_cond);
uv_mutex_unlock(&node_impl_mutex);
}
napi_value node_loader_impl_value(napi_env env, void * arg)
{
value arg_value = static_cast<value>(arg);
type_id id = value_type_id(arg_value);
napi_status status;
napi_value v;
if (id == TYPE_BOOL)
{
boolean bool_value = value_to_bool(arg_value);
status = napi_get_boolean(env, (bool_value == 0) ? false : true, &v);
assert(status == napi_ok);
}
else if (id == TYPE_CHAR)
{
char char_value = value_to_char(arg_value);
status = napi_create_int32(env, static_cast<int32_t>(char_value), &v);
assert(status == napi_ok);
}
else if (id == TYPE_SHORT)
{
short short_value = value_to_short(arg_value);
status = napi_create_int32(env, static_cast<int32_t>(short_value), &v);
assert(status == napi_ok);
}
else if (id == TYPE_INT)
{
int int_value = value_to_int(arg_value);
/* TODO: Check integer overflow */
status = napi_create_int32(env, static_cast<int32_t>(int_value), &v);
assert(status == napi_ok);
}
else if (id == TYPE_LONG)
{
long long_value = value_to_long(arg_value);
/* TODO: Check integer overflow */
status = napi_create_int64(env, static_cast<int64_t>(long_value), &v);
assert(status == napi_ok);
}
else if (id == TYPE_FLOAT)
{
float float_value = value_to_float(arg_value);
status = napi_create_double(env, static_cast<double>(float_value), &v);
assert(status == napi_ok);
}
else if (id == TYPE_DOUBLE)
{
double double_value = value_to_double(arg_value);
status = napi_create_double(env, double_value, &v);
assert(status == napi_ok);
}
else if (id == TYPE_STRING)
{
const char * str_value = value_to_string(arg_value);
size_t length = value_type_size(arg_value) - 1;
status = napi_create_string_utf8(env, str_value, length, &v);
assert(status == napi_ok);
}
else if (id == TYPE_BUFFER)
{
void * buff_value = value_to_buffer(arg_value);
size_t size = value_type_size(arg_value);
status = napi_create_buffer(env, size, &buff_value, &v);
assert(status == napi_ok);
}
else if (id == TYPE_ARRAY)
{
value * array_value = value_to_array(arg_value);
size_t array_size = value_type_count(arg_value);
uint32_t iterator;
status = napi_create_array_with_length(env, array_size, &v);
assert(status == napi_ok);
for (iterator = 0; iterator < array_size; ++iterator)
{
/* TODO: Review recursion overflow */
napi_value element_v = node_loader_impl_value(env, static_cast<void *>(array_value[iterator]));
status = napi_set_element(env, v, iterator, element_v);
assert(status == napi_ok);
}
}
else if (id == TYPE_MAP)
{
value * map_value = value_to_map(arg_value);
size_t iterator, map_size = value_type_count(arg_value);
status = napi_create_object(env, &v);
assert(status == napi_ok);
for (iterator = 0; iterator < map_size; ++iterator)
{
value * pair_value = value_to_array(map_value[iterator]);
const char * key = value_to_string(pair_value[0]);
/* TODO: Review recursion overflow */
napi_value element_v = node_loader_impl_value(env, static_cast<void *>(pair_value[1]));
status = napi_set_named_property(env, v, key, element_v);
assert(status == napi_ok);
}
}
/* TODO */
/*
else if (id == TYPE_PTR)
{
}
*/
else
{
status = napi_get_undefined(env, &v);
assert(status == napi_ok);
}
return v;
}
function_return node_loader_impl_return(napi_env env, napi_value v)
{
value ret = NULL;
napi_valuetype valuetype;
napi_status status = napi_typeof(env, v, &valuetype);
assert(status == napi_ok);
if (valuetype == napi_undefined)
{
/* TODO */
}
else if (valuetype == napi_null)
{
/* TODO */
}
else if (valuetype == napi_boolean)
{
bool b;
status = napi_get_value_bool(env, v, &b);
assert(status == napi_ok);
ret = value_create_bool((b == true) ? static_cast<boolean>(1) : static_cast<boolean>(0));
}
else if (valuetype == napi_number)
{
double d;
status = napi_get_value_double(env, v, &d);
assert(status == napi_ok);
ret = value_create_double(d);
}
else if (valuetype == napi_string)
{
size_t length;
status = napi_get_value_string_utf8(env, v, NULL, 0, &length);
assert(status == napi_ok);
ret = value_create_string(NULL, length);
if (ret != NULL)
{
char * str = value_to_string(ret);
status = napi_get_value_string_utf8(env, v, str, length + 1, &length);
assert(status == napi_ok);
}
}
else if (valuetype == napi_symbol)
{
/* TODO */
}
else if (valuetype == napi_object)
{
bool result = false;
if (napi_is_array(env, v, &result) == napi_ok && result == true)
{
uint32_t iterator, length = 0;
value * array_value;
status = napi_get_array_length(env, v, &length);
assert(status == napi_ok);
ret = value_create_array(NULL, static_cast<size_t>(length));
array_value = value_to_array(ret);
for (iterator = 0; iterator < length; ++iterator)
{
napi_value element;
status = napi_get_element(env, v, iterator, &element);
assert(status == napi_ok);
/* TODO: Review recursion overflow */
array_value[iterator] = node_loader_impl_return(env, element);
}
}
else if (napi_is_buffer(env, v, &result) == napi_ok && result == true)
{
/* TODO */
}
else if (napi_is_error(env, v, &result) == napi_ok && result == true)
{
/* TODO */
}
else if (napi_is_typedarray(env, v, &result) == napi_ok && result == true)
{
/* TODO */
}
else if (napi_is_dataview(env, v, &result) == napi_ok && result == true)
{
/* TODO */
}
else
{
/* TODO: Strict check if it is an object (map) */
uint32_t iterator, length = 0;
napi_value keys;
value * map_value;
status = napi_get_property_names(env, v, &keys);
assert(status == napi_ok);
status = napi_get_array_length(env, keys, &length);
assert(status == napi_ok);
ret = value_create_map(NULL, static_cast<size_t>(length));
map_value = value_to_map(ret);
for (iterator = 0; iterator < length; ++iterator)
{
napi_value key;
size_t key_length;
value * tupla;
/* Create tupla */
map_value[iterator] = value_create_array(NULL, 2);
tupla = value_to_array(map_value[iterator]);
/* Get key from object */
status = napi_get_element(env, keys, iterator, &key);
assert(status == napi_ok);
/* Set key string in the tupla */
status = napi_get_value_string_utf8(env, key, NULL, 0, &key_length);
assert(status == napi_ok);
tupla[0] = value_create_string(NULL, key_length);
if (tupla[0] != NULL)
{
napi_value element;
char * str = value_to_string(tupla[0]);
status = napi_get_value_string_utf8(env, key, str, key_length + 1, &key_length);
assert(status == napi_ok);
status = napi_get_property(env, v, key, &element);
assert(status == napi_ok);
/* TODO: Review recursion overflow */
tupla[1] = node_loader_impl_return(env, element);
}
}
}
}
else if (valuetype == napi_function)
{
/* TODO */
}
else if (valuetype == napi_external)
{
/* TODO */
}
return static_cast<function_return>(ret);
}
void node_loader_impl_async_func_call(uv_async_t * async)
{
loader_impl_async_func_call async_data;
napi_env env;
napi_handle_scope handle_scope;
signature s;
size_t args_size;
void ** args;
loader_impl_node_function node_func;
size_t args_count;
/* Lock node implementation mutex */
uv_mutex_lock(&node_impl_mutex);
async_data = static_cast<loader_impl_async_func_call>(async->data);
/* Get environment reference */
env = async_data->node_impl->env;
/* Get function data */
s = function_signature(async_data->func);
args_size = signature_count(s);
args = static_cast<void **>(async_data->args);
node_func = async_data->node_func;
/* Create scope */
napi_status status = napi_open_handle_scope(env, &handle_scope);
assert(status == napi_ok);
/* Build parameters */
for (args_count = 0; args_count < args_size; ++args_count)
{
/* Define parameter */
node_func->argv[args_count] = node_loader_impl_value(env, args[args_count]);
}
/* Get function reference */
napi_value function_ptr;
status = napi_get_reference_value(env, node_func->func_ref, &function_ptr);
assert(status == napi_ok);
/* Call to function */
napi_value global, func_return;
status = napi_get_reference_value(env, async_data->node_impl->global_ref, &global);
assert(status == napi_ok);
status = napi_call_function(env, global, function_ptr, args_size, node_func->argv, &func_return);
assert(status == napi_ok);
/* Convert function return to value */
async_data->ret = node_loader_impl_return(env, func_return);
/* Close scope */
status = napi_close_handle_scope(env, handle_scope);
assert(status == napi_ok);
/* Signal function call condition */
uv_cond_signal(&node_impl_cond);
uv_mutex_unlock(&node_impl_mutex);
}
void node_loader_impl_async_func_destroy(uv_async_t * async)
{
loader_impl_async_func_destroy async_data;
napi_env env;
uint32_t ref_count = 0;
napi_handle_scope handle_scope;
/* Lock node implementation mutex */
uv_mutex_lock(&node_impl_mutex);
async_data = static_cast<loader_impl_async_func_destroy>(async->data);
/* Get environment reference */
env = async_data->node_impl->env;
/* Create scope */
napi_status status = napi_open_handle_scope(env, &handle_scope);
assert(status == napi_ok);
/* Clear function persistent reference */
status = napi_reference_unref(env, async_data->node_func->func_ref, &ref_count);
assert(status == napi_ok && ref_count == 0);
status = napi_delete_reference(env, async_data->node_func->func_ref);
assert(status == napi_ok);
/* Close scope */
status = napi_close_handle_scope(env, handle_scope);
assert(status == napi_ok);
/* Signal function destroy condition */
uv_cond_signal(&node_impl_cond);
uv_mutex_unlock(&node_impl_mutex);
}
void node_loader_impl_async_load_from_file(uv_async_t * async)
{
loader_impl_async_load_from_file async_data;
napi_env env;
napi_value function_table_object;
const char load_from_file_str[] = "load_from_file";
napi_value load_from_file_str_value;
bool result = false;
napi_handle_scope handle_scope;
/* Lock node implementation mutex */
uv_mutex_lock(&node_impl_mutex);
async_data = static_cast<loader_impl_async_load_from_file>(async->data);
/* Get environment reference */
env = async_data->node_impl->env;
/* Create scope */
napi_status status = napi_open_handle_scope(env, &handle_scope);
assert(status == napi_ok);
/* Get function table object from reference */
status = napi_get_reference_value(env, async_data->node_impl->function_table_object_ref, &function_table_object);
assert(status == napi_ok);
/* Create function string */
status = napi_create_string_utf8(env, load_from_file_str, sizeof(load_from_file_str) - 1, &load_from_file_str_value);
assert(status == napi_ok);
/* Check if exists in the table */
status = napi_has_own_property(env, function_table_object, load_from_file_str_value, &result);
assert(status == napi_ok);
if (result == true)
{
napi_value function_trampoline_load_from_file;
napi_valuetype valuetype;
napi_value argv[1];
uint32_t ref_count = 0;
status = napi_get_named_property(env, function_table_object, load_from_file_str, &function_trampoline_load_from_file);
assert(status == napi_ok);
status = napi_typeof(env, function_trampoline_load_from_file, &valuetype);
assert(status == napi_ok);
if (valuetype != napi_function)
{
napi_throw_type_error(env, nullptr, "Invalid function load_from_file in function table object");
}
/* Define parameters */
status = napi_create_array_with_length(env, async_data->size, &argv[0]);
assert(status == napi_ok);
for (size_t index = 0; index < async_data->size; ++index)
{
napi_value path_str;
size_t length = strnlen(async_data->paths[index], LOADER_NAMING_PATH_SIZE);
status = napi_create_string_utf8(env, async_data->paths[index], length, &path_str);
assert(status == napi_ok);
status = napi_set_element(env, argv[0], (uint32_t)index, path_str);
assert(status == napi_ok);
}
/* Call to load from file function */
napi_value global, return_value;
status = napi_get_reference_value(env, async_data->node_impl->global_ref, &global);
assert(status == napi_ok);
status = napi_call_function(env, global, function_trampoline_load_from_file, 1, argv, &return_value);
assert(status == napi_ok);
/* Check return value */
napi_valuetype return_valuetype;
status = napi_typeof(env, return_value, &return_valuetype);
assert(status == napi_ok);
if (return_valuetype != napi_null)
{
/* Make handle persistent */
status = napi_create_reference(env, return_value, 0, &async_data->handle_ref);
assert(status == napi_ok);
status = napi_reference_ref(env, async_data->handle_ref, &ref_count);
assert(status == napi_ok && ref_count == 1);
}
}
/* Close scope */
status = napi_close_handle_scope(env, handle_scope);
assert(status == napi_ok);
/* Signal load from file condition */
uv_cond_signal(&node_impl_cond);
uv_mutex_unlock(&node_impl_mutex);
}
void node_loader_impl_async_clear(uv_async_t * async)
{
loader_impl_async_clear async_data;
napi_env env;
napi_value function_table_object;
const char clear_str[] = "clear";
napi_value clear_str_value;
bool result = false;
napi_handle_scope handle_scope;
uint32_t ref_count = 0;
/* Lock node implementation mutex */
uv_mutex_lock(&node_impl_mutex);
async_data = static_cast<loader_impl_async_clear>(async->data);
/* Get environment reference */
env = async_data->node_impl->env;
/* Create scope */
napi_status status = napi_open_handle_scope(env, &handle_scope);
assert(status == napi_ok);
/* Get function table object from reference */
status = napi_get_reference_value(env, async_data->node_impl->function_table_object_ref, &function_table_object);
assert(status == napi_ok);
/* Create function string */
status = napi_create_string_utf8(env, clear_str, sizeof(clear_str) - 1, &clear_str_value);
assert(status == napi_ok);
/* Check if exists in the table */
status = napi_has_own_property(env, function_table_object, clear_str_value, &result);
assert(status == napi_ok);
if (result == true)
{
napi_value function_trampoline_clear;
napi_valuetype valuetype;
napi_value argv[1];
status = napi_get_named_property(env, function_table_object, clear_str, &function_trampoline_clear);
assert(status == napi_ok);
status = napi_typeof(env, function_trampoline_clear, &valuetype);
assert(status == napi_ok);
if (valuetype != napi_function)
{
napi_throw_type_error(env, nullptr, "Invalid function clear in function table object");
}
/* Define parameters */
status = napi_get_reference_value(env, async_data->handle_ref, &argv[0]);
assert(status == napi_ok);
/* Call to load from file function */
napi_value global, clear_return;
status = napi_get_reference_value(env, async_data->node_impl->global_ref, &global);
assert(status == napi_ok);
status = napi_call_function(env, global, function_trampoline_clear, 1, argv, &clear_return);
assert(status == napi_ok);
}
/* Clear handle persistent reference */
status = napi_reference_unref(env, async_data->handle_ref, &ref_count);
assert(status == napi_ok && ref_count == 0);
status = napi_delete_reference(env, async_data->handle_ref);
assert(status == napi_ok);
/* Close scope */
status = napi_close_handle_scope(env, handle_scope);
assert(status == napi_ok);
/* Signal clear condition */
uv_cond_signal(&node_impl_cond);
uv_mutex_unlock(&node_impl_mutex);
}
void node_loader_impl_async_discover(uv_async_t * async)
{
loader_impl_async_discover async_data;
napi_env env;
napi_value function_table_object;
const char discover_str[] = "discover";
napi_value discover_str_value;
bool result = false;
napi_handle_scope handle_scope;
/* Lock node implementation mutex */