@@ -58,6 +58,13 @@ typedef struct loader_impl_rb_function_type
5858
5959} * loader_impl_rb_function ;
6060
61+ typedef struct loader_impl_rb_module_eval_protect_type
62+ {
63+ int argc ;
64+ VALUE * argv ;
65+ VALUE module ;
66+ } * loader_impl_rb_module_eval_protect ;
67+
6168int function_rb_interface_create (function func , function_impl impl )
6269{
6370 signature s = function_signature (func );
@@ -515,17 +522,31 @@ VALUE rb_loader_impl_load_data(loader_impl impl, const loader_naming_path path)
515522 return Qnil ;
516523}
517524
525+ VALUE rb_loader_impl_module_eval_protect (VALUE args )
526+ {
527+ /* TODO: Do this properly */
528+ loader_impl_rb_module_eval_protect protect = (loader_impl_rb_module_eval_protect )args ;
529+
530+ return rb_mod_module_eval (protect -> argc , protect -> argv , protect -> module );
531+ }
532+
518533VALUE rb_loader_impl_module_eval (VALUE module , VALUE module_data )
519534{
520535 const int argc = 1 ;
521536 VALUE result ;
522- VALUE args [argc ];
537+ VALUE argv [argc ];
538+ struct loader_impl_rb_module_eval_protect_type protect ;
539+ int state ;
540+
541+ argv [0 ] = module_data ;
523542
524- args [0 ] = module_data ;
543+ protect .argc = argc ;
544+ protect .argv = argv ;
545+ protect .module = module ;
525546
526- result = rb_mod_module_eval ( 1 , args , module );
547+ result = rb_protect ( rb_loader_impl_module_eval_protect , ( VALUE ) & protect , & state );
527548
528- if (result == Qnil )
549+ if (state || result == Qnil )
529550 {
530551 VALUE exception ;
531552
@@ -544,11 +565,13 @@ VALUE rb_loader_impl_module_eval(VALUE module, VALUE module_data)
544565 backtrace = rb_funcall (exception , rb_intern ("backtrace" ), 0 );
545566
546567 rb_io_puts (1 , & backtrace , rb_stderr );
547-
548- rb_raise (rb_eLoadError , "Invalid module evaluation" );
549-
550- return Qnil ;
551568 }
569+ else
570+ {
571+ log_write ("metacall" , LOG_LEVEL_ERROR , "Ruby module backtrace not available" );
572+ }
573+
574+ return Qnil ;
552575 }
553576
554577 return result ;
@@ -604,12 +627,6 @@ loader_impl_rb_module rb_loader_impl_load_from_file_module(loader_impl impl, con
604627 return rb_module ;
605628 }
606629 }
607- else
608- {
609- VALUE exception = rb_errinfo ();
610-
611- log_write ("metacall" , LOG_LEVEL_DEBUG , "Ruby loader error (%s)" , RSTRING_PTR (exception ));
612- }
613630 }
614631 }
615632
@@ -654,11 +671,23 @@ loader_handle rb_loader_impl_load_from_file(loader_impl impl, const loader_namin
654671 if (rb_module == NULL )
655672 {
656673 log_write ("metacall" , LOG_LEVEL_ERROR , "Invalid ruby module loading %s" , paths [iterator ]);
657-
658- continue ;
659674 }
675+ else
676+ {
677+ vector_push_back (handle -> modules , & rb_module );
678+ }
679+ }
680+
681+ // Do not load the handle in case there isn't modules
682+ if (vector_size (handle -> modules ) == 0 )
683+ {
684+ log_write ("metacall" , LOG_LEVEL_ERROR , "No module could be loaded" );
685+
686+ vector_destroy (handle -> modules );
687+
688+ free (handle );
660689
661- vector_push_back ( handle -> modules , & rb_module ) ;
690+ return NULL ;
662691 }
663692
664693 return (loader_handle )handle ;
@@ -717,12 +746,6 @@ loader_impl_rb_module rb_loader_impl_load_from_memory_module(loader_impl impl, c
717746 return rb_module ;
718747 }
719748 }
720- else
721- {
722- VALUE exception = rb_errinfo ();
723-
724- log_write ("metacall" , LOG_LEVEL_DEBUG , "Ruby loader error (%s)" , RSTRING_PTR (exception ));
725- }
726749 }
727750 }
728751
0 commit comments