@@ -1054,16 +1054,16 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
10541054 loader_impl_async_safe_cast<loader_impl_class_constructor_callback_closure> closure_cast = { NULL };
10551055
10561056 napi_get_cb_info (env, info, NULL , NULL , NULL , &closure_cast.ptr );
1057+
10571058 /* Set environment */
10581059 closure_cast.safe ->node_impl ->env = env;
1059- constructor ctor = class_default_constructor (closure_cast.safe ->cls );
10601060
10611061 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 ) ;
10641064 if (ctor == NULL )
10651065 {
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 );
10671067 }
10681068
10691069 size_t argc = constructor_count (ctor);
@@ -1083,13 +1083,13 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
10831083 ctor = class_constructor (closure_cast.safe ->cls , ids, argc);
10841084
10851085 // 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++)
10871088 {
1088- *klass_name_str = std::tolower (*klass_name_str);
1089- klass_name_str++;
1089+ obj_name[i] = std::tolower (obj_name[i]);
10901090 }
10911091
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);
10931093
10941094 if (ids != NULL )
10951095 {
@@ -1106,7 +1106,7 @@ static napi_value node_loader_impl_class_constructor_callback(napi_env env, napi
11061106 if (v == NULL )
11071107 {
11081108 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: " ;
11101110 error_msg += klass_name_str;
11111111 napi_throw_error (env, NULL , error_msg.c_str ());
11121112 }
@@ -4474,7 +4474,7 @@ static value node_loader_impl_discover_object_safe(napi_env env, loader_impl_dis
44744474void node_loader_impl_discover_safe (napi_env env, loader_impl_async_discover_safe discover_safe)
44754475{
44764476 static const char discover_str[] = " discover" ;
4477- napi_value function_table_object ;
4477+ napi_value discover_table_object ;
44784478 napi_value discover_str_value;
44794479 bool result = false ;
44804480 napi_handle_scope handle_scope;
@@ -4485,7 +4485,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
44854485 node_loader_impl_exception (env, status);
44864486
44874487 /* 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 );
44894489
44904490 node_loader_impl_exception (env, status);
44914491
@@ -4495,21 +4495,21 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
44954495 node_loader_impl_exception (env, status);
44964496
44974497 /* 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);
44994499
45004500 node_loader_impl_exception (env, status);
45014501
45024502 if (result == true )
45034503 {
4504- napi_value function_trampoline_discover ;
4504+ napi_value trampoline_discover ;
45054505 napi_valuetype valuetype;
45064506 napi_value argv[1 ];
45074507
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 );
45094509
45104510 node_loader_impl_exception (env, status);
45114511
4512- status = napi_typeof (env, function_trampoline_discover , &valuetype);
4512+ status = napi_typeof (env, trampoline_discover , &valuetype);
45134513
45144514 node_loader_impl_exception (env, status);
45154515
@@ -4530,72 +4530,79 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
45304530
45314531 node_loader_impl_exception (env, status);
45324532
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);
45344534
45354535 node_loader_impl_exception (env, status);
45364536
45374537 /* 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 ;
45404540
4541- status = napi_get_property_names (env, discover_map, &func_names );
4541+ status = napi_get_property_names (env, discover_map, &prop_names );
45424542
45434543 node_loader_impl_exception (env, status);
45444544
4545- status = napi_get_array_length (env, func_names , &func_names_length );
4545+ status = napi_get_array_length (env, prop_names , &prop_names_length );
45464546
45474547 node_loader_impl_exception (env, status);
45484548
4549- for (uint32_t index = 0 ; index < func_names_length ; ++index)
4549+ for (uint32_t index = 0 ; index < prop_names_length ; ++index)
45504550 {
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 ;
45544554
4555- status = napi_get_element (env, func_names , index, &func_name );
4555+ status = napi_get_element (env, prop_names , index, &prop_name );
45564556
45574557 node_loader_impl_exception (env, status);
45584558
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 );
45604560
45614561 node_loader_impl_exception (env, status);
45624562
4563- if (func_name_length > 0 )
4563+ if (prop_name_length > 0 )
45644564 {
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 )));
45664566 }
45674567
4568- if (func_name_str != NULL )
4568+ if (prop_name_str != NULL )
45694569 {
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;
45774571
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 );
45804574
45814575 node_loader_impl_exception (env, status);
45824576
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 );
45854579
45864580 node_loader_impl_exception (env, status);
45874581
4588- /* Check if function pointer exists */
45894582 bool is_func = false ;
45904583
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);
45924591
45934592 node_loader_impl_exception (env, status);
45944593
4594+ /* Check if a function pointer exists */
45954595 if (is_func == true )
45964596 {
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+
45974604 /* 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);
45994606
46004607 node_loader_impl_exception (env, status);
46014608
@@ -4610,7 +4617,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
46104617 }
46114618
46124619 /* 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);
46144621
46154622 node_loader_impl_exception (env, status);
46164623
@@ -4630,7 +4637,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
46304637 node_loader_impl_exception (env, status);
46314638
46324639 /* 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);
46344641
46354642 node_loader_impl_exception (env, status);
46364643
@@ -4648,13 +4655,13 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
46484655 static const char types_str[] = " types" ;
46494656 bool has_types = false ;
46504657
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);
46524659
46534660 node_loader_impl_exception (env, status);
46544661
46554662 if (has_types == true )
46564663 {
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);
46584665
46594666 node_loader_impl_exception (env, status);
46604667
@@ -4673,13 +4680,13 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
46734680 static const char ret_str[] = " ret" ;
46744681 bool has_ret = false ;
46754682
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);
46774684
46784685 node_loader_impl_exception (env, status);
46794686
46804687 if (has_ret == true )
46814688 {
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);
46834690
46844691 node_loader_impl_exception (env, status);
46854692
@@ -4706,7 +4713,7 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
47064713 node_func->impl = discover_safe->node_impl ->impl ;
47074714
47084715 /* 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);
47104717
47114718 if (f != NULL )
47124719 {
@@ -4838,8 +4845,45 @@ void node_loader_impl_discover_safe(napi_env env, loader_impl_async_discover_saf
48384845 break ;
48394846 }
48404847 }
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+ }
48414885
4842- free (func_name_str );
4886+ free (prop_name_str );
48434887 }
48444888 }
48454889 }
0 commit comments