git.delta.rocks / jrsonnet / refs/commits / 64f3674f18c4

difftreelog

perf specialize super.field gets

Yaroslav Bolyukin2022-11-20parent: #30b370e.patch.diff
in: master

4 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -139,7 +139,7 @@
 }
 
 fn main_real(s: &State, opts: Opts) -> Result<(), Error> {
-	let (_stack_guard, tla, _gc_guard) = opts.general.configure(s)?;
+	let (tla, _gc_guard) = opts.general.configure(s)?;
 	let manifest_format = opts.manifest.configure(s)?;
 
 	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -6,9 +6,7 @@
 use std::{env, marker::PhantomData, path::PathBuf};
 
 use clap::Parser;
-use jrsonnet_evaluator::{
-	error::Result, stack::StackDepthLimitOverrideGuard, FileImportResolver, State,
-};
+use jrsonnet_evaluator::{error::Result, stack::set_stack_depth_limit, FileImportResolver, State};
 use jrsonnet_gcmodule::with_thread_object_space;
 pub use manifest::*;
 pub use stdlib::*;
@@ -48,7 +46,7 @@
 	jpath: Vec<PathBuf>,
 }
 impl ConfigureState for MiscOpts {
-	type Guards = StackDepthLimitOverrideGuard;
+	type Guards = ();
 	fn configure(&self, s: &State) -> Result<Self::Guards> {
 		let mut library_paths = self.jpath.clone();
 		library_paths.reverse();
@@ -58,8 +56,8 @@
 
 		s.set_import_resolver(Box::new(FileImportResolver::new(library_paths)));
 
-		let _depth_limit = jrsonnet_evaluator::stack::limit_stack_depth(self.max_stack);
-		Ok(_depth_limit)
+		set_stack_depth_limit(self.max_stack);
+		Ok(())
 	}
 }
 
@@ -81,17 +79,16 @@
 
 impl ConfigureState for GeneralOpts {
 	type Guards = (
-		<MiscOpts as ConfigureState>::Guards,
 		<TlaOpts as ConfigureState>::Guards,
 		<GcOpts as ConfigureState>::Guards,
 	);
 	fn configure(&self, s: &State) -> Result<Self::Guards> {
 		// Configure trace first, because tla-code/ext-code can throw
-		let misc_guards = self.misc.configure(s)?;
+		self.misc.configure(s)?;
 		let tla_guards = self.tla.configure(s)?;
 		self.std.configure(s)?;
 		let gc_guards = self.gc.configure(s)?;
-		Ok((misc_guards, tla_guards, gc_guards))
+		Ok((tla_guards, gc_guards))
 	}
 }
 
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -389,7 +389,7 @@
 			ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
 				ctx.this()
 					.clone()
-					.expect("if super exists - then this should to"),
+					.expect("if super exists - then this should too"),
 			),
 		),
 		Literal(LiteralType::Dollar) => {
@@ -408,6 +408,21 @@
 			|| format!("variable <{name}> access"),
 			|| ctx.binding(name.clone())?.evaluate(),
 		)?,
+		Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
+			let name = evaluate(ctx.clone(), index)?;
+			let Val::Str(name) = name else {
+				throw!(ValueIndexMustBeTypeGot(
+					ValType::Obj,
+					ValType::Str,
+					name.value_type(),
+				))
+			};
+			ctx.super_obj()
+				.clone()
+				.expect("no super found")
+				.get_for(name, 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),
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/obj.rs
1use std::{2	cell::RefCell,3	fmt::Debug,4	hash::{Hash, Hasher},5	ptr::addr_of,6};78use jrsonnet_gcmodule::{Cc, Trace, Weak};9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ExprLocation, Visibility};11use rustc_hash::FxHashMap;1213use crate::{14	error::{Error, ErrorKind::*},15	function::CallLocation,16	gc::{GcHashMap, GcHashSet, TraceBox},17	operator::evaluate_add_op,18	throw, MaybeUnbound, Result, State, Thunk, Unbound, Val,19};2021#[cfg(not(feature = "exp-preserve-order"))]22mod ordering {23	#![allow(24		// This module works as stub for preserve-order feature25		clippy::unused_self,26	)]2728	use jrsonnet_gcmodule::Trace;2930	#[derive(Clone, Copy, Default, Debug, Trace)]31	pub struct FieldIndex;32	impl FieldIndex {33		pub const fn next(self) -> Self {34			Self35		}36	}3738	#[derive(Clone, Copy, Default, Debug, Trace)]39	pub struct SuperDepth;40	impl SuperDepth {41		pub const fn deeper(self) -> Self {42			Self43		}44	}4546	#[derive(Clone, Copy)]47	pub struct FieldSortKey;48	impl FieldSortKey {49		pub const fn new(_: SuperDepth, _: FieldIndex) -> Self {50			Self51		}52	}53}5455#[cfg(feature = "exp-preserve-order")]56mod ordering {57	use std::cmp::Reverse;5859	use jrsonnet_gcmodule::Trace;6061	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]62	pub struct FieldIndex(u32);63	impl FieldIndex {64		pub fn next(self) -> Self {65			Self(self.0 + 1)66		}67	}6869	#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]70	pub struct SuperDepth(u32);71	impl SuperDepth {72		pub fn deeper(self) -> Self {73			Self(self.0 + 1)74		}75	}7677	#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]78	pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);79	impl FieldSortKey {80		pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {81			Self(Reverse(depth), index)82		}83		pub fn collide(self, other: Self) -> Self {84			if self.0 .0 > other.0 .0 {85				self86			} else if self.0 .0 < other.0 .0 {87				other88			} else {89				unreachable!("object can't have two fields with same name")90			}91		}92	}93}9495use ordering::*;9697#[allow(clippy::module_name_repetitions)]98#[derive(Debug, Trace)]99pub struct ObjMember {100	pub add: bool,101	pub visibility: Visibility,102	original_index: FieldIndex,103	pub invoke: MaybeUnbound,104	pub location: Option<ExprLocation>,105}106107pub trait ObjectAssertion: Trace {108	fn run(&self, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;109}110111// Field => This112113#[derive(Trace)]114enum CacheValue {115	Cached(Val),116	NotFound,117	Pending,118	Errored(Error),119}120121#[allow(clippy::module_name_repetitions)]122#[derive(Trace)]123#[trace(tracking(force))]124pub struct ObjValueInternals {125	sup: Option<ObjValue>,126	this: Option<ObjValue>,127128	assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,129	assertions_ran: RefCell<GcHashSet<ObjValue>>,130	this_entries: Cc<GcHashMap<IStr, ObjMember>>,131	value_cache: RefCell<GcHashMap<IStr, CacheValue>>,132}133134#[derive(Clone, Trace)]135pub struct WeakObjValue(#[trace(skip)] pub(crate) Weak<ObjValueInternals>);136137impl PartialEq for WeakObjValue {138	fn eq(&self, other: &Self) -> bool {139		Weak::ptr_eq(&self.0, &other.0)140	}141}142143impl Eq for WeakObjValue {}144impl Hash for WeakObjValue {145	fn hash<H: Hasher>(&self, hasher: &mut H) {146		// Safety: usize is POD147		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };148		hasher.write_usize(addr);149	}150}151152#[allow(clippy::module_name_repetitions)]153#[derive(Clone, Trace)]154pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);155impl Debug for ObjValue {156	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {157		if let Some(super_obj) = self.0.sup.as_ref() {158			if f.alternate() {159				write!(f, "{super_obj:#?}")?;160			} else {161				write!(f, "{super_obj:?}")?;162			}163			write!(f, " + ")?;164		}165		let mut debug = f.debug_struct("ObjValue");166		for (name, member) in self.0.this_entries.iter() {167			debug.field(name, member);168		}169		debug.finish_non_exhaustive()170	}171}172173impl ObjValue {174	pub fn new(175		sup: Option<Self>,176		this_entries: Cc<GcHashMap<IStr, ObjMember>>,177		assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,178	) -> Self {179		Self(Cc::new(ObjValueInternals {180			sup,181			this: None,182			assertions,183			assertions_ran: RefCell::new(GcHashSet::new()),184			this_entries,185			value_cache: RefCell::new(GcHashMap::new()),186		}))187	}188	pub fn new_empty() -> Self {189		Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))190	}191	#[must_use]192	pub fn extend_from(&self, sup: Self) -> Self {193		match &self.0.sup {194			None => Self::new(195				Some(sup),196				self.0.this_entries.clone(),197				self.0.assertions.clone(),198			),199			Some(v) => Self::new(200				Some(v.extend_from(sup)),201				self.0.this_entries.clone(),202				self.0.assertions.clone(),203			),204		}205	}206	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {207		let mut new = GcHashMap::with_capacity(1);208		new.insert(key, value);209		Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))210	}211	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {212		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())213	}214215	#[must_use]216	pub fn with_this(&self, this: Self) -> Self {217		Self(Cc::new(ObjValueInternals {218			sup: self.0.sup.clone(),219			assertions: self.0.assertions.clone(),220			assertions_ran: RefCell::new(GcHashSet::new()),221			this: Some(this),222			this_entries: self.0.this_entries.clone(),223			value_cache: RefCell::new(GcHashMap::new()),224		}))225	}226227	pub fn len(&self) -> usize {228		self.fields_visibility()229			.into_iter()230			.filter(|(_, (visible, _))| *visible)231			.count()232	}233234	pub fn is_empty(&self) -> bool {235		if !self.0.this_entries.is_empty() {236			return false;237		}238		self.0.sup.as_ref().map_or(true, Self::is_empty)239	}240241	/// Run callback for every field found in object242	///243	/// Returns true if ended prematurely244	pub(crate) fn enum_fields(245		&self,246		depth: SuperDepth,247		handler: &mut impl FnMut(SuperDepth, &IStr, &ObjMember) -> bool,248	) -> bool {249		if let Some(s) = &self.0.sup {250			if s.enum_fields(depth.deeper(), handler) {251				return true;252			}253		}254		for (name, member) in self.0.this_entries.iter() {255			if handler(depth, name, member) {256				return true;257			}258		}259		false260	}261262	pub fn fields_visibility(&self) -> FxHashMap<IStr, (bool, FieldSortKey)> {263		let mut out = FxHashMap::default();264		self.enum_fields(SuperDepth::default(), &mut |depth, name, member| {265			let new_sort_key = FieldSortKey::new(depth, member.original_index);266			let entry = out.entry(name.clone());267			let (visible, _) = entry.or_insert((true, new_sort_key));268			match member.visibility {269				Visibility::Normal => {}270				Visibility::Hidden => {271					*visible = false;272				}273				Visibility::Unhide => {274					*visible = true;275				}276			};277			false278		});279		out280	}281	pub fn fields_ex(282		&self,283		include_hidden: bool,284		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,285	) -> Vec<IStr> {286		#[cfg(feature = "exp-preserve-order")]287		if preserve_order {288			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self289				.fields_visibility()290				.into_iter()291				.filter(|(_, (visible, _))| include_hidden || *visible)292				.enumerate()293				.map(|(idx, (k, (_, sk)))| (k, (sk, idx)))294				.unzip();295			keys.sort_unstable_by_key(|v| v.0);296			// Reorder in-place by resulting indexes297			for i in 0..fields.len() {298				let x = fields[i].clone();299				let mut j = i;300				loop {301					let k = keys[j].1;302					keys[j].1 = j;303					if k == i {304						break;305					}306					fields[j] = fields[k].clone();307					j = k308				}309				fields[j] = x;310			}311			return fields;312		}313314		let mut fields: Vec<_> = self315			.fields_visibility()316			.into_iter()317			.filter(|(_, (visible, _))| include_hidden || *visible)318			.map(|(k, _)| k)319			.collect();320		fields.sort_unstable();321		fields322	}323	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {324		self.fields_ex(325			false,326			#[cfg(feature = "exp-preserve-order")]327			preserve_order,328		)329	}330331	pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {332		if let Some(m) = self.0.this_entries.get(&name) {333			Some(match &m.visibility {334				Visibility::Normal => self335					.0336					.sup337					.as_ref()338					.and_then(|super_obj| super_obj.field_visibility(name))339					.unwrap_or(Visibility::Normal),340				v => *v,341			})342		} else if let Some(super_obj) = &self.0.sup {343			super_obj.field_visibility(name)344		} else {345			None346		}347	}348349	fn has_field_include_hidden(&self, name: IStr) -> bool {350		if self.0.this_entries.contains_key(&name) {351			true352		} else if let Some(super_obj) = &self.0.sup {353			super_obj.has_field_include_hidden(name)354		} else {355			false356		}357	}358359	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {360		if include_hidden {361			self.has_field_include_hidden(name)362		} else {363			self.has_field(name)364		}365	}366	pub fn has_field(&self, name: IStr) -> bool {367		self.field_visibility(name)368			.map_or(false, |v| v.is_visible())369	}370371	pub fn iter(372		&self,373		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,374	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {375		let fields = self.fields(376			#[cfg(feature = "exp-preserve-order")]377			preserve_order,378		);379		fields.into_iter().map(|field| {380			(381				field.clone(),382				self.get(field)383					.map(|opt| opt.expect("iterating over keys, field exists")),384			)385		})386	}387388	pub fn get(&self, key: IStr) -> Result<Option<Val>> {389		self.run_assertions()?;390		if let Some(v) = self.0.value_cache.borrow().get(&key) {391			return Ok(match v {392				CacheValue::Cached(v) => Some(v.clone()),393				CacheValue::NotFound => None,394				CacheValue::Pending => throw!(InfiniteRecursionDetected),395				CacheValue::Errored(e) => return Err(e.clone()),396			});397		}398		self.0399			.value_cache400			.borrow_mut()401			.insert(key.clone(), CacheValue::Pending);402		let value = self403			.get_raw(404				key.clone(),405				self.0.this.clone().unwrap_or_else(|| self.clone()),406			)407			.map_err(|e| {408				self.0409					.value_cache410					.borrow_mut()411					.insert(key.clone(), CacheValue::Errored(e.clone()));412				e413			})?;414		self.0.value_cache.borrow_mut().insert(415			key,416			value417				.as_ref()418				.map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),419		);420		Ok(value)421	}422423	fn get_raw(&self, key: IStr, real_this: Self) -> Result<Option<Val>> {424		match (self.0.this_entries.get(&key), &self.0.sup) {425			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),426			(Some(k), Some(super_obj)) => {427				let our = self.evaluate_this(k, real_this.clone())?;428				if k.add {429					super_obj430						.get_raw(key, real_this)?431						.map_or(Ok(Some(our.clone())), |v| {432							Ok(Some(evaluate_add_op(&v, &our)?))433						})434				} else {435					Ok(Some(our))436				}437			}438			(None, Some(super_obj)) => super_obj.get_raw(key, real_this),439			(None, None) => Ok(None),440		}441	}442	fn evaluate_this(&self, v: &ObjMember, real_this: Self) -> Result<Val> {443		v.invoke.evaluate(self.0.sup.clone(), Some(real_this))444	}445446	fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {447		if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {448			for assertion in self.0.assertions.iter() {449				if let Err(e) = assertion.run(self.0.sup.clone(), Some(real_this.clone())) {450					self.0.assertions_ran.borrow_mut().remove(real_this);451					return Err(e);452				}453			}454			if let Some(super_obj) = &self.0.sup {455				super_obj.run_assertions_raw(real_this)?;456			}457		}458		Ok(())459	}460	pub fn run_assertions(&self) -> Result<()> {461		self.run_assertions_raw(self)462	}463464	pub fn ptr_eq(a: &Self, b: &Self) -> bool {465		Cc::ptr_eq(&a.0, &b.0)466	}467	pub fn downgrade(self) -> WeakObjValue {468		WeakObjValue(self.0.downgrade())469	}470}471472impl PartialEq for ObjValue {473	fn eq(&self, other: &Self) -> bool {474		Cc::ptr_eq(&self.0, &other.0)475	}476}477478impl Eq for ObjValue {}479impl Hash for ObjValue {480	fn hash<H: Hasher>(&self, hasher: &mut H) {481		hasher.write_usize(addr_of!(*self.0) as usize);482	}483}484485#[allow(clippy::module_name_repetitions)]486pub struct ObjValueBuilder {487	sup: Option<ObjValue>,488	map: GcHashMap<IStr, ObjMember>,489	assertions: Vec<TraceBox<dyn ObjectAssertion>>,490	next_field_index: FieldIndex,491}492impl ObjValueBuilder {493	pub fn new() -> Self {494		Self::with_capacity(0)495	}496	pub fn with_capacity(capacity: usize) -> Self {497		Self {498			sup: None,499			map: GcHashMap::with_capacity(capacity),500			assertions: Vec::new(),501			next_field_index: FieldIndex::default(),502		}503	}504	pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {505		self.assertions.reserve_exact(capacity);506		self507	}508	pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {509		self.sup = Some(super_obj);510		self511	}512513	pub fn assert(&mut self, assertion: TraceBox<dyn ObjectAssertion>) -> &mut Self {514		self.assertions.push(assertion);515		self516	}517	pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder<'_>> {518		let field_index = self.next_field_index;519		self.next_field_index = self.next_field_index.next();520		ObjMemberBuilder::new(ValueBuilder(self), name, field_index)521	}522523	pub fn build(self) -> ObjValue {524		ObjValue::new(self.sup, Cc::new(self.map), Cc::new(self.assertions))525	}526}527impl Default for ObjValueBuilder {528	fn default() -> Self {529		Self::with_capacity(0)530	}531}532533#[allow(clippy::module_name_repetitions)]534#[must_use = "value not added unless binding() was called"]535pub struct ObjMemberBuilder<Kind> {536	kind: Kind,537	name: IStr,538	add: bool,539	visibility: Visibility,540	original_index: FieldIndex,541	location: Option<ExprLocation>,542}543544#[allow(clippy::missing_const_for_fn)]545impl<Kind> ObjMemberBuilder<Kind> {546	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {547		Self {548			kind,549			name,550			original_index,551			add: false,552			visibility: Visibility::Normal,553			location: None,554		}555	}556557	pub const fn with_add(mut self, add: bool) -> Self {558		self.add = add;559		self560	}561	pub fn add(self) -> Self {562		self.with_add(true)563	}564	pub fn with_visibility(mut self, visibility: Visibility) -> Self {565		self.visibility = visibility;566		self567	}568	pub fn hide(self) -> Self {569		self.with_visibility(Visibility::Hidden)570	}571	pub fn with_location(mut self, location: ExprLocation) -> Self {572		self.location = Some(location);573		self574	}575	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {576		(577			self.kind,578			self.name,579			ObjMember {580				add: self.add,581				visibility: self.visibility,582				original_index: self.original_index,583				invoke: binding,584				location: self.location,585			},586		)587	}588}589590pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);591impl ObjMemberBuilder<ValueBuilder<'_>> {592	/// Inserts value, replacing if it is already defined593	pub fn value_unchecked(self, value: Val) {594		let (receiver, name, member) =595			self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));596		let entry = receiver.0.map.entry(name);597		entry.insert(member);598	}599600	pub fn value(self, value: Val) -> Result<()> {601		self.thunk(Thunk::evaluated(value))602	}603	pub fn thunk(self, value: Thunk<Val>) -> Result<()> {604		self.binding(MaybeUnbound::Bound(value))605	}606	pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) -> Result<()> {607		self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))608	}609	pub fn binding(self, binding: MaybeUnbound) -> Result<()> {610		let (receiver, name, member) = self.build_member(binding);611		let location = member.location.clone();612		let old = receiver.0.map.insert(name.clone(), member);613		if old.is_some() {614			State::push(615				CallLocation(location.as_ref()),616				|| format!("field <{}> initializtion", name.clone()),617				|| throw!(DuplicateFieldName(name.clone())),618			)?;619		}620		Ok(())621	}622}623624pub struct ExtendBuilder<'v>(&'v mut ObjValue);625impl ObjMemberBuilder<ExtendBuilder<'_>> {626	pub fn value(self, value: Val) {627		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));628	}629	pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {630		self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));631	}632	pub fn binding(self, binding: MaybeUnbound) {633		let (receiver, name, member) = self.build_member(binding);634		let new = receiver.0.clone();635		*receiver.0 = new.extend_with_raw_member(name, member);636	}637}
after · crates/jrsonnet-evaluator/src/obj.rs
1use std::{2	cell::RefCell,3	fmt::Debug,4	hash::{Hash, Hasher},5	ptr::addr_of,6};78use jrsonnet_gcmodule::{Cc, Trace, Weak};9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ExprLocation, Visibility};11use rustc_hash::FxHashMap;1213use crate::{14	error::{Error, ErrorKind::*},15	function::CallLocation,16	gc::{GcHashMap, GcHashSet, TraceBox},17	operator::evaluate_add_op,18	throw, MaybeUnbound, Result, State, Thunk, Unbound, Val,19};2021#[cfg(not(feature = "exp-preserve-order"))]22mod ordering {23	#![allow(24		// This module works as stub for preserve-order feature25		clippy::unused_self,26	)]2728	use jrsonnet_gcmodule::Trace;2930	#[derive(Clone, Copy, Default, Debug, Trace)]31	pub struct FieldIndex;32	impl FieldIndex {33		pub const fn next(self) -> Self {34			Self35		}36	}3738	#[derive(Clone, Copy, Default, Debug, Trace)]39	pub struct SuperDepth;40	impl SuperDepth {41		pub const fn deeper(self) -> Self {42			Self43		}44	}4546	#[derive(Clone, Copy)]47	pub struct FieldSortKey;48	impl FieldSortKey {49		pub const fn new(_: SuperDepth, _: FieldIndex) -> Self {50			Self51		}52	}53}5455#[cfg(feature = "exp-preserve-order")]56mod ordering {57	use std::cmp::Reverse;5859	use jrsonnet_gcmodule::Trace;6061	#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]62	pub struct FieldIndex(u32);63	impl FieldIndex {64		pub fn next(self) -> Self {65			Self(self.0 + 1)66		}67	}6869	#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]70	pub struct SuperDepth(u32);71	impl SuperDepth {72		pub fn deeper(self) -> Self {73			Self(self.0 + 1)74		}75	}7677	#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]78	pub struct FieldSortKey(Reverse<SuperDepth>, FieldIndex);79	impl FieldSortKey {80		pub fn new(depth: SuperDepth, index: FieldIndex) -> Self {81			Self(Reverse(depth), index)82		}83		pub fn collide(self, other: Self) -> Self {84			if self.0 .0 > other.0 .0 {85				self86			} else if self.0 .0 < other.0 .0 {87				other88			} else {89				unreachable!("object can't have two fields with same name")90			}91		}92	}93}9495use ordering::*;9697#[allow(clippy::module_name_repetitions)]98#[derive(Debug, Trace)]99pub struct ObjMember {100	pub add: bool,101	pub visibility: Visibility,102	original_index: FieldIndex,103	pub invoke: MaybeUnbound,104	pub location: Option<ExprLocation>,105}106107pub trait ObjectAssertion: Trace {108	fn run(&self, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;109}110111// Field => This112113#[derive(Trace)]114enum CacheValue {115	Cached(Val),116	NotFound,117	Pending,118	Errored(Error),119}120121#[allow(clippy::module_name_repetitions)]122#[derive(Trace)]123#[trace(tracking(force))]124pub struct ObjValueInternals {125	sup: Option<ObjValue>,126	this: Option<ObjValue>,127128	assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,129	assertions_ran: RefCell<GcHashSet<ObjValue>>,130	this_entries: Cc<GcHashMap<IStr, ObjMember>>,131	value_cache: RefCell<GcHashMap<(IStr, Option<WeakObjValue>), CacheValue>>,132}133134#[derive(Clone, Trace)]135pub struct WeakObjValue(#[trace(skip)] pub(crate) Weak<ObjValueInternals>);136137impl PartialEq for WeakObjValue {138	fn eq(&self, other: &Self) -> bool {139		Weak::ptr_eq(&self.0, &other.0)140	}141}142143impl Eq for WeakObjValue {}144impl Hash for WeakObjValue {145	fn hash<H: Hasher>(&self, hasher: &mut H) {146		// Safety: usize is POD147		let addr = unsafe { *std::ptr::addr_of!(self.0).cast() };148		hasher.write_usize(addr);149	}150}151152#[allow(clippy::module_name_repetitions)]153#[derive(Clone, Trace)]154pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);155impl Debug for ObjValue {156	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {157		if let Some(super_obj) = self.0.sup.as_ref() {158			if f.alternate() {159				write!(f, "{super_obj:#?}")?;160			} else {161				write!(f, "{super_obj:?}")?;162			}163			write!(f, " + ")?;164		}165		let mut debug = f.debug_struct("ObjValue");166		for (name, member) in self.0.this_entries.iter() {167			debug.field(name, member);168		}169		debug.finish_non_exhaustive()170	}171}172173impl ObjValue {174	pub fn new(175		sup: Option<Self>,176		this_entries: Cc<GcHashMap<IStr, ObjMember>>,177		assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,178	) -> Self {179		Self(Cc::new(ObjValueInternals {180			sup,181			this: None,182			assertions,183			assertions_ran: RefCell::new(GcHashSet::new()),184			this_entries,185			value_cache: RefCell::new(GcHashMap::new()),186		}))187	}188	pub fn new_empty() -> Self {189		Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))190	}191	#[must_use]192	pub fn extend_from(&self, sup: Self) -> Self {193		match &self.0.sup {194			None => Self::new(195				Some(sup),196				self.0.this_entries.clone(),197				self.0.assertions.clone(),198			),199			Some(v) => Self::new(200				Some(v.extend_from(sup)),201				self.0.this_entries.clone(),202				self.0.assertions.clone(),203			),204		}205	}206	pub(crate) fn extend_with_raw_member(self, key: IStr, value: ObjMember) -> Self {207		let mut new = GcHashMap::with_capacity(1);208		new.insert(key, value);209		Self::new(Some(self), Cc::new(new), Cc::new(Vec::new()))210	}211	pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder<'_>> {212		ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())213	}214215	#[must_use]216	pub fn with_this(&self, this: Self) -> Self {217		Self(Cc::new(ObjValueInternals {218			sup: self.0.sup.clone(),219			assertions: self.0.assertions.clone(),220			assertions_ran: RefCell::new(GcHashSet::new()),221			this: Some(this),222			this_entries: self.0.this_entries.clone(),223			value_cache: RefCell::new(GcHashMap::new()),224		}))225	}226227	pub fn len(&self) -> usize {228		self.fields_visibility()229			.into_iter()230			.filter(|(_, (visible, _))| *visible)231			.count()232	}233234	pub fn is_empty(&self) -> bool {235		if !self.0.this_entries.is_empty() {236			return false;237		}238		self.0.sup.as_ref().map_or(true, Self::is_empty)239	}240241	/// Run callback for every field found in object242	///243	/// Returns true if ended prematurely244	pub(crate) fn enum_fields(245		&self,246		depth: SuperDepth,247		handler: &mut impl FnMut(SuperDepth, &IStr, &ObjMember) -> bool,248	) -> bool {249		if let Some(s) = &self.0.sup {250			if s.enum_fields(depth.deeper(), handler) {251				return true;252			}253		}254		for (name, member) in self.0.this_entries.iter() {255			if handler(depth, name, member) {256				return true;257			}258		}259		false260	}261262	pub fn fields_visibility(&self) -> FxHashMap<IStr, (bool, FieldSortKey)> {263		let mut out = FxHashMap::default();264		self.enum_fields(SuperDepth::default(), &mut |depth, name, member| {265			let new_sort_key = FieldSortKey::new(depth, member.original_index);266			let entry = out.entry(name.clone());267			let (visible, _) = entry.or_insert((true, new_sort_key));268			match member.visibility {269				Visibility::Normal => {}270				Visibility::Hidden => {271					*visible = false;272				}273				Visibility::Unhide => {274					*visible = true;275				}276			};277			false278		});279		out280	}281	pub fn fields_ex(282		&self,283		include_hidden: bool,284		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,285	) -> Vec<IStr> {286		#[cfg(feature = "exp-preserve-order")]287		if preserve_order {288			let (mut fields, mut keys): (Vec<_>, Vec<_>) = self289				.fields_visibility()290				.into_iter()291				.filter(|(_, (visible, _))| include_hidden || *visible)292				.enumerate()293				.map(|(idx, (k, (_, sk)))| (k, (sk, idx)))294				.unzip();295			keys.sort_unstable_by_key(|v| v.0);296			// Reorder in-place by resulting indexes297			for i in 0..fields.len() {298				let x = fields[i].clone();299				let mut j = i;300				loop {301					let k = keys[j].1;302					keys[j].1 = j;303					if k == i {304						break;305					}306					fields[j] = fields[k].clone();307					j = k308				}309				fields[j] = x;310			}311			return fields;312		}313314		let mut fields: Vec<_> = self315			.fields_visibility()316			.into_iter()317			.filter(|(_, (visible, _))| include_hidden || *visible)318			.map(|(k, _)| k)319			.collect();320		fields.sort_unstable();321		fields322	}323	pub fn fields(&self, #[cfg(feature = "exp-preserve-order")] preserve_order: bool) -> Vec<IStr> {324		self.fields_ex(325			false,326			#[cfg(feature = "exp-preserve-order")]327			preserve_order,328		)329	}330331	pub fn field_visibility(&self, name: IStr) -> Option<Visibility> {332		if let Some(m) = self.0.this_entries.get(&name) {333			Some(match &m.visibility {334				Visibility::Normal => self335					.0336					.sup337					.as_ref()338					.and_then(|super_obj| super_obj.field_visibility(name))339					.unwrap_or(Visibility::Normal),340				v => *v,341			})342		} else if let Some(super_obj) = &self.0.sup {343			super_obj.field_visibility(name)344		} else {345			None346		}347	}348349	fn has_field_include_hidden(&self, name: IStr) -> bool {350		if self.0.this_entries.contains_key(&name) {351			true352		} else if let Some(super_obj) = &self.0.sup {353			super_obj.has_field_include_hidden(name)354		} else {355			false356		}357	}358359	pub fn has_field_ex(&self, name: IStr, include_hidden: bool) -> bool {360		if include_hidden {361			self.has_field_include_hidden(name)362		} else {363			self.has_field(name)364		}365	}366	pub fn has_field(&self, name: IStr) -> bool {367		self.field_visibility(name)368			.map_or(false, |v| v.is_visible())369	}370371	pub fn iter(372		&self,373		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,374	) -> impl Iterator<Item = (IStr, Result<Val>)> + '_ {375		let fields = self.fields(376			#[cfg(feature = "exp-preserve-order")]377			preserve_order,378		);379		fields.into_iter().map(|field| {380			(381				field.clone(),382				self.get(field)383					.map(|opt| opt.expect("iterating over keys, field exists")),384			)385		})386	}387388	pub fn get(&self, key: IStr) -> Result<Option<Val>> {389		self.run_assertions()?;390		let cache_key = (key.clone(), None);391		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {392			return Ok(match v {393				CacheValue::Cached(v) => Some(v.clone()),394				CacheValue::NotFound => None,395				CacheValue::Pending => throw!(InfiniteRecursionDetected),396				CacheValue::Errored(e) => return Err(e.clone()),397			});398		}399		self.0400			.value_cache401			.borrow_mut()402			.insert(cache_key.clone(), CacheValue::Pending);403		let value = self404			.get_raw(key, self.0.this.clone().unwrap_or_else(|| self.clone()))405			.map_err(|e| {406				self.0407					.value_cache408					.borrow_mut()409					.insert(cache_key.clone(), CacheValue::Errored(e.clone()));410				e411			})?;412		self.0.value_cache.borrow_mut().insert(413			cache_key,414			value415				.as_ref()416				.map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),417		);418		Ok(value)419	}420	pub fn get_for(&self, key: IStr, this: Self) -> Result<Option<Val>> {421		self.run_assertions()?;422		let cache_key = (key.clone(), Some(this.clone().downgrade()));423		if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {424			return Ok(match v {425				CacheValue::Cached(v) => Some(v.clone()),426				CacheValue::NotFound => None,427				CacheValue::Pending => throw!(InfiniteRecursionDetected),428				CacheValue::Errored(e) => return Err(e.clone()),429			});430		}431		self.0432			.value_cache433			.borrow_mut()434			.insert(cache_key.clone(), CacheValue::Pending);435		let value = self.get_raw(key, this).map_err(|e| {436			self.0437				.value_cache438				.borrow_mut()439				.insert(cache_key.clone(), CacheValue::Errored(e.clone()));440			e441		})?;442		self.0.value_cache.borrow_mut().insert(443			cache_key,444			value445				.as_ref()446				.map_or(CacheValue::NotFound, |v| CacheValue::Cached(v.clone())),447		);448		Ok(value)449	}450451	fn get_raw(&self, key: IStr, real_this: Self) -> Result<Option<Val>> {452		match (self.0.this_entries.get(&key), &self.0.sup) {453			(Some(k), None) => Ok(Some(self.evaluate_this(k, real_this)?)),454			(Some(k), Some(super_obj)) => {455				let our = self.evaluate_this(k, real_this.clone())?;456				if k.add {457					super_obj458						.get_raw(key, real_this)?459						.map_or(Ok(Some(our.clone())), |v| {460							Ok(Some(evaluate_add_op(&v, &our)?))461						})462				} else {463					Ok(Some(our))464				}465			}466			(None, Some(super_obj)) => super_obj.get_raw(key, real_this),467			(None, None) => Ok(None),468		}469	}470	fn evaluate_this(&self, v: &ObjMember, real_this: Self) -> Result<Val> {471		v.invoke.evaluate(self.0.sup.clone(), Some(real_this))472	}473474	fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {475		if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {476			for assertion in self.0.assertions.iter() {477				if let Err(e) = assertion.run(self.0.sup.clone(), Some(real_this.clone())) {478					self.0.assertions_ran.borrow_mut().remove(real_this);479					return Err(e);480				}481			}482			if let Some(super_obj) = &self.0.sup {483				super_obj.run_assertions_raw(real_this)?;484			}485		}486		Ok(())487	}488	pub fn run_assertions(&self) -> Result<()> {489		self.run_assertions_raw(self)490	}491492	pub fn ptr_eq(a: &Self, b: &Self) -> bool {493		Cc::ptr_eq(&a.0, &b.0)494	}495	pub fn downgrade(self) -> WeakObjValue {496		WeakObjValue(self.0.downgrade())497	}498}499500impl PartialEq for ObjValue {501	fn eq(&self, other: &Self) -> bool {502		Cc::ptr_eq(&self.0, &other.0)503	}504}505506impl Eq for ObjValue {}507impl Hash for ObjValue {508	fn hash<H: Hasher>(&self, hasher: &mut H) {509		hasher.write_usize(addr_of!(*self.0) as usize);510	}511}512513#[allow(clippy::module_name_repetitions)]514pub struct ObjValueBuilder {515	sup: Option<ObjValue>,516	map: GcHashMap<IStr, ObjMember>,517	assertions: Vec<TraceBox<dyn ObjectAssertion>>,518	next_field_index: FieldIndex,519}520impl ObjValueBuilder {521	pub fn new() -> Self {522		Self::with_capacity(0)523	}524	pub fn with_capacity(capacity: usize) -> Self {525		Self {526			sup: None,527			map: GcHashMap::with_capacity(capacity),528			assertions: Vec::new(),529			next_field_index: FieldIndex::default(),530		}531	}532	pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {533		self.assertions.reserve_exact(capacity);534		self535	}536	pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {537		self.sup = Some(super_obj);538		self539	}540541	pub fn assert(&mut self, assertion: TraceBox<dyn ObjectAssertion>) -> &mut Self {542		self.assertions.push(assertion);543		self544	}545	pub fn member(&mut self, name: IStr) -> ObjMemberBuilder<ValueBuilder<'_>> {546		let field_index = self.next_field_index;547		self.next_field_index = self.next_field_index.next();548		ObjMemberBuilder::new(ValueBuilder(self), name, field_index)549	}550551	pub fn build(self) -> ObjValue {552		ObjValue::new(self.sup, Cc::new(self.map), Cc::new(self.assertions))553	}554}555impl Default for ObjValueBuilder {556	fn default() -> Self {557		Self::with_capacity(0)558	}559}560561#[allow(clippy::module_name_repetitions)]562#[must_use = "value not added unless binding() was called"]563pub struct ObjMemberBuilder<Kind> {564	kind: Kind,565	name: IStr,566	add: bool,567	visibility: Visibility,568	original_index: FieldIndex,569	location: Option<ExprLocation>,570}571572#[allow(clippy::missing_const_for_fn)]573impl<Kind> ObjMemberBuilder<Kind> {574	pub(crate) fn new(kind: Kind, name: IStr, original_index: FieldIndex) -> Self {575		Self {576			kind,577			name,578			original_index,579			add: false,580			visibility: Visibility::Normal,581			location: None,582		}583	}584585	pub const fn with_add(mut self, add: bool) -> Self {586		self.add = add;587		self588	}589	pub fn add(self) -> Self {590		self.with_add(true)591	}592	pub fn with_visibility(mut self, visibility: Visibility) -> Self {593		self.visibility = visibility;594		self595	}596	pub fn hide(self) -> Self {597		self.with_visibility(Visibility::Hidden)598	}599	pub fn with_location(mut self, location: ExprLocation) -> Self {600		self.location = Some(location);601		self602	}603	fn build_member(self, binding: MaybeUnbound) -> (Kind, IStr, ObjMember) {604		(605			self.kind,606			self.name,607			ObjMember {608				add: self.add,609				visibility: self.visibility,610				original_index: self.original_index,611				invoke: binding,612				location: self.location,613			},614		)615	}616}617618pub struct ValueBuilder<'v>(&'v mut ObjValueBuilder);619impl ObjMemberBuilder<ValueBuilder<'_>> {620	/// Inserts value, replacing if it is already defined621	pub fn value_unchecked(self, value: Val) {622		let (receiver, name, member) =623			self.build_member(MaybeUnbound::Bound(Thunk::evaluated(value)));624		let entry = receiver.0.map.entry(name);625		entry.insert(member);626	}627628	pub fn value(self, value: Val) -> Result<()> {629		self.thunk(Thunk::evaluated(value))630	}631	pub fn thunk(self, value: Thunk<Val>) -> Result<()> {632		self.binding(MaybeUnbound::Bound(value))633	}634	pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) -> Result<()> {635		self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))636	}637	pub fn binding(self, binding: MaybeUnbound) -> Result<()> {638		let (receiver, name, member) = self.build_member(binding);639		let location = member.location.clone();640		let old = receiver.0.map.insert(name.clone(), member);641		if old.is_some() {642			State::push(643				CallLocation(location.as_ref()),644				|| format!("field <{}> initializtion", name.clone()),645				|| throw!(DuplicateFieldName(name.clone())),646			)?;647		}648		Ok(())649	}650}651652pub struct ExtendBuilder<'v>(&'v mut ObjValue);653impl ObjMemberBuilder<ExtendBuilder<'_>> {654	pub fn value(self, value: Val) {655		self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));656	}657	pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {658		self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));659	}660	pub fn binding(self, binding: MaybeUnbound) {661		let (receiver, name, member) = self.build_member(binding);662		let new = receiver.0.clone();663		*receiver.0 = new.extend_with_raw_member(name, member);664	}665}