Skip to content

Commit 985c94b

Browse files
committed
Don't use new eval API unless RCPP_PROTECTED_EVAL is defined
1 parent ad7e397 commit 985c94b

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

ChangeLog

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
catch those, consider using Rcpp_fast_eval() instead to avoid the
1717
overhead.
1818

19-
These improvements are only available for R 3.5.0 and greater. When
20-
compiled with old versions of R, Rcpp_fast_eval() falls back to
21-
Rcpp_eval(). This is in contrast to internal::Rcpp_eval_impl() which
19+
These improvements are only available for R 3.5.0 and greater. You also
20+
need to explicitly define `RCPP_PROTECTED_EVAL` before including Rcpp.h.
21+
When compiled with old versions of R, Rcpp_fast_eval() always falls back
22+
to Rcpp_eval(). This is in contrast to internal::Rcpp_eval_impl() which
2223
falls back to Rf_eval() and which is used in performance-sensititive
2324
places.
2425

inst/NEWS.Rd

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
set an initial format string (Dirk in \ghpr{777} fixing \ghit{776}).
1212
\item The 'new' Date and Datetime vectors now have \code{is_na} methods
1313
too. (Dirk in \ghpr{783} fixing \ghit{781}).
14-
1514
\item Evaluation of R code is now safer when compiled against R
16-
3.5. Longjumps of all kinds (condition catching, returns,
17-
restarts, debugger exit) are appropriately detected and handled,
18-
e.g. the C++ stack unwinds correctly. The new function
19-
\code{Rcpp_fast_eval()} can be used for performance-sensitive
20-
evaluation of R code. Unlike \code{Rcpp_eval()}, it does not try
21-
to catch errors with \code{tryEval} in order to avoid the catching
22-
overhead. While this is safe thanks to the stack unwinding
23-
protection, this also means that R errors are not transformed to
24-
an \code{Rcpp::exception}. If you are relying on error rethrowing,
25-
you have to use the slower \code{Rcpp_eval()}. On old R versions
26-
\code{Rcpp_fast_eval()} falls back to \code{Rcpp_eval()} so it is
27-
safe to use against any versions of R.
15+
3.5 (you also need to explicitly define \code{RCPP_PROTECTED_EVAL}
16+
before including \code{Rcpp.h}). Longjumps of all kinds (condition
17+
catching, returns, restarts, debugger exit) are appropriately
18+
detected and handled, e.g. the C++ stack unwinds correctly.
19+
\item The new function \code{Rcpp_fast_eval()} can be used for
20+
performance-sensitive evaluation of R code. Unlike
21+
\code{Rcpp_eval()}, it does not try to catch errors with
22+
\code{tryEval} in order to avoid the catching overhead. While this
23+
is safe thanks to the stack unwinding protection, this also means
24+
that R errors are not transformed to an \code{Rcpp::exception}. If
25+
you are relying on error rethrowing, you have to use the slower
26+
\code{Rcpp_eval()}. On old R versions \code{Rcpp_fast_eval()}
27+
falls back to \code{Rcpp_eval()} so it is safe to use against any
28+
versions of R.
2829
}
2930
\item Changes in Rcpp Attributes:
3031
\itemize{

inst/include/Rcpp/api/meat/Rcpp_eval.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
#include <Rcpp/Interrupt.h>
2222
#include <Rversion.h>
2323

24-
#if (defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0))
25-
#define R_HAS_UNWIND
24+
#if (defined(RCPP_PROTECTED_EVAL) && defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0))
25+
#define RCPP_USE_PROTECT_UNWIND
2626
#endif
2727

2828

2929
namespace Rcpp {
3030
namespace internal {
3131

32-
#ifdef R_HAS_UNWIND
32+
#ifdef RCPP_USE_PROTECT_UNWIND
3333

3434
struct EvalData {
3535
SEXP expr;
@@ -65,7 +65,7 @@ namespace internal {
6565
} // namespace internal
6666

6767

68-
#ifdef R_HAS_UNWIND
68+
#ifdef RCPP_USE_PROTECT_UNWIND
6969

7070
inline SEXP Rcpp_fast_eval(SEXP expr, SEXP env) {
7171
internal::EvalData data(expr, env);

inst/include/Rcpp/exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ namespace Rcpp {
119119
};
120120

121121
inline void resumeJump(SEXP token) {
122-
#if (defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0))
122+
#if (defined(RCPP_PROTECTED_EVAL) && defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0))
123123
::R_ContinueUnwind(token);
124124
#endif
125125
}

inst/unitTests/cpp/misc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22+
#define RCPP_PROTECTED_EVAL
23+
2224
#include <Rcpp.h>
2325
using namespace Rcpp;
2426
using namespace std;

0 commit comments

Comments
 (0)