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
before · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13	arr::ArrValue,14	destructure::evaluate_dest,15	error::ErrorKind::*,16	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17	function::{CallLocation, FuncDesc, FuncVal},18	tb, throw,19	typed::Typed,20	val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},21	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22	Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28	fn is_trivial(expr: &LocExpr) -> bool {29		match &*expr.0 {30			Expr::Str(_)31			| Expr::Num(_)32			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33			Expr::Arr(a) => a.iter().all(is_trivial),34			Expr::Parened(e) => is_trivial(e),35			_ => false,36		}37	}38	Some(match &*expr.0 {39		Expr::Str(s) => Val::Str(s.clone()),40		Expr::Num(n) => Val::Num(*n),41		Expr::Literal(LiteralType::False) => Val::Bool(false),42		Expr::Literal(LiteralType::True) => Val::Bool(true),43		Expr::Literal(LiteralType::Null) => Val::Null,44		Expr::Arr(n) => {45			if n.iter().any(|e| !is_trivial(e)) {46				return None;47			}48			Val::Arr(ArrValue::eager(Cc::new(49				n.iter()50					.map(evaluate_trivial)51					.map(|e| e.expect("checked trivial"))52					.collect(),53			)))54		}55		Expr::Parened(e) => evaluate_trivial(e)?,56		_ => return None,57	})58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62		name,63		ctx,64		params,65		body,66	})))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70	Ok(match field_name {71		FieldName::Fixed(n) => Some(n.clone()),72		FieldName::Dyn(expr) => State::push(73			CallLocation::new(&expr.1),74			|| "evaluating field name".to_string(),75			|| {76				let value = evaluate(ctx, expr)?;77				if matches!(value, Val::Null) {78					Ok(None)79				} else {80					Ok(Some(IStr::from_untyped(value)?))81				}82			},83		)?,84	})85}8687pub fn evaluate_comp(88	ctx: Context,89	specs: &[CompSpec],90	callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92	match specs.get(0) {93		None => callback(ctx)?,94		Some(CompSpec::IfSpec(IfSpecData(cond))) => {95			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96				evaluate_comp(ctx, &specs[1..], callback)?;97			}98		}99		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100			Val::Arr(list) => {101				for item in list.iter_lazy() {102					let fctx = Pending::new();103					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104					destruct(var, item, fctx.clone(), &mut new_bindings)?;105					let ctx = ctx106						.clone()107						.extend(new_bindings, None, None, None)108						.into_future(fctx);109110					evaluate_comp(ctx, &specs[1..], callback)?;111				}112			}113			#[cfg(feature = "exp-object-iteration")]114			Val::Obj(obj) => {115				for field in obj.fields(116					// TODO: Should there be ability to preserve iteration order?117					#[cfg(feature = "exp-preserve-order")]118					false,119				) {120					#[derive(Trace)]121					struct ObjectFieldThunk {122						obj: ObjValue,123						field: IStr,124					}125					impl ThunkValue for ObjectFieldThunk {126						type Output = Val;127128						fn get(self: Box<Self>) -> Result<Self::Output> {129							self.obj.get(self.field).transpose().expect(130								"field exists, as field name was obtained from object.fields()",131							)132						}133					}134135					let fctx = Pending::new();136					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138						Thunk::evaluated(Val::Str(field.clone())),139						Thunk::new(tb!(ObjectFieldThunk {140							field: field.clone(),141							obj: obj.clone(),142						})),143					]))));144					destruct(var, value, fctx.clone(), &mut new_bindings)?;145					let ctx = ctx146						.clone()147						.extend(new_bindings, None, None, None)148						.into_future(fctx);149150					evaluate_comp(ctx, &specs[1..], callback)?;151				}152			}153			_ => throw!(InComprehensionCanOnlyIterateOverArray),154		},155	}156	Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163	fctx: Pending<Context>,164	locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166	#[derive(Trace, Clone)]167	struct UnboundLocals {168		fctx: Pending<Context>,169		locals: Rc<Vec<BindSpec>>,170	}171	impl Unbound for UnboundLocals {172		type Bound = Context;173174		fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175			let fctx = Context::new_future();176			let mut new_bindings =177				GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178			for b in self.locals.iter() {179				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180			}181182			let ctx = self.fctx.unwrap();183			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());184185			let ctx = ctx186				.extend(new_bindings, new_dollar, sup, this)187				.into_future(fctx);188189			Ok(ctx)190		}191	}192193	UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197	builder: &mut ObjValueBuilder,198	ctx: Context,199	uctx: B,200	field: &FieldMember,201) -> Result<()> {202	let name = evaluate_field_name(ctx, &field.name)?;203	let Some(name) = name else {204		return Ok(());205	};206207	match field {208		FieldMember {209			plus,210			params: None,211			visibility,212			value,213			..214		} => {215			#[derive(Trace)]216			struct UnboundValue<B: Trace> {217				uctx: B,218				value: LocExpr,219				name: IStr,220			}221			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222				type Bound = Val;223				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224					evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225				}226			}227228			builder229				.member(name.clone())230				.with_add(*plus)231				.with_visibility(*visibility)232				.with_location(value.1.clone())233				.bindable(tb!(UnboundValue {234					uctx,235					value: value.clone(),236					name,237				}))?;238		}239		FieldMember {240			params: Some(params),241			visibility,242			value,243			..244		} => {245			#[derive(Trace)]246			struct UnboundMethod<B: Trace> {247				uctx: B,248				value: LocExpr,249				params: ParamsDesc,250				name: IStr,251			}252			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253				type Bound = Val;254				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255					Ok(evaluate_method(256						self.uctx.bind(sup, this)?,257						self.name.clone(),258						self.params.clone(),259						self.value.clone(),260					))261				}262			}263264			builder265				.member(name.clone())266				.with_visibility(*visibility)267				.with_location(value.1.clone())268				.bindable(tb!(UnboundMethod {269					uctx,270					value: value.clone(),271					params: params.clone(),272					name,273				}))?;274		}275	}276	Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281	let mut builder = ObjValueBuilder::new();282	let locals = Rc::new(283		members284			.iter()285			.filter_map(|m| match m {286				Member::BindStmt(bind) => Some(bind.clone()),287				_ => None,288			})289			.collect::<Vec<_>>(),290	);291292	let fctx = Context::new_future();293294	// We have single context for all fields, so we can cache binds295	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297	for member in members.iter() {298		match member {299			Member::Field(field) => {300				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301			}302			Member::AssertStmt(stmt) => {303				#[derive(Trace)]304				struct ObjectAssert<B: Trace> {305					uctx: B,306					assert: AssertStmt,307				}308				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309					fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310						let ctx = self.uctx.bind(sup, this)?;311						evaluate_assert(ctx, &self.assert)312					}313				}314				builder.assert(tb!(ObjectAssert {315					uctx: uctx.clone(),316					assert: stmt.clone(),317				}));318			}319			Member::BindStmt(_) => {320				// Already handled321			}322		}323	}324	let this = builder.build();325	fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326	Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330	Ok(match object {331		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332		ObjBody::ObjComp(obj) => {333			let mut builder = ObjValueBuilder::new();334			let locals = Rc::new(335				obj.pre_locals336					.iter()337					.chain(obj.post_locals.iter())338					.cloned()339					.collect::<Vec<_>>(),340			);341			let mut ctxs = vec![];342			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343				let fctx = Context::new_future();344				ctxs.push((ctx.clone(), fctx.clone()));345				let uctx = evaluate_object_locals(fctx, locals.clone());346347				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348			})?;349350			let this = builder.build();351			for (ctx, fctx) in ctxs {352				let _ctx = ctx353					.extend(GcHashMap::new(), None, None, Some(this.clone()))354					.into_future(fctx);355			}356			this357		}358	})359}360361pub fn evaluate_apply(362	ctx: Context,363	value: &LocExpr,364	args: &ArgsDesc,365	loc: CallLocation<'_>,366	tailstrict: bool,367) -> Result<Val> {368	let value = evaluate(ctx.clone(), value)?;369	Ok(match value {370		Val::Func(f) => {371			let body = || f.evaluate(ctx, loc, args, tailstrict);372			if tailstrict {373				body()?374			} else {375				State::push(loc, || format!("function <{}> call", f.name()), body)?376			}377		}378		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379	})380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383	let value = &assertion.0;384	let msg = &assertion.1;385	let assertion_result = State::push(386		CallLocation::new(&value.1),387		|| "assertion condition".to_owned(),388		|| bool::from_untyped(evaluate(ctx.clone(), value)?),389	)?;390	if !assertion_result {391		State::push(392			CallLocation::new(&value.1),393			|| "assertion failure".to_owned(),394			|| {395				if let Some(msg) = msg {396					throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397				}398				throw!(AssertionFailed(Val::Null.to_string()?));399			},400		)?;401	}402	Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406	use Expr::*;407	let LocExpr(raw_expr, _loc) = expr;408	Ok(match &**raw_expr {409		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410		_ => evaluate(ctx, expr)?,411	})412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416	use Expr::*;417	if let Some(trivial) = evaluate_trivial(&expr) {418		return Ok(trivial);419	}420	let LocExpr(expr, loc) = expr;421	Ok(match &**expr {422		Literal(LiteralType::This) => {423			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)424		}425		Literal(LiteralType::Super) => Val::Obj(426			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(427				ctx.this()428					.clone()429					.expect("if super exists - then this should too"),430			),431		),432		Literal(LiteralType::Dollar) => {433			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)434		}435		Literal(LiteralType::True) => Val::Bool(true),436		Literal(LiteralType::False) => Val::Bool(false),437		Literal(LiteralType::Null) => Val::Null,438		Parened(e) => evaluate(ctx, e)?,439		Str(v) => Val::Str(v.clone()),440		Num(v) => Val::new_checked_num(*v)?,441		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,442		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,443		Var(name) => State::push(444			CallLocation::new(loc),445			|| format!("variable <{name}> access"),446			|| ctx.binding(name.clone())?.evaluate(),447		)?,448		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {449			let name = evaluate(ctx.clone(), index)?;450			let Val::Str(name) = name else {451				throw!(ValueIndexMustBeTypeGot(452					ValType::Obj,453					ValType::Str,454					name.value_type(),455				))456			};457			ctx.super_obj()458				.clone()459				.expect("no super found")460				.get_for(name, ctx.this().clone().expect("no this found"))?461				.expect("value not found")462		}463		Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {464			(Val::Obj(v), Val::Str(key)) => State::push(465				CallLocation::new(loc),466				|| format!("field <{key}> access"),467				|| match v.get(key.clone()) {468					Ok(Some(v)) => Ok(v),469					#[cfg(not(feature = "friendly-errors"))]470					Ok(None) => throw!(NoSuchField(key.clone(), vec![])),471					#[cfg(feature = "friendly-errors")]472					Ok(None) => {473						let mut heap = Vec::new();474						for field in v.fields_ex(475							true,476							#[cfg(feature = "exp-preserve-order")]477							false,478						) {479							let conf = strsim::jaro_winkler(&field as &str, &key as &str);480							if conf < 0.8 {481								continue;482							}483							heap.push((conf, field));484						}485						heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));486487						throw!(NoSuchField(488							key.clone(),489							heap.into_iter().map(|(_, v)| v).collect()490						))491					}492					Err(e) => Err(e),493				},494			)?,495			(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(496				ValType::Obj,497				ValType::Str,498				n.value_type(),499			)),500501			(Val::Arr(v), Val::Num(n)) => {502				if n.fract() > f64::EPSILON {503					throw!(FractionalIndex)504				}505				v.get(n as usize)?506					.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?507			}508			(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),509			(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(510				ValType::Arr,511				ValType::Num,512				n.value_type(),513			)),514515			(Val::Str(s), Val::Num(n)) => Val::Str({516				let v: IStr = s517					.chars()518					.skip(n as usize)519					.take(1)520					.collect::<String>()521					.into();522				if v.is_empty() {523					let size = s.chars().count();524					throw!(StringBoundsError(n as usize, size))525				}526				v527			}),528			(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(529				ValType::Str,530				ValType::Num,531				n.value_type(),532			)),533534			(v, _) => throw!(CantIndexInto(v.value_type())),535		},536		LocalExpr(bindings, returned) => {537			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =538				GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());539			let fctx = Context::new_future();540			for b in bindings {541				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;542			}543			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);544			evaluate(ctx, &returned.clone())?545		}546		Arr(items) => {547			if items.is_empty() {548				Val::Arr(ArrValue::empty())549			} else if items.len() == 1 {550				#[derive(Trace)]551				struct ArrayElement {552					ctx: Context,553					item: LocExpr,554				}555				impl ThunkValue for ArrayElement {556					type Output = Val;557					fn get(self: Box<Self>) -> Result<Val> {558						evaluate(self.ctx, &self.item)559					}560				}561				Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(562					ArrayElement {563						ctx,564						item: items[0].clone(),565					}566				))])))567			} else {568				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))569			}570		}571		ArrComp(expr, comp_specs) => {572			let mut out = Vec::new();573			evaluate_comp(ctx, comp_specs, &mut |ctx| {574				out.push(evaluate(ctx, expr)?);575				Ok(())576			})?;577			Val::Arr(ArrValue::eager(Cc::new(out)))578		}579		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),580		ObjExtend(a, b) => evaluate_add_op(581			&evaluate(ctx.clone(), a)?,582			&Val::Obj(evaluate_object(ctx, b)?),583		)?,584		Apply(value, args, tailstrict) => {585			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?586		}587		Function(params, body) => {588			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())589		}590		AssertExpr(assert, returned) => {591			evaluate_assert(ctx.clone(), assert)?;592			evaluate(ctx, returned)?593		}594		ErrorStmt(e) => State::push(595			CallLocation::new(loc),596			|| "error statement".to_owned(),597			|| throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),598		)?,599		IfElse {600			cond,601			cond_then,602			cond_else,603		} => {604			if State::push(605				CallLocation::new(loc),606				|| "if condition".to_owned(),607				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),608			)? {609				evaluate(ctx, cond_then)?610			} else {611				match cond_else {612					Some(v) => evaluate(ctx, v)?,613					None => Val::Null,614				}615			}616		}617		Slice(value, desc) => {618			fn parse_idx<T: Typed>(619				loc: CallLocation<'_>,620				ctx: &Context,621				expr: &Option<LocExpr>,622				desc: &'static str,623			) -> Result<Option<T>> {624				if let Some(value) = expr {625					Ok(Some(State::push(626						loc,627						|| format!("slice {desc}"),628						|| T::from_untyped(evaluate(ctx.clone(), value)?),629					)?))630				} else {631					Ok(None)632				}633			}634635			let indexable = evaluate(ctx.clone(), value)?;636			let loc = CallLocation::new(loc);637638			let start = parse_idx(loc, &ctx, &desc.start, "start")?;639			let end = parse_idx(loc, &ctx, &desc.end, "end")?;640			let step = parse_idx(loc, &ctx, &desc.step, "step")?;641642			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?643		}644		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {645			let Expr::Str(path) = &*path.0 else {646				throw!("computed imports are not supported")647			};648			let tmp = loc.clone().0;649			let s = ctx.state();650			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;651			match i {652				Import(_) => State::push(653					CallLocation::new(loc),654					|| format!("import {:?}", path.clone()),655					|| s.import_resolved(resolved_path),656				)?,657				ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),658				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),659				_ => unreachable!(),660			}661		}662	})663}
after · crates/jrsonnet-evaluator/src/evaluate/mod.rs
1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6	ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7	IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13	arr::ArrValue,14	destructure::evaluate_dest,15	error::ErrorKind::*,16	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17	function::{CallLocation, FuncDesc, FuncVal},18	tb, throw,19	typed::Typed,20	val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21	Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22	Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28	fn is_trivial(expr: &LocExpr) -> bool {29		match &*expr.0 {30			Expr::Str(_)31			| Expr::Num(_)32			| Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33			Expr::Arr(a) => a.iter().all(is_trivial),34			Expr::Parened(e) => is_trivial(e),35			_ => false,36		}37	}38	Some(match &*expr.0 {39		Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40		Expr::Num(n) => Val::Num(*n),41		Expr::Literal(LiteralType::False) => Val::Bool(false),42		Expr::Literal(LiteralType::True) => Val::Bool(true),43		Expr::Literal(LiteralType::Null) => Val::Null,44		Expr::Arr(n) => {45			if n.iter().any(|e| !is_trivial(e)) {46				return None;47			}48			Val::Arr(ArrValue::eager(Cc::new(49				n.iter()50					.map(evaluate_trivial)51					.map(|e| e.expect("checked trivial"))52					.collect(),53			)))54		}55		Expr::Parened(e) => evaluate_trivial(e)?,56		_ => return None,57	})58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61	Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62		name,63		ctx,64		params,65		body,66	})))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70	Ok(match field_name {71		FieldName::Fixed(n) => Some(n.clone()),72		FieldName::Dyn(expr) => State::push(73			CallLocation::new(&expr.1),74			|| "evaluating field name".to_string(),75			|| {76				let value = evaluate(ctx, expr)?;77				if matches!(value, Val::Null) {78					Ok(None)79				} else {80					Ok(Some(IStr::from_untyped(value)?))81				}82			},83		)?,84	})85}8687pub fn evaluate_comp(88	ctx: Context,89	specs: &[CompSpec],90	callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92	match specs.get(0) {93		None => callback(ctx)?,94		Some(CompSpec::IfSpec(IfSpecData(cond))) => {95			if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96				evaluate_comp(ctx, &specs[1..], callback)?;97			}98		}99		Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100			Val::Arr(list) => {101				for item in list.iter_lazy() {102					let fctx = Pending::new();103					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104					destruct(var, item, fctx.clone(), &mut new_bindings)?;105					let ctx = ctx106						.clone()107						.extend(new_bindings, None, None, None)108						.into_future(fctx);109110					evaluate_comp(ctx, &specs[1..], callback)?;111				}112			}113			#[cfg(feature = "exp-object-iteration")]114			Val::Obj(obj) => {115				for field in obj.fields(116					// TODO: Should there be ability to preserve iteration order?117					#[cfg(feature = "exp-preserve-order")]118					false,119				) {120					#[derive(Trace)]121					struct ObjectFieldThunk {122						obj: ObjValue,123						field: IStr,124					}125					impl ThunkValue for ObjectFieldThunk {126						type Output = Val;127128						fn get(self: Box<Self>) -> Result<Self::Output> {129							self.obj.get(self.field).transpose().expect(130								"field exists, as field name was obtained from object.fields()",131							)132						}133					}134135					let fctx = Pending::new();136					let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137					let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138						Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139						Thunk::new(tb!(ObjectFieldThunk {140							field: field.clone(),141							obj: obj.clone(),142						})),143					]))));144					destruct(var, value, fctx.clone(), &mut new_bindings)?;145					let ctx = ctx146						.clone()147						.extend(new_bindings, None, None, None)148						.into_future(fctx);149150					evaluate_comp(ctx, &specs[1..], callback)?;151				}152			}153			_ => throw!(InComprehensionCanOnlyIterateOverArray),154		},155	}156	Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163	fctx: Pending<Context>,164	locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166	#[derive(Trace, Clone)]167	struct UnboundLocals {168		fctx: Pending<Context>,169		locals: Rc<Vec<BindSpec>>,170	}171	impl Unbound for UnboundLocals {172		type Bound = Context;173174		fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175			let fctx = Context::new_future();176			let mut new_bindings =177				GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178			for b in self.locals.iter() {179				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180			}181182			let ctx = self.fctx.unwrap();183			let new_dollar = ctx.dollar().clone().or_else(|| this.clone());184185			let ctx = ctx186				.extend(new_bindings, new_dollar, sup, this)187				.into_future(fctx);188189			Ok(ctx)190		}191	}192193	UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197	builder: &mut ObjValueBuilder,198	ctx: Context,199	uctx: B,200	field: &FieldMember,201) -> Result<()> {202	let name = evaluate_field_name(ctx, &field.name)?;203	let Some(name) = name else {204		return Ok(());205	};206207	match field {208		FieldMember {209			plus,210			params: None,211			visibility,212			value,213			..214		} => {215			#[derive(Trace)]216			struct UnboundValue<B: Trace> {217				uctx: B,218				value: LocExpr,219				name: IStr,220			}221			impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222				type Bound = Val;223				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224					evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225				}226			}227228			builder229				.member(name.clone())230				.with_add(*plus)231				.with_visibility(*visibility)232				.with_location(value.1.clone())233				.bindable(tb!(UnboundValue {234					uctx,235					value: value.clone(),236					name,237				}))?;238		}239		FieldMember {240			params: Some(params),241			visibility,242			value,243			..244		} => {245			#[derive(Trace)]246			struct UnboundMethod<B: Trace> {247				uctx: B,248				value: LocExpr,249				params: ParamsDesc,250				name: IStr,251			}252			impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253				type Bound = Val;254				fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255					Ok(evaluate_method(256						self.uctx.bind(sup, this)?,257						self.name.clone(),258						self.params.clone(),259						self.value.clone(),260					))261				}262			}263264			builder265				.member(name.clone())266				.with_visibility(*visibility)267				.with_location(value.1.clone())268				.bindable(tb!(UnboundMethod {269					uctx,270					value: value.clone(),271					params: params.clone(),272					name,273				}))?;274		}275	}276	Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281	let mut builder = ObjValueBuilder::new();282	let locals = Rc::new(283		members284			.iter()285			.filter_map(|m| match m {286				Member::BindStmt(bind) => Some(bind.clone()),287				_ => None,288			})289			.collect::<Vec<_>>(),290	);291292	let fctx = Context::new_future();293294	// We have single context for all fields, so we can cache binds295	let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297	for member in members.iter() {298		match member {299			Member::Field(field) => {300				evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301			}302			Member::AssertStmt(stmt) => {303				#[derive(Trace)]304				struct ObjectAssert<B: Trace> {305					uctx: B,306					assert: AssertStmt,307				}308				impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309					fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310						let ctx = self.uctx.bind(sup, this)?;311						evaluate_assert(ctx, &self.assert)312					}313				}314				builder.assert(tb!(ObjectAssert {315					uctx: uctx.clone(),316					assert: stmt.clone(),317				}));318			}319			Member::BindStmt(_) => {320				// Already handled321			}322		}323	}324	let this = builder.build();325	fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326	Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330	Ok(match object {331		ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332		ObjBody::ObjComp(obj) => {333			let mut builder = ObjValueBuilder::new();334			let locals = Rc::new(335				obj.pre_locals336					.iter()337					.chain(obj.post_locals.iter())338					.cloned()339					.collect::<Vec<_>>(),340			);341			let mut ctxs = vec![];342			evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343				let fctx = Context::new_future();344				ctxs.push((ctx.clone(), fctx.clone()));345				let uctx = evaluate_object_locals(fctx, locals.clone());346347				evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348			})?;349350			let this = builder.build();351			for (ctx, fctx) in ctxs {352				let _ctx = ctx353					.extend(GcHashMap::new(), None, None, Some(this.clone()))354					.into_future(fctx);355			}356			this357		}358	})359}360361pub fn evaluate_apply(362	ctx: Context,363	value: &LocExpr,364	args: &ArgsDesc,365	loc: CallLocation<'_>,366	tailstrict: bool,367) -> Result<Val> {368	let value = evaluate(ctx.clone(), value)?;369	Ok(match value {370		Val::Func(f) => {371			let body = || f.evaluate(ctx, loc, args, tailstrict);372			if tailstrict {373				body()?374			} else {375				State::push(loc, || format!("function <{}> call", f.name()), body)?376			}377		}378		v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379	})380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383	let value = &assertion.0;384	let msg = &assertion.1;385	let assertion_result = State::push(386		CallLocation::new(&value.1),387		|| "assertion condition".to_owned(),388		|| bool::from_untyped(evaluate(ctx.clone(), value)?),389	)?;390	if !assertion_result {391		State::push(392			CallLocation::new(&value.1),393			|| "assertion failure".to_owned(),394			|| {395				if let Some(msg) = msg {396					throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397				}398				throw!(AssertionFailed(Val::Null.to_string()?));399			},400		)?;401	}402	Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406	use Expr::*;407	let LocExpr(raw_expr, _loc) = expr;408	Ok(match &**raw_expr {409		Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410		_ => evaluate(ctx, expr)?,411	})412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416	use Expr::*;417	if let Some(trivial) = evaluate_trivial(&expr) {418		return Ok(trivial);419	}420	let LocExpr(expr, loc) = expr;421	Ok(match &**expr {422		Literal(LiteralType::This) => {423			Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)424		}425		Literal(LiteralType::Super) => Val::Obj(426			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(427				ctx.this()428					.clone()429					.expect("if super exists - then this should too"),430			),431		),432		Literal(LiteralType::Dollar) => {433			Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)434		}435		Literal(LiteralType::True) => Val::Bool(true),436		Literal(LiteralType::False) => Val::Bool(false),437		Literal(LiteralType::Null) => Val::Null,438		Parened(e) => evaluate(ctx, e)?,439		Str(v) => Val::Str(StrValue::Flat(v.clone())),440		Num(v) => Val::new_checked_num(*v)?,441		BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,442		UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,443		Var(name) => State::push(444			CallLocation::new(loc),445			|| format!("variable <{name}> access"),446			|| ctx.binding(name.clone())?.evaluate(),447		)?,448		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {449			let name = evaluate(ctx.clone(), index)?;450			let Val::Str(name) = name else {451				throw!(ValueIndexMustBeTypeGot(452					ValType::Obj,453					ValType::Str,454					name.value_type(),455				))456			};457			ctx.super_obj()458				.clone()459				.expect("no super found")460				.get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?461				.expect("value not found")462		}463		Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {464			(Val::Obj(v), Val::Str(key)) => State::push(465				CallLocation::new(loc),466				|| format!("field <{key}> access"),467				|| match v.get(key.clone().into_flat()) {468					Ok(Some(v)) => Ok(v),469					#[cfg(not(feature = "friendly-errors"))]470					Ok(None) => throw!(NoSuchField(key.clone(), vec![])),471					#[cfg(feature = "friendly-errors")]472					Ok(None) => {473						let mut heap = Vec::new();474						for field in v.fields_ex(475							true,476							#[cfg(feature = "exp-preserve-order")]477							false,478						) {479							let conf = strsim::jaro_winkler(480								&field as &str,481								&key.clone().into_flat() as &str,482							);483							if conf < 0.8 {484								continue;485							}486							heap.push((conf, field));487						}488						heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));489490						throw!(NoSuchField(491							key.clone().into_flat(),492							heap.into_iter().map(|(_, v)| v).collect()493						))494					}495					Err(e) => Err(e),496				},497			)?,498			(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(499				ValType::Obj,500				ValType::Str,501				n.value_type(),502			)),503504			(Val::Arr(v), Val::Num(n)) => {505				if n.fract() > f64::EPSILON {506					throw!(FractionalIndex)507				}508				v.get(n as usize)?509					.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?510			}511			(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),512			(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(513				ValType::Arr,514				ValType::Num,515				n.value_type(),516			)),517518			(Val::Str(s), Val::Num(n)) => Val::Str({519				let v: IStr = s520					.clone()521					.into_flat()522					.chars()523					.skip(n as usize)524					.take(1)525					.collect::<String>()526					.into();527				if v.is_empty() {528					let size = s.into_flat().chars().count();529					throw!(StringBoundsError(n as usize, size))530				}531				StrValue::Flat(v)532			}),533			(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(534				ValType::Str,535				ValType::Num,536				n.value_type(),537			)),538539			(v, _) => throw!(CantIndexInto(v.value_type())),540		},541		LocalExpr(bindings, returned) => {542			let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =543				GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());544			let fctx = Context::new_future();545			for b in bindings {546				evaluate_dest(b, fctx.clone(), &mut new_bindings)?;547			}548			let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);549			evaluate(ctx, &returned.clone())?550		}551		Arr(items) => {552			if items.is_empty() {553				Val::Arr(ArrValue::empty())554			} else if items.len() == 1 {555				#[derive(Trace)]556				struct ArrayElement {557					ctx: Context,558					item: LocExpr,559				}560				impl ThunkValue for ArrayElement {561					type Output = Val;562					fn get(self: Box<Self>) -> Result<Val> {563						evaluate(self.ctx, &self.item)564					}565				}566				Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(567					ArrayElement {568						ctx,569						item: items[0].clone(),570					}571				))])))572			} else {573				Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))574			}575		}576		ArrComp(expr, comp_specs) => {577			let mut out = Vec::new();578			evaluate_comp(ctx, comp_specs, &mut |ctx| {579				out.push(evaluate(ctx, expr)?);580				Ok(())581			})?;582			Val::Arr(ArrValue::eager(Cc::new(out)))583		}584		Obj(body) => Val::Obj(evaluate_object(ctx, body)?),585		ObjExtend(a, b) => evaluate_add_op(586			&evaluate(ctx.clone(), a)?,587			&Val::Obj(evaluate_object(ctx, b)?),588		)?,589		Apply(value, args, tailstrict) => {590			evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?591		}592		Function(params, body) => {593			evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())594		}595		AssertExpr(assert, returned) => {596			evaluate_assert(ctx.clone(), assert)?;597			evaluate(ctx, returned)?598		}599		ErrorStmt(e) => State::push(600			CallLocation::new(loc),601			|| "error statement".to_owned(),602			|| throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),603		)?,604		IfElse {605			cond,606			cond_then,607			cond_else,608		} => {609			if State::push(610				CallLocation::new(loc),611				|| "if condition".to_owned(),612				|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),613			)? {614				evaluate(ctx, cond_then)?615			} else {616				match cond_else {617					Some(v) => evaluate(ctx, v)?,618					None => Val::Null,619				}620			}621		}622		Slice(value, desc) => {623			fn parse_idx<T: Typed>(624				loc: CallLocation<'_>,625				ctx: &Context,626				expr: &Option<LocExpr>,627				desc: &'static str,628			) -> Result<Option<T>> {629				if let Some(value) = expr {630					Ok(Some(State::push(631						loc,632						|| format!("slice {desc}"),633						|| T::from_untyped(evaluate(ctx.clone(), value)?),634					)?))635				} else {636					Ok(None)637				}638			}639640			let indexable = evaluate(ctx.clone(), value)?;641			let loc = CallLocation::new(loc);642643			let start = parse_idx(loc, &ctx, &desc.start, "start")?;644			let end = parse_idx(loc, &ctx, &desc.end, "end")?;645			let step = parse_idx(loc, &ctx, &desc.step, "step")?;646647			IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?648		}649		i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {650			let Expr::Str(path) = &*path.0 else {651				throw!("computed imports are not supported")652			};653			let tmp = loc.clone().0;654			let s = ctx.state();655			let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;656			match i {657				Import(_) => State::push(658					CallLocation::new(loc),659					|| format!("import {:?}", path.clone()),660					|| s.import_resolved(resolved_path),661				)?,662				ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),663				ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),664				_ => unreachable!(),665			}666		}667	})668}
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
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -320,15 +320,19 @@
 	}
 	#[cfg(feature = "legacy-this-file")]
 	fn initialize(&self, s: State, source: Source) -> Context {
+		use jrsonnet_evaluator::val::StrValue;
+
 		let mut builder = ObjValueBuilder::new();
 		builder.with_super(self.stdlib_obj.clone());
 		builder
 			.member("thisFile".into())
 			.hide()
-			.value(Val::Str(match source.source_path().path() {
-				Some(p) => self.settings().path_resolver.resolve(p).into(),
-				None => source.source_path().to_string().into(),
-			}))
+			.value(Val::Str(StrValue::Flat(
+				match source.source_path().path() {
+					Some(p) => self.settings().path_resolver.resolve(p).into(),
+					None => source.source_path().to_string().into(),
+				},
+			)))
 			.expect("this object builder is empty");
 		let stdlib_with_this_file = builder.build();
 
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(),
 	})))
 }