|
| 1 | +#include "cpp11/protect.hpp" |
| 2 | +#include "cpp11/function.hpp" |
| 3 | +#include "testthat.h" |
| 4 | + |
| 5 | +#ifdef HAS_UNWIND_PROTECT |
| 6 | + |
| 7 | +/* |
| 8 | + * See https://github.com/r-lib/cpp11/pull/327 for full details. |
| 9 | + * |
| 10 | + * - `cpp11::package("cpp11test")["test_destruction_outer"]` uses |
| 11 | + * `unwind_protect()` to call R level `test_destruction_outer()` but no entry |
| 12 | + * macros are set up. Instead we are going to catch exceptions that get here |
| 13 | + * with `expect_error_as()`. |
| 14 | + * |
| 15 | + * - Call R level `test_destruction_outer()` to set up `BEGIN_CPP11` / |
| 16 | + * `END_CPP11` entry macros. |
| 17 | + * |
| 18 | + * - C++ `test_destruction_outer()` goes through `unwind_protect()` to call |
| 19 | + * the R level `test_destruction_inner()`. |
| 20 | + * |
| 21 | + * - R level `test_destruction_inner()` sets up its own `BEGIN_CPP11` / |
| 22 | + * `END_CPP11` entry macros. |
| 23 | + * |
| 24 | + * - C++ `test_destruction_inner()` goes through `unwind_protect()` to call |
| 25 | + * `Rf_error()` (i.e., we are nested within `unwind_protect()`s!). |
| 26 | + * |
| 27 | + * - `longjmp()` is caught from inner `unwind_protect()`, and an exception |
| 28 | + * is thrown which is caught by the inner entry macros, allowing us to run |
| 29 | + * the destructor of `x`, then we let R continue the unwind process. |
| 30 | + * |
| 31 | + * - This `longjmp()`s again and is caught by the outer `unwind_protect()`, an |
| 32 | + * exception is thrown which is caught by the outer entry macros, and we let |
| 33 | + * R continue the unwind process one more time. |
| 34 | + * |
| 35 | + * - This `longjmp()` is caught by `cpp11::package()`'s `unwind_protect()`, |
| 36 | + * an exception is thrown, and that is caught by `expect_error_as()`. |
| 37 | + */ |
| 38 | + |
| 39 | +// Global variable to detect if the destructor has been run or not |
| 40 | +static bool destructed = false; |
| 41 | + |
| 42 | +class HasDestructor { |
| 43 | +public: |
| 44 | + ~HasDestructor(); |
| 45 | +}; |
| 46 | + |
| 47 | +HasDestructor::~HasDestructor() { |
| 48 | + // Destructor has run! |
| 49 | + destructed = true; |
| 50 | +} |
| 51 | + |
| 52 | +[[cpp11::register]] |
| 53 | +void test_destruction_inner() { |
| 54 | + // Expect that `x`'s destructor gets to run on the way out |
| 55 | + HasDestructor x{}; |
| 56 | + cpp11::stop("oh no!"); |
| 57 | +} |
| 58 | + |
| 59 | +[[cpp11::register]] |
| 60 | +void test_destruction_outer() { |
| 61 | + const auto test_destruction_inner = cpp11::package("cpp11test")["test_destruction_inner"]; |
| 62 | + test_destruction_inner(); |
| 63 | +} |
| 64 | + |
| 65 | +context("unwind_protect-nested-C++") { |
| 66 | + test_that("nested `unwind_protect()` (with entry macros set up) will run destructors (#327)") { |
| 67 | + const auto fn = [&] { |
| 68 | + const auto test_destruction_outer = cpp11::package("cpp11test")["test_destruction_outer"]; |
| 69 | + test_destruction_outer(); |
| 70 | + }; |
| 71 | + |
| 72 | + expect_error_as(fn(), cpp11::unwind_exception); |
| 73 | + expect_true(destructed); |
| 74 | + |
| 75 | + destructed = false; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#endif // HAS_UNWIND_PROTECT |
0 commit comments