3232# ' @export
3333setGeneric ("eval_code ", function(object, code, cache = FALSE, ...) standardGeneric("eval_code"))
3434
35- setMethod ("eval_code ", signature = c("qenv"), function(object, code, cache = FALSE, ...) {
35+ setMethod ("eval_code ", signature = c(object = "qenv", code = "ANY "), function(object, code, cache = FALSE, ...) {
3636 code <- .preprocess_code(code ) # preprocess code to ensure it is a character vector
37+ srcref <- attr(code , " wholeSrcref" )
38+ if (is.expression(code ) && length(srcref ) == 0L ) {
39+ result <- Reduce(function (u , v ) {
40+ if (inherits(v , " =" ) && identical(typeof(v ), " language" )) {
41+ # typeof(`=`) is language, but it doesn't dispatch on it, so we need to
42+ # explicitly pass it as first class of the object
43+ class(v ) <- unique(c(" language" , class(v )))
44+ }
45+ .eval_code(u , v , cache = FALSE , ... )
46+ }, init = object , x = code )
47+ return (result )
48+ } else if (is.expression(code )) {
49+ code <- paste(attr(code , " wholeSrcref" ), collapse = " \n " )
50+ }
51+ .eval_code(object = object , code = code , cache = cache , ... )
52+ })
53+
54+ setMethod ("eval_code ", signature = c("qenv.error", "ANY"), function(object, code, cache = FALSE, ...) object)
55+
56+ # ' @keywords internal
57+ .eval_code <- function (object , code , cache = FALSE , ... ) {
3758 parsed_code <- parse(text = code , keep.source = TRUE )
3859 object @ .xData <- rlang :: env_clone(object @ .xData , parent = parent.env(.GlobalEnv ))
3960 if (length(parsed_code ) == 0 ) {
@@ -66,7 +87,7 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
6687 errorCondition(
6788 message = sprintf(
6889 " %s \n when evaluating qenv code:\n %s" ,
69- . ansi_strip(conditionMessage(e )),
90+ cli :: ansi_strip(conditionMessage(e )),
7091 current_code
7192 ),
7293 class = c(" qenv.error" , " try-error" , " simpleError" ),
@@ -75,11 +96,11 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
7596 }
7697 ),
7798 warning = function (w ) {
78- attr(current_code , " warning" ) <<- . ansi_strip(sprintf(" > %s\n " , conditionMessage(w )))
99+ attr(current_code , " warning" ) <<- cli :: ansi_strip(sprintf(" > %s\n " , conditionMessage(w )))
79100 invokeRestart(" muffleWarning" )
80101 },
81102 message = function (m ) {
82- attr(current_code , " message" ) <<- . ansi_strip(sprintf(" > %s" , conditionMessage(m )))
103+ attr(current_code , " message" ) <<- cli :: ansi_strip(sprintf(" > %s" , conditionMessage(m )))
83104 invokeRestart(" muffleMessage" )
84105 }
85106 )
@@ -93,49 +114,10 @@ setMethod("eval_code", signature = c("qenv"), function(object, code, cache = FAL
93114
94115 lockEnvironment(object @ .xData , bindings = TRUE )
95116 object
96- })
97-
98-
99- setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code, cache = FALSE, ...) {
100- srcref <- attr(code , " wholeSrcref" )
101- if (length(srcref )) {
102- eval_code(object , code = paste(attr(code , " wholeSrcref" ), collapse = " \n " ))
103- } else {
104- Reduce(function (u , v ) {
105- if (inherits(v , " =" ) && identical(typeof(v ), " language" )) {
106- # typeof(`=`) is language, but it doesn't dispatch on it, so we need to
107- # explicitly pass it as first class of the object
108- class(v ) <- unique(c(" language" , class(v )))
109- }
110- eval_code(u , v )
111- }, init = object , x = code )
112- }
113- })
117+ }
114118
115119setGeneric (".preprocess_code ", function(code) standardGeneric(".preprocess_code"))
116-
117- setMethod (".preprocess_code ", signature = c("ANY"), function(code) {
118- as.character(code )
119- })
120-
120+ setMethod (".preprocess_code ", signature = c("ANY"), function(code) as.character(code))
121121setMethod (".preprocess_code ", signature = c("language"), function(code) {
122122 paste(vapply(lang2calls(code ), deparse1 , collapse = " \n " , character (1L )))
123123})
124-
125- setMethod ("eval_code ", signature = c("qenv.error", "ANY"), function(object, code, cache = FALSE, ...) {
126- object
127- })
128-
129- # if cli is installed rlang adds terminal printing characters
130- # which need to be removed
131- .ansi_strip <- function (chr ) {
132- if (requireNamespace(" cli" , quietly = TRUE )) {
133- cli :: ansi_strip(chr )
134- } else {
135- chr
136- }
137- }
138-
139- get_code_attr <- function (qenv , attr ) {
140- unlist(lapply(qenv @ code , function (x ) attr(x , attr )))
141- }
0 commit comments