git.delta.rocks / jrsonnet / refs/commits / 2afd5ff0dd7a

difftreelog

refactor extended strings

Yaroslav Bolyukin2022-12-03parent: #81f0998.patch.diff
in: master

16 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -17,7 +17,7 @@
 	function::{CallLocation, FuncDesc, FuncVal},
 	tb, throw,
 	typed::Typed,
-	val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},
+	val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},
 	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
 	Unbound, Val,
 };
@@ -36,7 +36,7 @@
 		}
 	}
 	Some(match &*expr.0 {
-		Expr::Str(s) => Val::Str(s.clone()),
+		Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),
 		Expr::Num(n) => Val::Num(*n),
 		Expr::Literal(LiteralType::False) => Val::Bool(false),
 		Expr::Literal(LiteralType::True) => Val::Bool(true),
@@ -135,7 +135,7 @@
 					let fctx = Pending::new();
 					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
 					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![
-						Thunk::evaluated(Val::Str(field.clone())),
+						Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),
 						Thunk::new(tb!(ObjectFieldThunk {
 							field: field.clone(),
 							obj: obj.clone(),
@@ -436,7 +436,7 @@
 		Literal(LiteralType::False) => Val::Bool(false),
 		Literal(LiteralType::Null) => Val::Null,
 		Parened(e) => evaluate(ctx, e)?,
-		Str(v) => Val::Str(v.clone()),
+		Str(v) => Val::Str(StrValue::Flat(v.clone())),
 		Num(v) => Val::new_checked_num(*v)?,
 		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
 		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
@@ -457,14 +457,14 @@
 			ctx.super_obj()
 				.clone()
 				.expect("no super found")
-				.get_for(name, ctx.this().clone().expect("no this found"))?
+				.get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?
 				.expect("value not found")
 		}
 		Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
 			(Val::Obj(v), Val::Str(key)) => State::push(
 				CallLocation::new(loc),
 				|| format!("field <{key}> access"),
-				|| match v.get(key.clone()) {
+				|| match v.get(key.clone().into_flat()) {
 					Ok(Some(v)) => Ok(v),
 					#[cfg(not(feature = "friendly-errors"))]
 					Ok(None) => throw!(NoSuchField(key.clone(), vec![])),
@@ -476,7 +476,10 @@
 							#[cfg(feature = "exp-preserve-order")]
 							false,
 						) {
-							let conf = strsim::jaro_winkler(&field as &str, &key as &str);
+							let conf = strsim::jaro_winkler(
+								&field as &str,
+								&key.clone().into_flat() as &str,
+							);
 							if conf < 0.8 {
 								continue;
 							}
@@ -485,7 +488,7 @@
 						heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
 
 						throw!(NoSuchField(
-							key.clone(),
+							key.clone().into_flat(),
 							heap.into_iter().map(|(_, v)| v).collect()
 						))
 					}
@@ -505,7 +508,7 @@
 				v.get(n as usize)?
 					.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
 			}
-			(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
+			(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),
 			(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
 				ValType::Arr,
 				ValType::Num,
@@ -514,16 +517,18 @@
 
 			(Val::Str(s), Val::Num(n)) => Val::Str({
 				let v: IStr = s
+					.clone()
+					.into_flat()
 					.chars()
 					.skip(n as usize)
 					.take(1)
 					.collect::<String>()
 					.into();
 				if v.is_empty() {
-					let size = s.chars().count();
+					let size = s.into_flat().chars().count();
 					throw!(StringBoundsError(n as usize, size))
 				}
-				v
+				StrValue::Flat(v)
 			}),
 			(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
 				ValType::Str,
@@ -654,7 +659,7 @@
 					|| format!("import {:?}", path.clone()),
 					|| s.import_resolved(resolved_path),
 				)?,
-				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+				ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),
 				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),
 				_ => unreachable!(),
 			}
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -3,8 +3,14 @@
 use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
 
 use crate::{
-	arr::ArrValue, error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed,
-	val::equals, Context, Result, Val,
+	arr::ArrValue,
+	error::ErrorKind::*,
+	evaluate,
+	stdlib::std_format,
+	throw,
+	typed::Typed,
+	val::{equals, StrValue},
+	Context, Result, Val,
 };
 
 pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
@@ -25,15 +31,21 @@
 	Ok(match (a, b) {
 		(Str(a), Str(b)) if a.is_empty() => Val::Str(b.clone()),
 		(Str(a), Str(b)) if b.is_empty() => Val::Str(a.clone()),
-		(Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),
+		(Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
 
 		// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
-		(Num(a), Str(b)) => Str(format!("{a}{b}").into()),
-		(Str(a), Num(b)) => Str(format!("{a}{b}").into()),
+		(Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
+		(Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
 
-		(Str(a), o) | (o, Str(a)) if a.is_empty() => Val::Str(o.clone().to_string()?),
-		(Str(a), o) => Str(format!("{a}{}", o.clone().to_string()?).into()),
-		(o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),
+		(Str(a), o) | (o, Str(a)) if a.is_empty() => {
+			Val::Str(StrValue::Flat(o.clone().to_string()?))
+		}
+		(Str(a), o) => Str(StrValue::Flat(
+			format!("{a}{}", o.clone().to_string()?).into(),
+		)),
+		(o, Str(a)) => Str(StrValue::Flat(
+			format!("{}{a}", o.clone().to_string()?).into(),
+		)),
 
 		(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
 		(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
@@ -56,7 +68,9 @@
 			}
 			Ok(Num(a % b))
 		}
-		(Str(str), vals) => String::into_untyped(std_format(str.clone(), vals.clone())?),
+		(Str(str), vals) => {
+			String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)
+		}
 		(a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
 			BinaryOpType::Mod,
 			a.value_type(),
@@ -120,10 +134,10 @@
 		(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
 		(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
 
-		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
+		(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
 		(a, Mod, b) => evaluate_mod_op(a, b)?,
 
-		(Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
+		(Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),
 
 		// Bool X Bool
 		(Bool(a), And, Bool(b)) => Bool(*a && *b),
modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,13 @@
 use jrsonnet_parser::{ArgsDesc, LocExpr};
 
 use crate::{
-	error::Result, evaluate, gc::GcHashMap, tb, typed::Typed, val::ThunkValue, Context, Thunk, Val,
+	error::Result,
+	evaluate,
+	gc::GcHashMap,
+	tb,
+	typed::Typed,
+	val::{StrValue, ThunkValue},
+	Context, Thunk, Val,
 };
 
 /// Marker for arguments, which can be evaluated with context set to None
@@ -59,7 +65,7 @@
 impl ArgLike for TlaArg {
 	fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
 		match self {
-			TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(s.clone()))),
+			TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(StrValue::Flat(s.clone())))),
 			TlaArg::Code(code) => Ok(if tailstrict {
 				Thunk::evaluated(evaluate(ctx, code)?)
 			} else {
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -7,7 +7,7 @@
 	Deserialize, Serialize,
 };
 
-use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};
+use crate::{arr::ArrValue, error::Result, val::StrValue, ObjValueBuilder, State, Val};
 
 impl<'de> Deserialize<'de> for Val {
 	fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
@@ -49,7 +49,7 @@
 			where
 				E: serde::de::Error,
 			{
-				Ok(Val::Str(v.into()))
+				Ok(Val::Str(StrValue::Flat(v.into())))
 			}
 
 			// visit_num! {
@@ -152,7 +152,7 @@
 		match self {
 			Val::Bool(v) => serializer.serialize_bool(*v),
 			Val::Null => serializer.serialize_none(),
-			Val::Str(s) => serializer.serialize_str(s),
+			Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
 			Val::Num(n) => serializer.serialize_f64(*n),
 			Val::Arr(arr) => {
 				let mut seq = serializer.serialize_seq(Some(arr.len()))?;
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -147,7 +147,7 @@
 			}
 		}
 		Val::Null => buf.push_str("null"),
-		Val::Str(s) => escape_string_json_buf(s, buf),
+		Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
 		Val::Num(n) => write!(buf, "{n}").unwrap(),
 		Val::Arr(items) => {
 			buf.push('[');
@@ -256,7 +256,7 @@
 		let Val::Str(s) = val else {
 			throw!("output should be string for string manifest format, got {}", val.value_type())
 		};
-		out.write_str(&s).unwrap();
+		write!(out, "{s}").unwrap();
 		Ok(())
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -589,6 +589,7 @@
 					.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
 			),
 			Val::Str(s) => {
+				let s = s.into_flat();
 				if s.chars().count() != 1 {
 					throw!("%c expected 1 char string, got {}", s.chars().count(),);
 				}
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -2,13 +2,12 @@
 #![allow(clippy::unnecessary_wraps)]
 
 use format::{format_arr, format_obj};
-use jrsonnet_interner::IStr;
 
 use crate::{error::Result, function::CallLocation, State, Val};
 
 pub mod format;
 
-pub fn std_format(str: IStr, vals: Val) -> Result<String> {
+pub fn std_format(str: &str, vals: Val) -> Result<String> {
 	State::push(
 		CallLocation::native(),
 		|| format!("std.format of {str}"),
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -11,7 +11,7 @@
 	function::{native::NativeDesc, FuncDesc, FuncVal},
 	throw,
 	typed::CheckType,
-	val::IndexableVal,
+	val::{IndexableVal, StrValue},
 	ObjValue, ObjValueBuilder, Val,
 };
 
@@ -187,13 +187,13 @@
 	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
 
 	fn into_untyped(value: Self) -> Result<Val> {
-		Ok(Val::Str(value))
+		Ok(Val::Str(StrValue::Flat(value)))
 	}
 
 	fn from_untyped(value: Val) -> Result<Self> {
 		<Self as Typed>::TYPE.check(&value)?;
 		match value {
-			Val::Str(s) => Ok(s),
+			Val::Str(s) => Ok(s.into_flat()),
 			_ => unreachable!(),
 		}
 	}
@@ -203,7 +203,7 @@
 	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
 
 	fn into_untyped(value: Self) -> Result<Val> {
-		Ok(Val::Str(value.into()))
+		Ok(Val::Str(StrValue::Flat(value.into())))
 	}
 
 	fn from_untyped(value: Val) -> Result<Self> {
@@ -219,13 +219,13 @@
 	const TYPE: &'static ComplexValType = &ComplexValType::Char;
 
 	fn into_untyped(value: Self) -> Result<Val> {
-		Ok(Val::Str(value.to_string().into()))
+		Ok(Val::Str(StrValue::Flat(value.to_string().into())))
 	}
 
 	fn from_untyped(value: Val) -> Result<Self> {
 		<Self as Typed>::TYPE.check(&value)?;
 		match value {
-			Val::Str(s) => Ok(s.chars().next().unwrap()),
+			Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),
 			_ => unreachable!(),
 		}
 	}
@@ -480,7 +480,7 @@
 
 	fn into_untyped(value: Self) -> Result<Val> {
 		match value {
-			IndexableVal::Str(s) => Ok(Val::Str(s)),
+			IndexableVal::Str(s) => Ok(Val::Str(StrValue::Flat(s))),
 			IndexableVal::Arr(a) => Ok(Val::Arr(a)),
 		}
 	}
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -150,7 +150,7 @@
 			Self::Any => Ok(()),
 			Self::Simple(t) => t.check(value),
 			Self::Char => match value {
-				Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),
+				Val::Str(s) if s.len() == 1 || s.clone().into_flat().chars().count() == 1 => Ok(()),
 				v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
 			},
 			Self::BoundedNumber(from, to) => {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,4 +1,9 @@
-use std::{cell::RefCell, fmt::Debug, mem::replace};
+use std::{
+	cell::RefCell,
+	fmt::{self, Debug, Display},
+	mem::replace,
+	rc::Rc,
+};
 
 use jrsonnet_gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
@@ -117,7 +122,7 @@
 }
 
 impl<T: Debug + Trace> Debug for Thunk<T> {
-	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 		write!(f, "Lazy")
 	}
 }
@@ -187,6 +192,87 @@
 	}
 }
 
+#[derive(Debug, Clone, Trace)]
+pub enum StrValue {
+	Flat(IStr),
+	Tree(Rc<(StrValue, StrValue, usize)>),
+}
+impl StrValue {
+	pub fn concat(a: StrValue, b: StrValue) -> Self {
+		if a.is_empty() {
+			b
+		} else if b.is_empty() {
+			a
+		} else {
+			let len = a.len() + b.len();
+			Self::Tree(Rc::new((a, b, len)))
+		}
+	}
+	pub fn into_flat(self) -> IStr {
+		match self {
+			StrValue::Flat(f) => f,
+			StrValue::Tree(_) => {
+				let mut buf = String::new();
+				self.into_flat_buf(&mut buf);
+				buf.into()
+			}
+		}
+	}
+	fn into_flat_buf(&self, out: &mut String) {
+		match self {
+			StrValue::Flat(f) => out.push_str(f),
+			StrValue::Tree(t) => {
+				t.0.into_flat_buf(out);
+				t.1.into_flat_buf(out);
+			}
+		}
+	}
+	pub fn len(&self) -> usize {
+		match self {
+			StrValue::Flat(v) => v.len(),
+			StrValue::Tree(t) => t.2,
+		}
+	}
+	pub fn is_empty(&self) -> bool {
+		match self {
+			Self::Flat(v) => v.is_empty(),
+			_ => false,
+		}
+	}
+}
+impl Display for StrValue {
+	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+		match self {
+			StrValue::Flat(v) => write!(f, "{v}"),
+			StrValue::Tree(t) => {
+				write!(f, "{}", t.0)?;
+				write!(f, "{}", t.1)
+			}
+		}
+	}
+}
+impl PartialEq for StrValue {
+	fn eq(&self, other: &Self) -> bool {
+		let a = self.clone().into_flat();
+		let b = other.clone().into_flat();
+		a == b
+	}
+}
+impl Eq for StrValue {}
+impl PartialOrd for StrValue {
+	fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+		let a = self.clone().into_flat();
+		let b = other.clone().into_flat();
+		Some(a.cmp(&b))
+	}
+}
+impl Ord for StrValue {
+	fn cmp(&self, other: &Self) -> std::cmp::Ordering {
+		self.partial_cmp(other)
+			.expect("partial_cmp always returns Some")
+	}
+}
+
 /// Represents any valid Jsonnet value.
 #[derive(Debug, Clone, Trace)]
 pub enum Val {
@@ -195,7 +281,7 @@
 	/// Represents a Jsonnet null value.
 	Null,
 	/// Represents a Jsonnet string.
-	Str(IStr),
+	Str(StrValue),
 	/// Represents a Jsonnet number.
 	/// Should be finite, and not NaN
 	/// This restriction isn't enforced by enum, as enum field can't be marked as private
@@ -208,10 +294,12 @@
 	Func(FuncVal),
 }
 
+static_assertions::assert_eq_size!(Val, [u8; 24]);
+
 impl From<IndexableVal> for Val {
 	fn from(v: IndexableVal) -> Self {
 		match v {
-			IndexableVal::Str(s) => Self::Str(s),
+			IndexableVal::Str(s) => Self::Str(StrValue::Flat(s)),
 			IndexableVal::Arr(a) => Self::Arr(a),
 		}
 	}
@@ -232,7 +320,7 @@
 	}
 	pub fn as_str(&self) -> Option<IStr> {
 		match self {
-			Self::Str(s) => Some(s.clone()),
+			Self::Str(s) => Some(s.clone().into_flat()),
 			_ => None,
 		}
 	}
@@ -295,14 +383,14 @@
 			Self::Bool(true) => "true".into(),
 			Self::Bool(false) => "false".into(),
 			Self::Null => "null".into(),
-			Self::Str(s) => s.clone(),
+			Self::Str(s) => s.clone().into_flat(),
 			_ => self.manifest(ToStringFormat).map(IStr::from)?,
 		})
 	}
 
 	pub fn into_indexable(self) -> Result<IndexableVal> {
 		Ok(match self {
-			Val::Str(s) => IndexableVal::Str(s),
+			Val::Str(s) => IndexableVal::Str(s.into_flat()),
 			Val::Arr(arr) => IndexableVal::Arr(arr),
 			_ => throw!(ValueIsNotIndexable(self.value_type())),
 		})
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -37,12 +37,13 @@
 	func: NativeFn<((Either![String, Any],), Any)>,
 	arr: IndexableVal,
 ) -> Result<IndexableVal> {
+	use std::fmt::Write;
 	match arr {
 		IndexableVal::Str(str) => {
 			let mut out = String::new();
 			for c in str.chars() {
 				match func(Either2::A(c.to_string()))?.0 {
-					Val::Str(o) => out.push_str(&o),
+					Val::Str(o) => write!(out, "{o}").unwrap(),
 					Val::Null => continue,
 					_ => throw!("in std.join all items should be strings"),
 				};
@@ -101,6 +102,7 @@
 
 #[builtin]
 pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
+	use std::fmt::Write;
 	Ok(match sep {
 		IndexableVal::Arr(joiner_items) => {
 			let mut out = Vec::new();
@@ -141,7 +143,7 @@
 						out += &sep;
 					}
 					first = false;
-					out += &item;
+					write!(out, "{item}").unwrap()
 				} else if matches!(item, Val::Null) {
 					continue;
 				} else {
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/lib.rs
1use std::{2	cell::{Ref, RefCell, RefMut},3	collections::HashMap,4	rc::Rc,5};67use jrsonnet_evaluator::{8	error::{ErrorKind::*, Result},9	function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},10	gc::{GcHashMap, TraceBox},11	tb,12	trace::PathResolver,13	Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,14};15use jrsonnet_gcmodule::{Cc, Trace};16use jrsonnet_parser::Source;1718mod expr;19mod types;20pub use types::*;21mod arrays;22pub use arrays::*;23mod math;24pub use math::*;25mod operator;26pub use operator::*;27mod sort;28pub use sort::*;29mod hash;30pub use hash::*;31mod encoding;32pub use encoding::*;33mod objects;34pub use objects::*;35mod manifest;36pub use manifest::*;37mod parse;38pub use parse::*;39mod strings;40pub use strings::*;41mod misc;42pub use misc::*;4344pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {45	let mut builder = ObjValueBuilder::new();4647	let expr = expr::stdlib_expr();48	let eval = jrsonnet_evaluator::evaluate(ContextBuilder::dangerous_empty_state().build(), &expr)49		.expect("stdlib.jsonnet should have no errors")50		.as_obj()51		.expect("stdlib.jsonnet should evaluate to object");5253	builder.with_super(eval);5455	for (name, builtin) in [56		// Types57		("type", builtin_type::INST),58		("isString", builtin_is_string::INST),59		("isNumber", builtin_is_number::INST),60		("isBoolean", builtin_is_boolean::INST),61		("isObject", builtin_is_object::INST),62		("isArray", builtin_is_array::INST),63		("isFunction", builtin_is_function::INST),64		// Arrays65		("makeArray", builtin_make_array::INST),66		("slice", builtin_slice::INST),67		("map", builtin_map::INST),68		("flatMap", builtin_flatmap::INST),69		("filter", builtin_filter::INST),70		("foldl", builtin_foldl::INST),71		("foldr", builtin_foldr::INST),72		("range", builtin_range::INST),73		("join", builtin_join::INST),74		("reverse", builtin_reverse::INST),75		("any", builtin_any::INST),76		("all", builtin_all::INST),77		("member", builtin_member::INST),78		("count", builtin_count::INST),79		// Math80		("abs", builtin_abs::INST),81		("sign", builtin_sign::INST),82		("max", builtin_max::INST),83		("min", builtin_min::INST),84		("modulo", builtin_modulo::INST),85		("floor", builtin_floor::INST),86		("ceil", builtin_ceil::INST),87		("log", builtin_log::INST),88		("pow", builtin_pow::INST),89		("sqrt", builtin_sqrt::INST),90		("sin", builtin_sin::INST),91		("cos", builtin_cos::INST),92		("tan", builtin_tan::INST),93		("asin", builtin_asin::INST),94		("acos", builtin_acos::INST),95		("atan", builtin_atan::INST),96		("exp", builtin_exp::INST),97		("mantissa", builtin_mantissa::INST),98		("exponent", builtin_exponent::INST),99		// Operator100		("mod", builtin_mod::INST),101		("primitiveEquals", builtin_primitive_equals::INST),102		("equals", builtin_equals::INST),103		("format", builtin_format::INST),104		// Sort105		("sort", builtin_sort::INST),106		// Hash107		("md5", builtin_md5::INST),108		#[cfg(feature = "exp-more-hashes")]109		("sha256", builtin_sha256::INST),110		// Encoding111		("encodeUTF8", builtin_encode_utf8::INST),112		("decodeUTF8", builtin_decode_utf8::INST),113		("base64", builtin_base64::INST),114		("base64Decode", builtin_base64_decode::INST),115		("base64DecodeBytes", builtin_base64_decode_bytes::INST),116		// Objects117		("objectFieldsEx", builtin_object_fields_ex::INST),118		("objectHasEx", builtin_object_has_ex::INST),119		// Manifest120		("escapeStringJson", builtin_escape_string_json::INST),121		("manifestJsonEx", builtin_manifest_json_ex::INST),122		("manifestYamlDoc", builtin_manifest_yaml_doc::INST),123		// Parsing124		("parseJson", builtin_parse_json::INST),125		("parseYaml", builtin_parse_yaml::INST),126		// Strings127		("codepoint", builtin_codepoint::INST),128		("substr", builtin_substr::INST),129		("char", builtin_char::INST),130		("strReplace", builtin_str_replace::INST),131		("splitLimit", builtin_splitlimit::INST),132		("asciiUpper", builtin_ascii_upper::INST),133		("asciiLower", builtin_ascii_lower::INST),134		("findSubstr", builtin_find_substr::INST),135		("parseInt", builtin_parse_int::INST),136		("parseOctal", builtin_parse_octal::INST),137		("parseHex", builtin_parse_hex::INST),138		// Misc139		("length", builtin_length::INST),140		("startsWith", builtin_starts_with::INST),141		("endsWith", builtin_ends_with::INST),142	]143	.iter()144	.cloned()145	{146		builder147			.member(name.into())148			.hide()149			.value(Val::Func(FuncVal::StaticBuiltin(builtin)))150			.expect("no conflict");151	}152153	builder154		.member("extVar".into())155		.hide()156		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {157			settings: settings.clone()158		})))))159		.expect("no conflict");160	builder161		.member("native".into())162		.hide()163		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {164			settings: settings.clone()165		})))))166		.expect("no conflict");167	builder168		.member("trace".into())169		.hide()170		.value(Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace {171			settings172		})))))173		.expect("no conflict");174175	builder176		.member("id".into())177		.hide()178		.value(Val::Func(FuncVal::Id))179		.expect("no conflict");180181	builder.build()182}183184pub trait TracePrinter {185	fn print_trace(&self, loc: CallLocation, value: IStr);186}187188pub struct StdTracePrinter {189	resolver: PathResolver,190}191impl StdTracePrinter {192	pub fn new(resolver: PathResolver) -> Self {193		Self { resolver }194	}195}196impl TracePrinter for StdTracePrinter {197	fn print_trace(&self, loc: CallLocation, value: IStr) {198		eprint!("TRACE:");199		if let Some(loc) = loc.0 {200			let locs = loc.0.map_source_locations(&[loc.1]);201			eprint!(202				" {}:{}",203				match loc.0.source_path().path() {204					Some(p) => self.resolver.resolve(p),205					None => loc.0.source_path().to_string(),206				},207				locs[0].line208			);209		}210		eprintln!(" {}", value);211	}212}213214pub struct Settings {215	/// Used for `std.extVar`216	pub ext_vars: HashMap<IStr, TlaArg>,217	/// Used for `std.native`218	pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,219	/// Helper to add globals without implementing custom ContextInitializer220	pub globals: GcHashMap<IStr, Thunk<Val>>,221	/// Used for `std.trace`222	pub trace_printer: Box<dyn TracePrinter>,223	/// Used for `std.thisFile`224	pub path_resolver: PathResolver,225}226227fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {228	let source_name = format!("<extvar:{}>", name);229	Source::new_virtual(source_name.into(), code.into())230}231232#[derive(Trace)]233pub struct ContextInitializer {234	// When we don't need to support legacy-this-file, we can reuse same context for all files235	#[cfg(not(feature = "legacy-this-file"))]236	context: Context,237	// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it238	#[cfg(feature = "legacy-this-file")]239	stdlib_obj: ObjValue,240	settings: Rc<RefCell<Settings>>,241}242impl ContextInitializer {243	pub fn new(s: State, resolver: PathResolver) -> Self {244		let settings = Settings {245			ext_vars: Default::default(),246			ext_natives: Default::default(),247			globals: Default::default(),248			trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),249			path_resolver: resolver,250		};251		let settings = Rc::new(RefCell::new(settings));252		Self {253			#[cfg(not(feature = "legacy-this-file"))]254			context: {255				let mut context = ContextBuilder::with_capacity(s, 1);256				context.bind(257					"std".into(),258					Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),259				);260				context.build()261			},262			#[cfg(feature = "legacy-this-file")]263			stdlib_obj: stdlib_uncached(settings.clone()),264			settings,265		}266	}267	pub fn settings(&self) -> Ref<Settings> {268		self.settings.borrow()269	}270	pub fn settings_mut(&self) -> RefMut<Settings> {271		self.settings.borrow_mut()272	}273	pub fn add_ext_var(&self, name: IStr, value: Val) {274		self.settings_mut()275			.ext_vars276			.insert(name, TlaArg::Val(value));277	}278	pub fn add_ext_str(&self, name: IStr, value: IStr) {279		self.settings_mut()280			.ext_vars281			.insert(name, TlaArg::String(value));282	}283	pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {284		let code = code.into();285		let source = extvar_source(name, code.clone());286		let parsed = jrsonnet_parser::parse(287			&code,288			&jrsonnet_parser::ParserSettings {289				source: source.clone(),290			},291		)292		.map_err(|e| ImportSyntaxError {293			path: source,294			error: Box::new(e),295		})?;296		// self.data_mut().volatile_files.insert(source_name, code);297		self.settings_mut()298			.ext_vars299			.insert(name.into(), TlaArg::Code(parsed));300		Ok(())301	}302	pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {303		self.settings_mut().ext_natives.insert(name, cb);304	}305}306impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {307	#[cfg(not(feature = "legacy-this-file"))]308	fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {309		let out = self.context.clone();310		let globals = &self.settings().globals;311		if globals.is_empty() {312			return out;313		}314315		let mut out = ContextBuilder::extend(out);316		for (k, v) in globals.iter() {317			out.bind(k.clone(), v.clone());318		}319		out.build()320	}321	#[cfg(feature = "legacy-this-file")]322	fn initialize(&self, s: State, source: Source) -> Context {323		let mut builder = ObjValueBuilder::new();324		builder.with_super(self.stdlib_obj.clone());325		builder326			.member("thisFile".into())327			.hide()328			.value(Val::Str(match source.source_path().path() {329				Some(p) => self.settings().path_resolver.resolve(p).into(),330				None => source.source_path().to_string().into(),331			}))332			.expect("this object builder is empty");333		let stdlib_with_this_file = builder.build();334335		let mut context = ContextBuilder::with_capacity(s, 1);336		context.bind(337			"std".into(),338			Thunk::evaluated(Val::Obj(stdlib_with_this_file)),339		);340		for (k, v) in self.settings().globals.iter() {341			context.bind(k.clone(), v.clone());342		}343		context.build()344	}345	fn as_any(&self) -> &dyn std::any::Any {346		self347	}348}349350pub trait StateExt {351	/// This method was previously implemented in jrsonnet-evaluator itself352	fn with_stdlib(&self);353	fn add_global(&self, name: IStr, value: Thunk<Val>);354}355356impl StateExt for State {357	fn with_stdlib(&self) {358		let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());359		self.settings_mut().context_initializer = tb!(initializer)360	}361	fn add_global(&self, name: IStr, value: Thunk<Val>) {362		self.settings()363			.context_initializer364			.as_any()365			.downcast_ref::<ContextInitializer>()366			.expect("not standard context initializer")367			.settings_mut()368			.globals369			.insert(name, value);370	}371}
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -118,6 +118,7 @@
 		}
 		Val::Null => buf.push_str("null"),
 		Val::Str(s) => {
+			let s = s.clone().into_flat();
 			if s.is_empty() {
 				buf.push_str("\"\"");
 			} else if let Some(s) = s.strip_suffix('\n') {
@@ -128,10 +129,10 @@
 					buf.push_str(&options.padding);
 					buf.push_str(line);
 				}
-			} else if !options.quote_keys && !yaml_needs_quotes(s) {
-				buf.push_str(s);
+			} else if !options.quote_keys && !yaml_needs_quotes(&s) {
+				buf.push_str(&s);
 			} else {
-				escape_string_json_buf(s, buf);
+				escape_string_json_buf(&s, buf);
 			}
 		}
 		Val::Num(n) => write!(buf, "{}", *n).unwrap(),
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,5 +1,9 @@
 use jrsonnet_evaluator::{
-	error::Result, function::builtin, typed::VecVal, val::Val, IStr, ObjValue,
+	error::Result,
+	function::builtin,
+	typed::VecVal,
+	val::{StrValue, Val},
+	IStr, ObjValue,
 };
 use jrsonnet_gcmodule::Cc;
 
@@ -17,7 +21,10 @@
 		preserve_order,
 	);
 	Ok(VecVal(Cc::new(
-		out.into_iter().map(Val::Str).collect::<Vec<_>>(),
+		out.into_iter()
+			.map(StrValue::Flat)
+			.map(Val::Str)
+			.collect::<Vec<_>>(),
 	)))
 }
 
modifiedcrates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -7,7 +7,7 @@
 	operator::evaluate_mod_op,
 	stdlib::std_format,
 	typed::{Any, Either, Either2},
-	val::{equals, primitive_equals},
+	val::{equals, primitive_equals, StrValue},
 	IStr, Val,
 };
 
@@ -17,7 +17,7 @@
 	Ok(Any(evaluate_mod_op(
 		&match a {
 			A(v) => Val::Num(v),
-			B(s) => Val::Str(s),
+			B(s) => Val::Str(StrValue::Flat(s)),
 		},
 		&b.0,
 	)?))
@@ -35,5 +35,5 @@
 
 #[builtin]
 pub fn builtin_format(str: IStr, vals: Any) -> Result<String> {
-	std_format(str, vals.0)
+	std_format(&str, vals.0)
 }
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -3,7 +3,7 @@
 	function::builtin,
 	throw,
 	typed::{Either2, VecVal, M1},
-	val::ArrValue,
+	val::{ArrValue, StrValue},
 	Either, IStr, Val,
 };
 use jrsonnet_gcmodule::Cc;
@@ -34,9 +34,12 @@
 	Ok(VecVal(Cc::new(match maxsplits {
 		A(n) => str
 			.splitn(n + 1, &c as &str)
-			.map(|s| Val::Str(s.into()))
+			.map(|s| Val::Str(StrValue::Flat(s.into())))
+			.collect(),
+		B(_) => str
+			.split(&c as &str)
+			.map(|s| Val::Str(StrValue::Flat(s.into())))
 			.collect(),
-		B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
 	})))
 }