R/wrapping.R

Defines functions wrap_environment wrap_closure

wrap_closure <- function(auditor, term, ..stash.closure) {
  # Not sure why these needs to be here, but sure...
  term
  ..stash.closure
  function(...) {
    auditor[[term]] <- stable_digest(..stash.closure)
    ..stash.closure(...)
  }
}

#' @importFrom rlang env empty_env
wrap_environment <- function(envir) {
  accessed <- env(empty_env())
  wrapping_env <- env(envir)
  wrapping_env$..stash_accessed <- accessed
  for(term in ls(envir)) {
    env_object <- envir[[term]]
    # if it's a value, digest the value
    # if it's a function, wrap and digest the function...
    if (typeof(env_object) == "closure") {
      wrapping_env[[term]] <- wrap_closure(accessed, term, env_object)
    }
  }
  wrapping_env
}
MrMallIronmaker/stash documentation built on Feb. 5, 2022, 8:12 a.m.