@@ -123,82 +123,84 @@ namespace Rcpp{
123123 #undef RCPP_EXCEPTION_CLASS
124124 #undef RCPP_SIMPLE_EXCEPTION_CLASS
125125
126- inline SEXP get_last_call (){
127- SEXP sys_calls_symbol = Rf_install ( " sys.calls" ) ;
128- Shield<SEXP> sys_calls_expr ( Rf_lang1 (sys_calls_symbol) );
129- Shield<SEXP> calls ( Rf_eval ( sys_calls_expr, R_GlobalEnv ) );
130- SEXP res = calls ;
131- while ( !Rf_isNull (CDR (res)) ) res = CDR (res);
132- return CAR (res) ;
133- }
134-
135- inline SEXP get_exception_classes ( const std::string& ex_class) {
136- Shield<SEXP> res ( Rf_allocVector ( STRSXP, 4 ) );
137- SET_STRING_ELT ( res, 0 , Rf_mkChar ( ex_class.c_str () ) ) ;
138- SET_STRING_ELT ( res, 1 , Rf_mkChar ( " C++Error" ) ) ;
139- SET_STRING_ELT ( res, 2 , Rf_mkChar ( " error" ) ) ;
140- SET_STRING_ELT ( res, 3 , Rf_mkChar ( " condition" ) ) ;
141- return res;
142- }
143126
144- inline SEXP make_condition (const std::string& ex_msg, SEXP call, SEXP cppstack, SEXP classes){
145- Shield<SEXP> res ( Rf_allocVector ( VECSXP, 3 ) ) ;
146-
147- SET_VECTOR_ELT ( res, 0 , Rf_mkString ( ex_msg.c_str () ) ) ;
148- SET_VECTOR_ELT ( res, 1 , call ) ;
149- SET_VECTOR_ELT ( res, 2 , cppstack ) ;
150-
151- Shield<SEXP> names ( Rf_allocVector ( STRSXP, 3 ) );
152- SET_STRING_ELT ( names, 0 , Rf_mkChar ( " message" ) ) ;
153- SET_STRING_ELT ( names, 1 , Rf_mkChar ( " call" ) ) ;
154- SET_STRING_ELT ( names, 2 , Rf_mkChar ( " cppstack" ) ) ;
155- Rf_setAttrib ( res, R_NamesSymbol, names ) ;
156- Rf_setAttrib ( res, R_ClassSymbol, classes ) ;
157- return res ;
158- }
159-
160- inline SEXP exception_to_r_condition ( const std::exception& ex){
161- std::string ex_class = demangle ( typeid (ex).name () ) ;
162- std::string ex_msg = ex.what () ;
163-
164- Shield<SEXP> cppstack ( rcpp_get_stack_trace () );
165- Shield<SEXP> call ( get_last_call () );
166- Shield<SEXP> classes ( get_exception_classes (ex_class) );
167- Shield<SEXP> condition ( make_condition ( ex_msg, call, cppstack, classes) );
168- rcpp_set_stack_trace ( R_NilValue ) ;
169- return condition ;
170- }
171-
172- inline void forward_exception_to_r ( const std::exception& ex){
173- SEXP stop_sym = Rf_install ( " stop" ) ;
174- Shield<SEXP> condition ( exception_to_r_condition (ex) );
175- Shield<SEXP> expr ( Rf_lang2 ( stop_sym , condition ) ) ;
176- Rf_eval ( expr, R_GlobalEnv ) ;
177- }
178-
179- inline SEXP string_to_try_error ( const std::string& str){
180- using namespace Rcpp ;
181-
182- Shield<SEXP> simpleErrorExpr ( Rf_lang2 (::Rf_install (" simpleError" ), Rf_mkString (str.c_str ())) );
183- Shield<SEXP> simpleError ( Rf_eval (simpleErrorExpr, R_GlobalEnv) );
184- Shield<SEXP> tryError ( Rf_mkString ( str.c_str () ) );
185- Rf_setAttrib ( tryError, R_ClassSymbol, Rf_mkString (" try-error" ) ) ;
186- Rf_setAttrib ( tryError, Rf_install ( " condition" ) , simpleError ) ;
187-
188- return tryError;
189- }
127+ } // namespace Rcpp
128+
129+ inline SEXP get_last_call (){
130+ SEXP sys_calls_symbol = Rf_install ( " sys.calls" ) ;
131+ Rcpp::Shield<SEXP> sys_calls_expr ( Rf_lang1 (sys_calls_symbol) );
132+ Rcpp::Shield<SEXP> calls ( Rf_eval ( sys_calls_expr, R_GlobalEnv ) );
133+ SEXP res = calls ;
134+ while ( !Rf_isNull (CDR (res)) ) res = CDR (res);
135+ return CAR (res) ;
136+ }
137+
138+ inline SEXP get_exception_classes ( const std::string& ex_class) {
139+ Rcpp::Shield<SEXP> res ( Rf_allocVector ( STRSXP, 4 ) );
140+ SET_STRING_ELT ( res, 0 , Rf_mkChar ( ex_class.c_str () ) ) ;
141+ SET_STRING_ELT ( res, 1 , Rf_mkChar ( " C++Error" ) ) ;
142+ SET_STRING_ELT ( res, 2 , Rf_mkChar ( " error" ) ) ;
143+ SET_STRING_ELT ( res, 3 , Rf_mkChar ( " condition" ) ) ;
144+ return res;
145+ }
146+
147+ inline SEXP make_condition (const std::string& ex_msg, SEXP call, SEXP cppstack, SEXP classes){
148+ Rcpp::Shield<SEXP> res ( Rf_allocVector ( VECSXP, 3 ) ) ;
149+
150+ SET_VECTOR_ELT ( res, 0 , Rf_mkString ( ex_msg.c_str () ) ) ;
151+ SET_VECTOR_ELT ( res, 1 , call ) ;
152+ SET_VECTOR_ELT ( res, 2 , cppstack ) ;
153+
154+ Rcpp::Shield<SEXP> names ( Rf_allocVector ( STRSXP, 3 ) );
155+ SET_STRING_ELT ( names, 0 , Rf_mkChar ( " message" ) ) ;
156+ SET_STRING_ELT ( names, 1 , Rf_mkChar ( " call" ) ) ;
157+ SET_STRING_ELT ( names, 2 , Rf_mkChar ( " cppstack" ) ) ;
158+ Rf_setAttrib ( res, R_NamesSymbol, names ) ;
159+ Rf_setAttrib ( res, R_ClassSymbol, classes ) ;
160+ return res ;
161+ }
162+
163+ inline SEXP exception_to_r_condition ( const std::exception& ex){
164+ std::string ex_class = demangle ( typeid (ex).name () ) ;
165+ std::string ex_msg = ex.what () ;
166+
167+ Rcpp::Shield<SEXP> cppstack ( rcpp_get_stack_trace () );
168+ Rcpp::Shield<SEXP> call ( get_last_call () );
169+ Rcpp::Shield<SEXP> classes ( get_exception_classes (ex_class) );
170+ Rcpp::Shield<SEXP> condition ( make_condition ( ex_msg, call, cppstack, classes) );
171+ rcpp_set_stack_trace ( R_NilValue ) ;
172+ return condition ;
173+ }
174+
175+ inline SEXP string_to_try_error ( const std::string& str){
176+ using namespace Rcpp ;
190177
191- inline SEXP exception_to_try_error ( const std::exception& ex){
192- return string_to_try_error (ex.what ());
193- }
178+ Rcpp::Shield<SEXP> simpleErrorExpr ( Rf_lang2 (::Rf_install (" simpleError" ), Rf_mkString (str.c_str ())) );
179+ Rcpp::Shield<SEXP> simpleError ( Rf_eval (simpleErrorExpr, R_GlobalEnv) );
180+ Rcpp::Shield<SEXP> tryError ( Rf_mkString ( str.c_str () ) );
181+ Rf_setAttrib ( tryError, R_ClassSymbol, Rf_mkString (" try-error" ) ) ;
182+ Rf_setAttrib ( tryError, Rf_install ( " condition" ) , simpleError ) ;
194183
195- std::string demangle ( const std::string& name) ;
196- #define DEMANGLE (__TYPE__ ) demangle( typeid (__TYPE__).name() ).c_str()
184+ return tryError;
185+ }
186+
187+ inline SEXP exception_to_try_error ( const std::exception& ex){
188+ return string_to_try_error (ex.what ());
189+ }
190+
191+ std::string demangle ( const std::string& name) ;
192+ #define DEMANGLE (__TYPE__ ) demangle( typeid (__TYPE__).name() ).c_str()
197193
198- inline void stop (const std::string& message) {
199- throw Rcpp::exception (message.c_str ());
200- }
194+ inline void stop (const std::string& message) {
195+ throw Rcpp::exception (message.c_str ());
196+ }
197+
198+ inline void forward_exception_to_r ( const std::exception& ex){
199+ SEXP stop_sym = Rf_install ( " stop" ) ;
200+ Rcpp::Shield<SEXP> condition ( exception_to_r_condition (ex) );
201+ Rcpp::Shield<SEXP> expr ( Rf_lang2 ( stop_sym , condition ) ) ;
202+ Rf_eval ( expr, R_GlobalEnv ) ;
203+ }
201204
202- } // namespace Rcpp
203205
204206#endif
0 commit comments