@@ -630,17 +630,23 @@ namespace Rcpp {
630630#define RCPP_MODULE_BOOT (name ) _rcpp_module_boot_##name
631631
632632// 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- } \
633+ // the module body must be turned into R errors; it runs every time the
634+ // module is loaded, but the body adds to the static module, so it may only
635+ // run once
636+ #define RCPP_MODULE (name ) \
637+ void _rcpp_module_##name##_init() ; \
638+ static Rcpp::Module _rcpp_module_##name( # name ) ; \
639+ static bool _rcpp_module_##name##_initialized = false ; \
640+ extern " C" SEXP _rcpp_module_boot_##name(){ \
641+ BEGIN_RCPP \
642+ if ( ! _rcpp_module_##name##_initialized ){ \
643+ Rcpp::internal::ModuleScope _rcpp_module_scope ( & _rcpp_module_##name ) ; \
644+ _rcpp_module_##name##_init ( ) ; \
645+ _rcpp_module_##name##_initialized = true ; \
646+ } \
647+ return Rcpp::XPtr<Rcpp::Module>( & _rcpp_module_##name , false ) ; \
648+ END_RCPP \
649+ } \
644650 void _rcpp_module_##name##_init()
645651
646652// silly little dance to suppress a 'defined but not used variable' warning
0 commit comments