Skip to content

Commit

Permalink
add 'trace_one_list' fn to trace header for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Jun 23, 2023
1 parent a37c26b commit d17d10b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: typetracer
Title: Trace Function Parameter Types
Version: 0.2.1.003
Version: 0.2.1.004
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265")),
Expand Down
39 changes: 38 additions & 1 deletion R/tracer-define.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ typetracer_header <- function () {
utils::getFromNamespace ("get_param_str", "typetracer")
typetracer_env$trace_one_param <-
utils::getFromNamespace ("trace_one_param", "typetracer")
typetracer_env$trace_one_list <-
utils::getFromNamespace ("trace_one_list", "typetracer")
typetracer_env$data <- lapply (typetracer_env$par_names, function (p) {
typetracer_env$trace_one_param (typetracer_env, p, fn_env)
dat_i <- typetracer_env$trace_one_param (typetracer_env, p, fn_env)
if (dat_i$typeof == "list") {
dat_i$list_data <-
typetracer_env$trace_one_list (typetracer_env, p, fn_env)
}
return (dat_i)
})

typetracer_env$process_back_trace <-
Expand Down Expand Up @@ -162,6 +169,36 @@ trace_one_param <- function (typetracer_env, p, fn_env) {
)
}

#' Recurse into one list-type parameter to extract internal structure.
#'
#' Standard evaluation only!
#' @noRd
trace_one_list <- function (typetracer_env, p, fn_env) {

res <- tryCatch (
get (p, envir = fn_env, inherits = FALSE),
error = function (e) NULL
)
if (is.null (res)) {
return (res)
}

list_str <- lapply (seq_along (res), function (i) {
list (
par = names (res) [i],
class = class (res [[i]]),
typeof = typeof (res [[i]]),
storage_mode = storage.mode (res [[i]]),
mode = mode (res [[i]]),
length = length (res [[i]]),
par_uneval = NA_character_,
par_eval = NA_character_
)
})

return (list_str)
}

#' Extract environments of function calls
#'
#' Note that rlang enumerates envs from "0" for the calling environment. For
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"codeRepository": "https://github.com/mpadge/typetracer",
"issueTracker": "https://github.com/mpadge/typetracer/issues",
"license": "https://spdx.org/licenses/MIT",
"version": "0.2.1.003",
"version": "0.2.1.004",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
10 changes: 9 additions & 1 deletion tests/testthat/_snaps/trace-fns.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@
"typetracer")
typetracer_env$trace_one_param <- utils::getFromNamespace("trace_one_param",
"typetracer")
typetracer_env$trace_one_list <- utils::getFromNamespace("trace_one_list",
"typetracer")
typetracer_env$data <- lapply(typetracer_env$par_names, function(p) {
typetracer_env$trace_one_param(typetracer_env, p, fn_env)
dat_i <- typetracer_env$trace_one_param(typetracer_env,
p, fn_env)
if (dat_i$typeof == "list") {
dat_i$list_data <- typetracer_env$trace_one_list(typetracer_env,
p, fn_env)
}
return(dat_i)
})
typetracer_env$process_back_trace <- utils::getFromNamespace("process_back_trace",
"typetracer")
Expand Down

0 comments on commit d17d10b

Please sign in to comment.