@@ -610,18 +610,37 @@ namespace Rcpp {
610610
611611}
612612
613+ namespace Rcpp {
614+ namespace internal {
615+
616+ // makes module the target of class_<>, function(), etc. while an
617+ // RCPP_MODULE body runs, and clears it however the body exits
618+ class ModuleScope {
619+ public:
620+ explicit ModuleScope ( Module* module ){ ::setCurrentScope ( module ) ; }
621+ ~ModuleScope (){ ::setCurrentScope ( 0 ) ; }
622+
623+ ModuleScope ( const ModuleScope& ) = delete ;
624+ ModuleScope& operator =( const ModuleScope& ) = delete ;
625+ } ;
626+
627+ }
628+ }
629+
613630#define RCPP_MODULE_BOOT (name ) _rcpp_module_boot_##name
614631
615- #define RCPP_MODULE (name ) \
616- void _rcpp_module_##name##_init() ; \
617- static Rcpp::Module _rcpp_module_##name( # name ) ; \
618- extern " C" SEXP _rcpp_module_boot_##name(){ \
619- ::setCurrentScope ( & _rcpp_module_##name ) ; \
620- _rcpp_module_##name##_init ( ) ; \
621- Rcpp::XPtr<Rcpp::Module> mod_xp (& _rcpp_module_##name , false ); \
622- ::setCurrentScope ( 0 ) ; \
623- return mod_xp ; \
624- } \
632+ // the boot function is called from R via .Call(), so exceptions thrown by
633+ // the module body must be turned into R errors
634+ #define RCPP_MODULE (name ) \
635+ void _rcpp_module_##name##_init() ; \
636+ static Rcpp::Module _rcpp_module_##name( # name ) ; \
637+ extern " C" SEXP _rcpp_module_boot_##name(){ \
638+ BEGIN_RCPP \
639+ Rcpp::internal::ModuleScope _rcpp_module_scope ( & _rcpp_module_##name ) ; \
640+ _rcpp_module_##name##_init ( ) ; \
641+ return Rcpp::XPtr<Rcpp::Module>( & _rcpp_module_##name , false ) ; \
642+ END_RCPP \
643+ } \
625644 void _rcpp_module_##name##_init()
626645
627646// silly little dance to suppress a 'defined but not used variable' warning
0 commit comments