Skip to main content

calcit/codegen/emit_js/
gen_stack.rs

1use std::sync::{LazyLock, Mutex};
2
3use crate::calcit::Calcit;
4use crate::call_stack::{CalcitStack, CallStackList, StackKind};
5
6static CALL_STACK: LazyLock<Mutex<rpds::ListSync<CalcitStack>>> = LazyLock::new(|| Mutex::new(rpds::List::new_sync()));
7
8pub fn push_call_stack(ns: &str, def: &str, kind: StackKind, code: Calcit, args: &[Calcit]) {
9  let mut stack = CALL_STACK.lock().expect("open call stack");
10  stack.push_front_mut(CalcitStack {
11    ns: ns.into(),
12    def: def.into(),
13    code,
14    args: args.to_owned(),
15    kind,
16  })
17}
18
19pub fn pop_call_stack() {
20  let mut stack = CALL_STACK.lock().expect("open call stack");
21  if !stack.is_empty() {
22    let xs = stack.drop_first();
23    match xs {
24      Some(v) => *stack = v,
25      None => {
26        eprintln!("empty stack, nothing to pop")
27      }
28    }
29  }
30}
31
32pub fn clear_stack() {
33  let mut stack = CALL_STACK.lock().expect("open call stack");
34  *stack = rpds::List::new_sync();
35}
36
37pub fn get_gen_stack() -> CallStackList {
38  let stack = CALL_STACK.lock().expect("read call stack");
39  CallStackList(stack.to_owned())
40}