Skip to content

Commit d15dbf9

Browse files
committed
Merge pull request #259 from lionel-/new_env
Allow new_env() to create an environment with a specified parent
2 parents 8046a76 + 0ae813b commit d15dbf9

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

ChangeLog

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2015-02-19 Lionel Henry <[email protected]>
2+
3+
* inst/include/Rcpp/Environment.h Allow new_env() to create an
4+
environment with a specified parent
5+
16
2015-02-19 JJ Allaire <[email protected]>
27

38
* vignettes/Rcpp-attributes.Rnw: Add note on using inline
@@ -13,7 +18,7 @@
1318
* src/attributes.cpp: Allow includes of local files
1419
(e.g. #include "foo.hpp") in sourceCpp
1520
* Rcpp.Rproj: Specify Sweave as Rnw handler for RStudio
16-
* vignettes/*.Rnw: Add driver magic comment and turn off
21+
* vignettes/*.Rnw: Add driver magic comment and turn off
1722
Sweave concordance.
1823
* vignettes/.gitignore: Ignore artifacts of PDF preview
1924

inst/include/Rcpp/Environment.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ inline Environment new_env(int size = 29) {
361361
return R_NewHashedEnv(R_EmptyEnv, sizeSEXP);
362362
}
363363

364+
inline Environment new_env(SEXP parent, int size = 29) {
365+
Shield<SEXP> sizeSEXP(Rf_ScalarInteger(size));
366+
Shield<SEXP> parentSEXP(parent);
367+
if (!Rf_isEnvironment(parentSEXP)) {
368+
stop("parent is not an environment");
369+
}
370+
return R_NewHashedEnv(parentSEXP, sizeSEXP);
371+
}
372+
364373

365374
} // namespace Rcpp
366375

inst/unitTests/cpp/Environment.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,12 @@ Environment runit_child(){
150150
return global_env.new_child(false) ;
151151
}
152152

153+
// [[Rcpp::export]]
154+
Environment runit_new_env_default() {
155+
return Rcpp::new_env();
156+
}
153157

158+
// [[Rcpp::export]]
159+
Environment runit_new_env_parent(SEXP env) {
160+
return Rcpp::new_env(env);
161+
}

inst/unitTests/runit.environments.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,10 @@ if (.runThisTest) {
268268
checkEquals( parent.env(runit_child()), globalenv(), msg = "child environment" )
269269
}
270270

271+
test.environment.new_env <- function() {
272+
env <- new.env()
273+
checkIdentical(parent.env(runit_new_env_default()), emptyenv(), msg = "new environment with default parent")
274+
checkIdentical(parent.env(runit_new_env_parent(env)), env, msg = "new environment with specified parent")
275+
}
271276

272277
}

0 commit comments

Comments
 (0)