git.delta.rocks / jrsonnet / refs/commits / 80f37a416bf7

difftreelog

source

crates/jrsonnet-evaluator/src/val.rs17.3 KiBsourcehistory
1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{LocExpr, ParamsDesc};6use jrsonnet_types::ValType;78use crate::{9	builtin::manifest::{10		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,11	},12	cc_ptr_eq,13	error::{Error::*, LocError},14	evaluate,15	function::{16		parse_default_function_call, parse_function_call, ArgsLike, Builtin, CallLocation,17		StaticBuiltin,18	},19	gc::TraceBox,20	throw, Context, ObjValue, Result,21};2223pub trait LazyValValue: Trace {24	fn get(self: Box<Self>) -> Result<Val>;25}2627#[derive(Trace)]28enum LazyValInternals {29	Computed(Val),30	Errored(LocError),31	Waiting(TraceBox<dyn LazyValValue>),32	Pending,33}3435#[derive(Clone, Trace)]36pub struct LazyVal(Cc<RefCell<LazyValInternals>>);37impl LazyVal {38	pub fn new(f: TraceBox<dyn LazyValValue>) -> Self {39		Self(Cc::new(RefCell::new(LazyValInternals::Waiting(f))))40	}41	pub fn new_resolved(val: Val) -> Self {42		Self(Cc::new(RefCell::new(LazyValInternals::Computed(val))))43	}44	pub fn force(&self) -> Result<()> {45		self.evaluate()?;46		Ok(())47	}48	pub fn evaluate(&self) -> Result<Val> {49		match &*self.0.borrow() {50			LazyValInternals::Computed(v) => return Ok(v.clone()),51			LazyValInternals::Errored(e) => return Err(e.clone()),52			LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),53			_ => (),54		};55		let value = if let LazyValInternals::Waiting(value) =56			std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)57		{58			value59		} else {60			unreachable!()61		};62		let new_value = match value.0.get() {63			Ok(v) => v,64			Err(e) => {65				*self.0.borrow_mut() = LazyValInternals::Errored(e.clone());66				return Err(e);67			}68		};69		*self.0.borrow_mut() = LazyValInternals::Computed(new_value.clone());70		Ok(new_value)71	}72}7374impl Debug for LazyVal {75	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {76		write!(f, "Lazy")77	}78}79impl PartialEq for LazyVal {80	fn eq(&self, other: &Self) -> bool {81		cc_ptr_eq(&self.0, &other.0)82	}83}8485#[derive(Debug, PartialEq, Trace)]86pub struct FuncDesc {87	pub name: IStr,88	pub ctx: Context,89	pub params: ParamsDesc,90	pub body: LocExpr,91}92impl FuncDesc {93	/// Create body context, but fill arguments without defaults with lazy error94	pub fn default_body_context(&self) -> Context {95		parse_default_function_call(self.ctx.clone(), &self.params)96	}9798	/// Create context, with which body code will run99	pub fn call_body_context(100		&self,101		call_ctx: Context,102		args: &dyn ArgsLike,103		tailstrict: bool,104	) -> Result<Context> {105		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)106	}107}108109#[derive(Trace, Clone)]110pub enum FuncVal {111	/// Plain function implemented in jsonnet112	Normal(Cc<FuncDesc>),113	/// Standard library function114	StaticBuiltin(#[skip_trace] &'static dyn StaticBuiltin),115	/// User-provided function116	Builtin(Cc<TraceBox<dyn Builtin>>),117}118119impl Debug for FuncVal {120	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {121		match self {122			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),123			Self::StaticBuiltin(arg0) => {124				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()125			}126			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),127		}128	}129}130131impl FuncVal {132	pub fn args_len(&self) -> usize {133		match self {134			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),135			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),136			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),137		}138	}139	pub fn name(&self) -> IStr {140		match self {141			Self::Normal(normal) => normal.name.clone(),142			Self::StaticBuiltin(builtin) => builtin.name().into(),143			Self::Builtin(builtin) => builtin.name().into(),144		}145	}146	pub fn evaluate(147		&self,148		call_ctx: Context,149		loc: CallLocation,150		args: &dyn ArgsLike,151		tailstrict: bool,152	) -> Result<Val> {153		match self {154			Self::Normal(func) => {155				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;156				evaluate(body_ctx, &func.body)157			}158			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),159			Self::Builtin(b) => b.call(call_ctx, loc, args),160		}161	}162	pub fn evaluate_simple(&self, args: &dyn ArgsLike) -> Result<Val> {163		self.evaluate(Context::default(), CallLocation::native(), args, true)164	}165}166167#[derive(Clone)]168pub enum ManifestFormat {169	YamlStream(Box<ManifestFormat>),170	Yaml(usize),171	Json(usize),172	ToString,173	String,174}175176#[derive(Debug, Clone, Trace)]177pub struct Slice {178	pub(crate) inner: ArrValue,179	pub(crate) from: u32,180	pub(crate) to: u32,181	pub(crate) step: u32,182}183impl Slice {184	fn from(&self) -> usize {185		self.from as usize186	}187	fn to(&self) -> usize {188		self.to as usize189	}190	fn step(&self) -> usize {191		self.step as usize192	}193	fn len(&self) -> usize {194		// TODO: use div_ceil195		let diff = self.to() - self.from();196		let rem = diff % self.step();197		let div = diff / self.step();198199		if rem != 0 {200			div + 1201		} else {202			div203		}204	}205}206207#[derive(Debug, Clone, Trace)]208#[force_tracking]209pub enum ArrValue {210	Bytes(#[skip_trace] Rc<[u8]>),211	Lazy(Cc<Vec<LazyVal>>),212	Eager(Cc<Vec<Val>>),213	Extended(Box<(Self, Self)>),214	Range(i32, i32),215	Slice(Box<Slice>),216	Reversed(Box<Self>),217}218impl ArrValue {219	pub fn new_eager() -> Self {220		Self::Eager(Cc::new(Vec::new()))221	}222	pub fn new_range(a: i32, b: i32) -> Self {223		assert!(a <= b);224		Self::Range(a, b)225	}226227	pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {228		let len = self.len();229		let from = from.unwrap_or(0);230		let to = to.unwrap_or(len).min(len);231		let step = step.unwrap_or(1);232		assert!(from < to);233		assert!(step > 0);234235		Self::Slice(Box::new(Slice {236			inner: self,237			from: from as u32,238			to: to as u32,239			step: step as u32,240		}))241	}242243	pub fn len(&self) -> usize {244		match self {245			Self::Bytes(i) => i.len(),246			Self::Lazy(l) => l.len(),247			Self::Eager(e) => e.len(),248			Self::Extended(v) => v.0.len() + v.1.len(),249			Self::Range(a, b) => a.abs_diff(*b) as usize,250			Self::Reversed(i) => i.len(),251			Self::Slice(s) => s.len(),252		}253	}254255	pub fn is_empty(&self) -> bool {256		self.len() == 0257	}258259	pub fn get(&self, index: usize) -> Result<Option<Val>> {260		match self {261			Self::Bytes(i) => i262				.get(index)263				.map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),264			Self::Lazy(vec) => {265				if let Some(v) = vec.get(index) {266					Ok(Some(v.evaluate()?))267				} else {268					Ok(None)269				}270			}271			Self::Eager(vec) => Ok(vec.get(index).cloned()),272			Self::Extended(v) => {273				let a_len = v.0.len();274				if a_len > index {275					v.0.get(index)276				} else {277					v.1.get(index - a_len)278				}279			}280			Self::Range(a, _) => {281				if index >= self.len() {282					return Ok(None);283				}284				Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))285			}286			Self::Reversed(v) => {287				let len = v.len();288				if index >= len {289					return Ok(None);290				}291				v.get(len - index - 1)292			}293			Self::Slice(s) => {294				let index = s.from() + index * s.step();295				if index >= s.to() {296					return Ok(None);297				}298				s.inner.get(index as usize)299			}300		}301	}302303	pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {304		match self {305			Self::Bytes(i) => i306				.get(index)307				.map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),308			Self::Lazy(vec) => vec.get(index).cloned(),309			Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),310			Self::Extended(v) => {311				let a_len = v.0.len();312				if a_len > index {313					v.0.get_lazy(index)314				} else {315					v.1.get_lazy(index - a_len)316				}317			}318			Self::Range(a, _) => {319				if index >= self.len() {320					return None;321				}322				Some(LazyVal::new_resolved(Val::Num(323					((*a as isize) + index as isize) as f64,324				)))325			}326			Self::Reversed(v) => {327				let len = v.len();328				if index >= len {329					return None;330				}331				v.get_lazy(len - index - 1)332			}333			Self::Slice(s) => {334				let index = s.from() + index * s.step();335				if index >= s.to() {336					return None;337				}338				s.inner.get_lazy(index as usize)339			}340		}341	}342343	pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {344		Ok(match self {345			Self::Bytes(i) => {346				let mut out = Vec::with_capacity(i.len());347				for v in i.iter() {348					out.push(Val::Num(*v as f64));349				}350				Cc::new(out)351			}352			Self::Lazy(vec) => {353				let mut out = Vec::with_capacity(vec.len());354				for item in vec.iter() {355					out.push(item.evaluate()?);356				}357				Cc::new(out)358			}359			Self::Eager(vec) => vec.clone(),360			Self::Extended(_v) => {361				let mut out = Vec::with_capacity(self.len());362				for item in self.iter() {363					out.push(item?);364				}365				Cc::new(out)366			}367			Self::Range(a, b) => {368				let mut out = Vec::with_capacity(self.len());369				for i in *a..*b {370					out.push(Val::Num(i as f64));371				}372				Cc::new(out)373			}374			Self::Reversed(r) => {375				let mut r = r.evaluated()?;376				Cc::update_with(&mut r, |v| v.reverse());377				r378			}379			Self::Slice(v) => {380				let mut out = Vec::with_capacity(v.inner.len());381				for v in v382					.inner383					.iter_lazy()384					.skip(v.from())385					.take(v.to() - v.from())386					.step_by(v.step())387				{388					out.push(v.evaluate()?)389				}390				Cc::new(out)391			}392		})393	}394395	pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {396		(0..self.len()).map(move |idx| match self {397			Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),398			Self::Lazy(l) => l[idx].evaluate(),399			Self::Eager(e) => Ok(e[idx].clone()),400			Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),401			Self::Range(..) => self.get(idx).map(|e| e.unwrap()),402			Self::Reversed(..) => self.get(idx).map(|e| e.unwrap()),403			Self::Slice(..) => self.get(idx).map(|e| e.unwrap()),404		})405	}406407	pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {408		(0..self.len()).map(move |idx| match self {409			Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),410			Self::Lazy(l) => l[idx].clone(),411			Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),412			Self::Extended(_) => self.get_lazy(idx).unwrap(),413			Self::Range(..) => self.get_lazy(idx).unwrap(),414			Self::Reversed(..) => self.get_lazy(idx).unwrap(),415			Self::Slice(..) => self.get_lazy(idx).unwrap(),416		})417	}418419	pub fn reversed(self) -> Self {420		Self::Reversed(Box::new(self))421	}422423	pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {424		let mut out = Vec::with_capacity(self.len());425426		for value in self.iter() {427			out.push(mapper(value?)?);428		}429430		Ok(Self::Eager(Cc::new(out)))431	}432433	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {434		let mut out = Vec::with_capacity(self.len());435436		for value in self.iter() {437			let value = value?;438			if filter(&value)? {439				out.push(value);440			}441		}442443		Ok(Self::Eager(Cc::new(out)))444	}445446	pub fn ptr_eq(a: &Self, b: &Self) -> bool {447		match (a, b) {448			(Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),449			(Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),450			_ => false,451		}452	}453}454455impl From<Vec<LazyVal>> for ArrValue {456	fn from(v: Vec<LazyVal>) -> Self {457		Self::Lazy(Cc::new(v))458	}459}460461impl From<Vec<Val>> for ArrValue {462	fn from(v: Vec<Val>) -> Self {463		Self::Eager(Cc::new(v))464	}465}466467pub enum IndexableVal {468	Str(IStr),469	Arr(ArrValue),470}471472#[derive(Debug, Clone, Trace)]473pub enum Val {474	Bool(bool),475	Null,476	Str(IStr),477	Num(f64),478	Arr(ArrValue),479	Obj(ObjValue),480	Func(FuncVal),481}482483impl Val {484	pub const fn as_bool(&self) -> Option<bool> {485		match self {486			Val::Bool(v) => Some(*v),487			_ => None,488		}489	}490	pub const fn as_null(&self) -> Option<()> {491		match self {492			Val::Null => Some(()),493			_ => None,494		}495	}496	pub fn as_str(&self) -> Option<IStr> {497		match self {498			Val::Str(s) => Some(s.clone()),499			_ => None,500		}501	}502	pub const fn as_num(&self) -> Option<f64> {503		match self {504			Val::Num(n) => Some(*n),505			_ => None,506		}507	}508	pub fn as_arr(&self) -> Option<ArrValue> {509		match self {510			Val::Arr(a) => Some(a.clone()),511			_ => None,512		}513	}514	pub fn as_obj(&self) -> Option<ObjValue> {515		match self {516			Val::Obj(o) => Some(o.clone()),517			_ => None,518		}519	}520	pub fn as_func(&self) -> Option<FuncVal> {521		match self {522			Val::Func(f) => Some(f.clone()),523			_ => None,524		}525	}526527	/// Creates `Val::Num` after checking for numeric overflow.528	/// As numbers are `f64`, we can just check for their finity.529	pub fn new_checked_num(num: f64) -> Result<Self> {530		if num.is_finite() {531			Ok(Self::Num(num))532		} else {533			throw!(RuntimeError("overflow".into()))534		}535	}536537	pub const fn value_type(&self) -> ValType {538		match self {539			Self::Str(..) => ValType::Str,540			Self::Num(..) => ValType::Num,541			Self::Arr(..) => ValType::Arr,542			Self::Obj(..) => ValType::Obj,543			Self::Bool(_) => ValType::Bool,544			Self::Null => ValType::Null,545			Self::Func(..) => ValType::Func,546		}547	}548549	pub fn to_string(&self) -> Result<IStr> {550		Ok(match self {551			Self::Bool(true) => "true".into(),552			Self::Bool(false) => "false".into(),553			Self::Null => "null".into(),554			Self::Str(s) => s.clone(),555			v => manifest_json_ex(556				v,557				&ManifestJsonOptions {558					padding: "",559					mtype: ManifestType::ToString,560					newline: "\n",561					key_val_sep: ": ",562				},563			)?564			.into(),565		})566	}567568	/// Expects value to be object, outputs (key, manifested value) pairs569	pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {570		let obj = match self {571			Self::Obj(obj) => obj,572			_ => throw!(MultiManifestOutputIsNotAObject),573		};574		let keys = obj.fields();575		let mut out = Vec::with_capacity(keys.len());576		for key in keys {577			let value = obj578				.get(key.clone())?579				.expect("item in object")580				.manifest(ty)?;581			out.push((key, value));582		}583		Ok(out)584	}585586	/// Expects value to be array, outputs manifested values587	pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {588		let arr = match self {589			Self::Arr(a) => a,590			_ => throw!(StreamManifestOutputIsNotAArray),591		};592		let mut out = Vec::with_capacity(arr.len());593		for i in arr.iter() {594			out.push(i?.manifest(ty)?);595		}596		Ok(out)597	}598599	pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {600		Ok(match ty {601			ManifestFormat::YamlStream(format) => {602				let arr = match self {603					Self::Arr(a) => a,604					_ => throw!(StreamManifestOutputIsNotAArray),605				};606				let mut out = String::new();607608				match format as &ManifestFormat {609					ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),610					ManifestFormat::String => throw!(StreamManifestCannotNestString),611					_ => {}612				};613614				if !arr.is_empty() {615					for v in arr.iter() {616						out.push_str("---\n");617						out.push_str(&v?.manifest(format)?);618						out.push('\n');619					}620					out.push_str("...");621				}622623				out.into()624			}625			ManifestFormat::Yaml(padding) => self.to_yaml(*padding)?,626			ManifestFormat::Json(padding) => self.to_json(*padding)?,627			ManifestFormat::ToString => self.to_string()?,628			ManifestFormat::String => match self {629				Self::Str(s) => s.clone(),630				_ => throw!(StringManifestOutputIsNotAString),631			},632		})633	}634635	/// For manifestification636	pub fn to_json(&self, padding: usize) -> Result<IStr> {637		manifest_json_ex(638			self,639			&ManifestJsonOptions {640				padding: &" ".repeat(padding),641				mtype: if padding == 0 {642					ManifestType::Minify643				} else {644					ManifestType::Manifest645				},646				newline: "\n",647				key_val_sep: ": ",648			},649		)650		.map(|s| s.into())651	}652653	/// Calls `std.manifestJson`654	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {655		manifest_json_ex(656			self,657			&ManifestJsonOptions {658				padding: &" ".repeat(padding),659				mtype: ManifestType::Std,660				newline: "\n",661				key_val_sep: ": ",662			},663		)664		.map(|s| s.into())665	}666667	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {668		let padding = &" ".repeat(padding);669		manifest_yaml_ex(670			self,671			&ManifestYamlOptions {672				padding,673				arr_element_padding: padding,674				quote_keys: false,675			},676		)677		.map(|s| s.into())678	}679	pub fn into_indexable(self) -> Result<IndexableVal> {680		Ok(match self {681			Val::Str(s) => IndexableVal::Str(s),682			Val::Arr(arr) => IndexableVal::Arr(arr),683			_ => throw!(ValueIsNotIndexable(self.value_type())),684		})685	}686}687688const fn is_function_like(val: &Val) -> bool {689	matches!(val, Val::Func(_))690}691692/// Native implementation of `std.primitiveEquals`693pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {694	Ok(match (val_a, val_b) {695		(Val::Bool(a), Val::Bool(b)) => a == b,696		(Val::Null, Val::Null) => true,697		(Val::Str(a), Val::Str(b)) => a == b,698		(Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,699		(Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(700			"primitiveEquals operates on primitive types, got array".into(),701		)),702		(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(703			"primitiveEquals operates on primitive types, got object".into(),704		)),705		(a, b) if is_function_like(a) && is_function_like(b) => {706			throw!(RuntimeError("cannot test equality of functions".into()))707		}708		(_, _) => false,709	})710}711712/// Native implementation of `std.equals`713pub fn equals(val_a: &Val, val_b: &Val) -> Result<bool> {714	if val_a.value_type() != val_b.value_type() {715		return Ok(false);716	}717	match (val_a, val_b) {718		(Val::Arr(a), Val::Arr(b)) => {719			if ArrValue::ptr_eq(a, b) {720				return Ok(true);721			}722			if a.len() != b.len() {723				return Ok(false);724			}725			for (a, b) in a.iter().zip(b.iter()) {726				if !equals(&a?, &b?)? {727					return Ok(false);728				}729			}730			Ok(true)731		}732		(Val::Obj(a), Val::Obj(b)) => {733			if ObjValue::ptr_eq(a, b) {734				return Ok(true);735			}736			let fields = a.fields();737			if fields != b.fields() {738				return Ok(false);739			}740			for field in fields {741				if !equals(&a.get(field.clone())?.unwrap(), &b.get(field)?.unwrap())? {742					return Ok(false);743				}744			}745			Ok(true)746		}747		(a, b) => Ok(primitive_equals(a, b)?),748	}749}