Skip to content

Commit 9c5dd15

Browse files
committed
Set the preserve list as a global option
Rather than an object in the global environment. Using an option is consistent with CRAN policies, whereas it is against policy for a package to modify the global environment
1 parent 7a266de commit 9c5dd15

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

inst/include/cpp11/protect.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,18 @@ static struct {
281281
}
282282

283283
private:
284+
static void set_option(SEXP name, SEXP value) {
285+
SEXP opt = SYMVALUE(safe[Rf_install](".Options"));
286+
SEXP t = opt;
287+
while (CDR(t) != R_NilValue) {
288+
t = CDR(t);
289+
}
290+
SETCDR(t, Rf_allocList(1));
291+
opt = CDR(t);
292+
SET_TAG(opt, name);
293+
SETCAR(opt, value);
294+
}
295+
284296
static SEXP get_preserve_list() {
285297
static SEXP list_singleton = R_NilValue;
286298

@@ -290,15 +302,14 @@ static struct {
290302
// compiled, resulting in unrelated instances of each static variable.
291303

292304
// FIXME how can we create the cpp11 namespace when it doesn't already exist?
293-
SEXP list_singleton_sym = safe[Rf_install](".cpp11_preserve_list");
305+
SEXP list_singleton_sym = safe[Rf_install]("cpp11_preserve_list");
294306

295-
list_singleton = safe[Rf_findVarInFrame](R_GlobalEnv, list_singleton_sym);
307+
list_singleton = safe[Rf_GetOption1](list_singleton_sym);
296308

297-
if (list_singleton == R_UnboundValue) {
298-
list_singleton = PROTECT(Rf_cons(R_NilValue, R_NilValue));
309+
if (list_singleton == R_NilValue) {
310+
list_singleton = Rf_cons(R_NilValue, R_NilValue);
299311
R_PreserveObject(list_singleton);
300-
UNPROTECT(1);
301-
safe[Rf_defineVar](list_singleton_sym, list_singleton, R_GlobalEnv);
312+
set_option(list_singleton_sym, list_singleton);
302313
}
303314
}
304315

0 commit comments

Comments
 (0)