Skip to content

Commit ed60b32

Browse files
committed
WIP #70
1 parent a9e9c0e commit ed60b32

File tree

6 files changed

+20
-42
lines changed

6 files changed

+20
-42
lines changed

R/qenv-class.R

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,14 @@
1414
#' @exportClass qenv
1515
setClass(
1616
"qenv",
17-
slots = c(env = "environment", code = "character", id = "integer", warnings = "character", messages = "character"),
17+
slots = c(env = "environment", code = "list"),
1818
prototype = list(
19-
env = new.env(parent = parent.env(.GlobalEnv)), code = character(0), id = integer(0),
20-
warnings = character(0), messages = character(0)
19+
env = new.env(parent = parent.env(.GlobalEnv)), code = list()
2120
)
2221
)
2322

2423
#' It takes a `qenv` class and returns `TRUE` if the input is valid
2524
#' @name qenv-class
2625
#' @keywords internal
2726
setValidity("qenv", function(object) {
28-
if (length(object@code) != length(object@id)) {
29-
"@code and @id slots must have the same length."
30-
} else if (length(object@code) != length(object@warnings)) {
31-
"@code and @warnings slots must have the same length"
32-
} else if (length(object@code) != length(object@messages)) {
33-
"@code and @messages slots must have the same length"
34-
} else if (any(duplicated(object@id))) {
35-
"@id contains duplicated values."
36-
} else {
37-
TRUE
38-
}
3927
})

R/qenv-eval_code.R

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,20 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
3333
return(object)
3434
}
3535

36-
id <- sample.int(.Machine$integer.max, size = length(parsed_code))
37-
38-
object@id <- c(object@id, id)
3936
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
40-
4137
code_split <- split_code(paste(code, collapse = "\n"))
42-
object@code <- c(object@code, unlist(code_split))
43-
44-
current_warnings <- rep("", length(parsed_code))
45-
current_messages <- rep("", length(parsed_code))
4638

4739
for (i in seq_along(code_split)) {
48-
single_call <- parse(text = code_split[[i]], keep.source = FALSE)
40+
current_code <- code_split[[i]]
41+
current_call <- parse(text = current_code, keep.source = FALSE)
42+
new_object_code <- c(object@code, list(current_code))
43+
4944
# Using withCallingHandlers to capture warnings and messages.
5045
# Using tryCatch to capture the error and abort further evaluation.
5146
x <- withCallingHandlers(
5247
tryCatch(
5348
{
54-
eval(single_call, envir = object@env)
49+
eval(current_call, envir = object@env)
5550
if (!identical(parent.env(object@env), parent.env(.GlobalEnv))) {
5651
# needed to make sure that @env is always a sibling of .GlobalEnv
5752
# could be changed when any new package is added to search path (through library or require call)
@@ -64,31 +59,30 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
6459
message = sprintf(
6560
"%s \n when evaluating qenv code:\n%s",
6661
.ansi_strip(conditionMessage(e)),
67-
deparse1(single_call)
62+
deparse1(current_call)
6863
),
6964
class = c("qenv.error", "try-error", "simpleError"),
70-
trace = object@code
65+
trace = unlist(new_object_code)
7166
)
7267
}
7368
),
7469
warning = function(w) {
75-
current_warnings[i] <<- .ansi_strip(sprintf("> %s\n", conditionMessage(w)))
70+
attr(current_code, "warning") <<- .ansi_strip(sprintf("> %s\n", conditionMessage(w)))
7671
invokeRestart("muffleWarning")
7772
},
7873
message = function(m) {
79-
current_messages[i] <<- .ansi_strip(sprintf("> %s", conditionMessage(m)))
74+
attr(current_code, "message") <<- .ansi_strip(sprintf("> %s", conditionMessage(m)))
8075
invokeRestart("muffleMessage")
8176
}
8277
)
8378

8479
if (!is.null(x)) {
8580
return(x)
8681
}
87-
}
88-
8982

90-
object@warnings <- c(object@warnings, current_warnings)
91-
object@messages <- c(object@messages, current_messages)
83+
attr(current_code, "id") <- sample.int(.Machine$integer.max, size = 1)
84+
object@code <- new_object_code
85+
}
9286

9387
lockEnvironment(object@env, bindings = TRUE)
9488
object

R/qenv-extract.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@
3636
}
3737

3838
limited_code <- get_code(x, names = names)
39-
indexes <- which(x@code %in% limited_code)
40-
4139
x@env <- list2env(mget(x = names, envir = get_env(x)))
4240
x@code <- limited_code
43-
x@id <- x@id[indexes]
44-
x@warnings <- x@warnings[indexes]
45-
x@messages <- x@messages[indexes]
4641

4742
x
4843
}

R/qenv-get_code.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ setMethod("get_code", signature = "qenv", function(object, deparse = TRUE, names
118118
}
119119

120120
code <- if (!is.null(names)) {
121+
# todo:
121122
get_code_dependency(object@code, names, ...)
122123
} else {
123124
object@code
124125
}
125126

126127
if (deparse) {
127-
code
128+
unlist(code)
128129
} else {
129-
parse(text = paste(c("{", code, "}"), collapse = "\n"), keep.source = TRUE)
130+
parse(text = paste(c("{", unlist(code), "}"), collapse = "\n"), keep.source = TRUE)
130131
}
131132
})
132133

R/utils-get_code_dependency.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#'
2727
#' @keywords internal
2828
get_code_dependency <- function(code, names, check_names = TRUE) {
29-
checkmate::assert_character(code)
29+
checkmate::assert_list(code, "character")
3030
checkmate::assert_character(names, any.missing = FALSE)
3131

32-
if (identical(code, character(0)) || identical(trimws(code), "")) {
32+
if (length(code) == 0) {
3333
return(code)
3434
}
3535

tests/testthat/test-qenv_get_code.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ testthat::test_that("comments fall into proper calls", {
460460
# finishing comment
461461
"
462462

463-
q <- qenv() |> eval_code(code)
463+
q <- eval_code(qenv(), code)
464464
testthat::expect_identical(
465465
get_code(q),
466466
c(

0 commit comments

Comments
 (0)