@@ -1229,7 +1229,7 @@ loader_handle rb_loader_impl_load_from_file(loader_impl impl, const loader_path
12291229 This should run only once, the first time after the initialization */
12301230 if (rb_loader_impl_run_main == 0 && size == 1 && strcmp (paths [0 ], rb_loader_impl_main_module ) == 0 )
12311231 {
1232- VALUE module_data , result , module_name ;
1232+ VALUE module_data , result , module_name , module ;
12331233 int state ;
12341234 loader_impl_rb_module rb_module ;
12351235
@@ -1243,24 +1243,47 @@ loader_handle rb_loader_impl_load_from_file(loader_impl impl, const loader_path
12431243 goto load_error ;
12441244 }
12451245
1246+ /* Define module name */
1247+ {
1248+ loader_path name ;
1249+ size_t size = portability_path_get_name (paths [0 ], strnlen (paths [0 ], LOADER_PATH_SIZE ), name , LOADER_PATH_SIZE );
1250+ module_name = rb_str_new (name , size - 1 );
1251+ module_name = rb_funcallv (module_name , rb_intern ("capitalize" ), 0 , NULL );
1252+ }
1253+
1254+ /* Define module that wraps the code */
1255+ {
1256+ #define rb_str_new_static_size (str ) rb_str_new_static(str, sizeof(str) - 1)
1257+
1258+ VALUE wrapped_code = rb_str_plus (rb_str_new_static_size ("module " ), module_name );
1259+ wrapped_code = rb_str_plus (wrapped_code , rb_str_new_static_size ("\n" ));
1260+ wrapped_code = rb_str_plus (wrapped_code , module_data );
1261+ wrapped_code = rb_str_plus (wrapped_code , rb_str_new_static_size ("\nend" ));
1262+
1263+ #undef rb_str_new_static_size
1264+
1265+ module_data = wrapped_code ;
1266+ }
1267+
12461268 result = rb_eval_string_protect (StringValuePtr (module_data ), & state );
12471269
12481270 if (state != 0 )
12491271 {
1250- log_write ("metacall" , LOG_LEVEL_ERROR , "Ruby evaluation failed" );
1272+ log_write ("metacall" , LOG_LEVEL_ERROR , "Ruby evaluation failed %s" , paths [ 0 ] );
12511273 rb_loader_impl_print_exception ();
12521274 goto load_error ;
12531275 }
12541276
1255- /* Define module name */
1256- {
1257- loader_path name ;
1258- size_t size = portability_path_get_name (paths [0 ], strnlen (paths [0 ], LOADER_PATH_SIZE ), name , LOADER_PATH_SIZE );
1277+ /* Get the module reference */
1278+ module = rb_const_get (rb_cObject , rb_intern_str (module_name ));
12591279
1260- module_name = rb_str_new (name , size );
1280+ if (module == Qnil )
1281+ {
1282+ log_write ("metacall" , LOG_LEVEL_ERROR , "Ruby invalid module generation: %s" , paths [0 ]);
1283+ goto load_error ;
12611284 }
12621285
1263- rb_module = rb_loader_impl_create_module (module_name , result , module_data , result );
1286+ rb_module = rb_loader_impl_create_module (module_name , module , module_data , result );
12641287
12651288 if (rb_module == NULL )
12661289 {
@@ -1296,7 +1319,7 @@ loader_handle rb_loader_impl_load_from_file(loader_impl impl, const loader_path
12961319 }
12971320 }
12981321
1299- // Do not load the handle in case there isn't modules
1322+ /* Do not load the handle in case there isn't modules */
13001323 if (vector_size (handle -> modules ) == 0 )
13011324 {
13021325 log_write ("metacall" , LOG_LEVEL_ERROR , "No module could be loaded" );
0 commit comments