2828setGeneric ("eval_code ", function(object, code) standardGeneric("eval_code"))
2929
3030setMethod ("eval_code ", signature = c("qenv", "character"), function(object, code) {
31- id <- sample.int(.Machine $ integer.max , size = 1 )
32-
33- object @ id <- c(object @ id , id )
31+ parsed_code <- parse(text = code , keep.source = TRUE )
3432 object @ .xData <- rlang :: env_clone(object @ .xData , parent = parent.env(.GlobalEnv ))
35- code <- paste(code , collapse = " \n " )
36- object @ code <- c(object @ code , code )
33+ if (length(parsed_code ) == 0 ) {
34+ # empty code, or just comments
35+ attr(code , " id" ) <- sample.int(.Machine $ integer.max , size = 1 )
36+ attr(code , " dependency" ) <- extract_dependency(parsed_code ) # in case comment contains @linksto tag
37+ object @ code <- c(object @ code , list (code ))
38+ return (object )
39+ }
40+ code_split <- split_code(paste(code , collapse = " \n " ))
3741
38- current_warnings <- " "
39- current_messages <- " "
42+ for (i in seq_along(code_split )) {
43+ current_code <- code_split [[i ]]
44+ current_call <- parse(text = current_code , keep.source = TRUE )
4045
41- parsed_code <- parse(text = code , keep.source = TRUE )
42- for (single_call in parsed_code ) {
4346 # Using withCallingHandlers to capture warnings and messages.
4447 # Using tryCatch to capture the error and abort further evaluation.
4548 x <- withCallingHandlers(
4649 tryCatch(
4750 {
48- eval(single_call , envir = object @ .xData )
51+ eval(current_call , envir = object @ .xData )
4952 if (! identical(parent.env(object @ .xData ), parent.env(.GlobalEnv ))) {
5053 # needed to make sure that @.xData is always a sibling of .GlobalEnv
5154 # could be changed when any new package is added to search path (through library or require call)
@@ -58,30 +61,31 @@ setMethod("eval_code", signature = c("qenv", "character"), function(object, code
5861 message = sprintf(
5962 " %s \n when evaluating qenv code:\n %s" ,
6063 .ansi_strip(conditionMessage(e )),
61- deparse1( single_call )
64+ current_code
6265 ),
6366 class = c(" qenv.error" , " try-error" , " simpleError" ),
64- trace = object @ code
67+ trace = unlist(c( object @ code , list ( current_code )))
6568 )
6669 }
6770 ),
6871 warning = function (w ) {
69- current_warnings <<- paste0( current_warnings , .ansi_strip(sprintf(" > %s\n " , conditionMessage(w ) )))
72+ attr( current_code , " warning " ) <<- .ansi_strip(sprintf(" > %s\n " , conditionMessage(w )))
7073 invokeRestart(" muffleWarning" )
7174 },
7275 message = function (m ) {
73- current_messages <<- paste0( current_messages , .ansi_strip(sprintf(" > %s" , conditionMessage(m ) )))
76+ attr( current_code , " message " ) <<- .ansi_strip(sprintf(" > %s" , conditionMessage(m )))
7477 invokeRestart(" muffleMessage" )
7578 }
7679 )
7780
7881 if (! is.null(x )) {
7982 return (x )
8083 }
81- }
8284
83- object @ warnings <- c(object @ warnings , current_warnings )
84- object @ messages <- c(object @ messages , current_messages )
85+ attr(current_code , " id" ) <- sample.int(.Machine $ integer.max , size = 1 )
86+ attr(current_code , " dependency" ) <- extract_dependency(current_call )
87+ object @ code <- c(object @ code , list (current_code ))
88+ }
8589
8690 lockEnvironment(object @ .xData , bindings = TRUE )
8791 object
@@ -92,7 +96,12 @@ setMethod("eval_code", signature = c("qenv", "language"), function(object, code)
9296})
9397
9498setMethod ("eval_code ", signature = c("qenv", "expression"), function(object, code) {
95- eval_code(object , code = paste(vapply(lang2calls(code ), deparse1 , collapse = " \n " , character (1L )), collapse = " \n " ))
99+ srcref <- attr(code , " wholeSrcref" )
100+ if (length(srcref )) {
101+ eval_code(object , code = paste(attr(code , " wholeSrcref" ), collapse = " \n " ))
102+ } else {
103+ eval_code(object , code = paste(lang2calls(code ), collapse = " \n " ))
104+ }
96105})
97106
98107setMethod ("eval_code ", signature = c("qenv.error", "ANY"), function(object, code) {
@@ -108,3 +117,7 @@ setMethod("eval_code", signature = c("qenv.error", "ANY"), function(object, code
108117 chr
109118 }
110119}
120+
121+ get_code_attr <- function (qenv , attr ) {
122+ unlist(lapply(qenv @ code , function (x ) attr(x , attr )))
123+ }
0 commit comments