git.delta.rocks / jrsonnet / refs/commits / 773438d8aef3

difftreelog

source

crates/jrsonnet-evaluator/src/function/mod.rs6.8 KiBsourcehistory
1use std::fmt::Debug;23pub use arglike::{ArgLike, ArgsLike, TlaArg};4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6pub use jrsonnet_macros::builtin;7use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};89use self::{10	arglike::OptionalContext,11	builtin::{Builtin, BuiltinParam, ParamName, StaticBuiltin},12	native::NativeDesc,13	parse::{parse_default_function_call, parse_function_call},14};15use crate::{16	evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result,17	Val,18};1920pub mod arglike;21pub mod builtin;22pub mod native;23pub mod parse;2425/// Function callsite location.26/// Either from other jsonnet code, specified by expression location, or from native (without location).27#[derive(Clone, Copy)]28pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);29impl<'l> CallLocation<'l> {30	/// Construct new location for calls coming from specified jsonnet expression location.31	pub const fn new(loc: &'l ExprLocation) -> Self {32		Self(Some(loc))33	}34}35impl CallLocation<'static> {36	/// Construct new location for calls coming from native code.37	pub const fn native() -> Self {38		Self(None)39	}40}4142/// Represents Jsonnet function defined in code.43#[derive(Debug, PartialEq, Trace)]44pub struct FuncDesc {45	/// # Example46	///47	/// In expressions like this, deducted to `a`, unspecified otherwise.48	/// ```jsonnet49	/// local a = function() ...50	/// local a() ...51	/// { a: function() ... }52	/// { a() = ... }53	/// ```54	pub name: IStr,55	/// Context, in which this function was evaluated.56	///57	/// # Example58	/// In59	/// ```jsonnet60	/// local a = 2;61	/// function() ...62	/// ```63	/// context will contain `a`.64	pub ctx: Context,6566	/// Function parameter definition67	pub params: ParamsDesc,68	/// Function body69	pub body: LocExpr,70}71impl FuncDesc {72	/// Create body context, but fill arguments without defaults with lazy error73	pub fn default_body_context(&self) -> Result<Context> {74		parse_default_function_call(self.ctx.clone(), &self.params)75	}7677	/// Create context, with which body code will run78	pub fn call_body_context(79		&self,80		call_ctx: Context,81		args: &dyn ArgsLike,82		tailstrict: bool,83	) -> Result<Context> {84		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)85	}8687	pub fn evaluate_trivial(&self) -> Option<Val> {88		evaluate_trivial(&self.body)89	}90}9192/// Represents a Jsonnet function value, including plain functions and user-provided builtins.93#[allow(clippy::module_name_repetitions)]94#[derive(Trace, Clone)]95pub enum FuncVal {96	/// Identity function, kept this way for comparsions.97	Id,98	/// Plain function implemented in jsonnet.99	Normal(Cc<FuncDesc>),100	/// Standard library function.101	StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),102	/// User-provided function.103	Builtin(Cc<TraceBox<dyn Builtin>>),104}105106impl Debug for FuncVal {107	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {108		match self {109			Self::Id => f.debug_tuple("Id").finish(),110			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),111			Self::StaticBuiltin(arg0) => {112				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()113			}114			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),115		}116	}117}118119#[allow(clippy::unnecessary_wraps)]120#[builtin]121const fn builtin_id(x: Val) -> Val {122	x123}124static ID: &builtin_id = &builtin_id {};125126impl FuncVal {127	pub fn builtin(builtin: impl Builtin) -> Self {128		Self::Builtin(Cc::new(tb!(builtin)))129	}130	pub fn static_builtin(static_builtin: &'static dyn StaticBuiltin) -> Self {131		Self::StaticBuiltin(static_builtin)132	}133134	pub fn params(&self) -> Vec<BuiltinParam> {135		match self {136			Self::Id => ID.params().to_vec(),137			Self::StaticBuiltin(i) => i.params().to_vec(),138			Self::Builtin(i) => i.params().to_vec(),139			Self::Normal(p) => p140				.params141				.iter()142				.map(|p| {143					BuiltinParam::new(144						p.0.name()145							.as_ref()146							.map(IStr::to_string)147							.map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),148						p.1.is_some(),149					)150				})151				.collect(),152		}153	}154	/// Amount of non-default required arguments155	pub fn params_len(&self) -> usize {156		match self {157			Self::Id => 1,158			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),159			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),160			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),161		}162	}163	/// Function name, as defined in code.164	pub fn name(&self) -> IStr {165		match self {166			Self::Id => "id".into(),167			Self::Normal(normal) => normal.name.clone(),168			Self::StaticBuiltin(builtin) => builtin.name().into(),169			Self::Builtin(builtin) => builtin.name().into(),170		}171	}172	/// Call function using arguments evaluated in specified `call_ctx` [`Context`].173	///174	/// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.175	pub fn evaluate(176		&self,177		call_ctx: Context,178		loc: CallLocation<'_>,179		args: &dyn ArgsLike,180		tailstrict: bool,181	) -> Result<Val> {182		match self {183			Self::Id => ID.call(call_ctx, loc, args),184			Self::Normal(func) => {185				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;186				evaluate(body_ctx, &func.body)187			}188			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),189			Self::Builtin(b) => b.call(call_ctx, loc, args),190		}191	}192	pub fn evaluate_simple<A: ArgsLike + OptionalContext>(193		&self,194		args: &A,195		tailstrict: bool,196	) -> Result<Val> {197		self.evaluate(198			ContextBuilder::dangerous_empty_state().build(),199			CallLocation::native(),200			args,201			tailstrict,202		)203	}204	/// Convert jsonnet function to plain `Fn` value.205	pub fn into_native<D: NativeDesc>(self) -> D::Value {206		D::into_native(self)207	}208209	/// Is this function an indentity function.210	///211	/// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.212	///213	/// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too214	pub fn is_identity(&self) -> bool {215		match self {216			Self::Id => true,217			Self::Normal(desc) => {218				if desc.params.len() != 1 {219					return false;220				}221				let param = &desc.params[0];222				if param.1.is_some() {223					return false;224				}225				#[allow(clippy::infallible_destructuring_match)]226				let id = match &param.0 {227					Destruct::Full(id) => id,228					#[cfg(feature = "exp-destruct")]229					_ => return false,230				};231				&desc.body.0 as &Expr == &Expr::Var(id.clone())232			}233			_ => false,234		}235	}236	/// Identity function value.237	pub const fn identity() -> Self {238		Self::Id239	}240241	pub fn evaluate_trivial(&self) -> Option<Val> {242		match self {243			FuncVal::Normal(n) => n.evaluate_trivial(),244			_ => None,245		}246	}247}248249impl<T> From<T> for FuncVal250where251	T: Builtin,252{253	fn from(value: T) -> Self {254		Self::builtin(value)255	}256}257impl From<&'static dyn StaticBuiltin> for FuncVal {258	fn from(value: &'static dyn StaticBuiltin) -> Self {259		Self::static_builtin(value)260	}261}