Skip to content

Commit

Permalink
feat: Update vendored cpp11 to 0.5.1 (#636)
Browse files Browse the repository at this point in the history
* Revert "ifndef"

This reverts commit 9afb676.

* Revert "Reapply 782630b minus the whitespace changes"

This reverts commit f05d56c.

* Reapply "Revert a011d42"

This reverts commit fe615c4.

* Reapply "Revert fb18a2d"

This reverts commit 6f4979d.

* Vendor cpp11 0.5.1

* Add `prot` argument to `external_pointer()` constructor

r-lib/cpp11#420

This reverts commit abcae40e2aff8b1664098294959b8df743605737.

* END_CPP11_EX()

r-lib/cpp11#421

* Protection

r-lib/cpp11#422

This reverts commit 1e181e5e5c2cf0a21906b49dafe80364c8ca3f2b.

* External pointer premature release

r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
  • Loading branch information
krlmlr authored Dec 7, 2024
1 parent 081dcf9 commit 1bba551
Show file tree
Hide file tree
Showing 24 changed files with 73 additions and 134 deletions.
4 changes: 2 additions & 2 deletions inst/include/cpp11.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include "cpp11/R.hpp"
Expand Down
7 changes: 4 additions & 3 deletions inst/include/cpp11/R.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#ifdef R_INTERNALS_H_
Expand All @@ -13,9 +13,11 @@
#ifndef R_NO_REMAP
#define R_NO_REMAP
#endif

#ifndef STRICT_R_HEADERS
#define STRICT_R_HEADERS
#endif

#include "R_ext/Boolean.h"
#include "Rinternals.h"
#include "Rversion.h"
Expand All @@ -33,7 +35,6 @@
// clang-format on

#include <type_traits>
#include "cpp11/altrep.hpp"

#if defined(R_VERSION) && R_VERSION >= R_Version(4, 4, 0)
// Use R's new macro
Expand Down
46 changes: 5 additions & 41 deletions inst/include/cpp11/altrep.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include "Rversion.h"

#if defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0)
// It would be nice to remove this since all supported versions of R have ALTREP, but
// some groups rely on both this `#define` and `altrep.hpp` itself existing, like arrow:
// https://github.com/r-lib/cpp11/issues/413
#define HAS_ALTREP
#endif

#ifndef HAS_ALTREP

#define ALTREP(x) false

#define REAL_ELT(x, i) REAL(x)[i]
#define INTEGER_ELT(x, i) INTEGER(x)[i]
#define LOGICAL_ELT(x, i) LOGICAL(x)[i]
#define RAW_ELT(x, i) RAW(x)[i]

#define SET_REAL_ELT(x, i, val) REAL(x)[i] = val
#define SET_INTEGER_ELT(x, i, val) INTEGER(x)[i] = val
#define SET_LOGICAL_ELT(x, i, val) LOGICAL(x)[i] = val
#define SET_RAW_ELT(x, i, val) RAW(x)[i] = val

#define REAL_GET_REGION(...) \
do { \
} while (false)

#define INTEGER_GET_REGION(...) \
do { \
} while (false)
#endif

#if !defined HAS_ALTREP || (defined(R_VERSION) && R_VERSION < R_Version(3, 6, 0))

#define LOGICAL_GET_REGION(...) \
do { \
} while (false)

#define RAW_GET_REGION(...) \
do { \
} while (false)

#endif
4 changes: 2 additions & 2 deletions inst/include/cpp11/as.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <cmath> // for modf
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/attribute_proxy.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <initializer_list> // for initializer_list
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/data_frame.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <cstdlib> // for abs
Expand Down
15 changes: 6 additions & 9 deletions inst/include/cpp11/declarations.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <cstring>
Expand Down Expand Up @@ -32,13 +32,10 @@ T& unmove(T&& t) {
}
} // namespace cpp11

#ifdef HAS_UNWIND_PROTECT
// We would like to remove this, since all supported versions of R now support proper
// unwind protect, but some groups rely on it existing, like textshaping:
// https://github.com/r-lib/cpp11/issues/414
#define CPP11_UNWIND R_ContinueUnwind(err);
#else
#define CPP11_UNWIND \
do { \
} while (false);
#endif

#define CPP11_ERROR_BUFSIZE 8192

Expand All @@ -60,7 +57,7 @@ T& unmove(T&& t) {
if (buf[0] != '\0') { \
Rf_errorcall(R_NilValue, "%s", buf); \
} else if (err != R_NilValue) { \
CPP11_UNWIND \
R_ContinueUnwind(err); \
} \
return RET;
#define END_CPP11 END_CPP11_EX(R_NilValue)
6 changes: 3 additions & 3 deletions inst/include/cpp11/doubles.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <algorithm> // for min, tranform
Expand Down Expand Up @@ -50,7 +50,7 @@ inline void r_vector<double>::get_region(SEXP x, R_xlen_t i, R_xlen_t n,
typename r_vector::underlying_type* buf) {
// NOPROTECT: likely too costly to unwind protect here
REAL_GET_REGION(x, i, n, buf);
};
}

template <>
inline bool r_vector<double>::const_iterator::use_buf(bool is_altrep) {
Expand Down
18 changes: 2 additions & 16 deletions inst/include/cpp11/environment.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <string> // for string, basic_string

#include "Rversion.h" // for R_VERSION, R_Version
#include "cpp11/R.hpp" // for SEXP, SEXPREC, Rf_install, r_env_get...
#include "cpp11/as.hpp" // for as_sexp
#include "cpp11/protect.hpp" // for protect, protect::function, safe, unwin...
#include "cpp11/sexp.hpp" // for sexp

#if R_VERSION >= R_Version(4, 0, 0)
#define HAS_REMOVE_VAR_FROM_FRAME
#endif

#ifndef HAS_REMOVE_VAR_FROM_FRAME
#include "cpp11/function.hpp"
#endif

namespace cpp11 {

class environment {
Expand Down Expand Up @@ -53,12 +44,7 @@ class environment {

void remove(SEXP name) {
PROTECT(name);
#ifdef HAS_REMOVE_VAR_FROM_FRAME
R_removeVarFromFrame(name, env_);
#else
auto remove = package("base")["remove"];
remove(name, "envir"_nm = env_);
#endif
UNPROTECT(1);
}

Expand Down
7 changes: 4 additions & 3 deletions inst/include/cpp11/external_pointer.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <cstddef> // for nullptr_t, NULL
Expand Down Expand Up @@ -57,7 +57,8 @@ class external_pointer {

external_pointer(SEXP data) : data_(valid_type(data)) {}

external_pointer(pointer p, bool use_deleter = true, bool finalize_on_exit = true, SEXP prot = R_NilValue)
external_pointer(pointer p, bool use_deleter = true, bool finalize_on_exit = true,
SEXP prot = R_NilValue)
: data_(safe[R_MakeExternalPtr]((void*)p, R_NilValue, prot)) {
if (use_deleter) {
R_RegisterCFinalizerEx(data_, r_deleter, static_cast<r_bool>(finalize_on_exit));
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/function.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <string.h> // for strcmp
Expand Down
6 changes: 3 additions & 3 deletions inst/include/cpp11/integers.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <algorithm> // for min
Expand Down Expand Up @@ -51,7 +51,7 @@ inline void r_vector<int>::get_region(SEXP x, R_xlen_t i, R_xlen_t n,
typename r_vector::underlying_type* buf) {
// NOPROTECT: likely too costly to unwind protect here
INTEGER_GET_REGION(x, i, n, buf);
};
}

template <>
inline bool r_vector<int>::const_iterator::use_buf(bool is_altrep) {
Expand Down
6 changes: 3 additions & 3 deletions inst/include/cpp11/list.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <initializer_list> // for initializer_list
Expand Down Expand Up @@ -54,7 +54,7 @@ template <>
inline void r_vector<SEXP>::get_region(SEXP x, R_xlen_t i, R_xlen_t n,
typename r_vector::underlying_type* buf) {
cpp11::stop("Unreachable!");
};
}

template <>
inline bool r_vector<SEXP>::const_iterator::use_buf(bool is_altrep) {
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/list_of.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <string> // for string, basic_string
Expand Down
6 changes: 3 additions & 3 deletions inst/include/cpp11/logicals.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <algorithm> // for min
Expand Down Expand Up @@ -50,7 +50,7 @@ inline void r_vector<r_bool>::get_region(SEXP x, R_xlen_t i, R_xlen_t n,
typename r_vector::underlying_type* buf) {
// NOPROTECT: likely too costly to unwind protect here
LOGICAL_GET_REGION(x, i, n, buf);
};
}

template <>
inline bool r_vector<r_bool>::const_iterator::use_buf(bool is_altrep) {
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <iterator>
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/named_arg.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <stddef.h> // for size_t
Expand Down
21 changes: 5 additions & 16 deletions inst/include/cpp11/protect.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <csetjmp> // for longjmp, setjmp, jmp_buf
Expand All @@ -15,11 +15,11 @@
#include "R_ext/Error.h" // for Rf_error, Rf_warning
#include "R_ext/Print.h" // for REprintf
#include "R_ext/Utils.h" // for R_CheckUserInterrupt
#include "Rversion.h" // for R_VERSION, R_Version

#if defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0)
// We would like to remove this, since all supported versions of R now support proper
// unwind protect, but some groups rely on it existing, like arrow and systemfonts
// https://github.com/r-lib/cpp11/issues/412
#define HAS_UNWIND_PROTECT
#endif

#ifdef CPP11_USE_FMT
#define FMT_HEADER_ONLY
Expand All @@ -33,8 +33,6 @@ class unwind_exception : public std::exception {
unwind_exception(SEXP token_) : token(token_) {}
};

#ifdef HAS_UNWIND_PROTECT

/// Unwind Protection from C longjmp's, like those used in R error handling
///
/// @param code The code to which needs to be protected, as a nullary callable
Expand Down Expand Up @@ -97,15 +95,6 @@ unwind_protect(Fun&& code) {
return out;
}

#else
// Don't do anything if we don't have unwind protect. This will leak C++ resources,
// including those held by cpp11 objects, but the other alternatives are also not great.
template <typename Fun>
decltype(std::declval<Fun&&>()()) unwind_protect(Fun&& code) {
return std::forward<Fun>(code)();
}
#endif

namespace detail {

template <size_t...>
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/r_bool.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <limits> // for numeric_limits
Expand Down
4 changes: 2 additions & 2 deletions inst/include/cpp11/r_string.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// cpp11 version: 0.5.0
// vendored on: 2024-09-24
// cpp11 version: 0.5.1
// vendored on: 2024-12-07
#pragma once

#include <string> // for string, basic_string, operator==
Expand Down
Loading

0 comments on commit 1bba551

Please sign in to comment.