git.delta.rocks / jrsonnet / refs/commits / 03f24e72fbec

difftreelog

source

crates/jsonnet-evaluator/src/ctx.rs2.9 KiBsourcehistory
1use crate::{2	future_wrapper, lazy_binding, lazy_val, rc_fn_helper, LazyBinding, LazyVal, ObjValue, Result,3	Val,4};5use closure::closure;6use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};78rc_fn_helper!(9	ContextCreator,10	context_creator,11	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>12);1314future_wrapper!(Context, FutureContext);1516#[derive(Debug)]17struct ContextInternals {18	dollar: Option<ObjValue>,19	this: Option<ObjValue>,20	super_obj: Option<ObjValue>,21	bindings: Rc<HashMap<String, LazyVal>>,22}23pub struct Context(Rc<ContextInternals>);24impl Debug for Context {25	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {26		f.debug_struct("Context")27			.field("this", &self.0.this.clone().map(|e| Rc::as_ptr(&e.0)))28			.finish()29	}30}31impl Context {32	pub fn new_future() -> FutureContext {33		FutureContext(Rc::new(RefCell::new(None)))34	}3536	pub fn dollar(&self) -> &Option<ObjValue> {37		&self.0.dollar38	}3940	pub fn this(&self) -> &Option<ObjValue> {41		&self.0.this42	}4344	pub fn super_obj(&self) -> &Option<ObjValue> {45		&self.0.super_obj46	}4748	pub fn new() -> Context {49		Context(Rc::new(ContextInternals {50			dollar: None,51			this: None,52			super_obj: None,53			bindings: Rc::new(HashMap::new()),54		}))55	}5657	pub fn binding(&self, name: &str) -> LazyVal {58		self.0.bindings.get(name).cloned().unwrap_or_else(|| {59			panic!("can't find {} in {:?}", name, self);60		})61	}62	pub fn into_future(self, ctx: FutureContext) -> Context {63		{64			ctx.0.borrow_mut().replace(self);65		}66		ctx.unwrap()67	}6869	pub fn with_var(&self, name: String, value: Val) -> Result<Context> {70		let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();71		new_bindings.insert(72			name,73			lazy_binding!(74				closure!(clone value, |_t, _s|Ok(lazy_val!(closure!(clone value, ||Ok(value.clone())))))75			),76		);77		self.extend(new_bindings, None, None, None)78	}7980	pub fn extend(81		&self,82		new_bindings: HashMap<String, LazyBinding>,83		new_dollar: Option<ObjValue>,84		new_this: Option<ObjValue>,85		new_super_obj: Option<ObjValue>,86	) -> Result<Context> {87		let dollar = new_dollar.or_else(|| self.0.dollar.clone());88		let this = new_this.or_else(|| self.0.this.clone());89		let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());90		let bindings = if new_bindings.is_empty() {91			self.0.bindings.clone()92		} else {93			let mut new = HashMap::new(); // = self.0.bindings.clone();94			for (k, v) in self.0.bindings.iter() {95				new.insert(k.clone(), v.clone());96			}97			for (k, v) in new_bindings.into_iter() {98				new.insert(k, v.0(this.clone(), super_obj.clone())?);99			}100			Rc::new(new)101		};102		Ok(Context(Rc::new(ContextInternals {103			dollar,104			this,105			super_obj,106			bindings,107		})))108	}109}110111impl Default for Context {112	fn default() -> Self {113		Self::new()114	}115}116117impl PartialEq for Context {118	fn eq(&self, other: &Self) -> bool {119		Rc::ptr_eq(&self.0, &other.0)120	}121}122123impl Clone for Context {124	fn clone(&self) -> Self {125		Context(self.0.clone())126	}127}