1use std::cell::RefCell;
12use std::sync::Arc;
13
14use crate::Value;
15use crate::value::FnHandle;
16
17pub const FUNCTION_PREFIX: &str = "function.";
20
21#[derive(Debug, Clone, Copy)]
23pub enum This<'a> {
24 Value(&'a Value),
27 Guarded(&'a Value, &'a [(&'a str, &'a str)]),
30 Opaque(&'static str),
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum Settle {
38 Await,
40 AsIs,
42}
43
44pub trait Caller: Send + Sync {
46 fn call(
50 &self,
51 f: FnHandle,
52 this: This<'_>,
53 args: &[Value],
54 settle: Settle,
55 ) -> Result<Value, String>;
56}
57
58thread_local! {
59 static CALLER: RefCell<Option<Arc<dyn Caller>>> = const { RefCell::new(None) };
60 static UNIT: RefCell<Option<(u64, Arc<str>)>> = const { RefCell::new(None) };
61}
62
63thread_local! {
64 static OWN_PAGE: RefCell<Option<(crate::Value, Option<f64>)>> = const { RefCell::new(None) };
65}
66
67pub fn own_page() -> Option<(crate::Value, Option<f64>)> {
73 OWN_PAGE.with(|p| p.borrow().clone())
74}
75
76pub struct OwnPageGuard(Option<(crate::Value, Option<f64>)>);
79
80impl OwnPageGuard {
81 pub fn enter(data: &indexmap::IndexMap<String, crate::Value>) -> OwnPageGuard {
82 let own = match data.get("page") {
83 Some(page @ crate::Value::Object(_)) => {
84 let number = match data.get("pagination") {
85 Some(crate::Value::Object(p)) => match p.get("pageNumber") {
86 Some(crate::Value::Number(n)) => Some(*n),
87 _ => None,
88 },
89 _ => None,
90 };
91 Some((page.clone(), number))
92 }
93 _ => None,
94 };
95 OwnPageGuard(OWN_PAGE.with(|p| std::mem::replace(&mut *p.borrow_mut(), own)))
96 }
97}
98
99impl Drop for OwnPageGuard {
100 fn drop(&mut self) {
101 let before = self.0.take();
102 OWN_PAGE.with(|p| *p.borrow_mut() = before);
103 }
104}
105
106static NEXT_UNIT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
107
108pub fn unit() -> Option<(u64, Arc<str>)> {
112 UNIT.with(|u| u.borrow().clone())
113}
114
115pub struct UnitGuard(Option<(u64, Arc<str>)>);
118
119impl UnitGuard {
120 pub fn enter(label: impl Into<Arc<str>>) -> UnitGuard {
121 let id = NEXT_UNIT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
122 let before = UNIT.with(|u| u.borrow_mut().replace((id, label.into())));
123 UnitGuard(before)
124 }
125}
126
127impl Drop for UnitGuard {
128 fn drop(&mut self) {
129 let before = self.0.take();
130 UNIT.with(|u| *u.borrow_mut() = before);
131 }
132}
133
134pub fn set(caller: Option<Arc<dyn Caller>>) {
136 let before = CALLER.with(|c| std::mem::replace(&mut *c.borrow_mut(), caller));
137 drop(before);
138}
139
140pub fn installed() -> Option<Arc<dyn Caller>> {
142 CALLER.with(|c| c.borrow().clone())
143}
144
145pub fn call(f: FnHandle, this: This<'_>, args: &[Value], settle: Settle) -> Value {
148 let Some(caller) = installed() else {
149 crate::traps::record(format!(
150 "{FUNCTION_PREFIX}this page calls a function from JavaScript data where Brrrty can't call it yet"
151 ));
152 return Value::Undefined;
153 };
154 match caller.call(f, this, args, settle) {
155 Ok(v) => v,
156 Err(why) => {
157 crate::traps::record(format!("{FUNCTION_PREFIX}{why}"));
158 Value::Undefined
159 }
160 }
161}
162
163const FILTERS_FOR_FUNCTIONS: &[&str] = &["dump", "json", "default", "d"];
166
167pub fn check_filter_args<'a>(name: &str, values: impl IntoIterator<Item = &'a Value>) {
171 if FILTERS_FOR_FUNCTIONS.contains(&name) {
172 return;
173 }
174 if values.into_iter().any(|v| matches!(v, Value::Function(_))) {
175 crate::traps::record(format!(
176 "{FUNCTION_PREFIX}this page passes a function from JavaScript data to the `{name}` filter, which Brrrty can't follow"
177 ));
178 }
179}
180
181pub fn used_as_value(how: &str) {
184 crate::traps::record(format!(
185 "{FUNCTION_PREFIX}this page {how} a function from JavaScript data as a value, which 11ty shows as JavaScript's own text for it"
186 ));
187}