@@ -637,17 +637,23 @@ namespace Rcpp {
637637#define RCPP_MODULE_BOOT (name ) _rcpp_module_boot_##name
638638
639639// the boot function is called from R via .Call(), so exceptions thrown by
640- // the module body must be turned into R errors
641- #define RCPP_MODULE (name ) \
642- void _rcpp_module_##name##_init() ; \
643- static Rcpp::Module _rcpp_module_##name( # name ) ; \
644- extern " C" SEXP _rcpp_module_boot_##name(){ \
645- BEGIN_RCPP \
646- Rcpp::internal::ModuleScope _rcpp_module_scope ( & _rcpp_module_##name ) ; \
647- _rcpp_module_##name##_init ( ) ; \
648- return Rcpp::XPtr<Rcpp::Module>( & _rcpp_module_##name , false ) ; \
649- END_RCPP \
650- } \
640+ // the module body must be turned into R errors; it runs every time the
641+ // module is loaded, but the body adds to the static module, so it may only
642+ // run once
643+ #define RCPP_MODULE (name ) \
644+ void _rcpp_module_##name##_init() ; \
645+ static Rcpp::Module _rcpp_module_##name( # name ) ; \
646+ static bool _rcpp_module_##name##_initialized = false ; \
647+ extern " C" SEXP _rcpp_module_boot_##name(){ \
648+ BEGIN_RCPP \
649+ if ( ! _rcpp_module_##name##_initialized ){ \
650+ Rcpp::internal::ModuleScope _rcpp_module_scope ( & _rcpp_module_##name ) ; \
651+ _rcpp_module_##name##_init ( ) ; \
652+ _rcpp_module_##name##_initialized = true ; \
653+ } \
654+ return Rcpp::XPtr<Rcpp::Module>( & _rcpp_module_##name , false ) ; \
655+ END_RCPP \
656+ } \
651657 void _rcpp_module_##name##_init()
652658
653659// silly little dance to suppress a 'defined but not used variable' warning
0 commit comments