Skip to content

Commit 9412f1f

Browse files
committed
Move the check to an internal function
1 parent bb82bd9 commit 9412f1f

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

inst/include/Rcpp/exceptions.h

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,40 +126,49 @@ namespace Rcpp{
126126

127127
} // namespace Rcpp
128128

129-
inline SEXP nth(SEXP s, int n) {
130-
return Rf_length(s) > n ? (n == 0 ? CAR(s) : CAR(Rf_nthcdr(s, n))) : R_NilValue;
129+
namespace internal {
130+
131+
inline SEXP nth(SEXP s, int n) {
132+
return Rf_length(s) > n ? (n == 0 ? CAR(s) : CAR(Rf_nthcdr(s, n))) : R_NilValue;
133+
}
134+
135+
// We want the call just prior to the call from Rcpp_eval
136+
// This conditional matches
137+
// tryCatch(evalq(sys.calls(), .GlobalEnv), error = identity, interrupt = identity)
138+
bool is_Rcpp_eval_call(SEXP expr) {
139+
SEXP sys_calls_symbol = Rf_install("sys.calls");
140+
SEXP identity_symbol = Rf_install("identity");
141+
SEXP identity_fun = Rf_findFun(identity_symbol, R_BaseEnv);
142+
SEXP tryCatch_symbol = Rf_install("tryCatch");
143+
SEXP evalq_symbol = Rf_install("evalq");
144+
145+
return TYPEOF(expr) == LANGSXP &&
146+
Rf_length(expr) == 4 &&
147+
nth(expr, 0) == tryCatch_symbol &&
148+
CAR(nth(expr, 1)) == evalq_symbol &&
149+
CAR(nth(nth(expr, 1), 1)) == sys_calls_symbol &&
150+
nth(nth(expr, 1), 2) == R_GlobalEnv &&
151+
nth(expr, 2) == identity_fun &&
152+
nth(expr, 3) == identity_fun;
153+
}
131154
}
132155

133156
inline SEXP get_last_call(){
134157
SEXP sys_calls_symbol = Rf_install("sys.calls");
135-
SEXP identity_symbol = Rf_install("identity");
136-
SEXP identity_fun = Rf_findFun(identity_symbol, R_BaseEnv);
137-
SEXP tryCatch_symbol = Rf_install("tryCatch");
138-
SEXP evalq_symbol = Rf_install("evalq");
139158

140159
Rcpp::Shield<SEXP> sys_calls_expr(Rf_lang1(sys_calls_symbol));
141160
Rcpp::Shield<SEXP> calls(Rcpp_eval(sys_calls_expr, R_GlobalEnv));
142161

143162
SEXP cur, prev;
144163
prev = cur = calls;
145164
while(CDR(cur) != R_NilValue) {
146-
SEXP expr = CAR(cur);
147-
148-
// We want the call just prior to the call from Rcpp_eval
149-
// This conditional matches
150-
// tryCatch(evalq(sys.calls(), .GlobalEnv), error = identity, interrupt = identity)
151-
if (TYPEOF(expr) == LANGSXP &&
152-
Rf_length(expr) == 4 &&
153-
nth(expr, 0) == tryCatch_symbol &&
154-
CAR(nth(expr, 1)) == evalq_symbol &&
155-
CAR(nth(nth(expr, 1), 1)) == sys_calls_symbol &&
156-
nth(nth(expr, 1), 2) == R_GlobalEnv &&
157-
nth(expr, 2) == identity_fun &&
158-
nth(expr, 3) == identity_fun) {
159-
break;
160-
}
161-
prev = cur;
162-
cur = CDR(cur);
165+
SEXP expr = CAR(cur);
166+
167+
if (internal::is_Rcpp_eval_call(expr)) {
168+
break;
169+
}
170+
prev = cur;
171+
cur = CDR(cur);
163172
}
164173
return CAR(prev);
165174
}

0 commit comments

Comments
 (0)