Skip to content

Commit

Permalink
Eliminate enclose argument (#137)
Browse files Browse the repository at this point in the history
This eliminates one argument we need to thread through multiple function calls by creating a new environment with appropriate parent. (I doubt anyone actually uses this argument but it should be a safe transformation)
  • Loading branch information
hadley authored Jun 18, 2024
1 parent 7f8d7d5 commit ba8f5bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 6 additions & 8 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ evaluate <- function(input,
return(list(source, err))
}

if (is.null(enclos)) {
enclos <- if (is.list(envir) || is.pairlist(envir)) parent.frame() else baseenv()
if (is.list(envir)) {
envir <- list2env(envir, parent = enclos %||% parent.frame())
}

if (new_device) {
Expand Down Expand Up @@ -99,7 +99,6 @@ evaluate <- function(input,
exprs = parsed$expr[[i]],
src = parsed$src[[i]],
envir = envir,
enclos = enclos,
debug = debug,
last = i == length(out),
use_try = stop_on_error != 2L,
Expand Down Expand Up @@ -128,7 +127,6 @@ evaluate <- function(input,
evaluate_top_level_expression <- function(exprs,
src = NULL,
envir = parent.frame(),
enclos = NULL,
debug = FALSE,
last = FALSE,
use_try = FALSE,
Expand Down Expand Up @@ -242,7 +240,7 @@ evaluate_top_level_expression <- function(exprs,
srcindex <- length(output)
time <- timing_fn(handle(
ev <- withCallingHandlers(
withVisible(eval_with_user_handlers(expr, envir, enclos, user_handlers)),
withVisible(eval_with_user_handlers(expr, envir, user_handlers)),
warning = wHandler,
error = eHandler,
message = mHandler
Expand Down Expand Up @@ -274,9 +272,9 @@ evaluate_top_level_expression <- function(exprs,
output
}

eval_with_user_handlers <- function(expr, envir, enclos, calling_handlers) {
eval_with_user_handlers <- function(expr, envir, calling_handlers) {
if (!length(calling_handlers)) {
return(eval(expr, envir, enclos))
return(eval(expr, envir))
}

if (!is.list(calling_handlers)) {
Expand All @@ -285,7 +283,7 @@ eval_with_user_handlers <- function(expr, envir, enclos, calling_handlers) {

call <- as.call(c(
quote(withCallingHandlers),
quote(eval(expr, envir, enclos)),
quote(eval(expr, envir)),
calling_handlers
))

Expand Down
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ defer <- function(expr, frame = parent.frame(), after = FALSE) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, after), envir = frame)
}

`%||%` <- function(a, b) if (is.null(a)) b else a

0 comments on commit ba8f5bc

Please sign in to comment.