Skip to content

Commit

Permalink
meta_call_current()
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Oct 10, 2024
1 parent 2e0ab81 commit 57e82ec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions R/aaa-meta.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Overwritten in meta.R
meta_call_start <- function(...) {}
meta_call_end <- function(...) {}
meta_ext_register <- function(...) {}
meta_rel_register <- function(...) {}
meta_rel_register_df <- function(...) {}
Expand Down
23 changes: 22 additions & 1 deletion R/meta.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
call_stack <- collections::stack()
pre_code_cache <- collections::queue()
code_cache <- collections::queue()
ext_cache <- collections::dict()
macro_cache <- collections::dict()
df_cache <- collections::dict()
rel_cache <- collections::dict()

meta_call_start <- function(name) {
call_stack$push(name)
}

meta_call_end <- function(name) {
call_stack$pop()
}

meta_call_current <- function() {
if (call_stack$size() == 0) {
return(NULL)
}
call_stack$peek()
}

meta_clear <- function() {
pre_code_cache$clear()
code_cache$clear()
Expand Down Expand Up @@ -213,8 +229,13 @@ meta_rel_register <- function(rel, rel_expr) {
count <- rel_cache$size()
name <- sym(paste0("rel", count + 1))

# https://github.com/cynkra/constructive/issues/102
current_call <- meta_call_current()
if (!is.null(current_call)) {
# FIXME: This is probably too convoluted
meta_record(constructive::deparse_call(expr(!!current_call)))
}

# https://github.com/cynkra/constructive/issues/102
meta_record(constructive::deparse_call(expr(!!name <- !!rel_expr)))

obj <- list(rel = rel, name = name, df = df)
Expand Down
5 changes: 5 additions & 0 deletions R/relational.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
rel_try <- function(rel, ..., call = NULL) {
call_name <- as.character(sys.call(-1)[[1]])

if (!is.null(call$name)) {
meta_call_start(call$name)
withr::defer(meta_call_end(call$name))
}

# Avoid error when called via dplyr:::filter.data.frame() (in yamlet)
if (length(call_name) == 1 && !(call_name %in% stats$calls)) {
stats$calls <- c(stats$calls, call_name)
Expand Down

0 comments on commit 57e82ec

Please sign in to comment.