Skip to content

Commit

Permalink
TODO: why is enumerate breaking things
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Oct 3, 2024
1 parent e449f99 commit e13aa17
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 92 deletions.
23 changes: 23 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Library/Frameworks/R.framework/Resources/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
"-I/Library/Frameworks/R.framework/Resources/include"
]
}
],
"version": 4
}
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"CMake*.txt": "cmake",
"r.h": "c",
"rdynload.h": "c",
"rinternals.h": "c",
"__node_handle": "c"
}
}
18 changes: 18 additions & 0 deletions R/ext.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@

renv_ext_compile <- function(libdir) {

srcfiles <- list.files("tools/ext", pattern = "\\.c$", full.names = TRUE)
file.copy(srcfiles, libdir)

owd <- getwd()
on.exit(setwd(owd), add = TRUE)
setwd(libdir)

message("** extensions")
r <- file.path(R.home("bin"), if (.Platform$OS.type == "unix") "R" else "R.exe")
system2(r, c("CMD", "SHLIB", shQuote(basename(srcfiles))))

oldfiles <- list.files(pattern = "\\.[co]$", full.names = TRUE)
unlink(oldfiles)

}

renv_ext_init <- function() {
if (!is.null(the$dll_info)) {
envir <- renv_envir_self()
Expand Down
41 changes: 29 additions & 12 deletions R/ffi.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@

`__ffi__enumerate` <- function(x, f, ..., FUN.VALUE = NULL) {

f <- match.fun(f)

.Call(
"renv_ffi__enumerate",
x,
FUN.VALUE,
environment(),
PACKAGE = "renv"
)

}

`__ffi__renv_call_expect` <- function(node, package, methods) {

.Call(
"renv_ffi__renv_call_expect",
node,
as.character(package),
as.character(methods),
PACKAGE = "renv"
)

}

`__ffi__renv_dependencies_recurse` <- function(object, callback) {

symbol <- as.symbol(names(formals(args(callback)))[[1L]])
envir <- new.env(parent = environment(callback))
expr <- body(callback)
envir <- new.env(parent = environment(callback))

.Call(
"renv_ffi_recurse",
"renv_ffi__renv_dependencies_recurse",
object,
symbol,
expr,
envir,
PACKAGE = .packageName
PACKAGE = "renv"
)

}

`__ffi__renv_call_expect` <- function(node, package, methods) {
.Call(
"renv_ffi_call_expect",
node,
as.character(package),
as.character(methods),
PACKAGE = .packageName
)
}
12 changes: 4 additions & 8 deletions R/hydrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,12 @@ hydrate <- function(packages = NULL,
renv_hydrate_filter <- function(packages, library, update) {

# run filter
keep <- enumerate(
packages,
renv_hydrate_filter_impl,
library = library,
update = update,
FUN.VALUE = logical(1)
)
keep <- enumerate(packages, function(package, path) {
renv_hydrate_filter_impl(package, path, library, update)
})

# filter based on kept packages
packages[keep]
packages[as.logical(keep)]

}

Expand Down
2 changes: 1 addition & 1 deletion R/methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ renv_methods_init <- function() {
# determine appropriate lookup key for finding alternative
key <- if (renv_platform_windows()) "win32" else "unix"
alts <- map(methods, `[[`, key)

# update methods in namespace
envir <- renv_envir_self()
enumerate(alts, function(name, alt) {
Expand Down
14 changes: 1 addition & 13 deletions R/zzz-libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,7 @@ renv_zzz_libs_impl <- function() {
libdir <- paste(c(dir, "libs", if (nzchar(arch)) arch), collapse = "/")
dir.create(libdir, recursive = TRUE, showWarnings = FALSE)

srcfiles <- list.files("tools/ext", pattern = "\\.c$", full.names = TRUE)
file.copy(srcfiles, libdir)

owd <- getwd()
on.exit(setwd(owd), add = TRUE)
setwd(libdir)

message("** extensions")
r <- file.path(R.home("bin"), if (.Platform$OS.type == "unix") "R" else "R.exe")
system2(r, c("CMD", "SHLIB", shQuote(basename(srcfiles))))

oldfiles <- list.files(pattern = "\\.[co]$", full.names = TRUE)
unlink(oldfiles)
renv_ext_compile(libdir)

}

Expand Down
17 changes: 13 additions & 4 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
the$envir_self <<- renv_envir_self()

# if we were build with a shared library, use it
arch <- .Platform$r_arch
name <- paste0("renv", .Platform$dynlib.ext)
parts <- c(libname, pkgname, "libs", if (nzchar(arch)) arch, name)
sofile <- paste(parts, collapse = "/")
arch <- if (nzchar(.Platform$r_arch)) .Platform$r_arch
libdir <- paste(c(libname, pkgname, "libs", arch), collapse = "/")

# if we were invoked via devtools::load_all(), build the library
load <- Sys.getenv("DEVTOOLS_LOAD", unset = NA)
if (interactive() && identical(load, "renv")) {
ensure_directory(libdir)
renv_ext_compile(libdir)
}

# now try to load it
soname <- paste0("renv", .Platform$dynlib.ext)
sofile <- file.path(libdir, soname)
if (file.exists(sofile)) {
info <- library.dynam("renv", pkgname, libname)
the$dll_info <- info
Expand Down
52 changes: 52 additions & 0 deletions tests/testthat/test-enumerate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

test_that("enumerate() works as expected", {

zip <- function(key, value) list(key, value)

data <- list(a = 1, b = 2, c = 3)
actual <- enumerate(data, zip)
expected <- list(a = list("a", 1), b = list("b", 2), c = list("c", 3))
expect_identical(actual, expected)

data <- list(a = "1", b = "2", c = "3")
actual <- enumerate(data, zip)
expected <- list(a = list("a", "1"), b = list("b", "2"), c = list("c", "3"))
expect_identical(actual, expected)

})

test_that("enumerate() handles dots", {

values <- list()
data <- list(a = 1, b = 2, c = 3)
enumerate(data, function(key, value, extra) {
values[[length(values) + 1L]] <<- list(key, value, extra)
}, extra = TRUE)

expect_identical(values, list(
list("a", 1, TRUE),
list("b", 2, TRUE),
list("c", 3, TRUE)
))

})

test_that("enum_chr() does what it should", {

actual <- enum_chr(
list(a = "1", b = "2", c = "3"),
function(key, value) value
)

expected <- c(a = "1", b = "2", c = "3")
expect_identical(actual, expected)

actual <- enum_chr(
list(1, 2, 3),
function(key, value) value
)

expected <- c("1", "2", "3")
expect_identical(actual, expected)

})
Loading

0 comments on commit e13aa17

Please sign in to comment.