|
| 1 | +// Copyright (C) 2013 Romain Francois |
| 2 | +// |
| 3 | +// This file is part of Rcpp. |
| 4 | +// |
| 5 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 6 | +// under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 2 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Rcpp is distributed in the hope that it will be useful, but |
| 11 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +#ifndef Rcpp_api_meat_Rcpp_eval_h |
| 19 | +#define Rcpp_api_meat_Rcpp_eval_h |
| 20 | + |
| 21 | +namespace Rcpp{ |
| 22 | + |
| 23 | + inline SEXP Rcpp_eval(SEXP expr_, SEXP env) { |
| 24 | + Shield<SEXP> expr( expr_) ; |
| 25 | + |
| 26 | + reset_current_error() ; |
| 27 | + |
| 28 | + Environment RCPP = Environment::Rcpp_namespace(); |
| 29 | + SEXP tryCatchSym = ::Rf_install("tryCatch"); |
| 30 | + SEXP evalqSym = ::Rf_install("evalq"); |
| 31 | + SEXP conditionMessageSym = ::Rf_install("conditionMessage"); |
| 32 | + SEXP errorRecorderSym = ::Rf_install(".rcpp_error_recorder"); |
| 33 | + SEXP errorSym = ::Rf_install("error"); |
| 34 | + |
| 35 | + Shield<SEXP> call( Rf_lang3( |
| 36 | + tryCatchSym, |
| 37 | + Rf_lang3( evalqSym, expr, env ), |
| 38 | + errorRecorderSym |
| 39 | + ) ) ; |
| 40 | + SET_TAG( CDDR(call), errorSym ) ; |
| 41 | + /* call the tryCatch call */ |
| 42 | + Shield<SEXP> res(::Rf_eval( call, RCPP ) ); |
| 43 | + |
| 44 | + if( error_occured() ) { |
| 45 | + Shield<SEXP> current_error ( rcpp_get_current_error() ) ; |
| 46 | + Shield<SEXP> conditionMessageCall (::Rf_lang2(conditionMessageSym, current_error)) ; |
| 47 | + Shield<SEXP> condition_message (::Rf_eval(conditionMessageCall, R_GlobalEnv)) ; |
| 48 | + std::string message(CHAR(::Rf_asChar(condition_message))); |
| 49 | + throw eval_error(message) ; |
| 50 | + } |
| 51 | + |
| 52 | + return res ; |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +#endif |
0 commit comments