Skip to content

Commit ca4cc5c

Browse files
committed
restore previous module scope instead of clearing it
A module loaded from within another module body (say, via a namespace load whose .onLoad calls loadModule()) would reset the outer scope to null; ModuleScope now saves and restores the previous scope.
1 parent 173cad0 commit ca4cc5c

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎ChangeLog‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module initializes raises an R error instead of terminating the session
66
(#1525)
77
* inst/include/Rcpp/Module.h (internal::ModuleScope): New RAII guard
8-
clearing the current module scope however the module body exits
8+
restoring the previous module scope however the module body exits
99
* inst/tinytest/cpp/ModuleInitError.cpp: New module whose initialization
1010
throws
1111
* inst/tinytest/cpp/Module.cpp (Module_scope_is_cleared): New

‎inst/include/Rcpp/Module.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,14 +614,21 @@ namespace Rcpp {
614614
namespace internal {
615615

616616
// makes module the target of class_<>, function(), etc. while an
617-
// RCPP_MODULE body runs, and clears it however the body exits
617+
// RCPP_MODULE body runs, and restores the previous scope however the
618+
// body exits, so a module loaded from within another module body
619+
// does not clobber the outer scope
618620
class ModuleScope {
619621
public:
620-
explicit ModuleScope( Module* module ){ ::setCurrentScope( module ) ; }
621-
~ModuleScope(){ ::setCurrentScope( 0 ) ; }
622+
explicit ModuleScope( Module* module ) : previous( ::getCurrentScope() ){
623+
::setCurrentScope( module ) ;
624+
}
625+
~ModuleScope(){ ::setCurrentScope( previous ) ; }
622626

623627
ModuleScope( const ModuleScope& ) = delete ;
624628
ModuleScope& operator=( const ModuleScope& ) = delete ;
629+
630+
private:
631+
Module* previous ;
625632
} ;
626633

627634
}

0 commit comments

Comments
 (0)