@@ -1054,16 +1054,16 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
1054
1054
loader_impl_async_safe_cast<loader_impl_class_constructor_callback_closure> closure_cast = { NULL };
1055
1055
1056
1056
napi_get_cb_info (env, info, NULL , NULL , NULL , &closure_cast.ptr );
1057
+
1057
1058
/* Set environment */
1058
1059
closure_cast.safe ->node_impl ->env = env;
1059
- constructor ctor = class_default_constructor (closure_cast.safe ->cls );
1060
1060
1061
1061
char *klass_name_str = const_cast <char *>(class_name (closure_cast.safe ->cls ));
1062
- std::string error_msg = " NodeJS Loader No default constructor in class: " ;
1063
- error_msg += klass_name_str ;
1062
+
1063
+ constructor ctor = class_default_constructor (closure_cast. safe -> cls ) ;
1064
1064
if (ctor == NULL )
1065
1065
{
1066
- napi_throw_error (env, NULL , error_msg. c_str () );
1066
+ log_write ( " metacall " , LOG_LEVEL_INFO, " NodeJS Loader No default constructor in class: " , klass_name_str );
1067
1067
}
1068
1068
1069
1069
size_t argc = constructor_count (ctor);
@@ -1083,13 +1083,13 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
1083
1083
ctor = class_constructor (closure_cast.safe ->cls , ids, argc);
1084
1084
1085
1085
// convert class_name to lower case
1086
- while (*klass_name_str != ' \0 ' )
1086
+ std::string obj_name (klass_name_str);
1087
+ for (size_t i = 0 ; i < obj_name.size (); i++)
1087
1088
{
1088
- *klass_name_str = std::tolower (*klass_name_str);
1089
- klass_name_str++;
1089
+ obj_name[i] = std::tolower (obj_name[i]);
1090
1090
}
1091
1091
1092
- object o = class_new (closure_cast.safe ->cls , klass_name_str , ctor, args, argc);
1092
+ object o = class_new (closure_cast.safe ->cls , obj_name. c_str () , ctor, args, argc);
1093
1093
1094
1094
if (ids != NULL )
1095
1095
{
@@ -1106,7 +1106,7 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
1106
1106
if (v == NULL )
1107
1107
{
1108
1108
object_destroy (o);
1109
- error_msg = " NodeJS Loader Failed to create object for class: " ;
1109
+ std::string error_msg = " NodeJS Loader Failed to create object for class: " ;
1110
1110
error_msg += klass_name_str;
1111
1111
napi_throw_error (env, NULL , error_msg.c_str ());
1112
1112
}
@@ -4474,7 +4474,7 @@ static value node_loader_impl_discover_object_safe(napi_env env, loader_impl_dis
4474
4474
void node_loader_impl_discover_safe (napi_env env, loader_impl_async_discover_safe discover_safe)
4475
4475
{
4476
4476
static const char discover_str[] = " discover" ;
4477
- napi_value function_table_object ;
4477
+ napi_value discover_table_object ;
4478
4478
napi_value discover_str_value;
4479
4479
bool result = false ;
4480
4480
napi_handle_scope handle_scope;
@@ -4485,7 +4485,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4485
4485
node_loader_impl_exception (env, status);
4486
4486
4487
4487
/* Get function table object from reference */
4488
- status = napi_get_reference_value (env, discover_safe->node_impl ->function_table_object_ref , &function_table_object );
4488
+ status = napi_get_reference_value (env, discover_safe->node_impl ->function_table_object_ref , &discover_table_object );
4489
4489
4490
4490
node_loader_impl_exception (env, status);
4491
4491
@@ -4495,21 +4495,21 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4495
4495
node_loader_impl_exception (env, status);
4496
4496
4497
4497
/* Check if exists in the table */
4498
- status = napi_has_own_property (env, function_table_object , discover_str_value, &result);
4498
+ status = napi_has_own_property (env, discover_table_object , discover_str_value, &result);
4499
4499
4500
4500
node_loader_impl_exception (env, status);
4501
4501
4502
4502
if (result == true )
4503
4503
{
4504
- napi_value function_trampoline_discover ;
4504
+ napi_value trampoline_discover ;
4505
4505
napi_valuetype valuetype;
4506
4506
napi_value argv[1 ];
4507
4507
4508
- status = napi_get_named_property (env, function_table_object , discover_str, &function_trampoline_discover );
4508
+ status = napi_get_named_property (env, discover_table_object , discover_str, &trampoline_discover );
4509
4509
4510
4510
node_loader_impl_exception (env, status);
4511
4511
4512
- status = napi_typeof (env, function_trampoline_discover , &valuetype);
4512
+ status = napi_typeof (env, trampoline_discover , &valuetype);
4513
4513
4514
4514
node_loader_impl_exception (env, status);
4515
4515
@@ -4530,72 +4530,79 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4530
4530
4531
4531
node_loader_impl_exception (env, status);
4532
4532
4533
- status = napi_call_function (env, global, function_trampoline_discover , 1 , argv, &discover_map);
4533
+ status = napi_call_function (env, global, trampoline_discover , 1 , argv, &discover_map);
4534
4534
4535
4535
node_loader_impl_exception (env, status);
4536
4536
4537
4537
/* Convert return value (discover object) to context */
4538
- napi_value func_names ;
4539
- uint32_t func_names_length ;
4538
+ napi_value prop_names ;
4539
+ uint32_t prop_names_length ;
4540
4540
4541
- status = napi_get_property_names (env, discover_map, &func_names );
4541
+ status = napi_get_property_names (env, discover_map, &prop_names );
4542
4542
4543
4543
node_loader_impl_exception (env, status);
4544
4544
4545
- status = napi_get_array_length (env, func_names , &func_names_length );
4545
+ status = napi_get_array_length (env, prop_names , &prop_names_length );
4546
4546
4547
4547
node_loader_impl_exception (env, status);
4548
4548
4549
- for (uint32_t index = 0 ; index < func_names_length ; ++index)
4549
+ for (uint32_t index = 0 ; index < prop_names_length ; ++index)
4550
4550
{
4551
- napi_value func_name ;
4552
- size_t func_name_length ;
4553
- char *func_name_str = NULL ;
4551
+ napi_value prop_name ;
4552
+ size_t prop_name_length ;
4553
+ char *prop_name_str = NULL ;
4554
4554
4555
- status = napi_get_element (env, func_names , index, &func_name );
4555
+ status = napi_get_element (env, prop_names , index, &prop_name );
4556
4556
4557
4557
node_loader_impl_exception (env, status);
4558
4558
4559
- status = napi_get_value_string_utf8 (env, func_name , NULL , 0 , &func_name_length );
4559
+ status = napi_get_value_string_utf8 (env, prop_name , NULL , 0 , &prop_name_length );
4560
4560
4561
4561
node_loader_impl_exception (env, status);
4562
4562
4563
- if (func_name_length > 0 )
4563
+ if (prop_name_length > 0 )
4564
4564
{
4565
- func_name_str = static_cast <char *>(malloc (sizeof (char ) * (func_name_length + 1 )));
4565
+ prop_name_str = static_cast <char *>(malloc (sizeof (char ) * (prop_name_length + 1 )));
4566
4566
}
4567
4567
4568
- if (func_name_str != NULL )
4568
+ if (prop_name_str != NULL )
4569
4569
{
4570
- napi_value function_descriptor;
4571
- napi_value function_ptr;
4572
- napi_value function_sig;
4573
- napi_value function_types = nullptr ;
4574
- napi_value function_ret = nullptr ;
4575
- napi_value function_is_async;
4576
- uint32_t function_sig_length;
4570
+ napi_value property_descriptor;
4577
4571
4578
- /* Get function name */
4579
- status = napi_get_value_string_utf8 (env, func_name, func_name_str, func_name_length + 1 , &func_name_length );
4572
+ /* Get property name */
4573
+ status = napi_get_value_string_utf8 (env, prop_name, prop_name_str, prop_name_length + 1 , &prop_name_length );
4580
4574
4581
4575
node_loader_impl_exception (env, status);
4582
4576
4583
- /* Get function descriptor */
4584
- status = napi_get_named_property (env, discover_map, func_name_str , &function_descriptor );
4577
+ /* Get property descriptor */
4578
+ status = napi_get_named_property (env, discover_map, prop_name_str , &property_descriptor );
4585
4579
4586
4580
node_loader_impl_exception (env, status);
4587
4581
4588
- /* Check if function pointer exists */
4589
4582
bool is_func = false ;
4590
4583
4591
- status = napi_has_named_property (env, function_descriptor, " func" , &is_func);
4584
+ status = napi_has_named_property (env, property_descriptor, " func" , &is_func);
4585
+
4586
+ node_loader_impl_exception (env, status);
4587
+
4588
+ bool is_klass = false ;
4589
+
4590
+ status = napi_has_named_property (env, property_descriptor, " klass" , &is_klass);
4592
4591
4593
4592
node_loader_impl_exception (env, status);
4594
4593
4594
+ /* Check if a function pointer exists */
4595
4595
if (is_func == true )
4596
4596
{
4597
+ napi_value function_ptr;
4598
+ napi_value function_sig;
4599
+ napi_value function_types = nullptr ;
4600
+ napi_value function_ret = nullptr ;
4601
+ napi_value function_is_async;
4602
+ uint32_t function_sig_length;
4603
+
4597
4604
/* Get function pointer */
4598
- status = napi_get_named_property (env, function_descriptor , " func" , &function_ptr);
4605
+ status = napi_get_named_property (env, property_descriptor , " func" , &function_ptr);
4599
4606
4600
4607
node_loader_impl_exception (env, status);
4601
4608
@@ -4610,7 +4617,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4610
4617
}
4611
4618
4612
4619
/* Get function signature */
4613
- status = napi_get_named_property (env, function_descriptor , " signature" , &function_sig);
4620
+ status = napi_get_named_property (env, property_descriptor , " signature" , &function_sig);
4614
4621
4615
4622
node_loader_impl_exception (env, status);
4616
4623
@@ -4630,7 +4637,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4630
4637
node_loader_impl_exception (env, status);
4631
4638
4632
4639
/* Get function async */
4633
- status = napi_get_named_property (env, function_descriptor , " async" , &function_is_async);
4640
+ status = napi_get_named_property (env, property_descriptor , " async" , &function_is_async);
4634
4641
4635
4642
node_loader_impl_exception (env, status);
4636
4643
@@ -4648,13 +4655,13 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4648
4655
static const char types_str[] = " types" ;
4649
4656
bool has_types = false ;
4650
4657
4651
- status = napi_has_named_property (env, function_descriptor , types_str, &has_types);
4658
+ status = napi_has_named_property (env, property_descriptor , types_str, &has_types);
4652
4659
4653
4660
node_loader_impl_exception (env, status);
4654
4661
4655
4662
if (has_types == true )
4656
4663
{
4657
- status = napi_get_named_property (env, function_descriptor , types_str, &function_types);
4664
+ status = napi_get_named_property (env, property_descriptor , types_str, &function_types);
4658
4665
4659
4666
node_loader_impl_exception (env, status);
4660
4667
@@ -4673,13 +4680,13 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4673
4680
static const char ret_str[] = " ret" ;
4674
4681
bool has_ret = false ;
4675
4682
4676
- status = napi_has_named_property (env, function_descriptor , ret_str, &has_ret);
4683
+ status = napi_has_named_property (env, property_descriptor , ret_str, &has_ret);
4677
4684
4678
4685
node_loader_impl_exception (env, status);
4679
4686
4680
4687
if (has_ret == true )
4681
4688
{
4682
- status = napi_get_named_property (env, function_descriptor , ret_str, &function_ret);
4689
+ status = napi_get_named_property (env, property_descriptor , ret_str, &function_ret);
4683
4690
4684
4691
node_loader_impl_exception (env, status);
4685
4692
@@ -4706,7 +4713,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4706
4713
node_func->impl = discover_safe->node_impl ->impl ;
4707
4714
4708
4715
/* Create function */
4709
- function f = function_create (func_name_str , (size_t )function_sig_length, node_func, &function_node_singleton);
4716
+ function f = function_create (prop_name_str , (size_t )function_sig_length, node_func, &function_node_singleton);
4710
4717
4711
4718
if (f != NULL )
4712
4719
{
@@ -4838,8 +4845,45 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
4838
4845
break ;
4839
4846
}
4840
4847
}
4848
+ else if (is_klass == true )
4849
+ {
4850
+ napi_value klass_ptr;
4851
+
4852
+ /* Get klass pointer */
4853
+ status = napi_get_named_property (env, property_descriptor, " klass" , &klass_ptr);
4854
+
4855
+ node_loader_impl_exception (env, status);
4856
+
4857
+ /* Check klass pointer type */
4858
+ status = napi_typeof (env, klass_ptr, &valuetype);
4859
+
4860
+ node_loader_impl_exception (env, status);
4861
+
4862
+ if (valuetype != napi_function)
4863
+ {
4864
+ napi_throw_type_error (env, nullptr , " Invalid NodeJS class" );
4865
+ }
4866
+
4867
+ struct loader_impl_discover_klass_safe_type discover_klass_safe = {
4868
+ discover_safe->node_impl ,
4869
+ klass_ptr
4870
+ };
4871
+
4872
+ value v = node_loader_impl_discover_klass_safe (env, &discover_klass_safe);
4873
+
4874
+ if (v != NULL )
4875
+ {
4876
+ scope sp = context_scope (discover_safe->ctx );
4877
+ if (scope_define (sp, class_name ((klass)metacall_value_to_class (v)), v) != 0 )
4878
+ {
4879
+ value_type_destroy (v);
4880
+ discover_safe->result = 1 ;
4881
+ break ;
4882
+ }
4883
+ }
4884
+ }
4841
4885
4842
- free (func_name_str );
4886
+ free (prop_name_str );
4843
4887
}
4844
4888
}
4845
4889
}
0 commit comments