Skip to content

Commit ebf1f89

Browse files
committed
use R_NilValue with R_getVarEx to avoid non-API R_UnboundValue + some refactoring
1 parent 44e6a1a commit ebf1f89

1 file changed

Lines changed: 12 additions & 42 deletions

File tree

inst/include/Rcpp/Environment.h

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,8 @@ namespace Rcpp{
9595
* @return a SEXP (possibly R_NilValue)
9696
*/
9797
SEXP get(const std::string& name) const {
98-
SEXP env = Storage::get__() ;
99-
SEXP nameSym = Rf_install(name.c_str());
100-
#if R_VERSION < R_Version(4,5,0)
101-
SEXP res = Rf_findVarInFrame( env, nameSym ) ;
102-
#else
103-
SEXP res = R_getVarEx(nameSym, env, FALSE, R_UnboundValue);
104-
#endif
105-
if( res == R_UnboundValue ) return R_NilValue ;
106-
107-
/* We need to evaluate if it is a promise */
108-
if( TYPEOF(res) == PROMSXP){
109-
res = internal::Rcpp_eval_impl( res, env ) ; // #nocov
110-
}
111-
return res ;
98+
Symbol nameSym = Rf_install(name.c_str());
99+
return get(nameSym);
112100
}
113101

114102
/**
@@ -122,12 +110,10 @@ namespace Rcpp{
122110
SEXP env = Storage::get__() ;
123111
#if R_VERSION < R_Version(4,5,0)
124112
SEXP res = Rf_findVarInFrame( env, name ) ;
113+
if (res == R_UnboundValue) return R_NilValue;
125114
#else
126-
SEXP res = R_getVarEx(name, env, FALSE, R_UnboundValue);
115+
SEXP res = R_getVarEx(name, env, FALSE, R_NilValue);
127116
#endif
128-
129-
if( res == R_UnboundValue ) return R_NilValue ;
130-
131117
/* We need to evaluate if it is a promise */
132118
if( TYPEOF(res) == PROMSXP){
133119
res = internal::Rcpp_eval_impl( res, env ) ;
@@ -144,21 +130,8 @@ namespace Rcpp{
144130
*
145131
*/
146132
SEXP find( const std::string& name) const{
147-
SEXP env = Storage::get__() ;
148-
SEXP nameSym = Rf_install(name.c_str());
149-
#if R_VERSION < R_Version(4,5,0)
150-
SEXP res = Rf_findVar( nameSym, env ) ;
151-
#else
152-
SEXP res = R_getVarEx(nameSym, env, TRUE, R_UnboundValue);
153-
#endif
154-
155-
if( res == R_UnboundValue ) throw binding_not_found(name) ;
156-
157-
/* We need to evaluate if it is a promise */
158-
if( TYPEOF(res) == PROMSXP){
159-
res = internal::Rcpp_eval_impl( res, env ) ;
160-
}
161-
return res ;
133+
Symbol nameSym = Rf_install(name.c_str());
134+
return find(nameSym);
162135
}
163136

164137
/**
@@ -171,15 +144,11 @@ namespace Rcpp{
171144
SEXP env = Storage::get__() ;
172145
#if R_VERSION < R_Version(4,5,0)
173146
SEXP res = Rf_findVar( name, env ) ;
147+
if (res == R_UnboundValue) throw binding_not_found(name.c_str());
174148
#else
175-
SEXP res = R_getVarEx(name, env, TRUE, R_UnboundValue);
149+
SEXP res = R_getVarEx(name, env, TRUE, R_NilValue);
150+
if (res == R_NilValue) throw binding_not_found(name.c_str());
176151
#endif
177-
if( res == R_UnboundValue ) {
178-
// Pass on the const char* to the RCPP_EXCEPTION_CLASS's
179-
// const std::string& requirement
180-
throw binding_not_found(name.c_str()) ;
181-
}
182-
183152
/* We need to evaluate if it is a promise */
184153
if( TYPEOF(res) == PROMSXP){
185154
res = internal::Rcpp_eval_impl( res, env ) ;
@@ -199,10 +168,11 @@ namespace Rcpp{
199168
SEXP nameSym = Rf_install(name.c_str());
200169
#if R_VERSION < R_Version(4,5,0)
201170
SEXP res = Rf_findVarInFrame( Storage::get__() , nameSym ) ;
171+
return res != R_UnboundValue;
202172
#else
203-
SEXP res = R_getVarEx(nameSym, Storage::get__(), FALSE, R_UnboundValue);
173+
SEXP res = R_getVarEx(nameSym, Storage::get__(), FALSE, R_NilValue);
174+
return res != R_NilValue;
204175
#endif
205-
return res != R_UnboundValue ;
206176
}
207177

208178
/**

0 commit comments

Comments
 (0)