Skip to content

Commit

Permalink
Add buggy example
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Mar 15, 2022
1 parent 25c4083 commit dc72e6a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
URL: https://github.com/DavisVaughan/vsexample
BugReports: https://github.com/DavisVaughan/vsexample/issues
LinkingTo:
cpp11
SystemRequirements: C++11
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Generated by roxygen2: do not edit by hand

useDynLib(vsexample, .registration = TRUE)
5 changes: 5 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by cpp11: do not edit by hand

buggy_init <- function(size) {
.Call(`_vsexample_buggy_init`, size)
}
7 changes: 7 additions & 0 deletions R/vsexample-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @useDynLib vsexample, .registration = TRUE
## usethis namespace: end
NULL
23 changes: 23 additions & 0 deletions man/vsexample-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.so
*.dll
17 changes: 17 additions & 0 deletions src/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <cpp11.hpp>

void buggy_fill(cpp11::writable::doubles& x, double value) {
R_xlen_t size = x.size();

for (R_xlen_t i = 1; i <= size; ++i) {
x[i] = value;
}
}

[[cpp11::register]]
cpp11::writable::doubles buggy_init(cpp11::integers size) {
R_xlen_t c_size = static_cast<R_xlen_t>(size[0]);
cpp11::writable::doubles out(c_size);
buggy_fill(out, 2);
return out;
}
27 changes: 27 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Generated by cpp11: do not edit by hand
// clang-format off


#include "cpp11/declarations.hpp"
#include <R_ext/Visibility.h>

// code.cpp
cpp11::writable::doubles buggy_init(cpp11::integers size);
extern "C" SEXP _vsexample_buggy_init(SEXP size) {
BEGIN_CPP11
return cpp11::as_sexp(buggy_init(cpp11::as_cpp<cpp11::decay_t<cpp11::integers>>(size)));
END_CPP11
}

extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_vsexample_buggy_init", (DL_FUNC) &_vsexample_buggy_init, 1},
{NULL, NULL, 0}
};
}

extern "C" attribute_visible void R_init_vsexample(DllInfo* dll){
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
R_forceSymbols(dll, TRUE);
}

0 comments on commit dc72e6a

Please sign in to comment.