git.delta.rocks / jrsonnet / refs/commits / 31da3dd5cc47

difftreelog

source

crates/jsonnet-evaluator/src/ctx.rs2.8 KiBsourcehistory
1use crate::{2	future_wrapper, rc_fn_helper, resolved_lazy_val, LazyBinding, LazyVal, ObjValue, Result, Val,3};4use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};56rc_fn_helper!(7	ContextCreator,8	context_creator,9	dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Context>10);1112future_wrapper!(Context, FutureContext);1314#[derive(Debug)]15struct ContextInternals {16	dollar: Option<ObjValue>,17	this: Option<ObjValue>,18	super_obj: Option<ObjValue>,19	bindings: Rc<HashMap<String, LazyVal>>,20}21pub struct Context(Rc<ContextInternals>);22impl Debug for Context {23	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {24		f.debug_struct("Context")25			.field("this", &self.0.this.clone().map(|e| Rc::as_ptr(&e.0)))26			.finish()27	}28}29impl Context {30	pub fn new_future() -> FutureContext {31		FutureContext(Rc::new(RefCell::new(None)))32	}3334	pub fn dollar(&self) -> &Option<ObjValue> {35		&self.0.dollar36	}3738	pub fn this(&self) -> &Option<ObjValue> {39		&self.0.this40	}4142	pub fn super_obj(&self) -> &Option<ObjValue> {43		&self.0.super_obj44	}4546	pub fn new() -> Context {47		Context(Rc::new(ContextInternals {48			dollar: None,49			this: None,50			super_obj: None,51			bindings: Rc::new(HashMap::new()),52		}))53	}5455	pub fn binding(&self, name: &str) -> LazyVal {56		self.0.bindings.get(name).cloned().unwrap_or_else(|| {57			panic!("can't find {} in {:?}", name, self);58		})59	}60	pub fn into_future(self, ctx: FutureContext) -> Context {61		{62			ctx.0.borrow_mut().replace(self);63		}64		ctx.unwrap()65	}6667	pub fn with_var(&self, name: String, value: Val) -> Result<Context> {68		let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();69		new_bindings.insert(name, LazyBinding::Bound(resolved_lazy_val!(value.clone())));70		self.extend(new_bindings, None, None, None)71	}7273	pub fn extend(74		&self,75		new_bindings: HashMap<String, LazyBinding>,76		new_dollar: Option<ObjValue>,77		new_this: Option<ObjValue>,78		new_super_obj: Option<ObjValue>,79	) -> Result<Context> {80		let dollar = new_dollar.or_else(|| self.0.dollar.clone());81		let this = new_this.or_else(|| self.0.this.clone());82		let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());83		let bindings = if new_bindings.is_empty() {84			self.0.bindings.clone()85		} else {86			let mut new = HashMap::new(); // = self.0.bindings.clone();87			for (k, v) in self.0.bindings.iter() {88				new.insert(k.clone(), v.clone());89			}90			for (k, v) in new_bindings.into_iter() {91				new.insert(k, v.evaluate(this.clone(), super_obj.clone())?);92			}93			Rc::new(new)94		};95		Ok(Context(Rc::new(ContextInternals {96			dollar,97			this,98			super_obj,99			bindings,100		})))101	}102}103104impl Default for Context {105	fn default() -> Self {106		Self::new()107	}108}109110impl PartialEq for Context {111	fn eq(&self, other: &Self) -> bool {112		Rc::ptr_eq(&self.0, &other.0)113	}114}115116impl Clone for Context {117	fn clone(&self) -> Self {118		Context(self.0.clone())119	}120}