use std::cell::RefCell;
use std::sync::Arc;
use crate::abi::ir::context::MacroContextIR;
thread_local! {
static CURRENT_CONTEXT: RefCell<Option<Arc<MacroContextIR>>> = const { RefCell::new(None) };
}
pub fn install_context(ctx: MacroContextIR) {
install_shared_context(Arc::new(ctx));
}
pub fn install_shared_context(ctx: Arc<MacroContextIR>) {
CURRENT_CONTEXT.with(|slot| *slot.borrow_mut() = Some(ctx));
}
pub fn clear_context() {
CURRENT_CONTEXT.with(|slot| *slot.borrow_mut() = None);
}
pub fn with_context<R>(f: impl FnOnce(Option<&MacroContextIR>) -> R) -> R {
CURRENT_CONTEXT.with(|slot| f(slot.borrow().as_deref()))
}