git.delta.rocks / jrsonnet / refs/commits / c41391dc9871

difftreelog

refactor fancier builtin param type

Yaroslav Bolyukin2023-08-06parent: #494be65.patch.diff
in: master

5 files changed

modifiedbindings/jsonnet/src/native.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,5 +1,4 @@
 use std::{
-	borrow::Cow,
 	ffi::{c_void, CStr},
 	os::raw::{c_char, c_int},
 };
@@ -82,7 +81,7 @@
 		let param = CStr::from_ptr(*raw_params)
 			.to_str()
 			.expect("param name is not utf-8");
-		params.push(Cow::Owned(param.into()));
+		params.push(param.into());
 		raw_params = raw_params.offset(1);
 	}
 
modifiedcrates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -1,18 +1,56 @@
 use std::{any::Any, borrow::Cow};
 
 use jrsonnet_gcmodule::Trace;
+use jrsonnet_interner::IStr;
 
 use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
 use crate::{error::Result, gc::TraceBox, tb, Context, Val};
 
-pub type BuiltinParamName = Cow<'static, str>;
+/// Can't have str | IStr, because constant BuiltinParam causes
+/// E0492: constant functions cannot refer to interior mutable data
+#[derive(Clone, Trace)]
+pub struct ParamName(Option<Cow<'static, str>>);
+impl ParamName {
+	pub const ANONYMOUS: Self = Self(None);
+	pub const fn new_static(name: &'static str) -> Self {
+		Self(Some(Cow::Borrowed(name)))
+	}
+	pub fn new_dynamic(name: String) -> Self {
+		Self(Some(Cow::Owned(name)))
+	}
+	pub fn as_str(&self) -> Option<&str> {
+		self.0.as_deref()
+	}
+	pub fn is_anonymous(&self) -> bool {
+		self.0.is_none()
+	}
+}
+impl PartialEq<IStr> for ParamName {
+	fn eq(&self, other: &IStr) -> bool {
+		match &self.0 {
+			Some(s) => s.as_bytes() == other.as_bytes(),
+			None => false,
+		}
+	}
+}
 
 #[derive(Clone, Trace)]
 pub struct BuiltinParam {
+	name: ParamName,
+	has_default: bool,
+}
+impl BuiltinParam {
+	pub const fn new(name: ParamName, has_default: bool) -> Self {
+		Self { name, has_default }
+	}
 	/// Parameter name for named call parsing
-	pub name: Option<BuiltinParamName>,
+	pub fn name(&self) -> &ParamName {
+		&self.name
+	}
 	/// Is implementation allowed to return empty value
-	pub has_default: bool,
+	pub fn has_default(&self) -> bool {
+		self.has_default
+	}
 }
 
 /// Description of function defined by native code
@@ -44,12 +82,12 @@
 }
 impl NativeCallback {
 	#[deprecated = "prefer using builtins directly, use this interface only for bindings"]
-	pub fn new(params: Vec<Cow<'static, str>>, handler: impl NativeCallbackHandler) -> Self {
+	pub fn new(params: Vec<String>, handler: impl NativeCallbackHandler) -> Self {
 		Self {
 			params: params
 				.into_iter()
 				.map(|n| BuiltinParam {
-					name: Some(n),
+					name: ParamName::new_dynamic(n.to_string()),
 					has_default: false,
 				})
 				.collect(),
modifiedcrates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/function/mod.rs
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, StaticBuiltin},12	native::NativeDesc,13	parse::{parse_default_function_call, parse_function_call},14};15use crate::{evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result, Val};1617pub mod arglike;18pub mod builtin;19pub mod native;20pub mod parse;2122/// Function callsite location.23/// Either from other jsonnet code, specified by expression location, or from native (without location).24#[derive(Clone, Copy)]25pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);26impl<'l> CallLocation<'l> {27	/// Construct new location for calls coming from specified jsonnet expression location.28	pub const fn new(loc: &'l ExprLocation) -> Self {29		Self(Some(loc))30	}31}32impl CallLocation<'static> {33	/// Construct new location for calls coming from native code.34	pub const fn native() -> Self {35		Self(None)36	}37}3839/// Represents Jsonnet function defined in code.40#[derive(Debug, PartialEq, Trace)]41pub struct FuncDesc {42	/// # Example43	///44	/// In expressions like this, deducted to `a`, unspecified otherwise.45	/// ```jsonnet46	/// local a = function() ...47	/// local a() ...48	/// { a: function() ... }49	/// { a() = ... }50	/// ```51	pub name: IStr,52	/// Context, in which this function was evaluated.53	///54	/// # Example55	/// In56	/// ```jsonnet57	/// local a = 2;58	/// function() ...59	/// ```60	/// context will contain `a`.61	pub ctx: Context,6263	/// Function parameter definition64	pub params: ParamsDesc,65	/// Function body66	pub body: LocExpr,67}68impl FuncDesc {69	/// Create body context, but fill arguments without defaults with lazy error70	pub fn default_body_context(&self) -> Result<Context> {71		parse_default_function_call(self.ctx.clone(), &self.params)72	}7374	/// Create context, with which body code will run75	pub fn call_body_context(76		&self,77		call_ctx: Context,78		args: &dyn ArgsLike,79		tailstrict: bool,80	) -> Result<Context> {81		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)82	}8384	pub fn evaluate_trivial(&self) -> Option<Val> {85		evaluate_trivial(&self.body)86	}87}8889/// Represents a Jsonnet function value, including plain functions and user-provided builtins.90#[allow(clippy::module_name_repetitions)]91#[derive(Trace, Clone)]92pub enum FuncVal {93	/// Identity function, kept this way for comparsions.94	Id,95	/// Plain function implemented in jsonnet.96	Normal(Cc<FuncDesc>),97	/// Standard library function.98	StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),99	/// User-provided function.100	Builtin(Cc<TraceBox<dyn Builtin>>),101}102103impl Debug for FuncVal {104	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {105		match self {106			Self::Id => f.debug_tuple("Id").finish(),107			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),108			Self::StaticBuiltin(arg0) => {109				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()110			}111			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),112		}113	}114}115116impl FuncVal {117	pub fn builtin(builtin: impl Builtin) -> Self {118		Self::Builtin(Cc::new(tb!(builtin)))119	}120	/// Amount of non-default required arguments121	pub fn params_len(&self) -> usize {122		match self {123			Self::Id => 1,124			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),125			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),126			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),127		}128	}129	/// Function name, as defined in code.130	pub fn name(&self) -> IStr {131		match self {132			Self::Id => "id".into(),133			Self::Normal(normal) => normal.name.clone(),134			Self::StaticBuiltin(builtin) => builtin.name().into(),135			Self::Builtin(builtin) => builtin.name().into(),136		}137	}138	/// Call function using arguments evaluated in specified `call_ctx` [`Context`].139	///140	/// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.141	pub fn evaluate(142		&self,143		call_ctx: Context,144		loc: CallLocation<'_>,145		args: &dyn ArgsLike,146		tailstrict: bool,147	) -> Result<Val> {148		match self {149			Self::Id => {150				#[allow(clippy::unnecessary_wraps)]151				#[builtin]152				const fn builtin_id(x: Val) -> Val {153					x154				}155				static ID: &builtin_id = &builtin_id {};156157				ID.call(call_ctx, loc, args)158			}159			Self::Normal(func) => {160				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;161				evaluate(body_ctx, &func.body)162			}163			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),164			Self::Builtin(b) => b.call(call_ctx, loc, args),165		}166	}167	pub fn evaluate_simple<A: ArgsLike + OptionalContext>(168		&self,169		args: &A,170		tailstrict: bool,171	) -> Result<Val> {172		self.evaluate(173			ContextBuilder::dangerous_empty_state().build(),174			CallLocation::native(),175			args,176			tailstrict,177		)178	}179	/// Convert jsonnet function to plain `Fn` value.180	pub fn into_native<D: NativeDesc>(self) -> D::Value {181		D::into_native(self)182	}183184	/// Is this function an indentity function.185	///186	/// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.187	///188	/// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too189	pub fn is_identity(&self) -> bool {190		match self {191			Self::Id => true,192			Self::Normal(desc) => {193				if desc.params.len() != 1 {194					return false;195				}196				let param = &desc.params[0];197				if param.1.is_some() {198					return false;199				}200				#[allow(clippy::infallible_destructuring_match)]201				let id = match &param.0 {202					Destruct::Full(id) => id,203					#[cfg(feature = "exp-destruct")]204					_ => return false,205				};206				&desc.body.0 as &Expr == &Expr::Var(id.clone())207			}208			_ => false,209		}210	}211	/// Identity function value.212	pub const fn identity() -> Self {213		Self::Id214	}215216	pub fn evaluate_trivial(&self) -> Option<Val> {217		match self {218			FuncVal::Normal(n) => n.evaluate_trivial(),219			_ => None,220		}221	}222}
after · crates/jrsonnet-evaluator/src/function/mod.rs
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::{evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result, Val};1617pub mod arglike;18pub mod builtin;19pub mod native;20pub mod parse;2122/// Function callsite location.23/// Either from other jsonnet code, specified by expression location, or from native (without location).24#[derive(Clone, Copy)]25pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);26impl<'l> CallLocation<'l> {27	/// Construct new location for calls coming from specified jsonnet expression location.28	pub const fn new(loc: &'l ExprLocation) -> Self {29		Self(Some(loc))30	}31}32impl CallLocation<'static> {33	/// Construct new location for calls coming from native code.34	pub const fn native() -> Self {35		Self(None)36	}37}3839/// Represents Jsonnet function defined in code.40#[derive(Debug, PartialEq, Trace)]41pub struct FuncDesc {42	/// # Example43	///44	/// In expressions like this, deducted to `a`, unspecified otherwise.45	/// ```jsonnet46	/// local a = function() ...47	/// local a() ...48	/// { a: function() ... }49	/// { a() = ... }50	/// ```51	pub name: IStr,52	/// Context, in which this function was evaluated.53	///54	/// # Example55	/// In56	/// ```jsonnet57	/// local a = 2;58	/// function() ...59	/// ```60	/// context will contain `a`.61	pub ctx: Context,6263	/// Function parameter definition64	pub params: ParamsDesc,65	/// Function body66	pub body: LocExpr,67}68impl FuncDesc {69	/// Create body context, but fill arguments without defaults with lazy error70	pub fn default_body_context(&self) -> Result<Context> {71		parse_default_function_call(self.ctx.clone(), &self.params)72	}7374	/// Create context, with which body code will run75	pub fn call_body_context(76		&self,77		call_ctx: Context,78		args: &dyn ArgsLike,79		tailstrict: bool,80	) -> Result<Context> {81		parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)82	}8384	pub fn evaluate_trivial(&self) -> Option<Val> {85		evaluate_trivial(&self.body)86	}87}8889/// Represents a Jsonnet function value, including plain functions and user-provided builtins.90#[allow(clippy::module_name_repetitions)]91#[derive(Trace, Clone)]92pub enum FuncVal {93	/// Identity function, kept this way for comparsions.94	Id,95	/// Plain function implemented in jsonnet.96	Normal(Cc<FuncDesc>),97	/// Standard library function.98	StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),99	/// User-provided function.100	Builtin(Cc<TraceBox<dyn Builtin>>),101}102103impl Debug for FuncVal {104	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {105		match self {106			Self::Id => f.debug_tuple("Id").finish(),107			Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),108			Self::StaticBuiltin(arg0) => {109				f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()110			}111			Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),112		}113	}114}115116#[allow(clippy::unnecessary_wraps)]117#[builtin]118const fn builtin_id(x: Val) -> Val {119	x120}121static ID: &builtin_id = &builtin_id {};122123impl FuncVal {124	pub fn builtin(builtin: impl Builtin) -> Self {125		Self::Builtin(Cc::new(tb!(builtin)))126	}127128	pub fn params(&self) -> Vec<BuiltinParam> {129		match self {130			Self::Id => ID.params().to_vec(),131			Self::StaticBuiltin(i) => i.params().to_vec(),132			Self::Builtin(i) => i.params().to_vec(),133			Self::Normal(p) => p134				.params135				.iter()136				.map(|p| {137					BuiltinParam::new(138						p.0.name()139							.as_ref()140							.map(IStr::to_string)141							.map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),142						p.1.is_some(),143					)144				})145				.collect(),146		}147	}148	/// Amount of non-default required arguments149	pub fn params_len(&self) -> usize {150		match self {151			Self::Id => 1,152			Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),153			Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),154			Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),155		}156	}157	/// Function name, as defined in code.158	pub fn name(&self) -> IStr {159		match self {160			Self::Id => "id".into(),161			Self::Normal(normal) => normal.name.clone(),162			Self::StaticBuiltin(builtin) => builtin.name().into(),163			Self::Builtin(builtin) => builtin.name().into(),164		}165	}166	/// Call function using arguments evaluated in specified `call_ctx` [`Context`].167	///168	/// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.169	pub fn evaluate(170		&self,171		call_ctx: Context,172		loc: CallLocation<'_>,173		args: &dyn ArgsLike,174		tailstrict: bool,175	) -> Result<Val> {176		match self {177			Self::Id => ID.call(call_ctx, loc, args),178			Self::Normal(func) => {179				let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;180				evaluate(body_ctx, &func.body)181			}182			Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),183			Self::Builtin(b) => b.call(call_ctx, loc, args),184		}185	}186	pub fn evaluate_simple<A: ArgsLike + OptionalContext>(187		&self,188		args: &A,189		tailstrict: bool,190	) -> Result<Val> {191		self.evaluate(192			ContextBuilder::dangerous_empty_state().build(),193			CallLocation::native(),194			args,195			tailstrict,196		)197	}198	/// Convert jsonnet function to plain `Fn` value.199	pub fn into_native<D: NativeDesc>(self) -> D::Value {200		D::into_native(self)201	}202203	/// Is this function an indentity function.204	///205	/// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.206	///207	/// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too208	pub fn is_identity(&self) -> bool {209		match self {210			Self::Id => true,211			Self::Normal(desc) => {212				if desc.params.len() != 1 {213					return false;214				}215				let param = &desc.params[0];216				if param.1.is_some() {217					return false;218				}219				#[allow(clippy::infallible_destructuring_match)]220				let id = match &param.0 {221					Destruct::Full(id) => id,222					#[cfg(feature = "exp-destruct")]223					_ => return false,224				};225				&desc.body.0 as &Expr == &Expr::Var(id.clone())226			}227			_ => false,228		}229	}230	/// Identity function value.231	pub const fn identity() -> Self {232		Self::Id233	}234235	pub fn evaluate_trivial(&self) -> Option<Val> {236		match self {237			FuncVal::Normal(n) => n.evaluate_trivial(),238			_ => None,239		}240	}241}
modifiedcrates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -163,7 +163,7 @@
 			params.len(),
 			params
 				.iter()
-				.map(|p| (p.name.as_ref().map(|v| v.as_ref().into()), p.has_default))
+				.map(|p| (p.name().as_str().map(IStr::from), p.has_default()))
 				.collect()
 		))
 	}
@@ -180,7 +180,7 @@
 		// FIXME: O(n) for arg existence check
 		let id = params
 			.iter()
-			.position(|p| p.name.as_ref().map_or(false, |v| v as &str == name as &str))
+			.position(|p| p.name() == name)
 			.ok_or_else(|| UnknownFunctionParameter((name as &str).to_owned()))?;
 		if replace(&mut passed_args[id], Some(arg)).is_some() {
 			throw!(BindingParameterASecondTime(name.clone()));
@@ -190,7 +190,7 @@
 	})?;
 
 	if filled_args < params.len() {
-		for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default) {
+		for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default()) {
 			if passed_args[id].is_some() {
 				continue;
 			}
@@ -202,20 +202,16 @@
 			for param in params.iter().skip(args.unnamed_len()) {
 				let mut found = false;
 				args.named_names(&mut |name| {
-					if param
-						.name
-						.as_ref()
-						.map_or(false, |v| v as &str == name as &str)
-					{
+					if param.name() == name {
 						found = true;
 					}
 				});
 				if !found {
 					throw!(FunctionParameterNotBoundInCall(
-						param.name.as_ref().map(|v| v.as_ref().into()),
+						param.name().as_str().map(IStr::from),
 						params
 							.iter()
-							.map(|p| (p.name.as_ref().map(|p| p.as_ref().into()), p.has_default))
+							.map(|p| (p.name().as_str().map(IStr::from), p.has_default()))
 							.collect()
 					));
 				}
modifiedtests/tests/common.rsdiffbeforeafterboth
--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -1,5 +1,3 @@
-use std::borrow::Cow;
-
 use jrsonnet_evaluator::{
 	error::Result,
 	function::{builtin, FuncVal},
@@ -66,22 +64,12 @@
 		FuncVal::StaticBuiltin(b) => b
 			.params()
 			.iter()
-			.map(|p| {
-				p.name
-					.as_ref()
-					.unwrap_or(&Cow::Borrowed("<unnamed>"))
-					.to_string()
-			})
+			.map(|p| p.name().as_str().unwrap_or(&"<unnamed>").to_string())
 			.collect(),
 		FuncVal::Builtin(b) => b
 			.params()
 			.iter()
-			.map(|p| {
-				p.name
-					.as_ref()
-					.unwrap_or(&Cow::Borrowed("<unnamed>"))
-					.to_string()
-			})
+			.map(|p| p.name().as_str().unwrap_or(&"<unnamed>").to_string())
 			.collect(),
 	}
 }