git.delta.rocks / jrsonnet / refs/commits / 5f620e2b9aa7

difftreelog

refactor remove tla from state

Yaroslav Bolyukin2022-11-09parent: #7b01ecb.patch.diff
in: master

6 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
after · cmds/jrsonnet/src/main.rs
1use std::{2	fs::{create_dir_all, File},3	io::{Read, Write},4};56use clap::{CommandFactory, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{ConfigureState, GeneralOpts, ManifestOpts, OutputOpts, TraceOpts};9use jrsonnet_evaluator::{apply_tla, error::LocError, throw, ResultExt, State, Val};1011#[cfg(feature = "mimalloc")]12#[global_allocator]13static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1415#[derive(Parser)]16enum SubOpts {17	/// Generate completions for specified shell18	Generate {19		/// Target shell name20		shell: Shell,21	},22}2324#[derive(Parser)]25#[clap(next_help_heading = "DEBUG")]26struct DebugOpts {27	/// Required OS stack size.28	/// This shouldn't be changed unless jrsonnet is failing with stack overflow error.29	#[clap(long, name = "size")]30	pub os_stack: Option<usize>,31}3233#[derive(Parser)]34#[clap(next_help_heading = "INPUT")]35struct InputOpts {36	/// Treat input as code, evaluate them instead of reading file37	#[clap(long, short = 'e')]38	pub exec: bool,3940	/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself41	pub input: Option<String>,42}4344#[derive(Parser)]45#[clap(args_conflicts_with_subcommands = true, disable_version_flag = true)]46struct Opts {47	#[clap(subcommand)]48	sub: Option<SubOpts>,4950	#[clap(flatten)]51	input: InputOpts,52	#[clap(flatten)]53	general: GeneralOpts,54	#[clap(flatten)]55	manifest: ManifestOpts,56	#[clap(flatten)]57	output: OutputOpts,58	#[clap(flatten)]59	debug: DebugOpts,60}6162fn main() {63	let opts: Opts = Opts::parse();6465	if let Some(sub) = opts.sub {66		match sub {67			SubOpts::Generate { shell } => {68				use clap_complete::generate;69				let app = &mut Opts::command();70				let buf = &mut std::io::stdout();71				generate(shell, app, "jrsonnet", buf);72				std::process::exit(0)73			}74		}75	}7677	let success = if let Some(size) = opts.debug.os_stack {78		std::thread::Builder::new()79			.stack_size(size * 1024 * 1024)80			.spawn(|| main_catch(opts))81			.expect("new thread spawned")82			.join()83			.expect("thread finished successfully")84	} else {85		main_catch(opts)86	};87	if !success {88		std::process::exit(1);89	}90}9192#[derive(thiserror::Error, Debug)]93enum Error {94	// Handled differently95	#[error("evaluation error")]96	Evaluation(jrsonnet_evaluator::error::LocError),97	#[error("io error")]98	Io(#[from] std::io::Error),99	#[error("input is not utf8 encoded")]100	Utf8(#[from] std::str::Utf8Error),101	#[error("missing input argument")]102	MissingInputArgument,103}104impl From<LocError> for Error {105	fn from(e: LocError) -> Self {106		Self::Evaluation(e)107	}108}109110fn main_catch(opts: Opts) -> bool {111	let s = State::default();112	if let Err(e) = main_real(&s, opts) {113		if let Error::Evaluation(e) = e {114			eprintln!("{}", s.stringify_err(&e));115		} else {116			eprintln!("{}", e);117		}118		return false;119	}120	true121}122123fn main_real(s: &State, opts: Opts) -> Result<(), Error> {124	let (_stack_guard, tla, _gc_guard) = opts.general.configure(s)?;125	let manifest_format = opts.manifest.configure(s)?;126127	let input = opts.input.input.ok_or(Error::MissingInputArgument)?;128	let val = if opts.input.exec {129		s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?130	} else if input == "-" {131		let mut input = Vec::new();132		std::io::stdin().read_to_end(&mut input)?;133		let input_str = std::str::from_utf8(&input)?;134		s.evaluate_snippet("<stdin>".to_owned(), input_str)?135	} else {136		s.import(&input)?137	};138139	let val = apply_tla(s.clone(), &tla, val)?;140141	if let Some(multi) = opts.output.multi {142		if opts.output.create_output_dirs {143			let mut dir = multi.clone();144			dir.pop();145			create_dir_all(dir)?;146		}147		for (file, data) in s.manifest_multi(val)?.iter() {148			let mut path = multi.clone();149			path.push(file as &str);150			if opts.output.create_output_dirs {151				let mut dir = path.clone();152				dir.pop();153				create_dir_all(dir)?;154			}155			println!("{}", path.to_str().expect("path"));156			let mut file = File::create(path)?;157			writeln!(file, "{}", data)?;158		}159	} else if let Some(path) = opts.output.output_file {160		if opts.output.create_output_dirs {161			let mut dir = path.clone();162			dir.pop();163			create_dir_all(dir)?;164		}165		let mut file = File::create(path)?;166		writeln!(file, "{}", s.manifest(val)?)?;167	} else {168		let output = s.manifest(val)?;169		if !output.is_empty() {170			println!("{}", output);171		}172	}173174	Ok(())175}
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -71,7 +71,7 @@
 	misc: MiscOpts,
 
 	#[clap(flatten)]
-	tla: TLAOpts,
+	tla: TlaOpts,
 	#[clap(flatten)]
 	std: StdOpts,
 
@@ -85,16 +85,17 @@
 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
 		self.trace.configure(s)?;
 		let misc_guards = self.misc.configure(s)?;
-		self.tla.configure(s)?;
+		let tla_guards = self.tla.configure(s)?;
 		self.std.configure(s)?;
 		let gc_guards = self.gc.configure(s)?;
-		Ok((misc_guards, gc_guards))
+		Ok((misc_guards, tla_guards, gc_guards))
 	}
 }
 
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,11 +1,17 @@
 use clap::Parser;
-use jrsonnet_evaluator::{error::Result, State};
+use jrsonnet_evaluator::{
+	error::{Error, Result},
+	function::TlaArg,
+	gc::GcHashMap,
+	IStr, State,
+};
+use jrsonnet_parser::{ParserSettings, Source};
 
 use crate::{ConfigureState, ExtFile, ExtStr};
 
 #[derive(Parser)]
 #[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
-pub struct TLAOpts {
+pub struct TlaOpts {
 	/// Add top level string argument.
 	/// Top level arguments will be passed to function before manifestification stage.
 	/// This is preferred to ExtVars method.
@@ -25,21 +31,41 @@
 	#[clap(long, name = "name=tla code path", number_of_values = 1)]
 	tla_code_file: Vec<ExtFile>,
 }
-impl ConfigureState for TLAOpts {
-	type Guards = ();
-	fn configure(&self, s: &State) -> Result<()> {
-		for tla in self.tla_str.iter() {
-			s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into());
+impl ConfigureState for TlaOpts {
+	type Guards = GcHashMap<IStr, TlaArg>;
+	fn configure(&self, _s: &State) -> Result<Self::Guards> {
+		let mut out = GcHashMap::new();
+		for (name, value) in self
+			.tla_str
+			.iter()
+			.map(|c| (&c.name, &c.value))
+			.chain(self.tla_str_file.iter().map(|c| (&c.name, &c.value)))
+		{
+			out.insert(name.into(), TlaArg::String(value.into()));
 		}
-		for tla in self.tla_str_file.iter() {
-			s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into())
-		}
-		for tla in self.tla_code.iter() {
-			s.add_tla_code((&tla.name as &str).into(), &tla.value as &str)?;
-		}
-		for tla in self.tla_code_file.iter() {
-			s.add_tla_code((&tla.name as &str).into(), &tla.value as &str)?;
+		for (name, code) in self
+			.tla_code
+			.iter()
+			.map(|c| (&c.name, &c.value))
+			.chain(self.tla_code_file.iter().map(|c| (&c.name, &c.value)))
+		{
+			let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
+			out.insert(
+				(&name as &str).into(),
+				TlaArg::Code(
+					jrsonnet_parser::parse(
+						&code,
+						&ParserSettings {
+							source: source.clone(),
+						},
+					)
+					.map_err(|e| Error::ImportSyntaxError {
+						path: source,
+						error: Box::new(e),
+					})?,
+				),
+			);
 		}
-		Ok(())
+		Ok(out)
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -1,10 +1,11 @@
-use std::collections::HashMap;
-
+use hashbrown::HashMap;
 use jrsonnet_gcmodule::Trace;
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ArgsDesc, LocExpr};
 
-use crate::{error::Result, evaluate, tb, typed::Typed, val::ThunkValue, Context, Thunk, Val};
+use crate::{
+	error::Result, evaluate, gc::GcHashMap, tb, typed::Typed, val::ThunkValue, Context, Thunk, Val,
+};
 
 /// Marker for arguments, which can be evaluated with context set to None
 pub trait OptionalContext {}
@@ -214,6 +215,34 @@
 }
 impl<A, S> OptionalContext for HashMap<IStr, A, S> where A: ArgLike + OptionalContext {}
 
+impl<A: ArgLike> ArgsLike for GcHashMap<IStr, A> {
+	fn unnamed_len(&self) -> usize {
+		self.0.unnamed_len()
+	}
+
+	fn unnamed_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
+	) -> Result<()> {
+		self.0.unnamed_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_iter(
+		&self,
+		ctx: Context,
+		tailstrict: bool,
+		handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
+	) -> Result<()> {
+		self.0.named_iter(ctx, tailstrict, handler)
+	}
+
+	fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
+		self.0.named_names(handler)
+	}
+}
+
 macro_rules! impl_args_like {
 	($count:expr; $($gen:ident)*) => {
 		impl<$($gen: ArgLike,)*> sealed::Unnamed for ($($gen,)*) {}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -55,6 +55,7 @@
 mod obj;
 pub mod stack;
 pub mod stdlib;
+mod tla;
 pub mod trace;
 pub mod typed;
 pub mod val;
@@ -81,7 +82,7 @@
 use jrsonnet_parser::*;
 pub use obj::*;
 use stack::check_depth;
-use trace::{CompactFormat, TraceFormat};
+pub use tla::apply_tla;
 pub use val::{ManifestFormat, Thunk, Val};
 
 /// Thunk without bound `super`/`this`
@@ -143,10 +144,6 @@
 /// Dynamically reconfigurable evaluation settings
 #[derive(Trace)]
 pub struct EvaluationSettings {
-	/// Limits amount of stack trace items preserved
-	pub max_trace: usize,
-	/// TLA vars
-	pub tla_vars: HashMap<IStr, TlaArg>,
 	/// Context initializer, which will be used for imports and everything
 	/// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`
 	pub context_initializer: TraceBox<dyn ContextInitializer>,
@@ -160,19 +157,8 @@
 impl Default for EvaluationSettings {
 	fn default() -> Self {
 		Self {
-			max_trace: 20,
 			context_initializer: tb!(DummyContextInitializer),
-			tla_vars: HashMap::default(),
 			import_resolver: tb!(DummyImportResolver),
-			manifest_format: ManifestFormat::Json {
-				padding: 4,
-				#[cfg(feature = "exp-preserve-order")]
-				preserve_order: false,
-			},
-			trace_format: tb!(CompactFormat {
-				padding: 4,
-				resolver: trace::PathResolver::Absolute,
-			}),
 		}
 	}
 }
@@ -404,51 +390,6 @@
 		let _guard = check_depth()?;
 
 		f().with_description(frame_desc)
-	}
-
-	/// # Panics
-	/// In case of formatting failure
-	pub fn stringify_err(&self, e: &LocError) -> String {
-		let mut out = String::new();
-		self.settings()
-			.trace_format
-			.write_trace(&mut out, self, e)
-			.unwrap();
-		out
-	}
-
-	pub fn manifest(&self, val: Val) -> Result<IStr> {
-		Self::push_description(
-			|| "manifestification".to_string(),
-			|| val.manifest(&self.manifest_format()),
-		)
-	}
-	pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {
-		val.manifest_multi(&self.manifest_format())
-	}
-	pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {
-		val.manifest_stream(&self.manifest_format())
-	}
-
-	/// If passed value is function then call with set TLA
-	pub fn with_tla(&self, val: Val) -> Result<Val> {
-		Ok(match val {
-			Val::Func(func) => State::push_description(
-				|| "during TLA call".to_owned(),
-				|| {
-					func.evaluate(
-						self.create_default_context(Source::new_virtual(
-							"<tla>".into(),
-							IStr::empty(),
-						)),
-						CallLocation::native(),
-						&self.settings().tla_vars,
-						true,
-					)
-				},
-			)?,
-			v => v,
-		})
 	}
 }
 
@@ -487,35 +428,6 @@
 
 /// Settings utilities
 impl State {
-	pub fn add_tla(&self, name: IStr, value: Val) {
-		self.settings_mut()
-			.tla_vars
-			.insert(name, TlaArg::Val(value));
-	}
-	pub fn add_tla_str(&self, name: IStr, value: IStr) {
-		self.settings_mut()
-			.tla_vars
-			.insert(name, TlaArg::String(value));
-	}
-	pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
-		let source_name = format!("<top-level-arg:{name}>");
-		let source = Source::new_virtual(source_name.into(), code.into());
-		let parsed = jrsonnet_parser::parse(
-			code,
-			&ParserSettings {
-				file_name: source.clone(),
-			},
-		)
-		.map_err(|e| ImportSyntaxError {
-			path: source,
-			error: Box::new(e),
-		})?;
-		self.settings_mut()
-			.tla_vars
-			.insert(name, TlaArg::Code(parsed));
-		Ok(())
-	}
-
 	// Only panics in case of [`ImportResolver`] contract violation
 	#[allow(clippy::missing_panics_doc)]
 	pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
addedcrates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -0,0 +1,25 @@
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::Source;
+
+use crate::{
+	function::{ArgsLike, CallLocation},
+	Result, State, Val,
+};
+
+pub fn apply_tla<A: ArgsLike>(s: State, args: &A, val: Val) -> Result<Val> {
+	Ok(if let Val::Func(func) = val {
+		State::push_description(
+			|| "during TLA call".to_owned(),
+			|| {
+				func.evaluate(
+					s.create_default_context(Source::new_virtual("<top-level-arg>".into(), IStr::empty())),
+					CallLocation::native(),
+					args,
+					false,
+				)
+			},
+		)?
+	} else {
+		val
+	})
+}