difftreelog
feat --exp-apply argument
in: master
3 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -8,7 +8,7 @@
edition = "2021"
[features]
-experimental = ["exp-preserve-order", "exp-destruct"]
+experimental = ["exp-preserve-order", "exp-destruct", "exp-null-coaelse", "exp-object-iteration", "exp-bigint", "exp-apply"]
# Use mimalloc as allocator
mimalloc = ["mimallocator"]
# Experimental feature, which allows to preserve order of object fields
@@ -22,6 +22,10 @@
exp-object-iteration = ["jrsonnet-evaluator/exp-object-iteration"]
# Bigint type
exp-bigint = ["jrsonnet-evaluator/exp-bigint", "jrsonnet-cli/exp-bigint"]
+# obj?.field, obj?.['field']
+exp-null-coaelse = ["jrsonnet-evaluator/exp-null-coaelse", "jrsonnet-parser/exp-null-coaelse"]
+# --exp-apply
+exp-apply = []
# std.thisFile support
legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -43,6 +43,12 @@
/// Path to the file to be compiled if `--evaluate` is unset, otherwise code itself
pub input: Option<String>,
+
+ /// After executing input, apply specified code.
+ /// Output of the initial input will be accessible using `$`
+ #[cfg(feature = "exp-apply")]
+ #[clap(long)]
+ pub exp_apply: Vec<String>,
}
/// Jsonnet commandline interpreter (Rust implementation)
@@ -181,7 +187,18 @@
};
let tla = opts.tla.tla_opts()?;
- let val = apply_tla(s.clone(), &tla, val)?;
+ #[allow(unused_mut)]
+ let mut val = apply_tla(s.clone(), &tla, val)?;
+
+ #[cfg(feature = "exp-apply")]
+ for apply in opts.input.exp_apply {
+ use jrsonnet_evaluator::{InitialUnderscore, Thunk};
+ val = s.evaluate_snippet_with(
+ "<exp_apply>".to_owned(),
+ &apply,
+ InitialUnderscore(Thunk::evaluated(val)),
+ )?;
+ }
let manifest_format = opts.manifest.manifest_format();
if let Some(multi) = opts.output.multi {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5 clippy::all,6 clippy::nursery,7 clippy::pedantic,8 // missing_docs,9 elided_lifetimes_in_paths,10 explicit_outlives_requirements,11 noop_method_call,12 single_use_lifetimes,13 variant_size_differences,14 rustdoc::all15)]16#![allow(17 macro_expanded_macro_exports_accessed_by_absolute_paths,18 clippy::ptr_arg,19 // Too verbose20 clippy::must_use_candidate,21 // A lot of functions pass around errors thrown by code22 clippy::missing_errors_doc,23 // A lot of pointers have interior Rc24 clippy::needless_pass_by_value,25 // Its fine26 clippy::wildcard_imports,27 clippy::enum_glob_use,28 clippy::module_name_repetitions,29 // TODO: fix individual issues, however this works as intended almost everywhere30 clippy::cast_precision_loss,31 clippy::cast_possible_wrap,32 clippy::cast_possible_truncation,33 clippy::cast_sign_loss,34 // False positives35 // https://github.com/rust-lang/rust-clippy/issues/690236 clippy::use_self,37 // https://github.com/rust-lang/rust-clippy/issues/853938 clippy::iter_with_drain,39 clippy::type_repetition_in_bounds,40 // ci is being run with nightly, but library should work on stable41 clippy::missing_const_for_fn,42)]4344// For jrsonnet-macros45extern crate self as jrsonnet_evaluator;4647mod arr;48#[cfg(feature = "async-import")]49pub mod async_import;50mod ctx;51mod dynamic;52pub mod error;53mod evaluate;54pub mod function;55pub mod gc;56mod import;57mod integrations;58pub mod manifest;59mod map;60mod obj;61pub mod stack;62pub mod stdlib;63mod tla;64pub mod trace;65pub mod typed;66pub mod val;6768use std::{69 any::Any,70 cell::{Ref, RefCell, RefMut},71 fmt::{self, Debug},72 path::Path,73};7475pub use ctx::*;76pub use dynamic::*;77pub use error::{Error, ErrorKind::*, Result, ResultExt};78pub use evaluate::*;79use function::CallLocation;80use gc::{GcHashMap, TraceBox};81use hashbrown::hash_map::RawEntryMut;82pub use import::*;83use jrsonnet_gcmodule::{Cc, Trace};84pub use jrsonnet_interner::{IBytes, IStr};85pub use jrsonnet_parser as parser;86use jrsonnet_parser::*;87pub use obj::*;88use stack::check_depth;89pub use tla::apply_tla;90pub use val::{Thunk, Val};9192/// Thunk without bound `super`/`this`93/// object inheritance may be overriden multiple times, and will be fixed only on field read94pub trait Unbound: Trace {95 /// Type of value after object context is bound96 type Bound;97 /// Create value bound to specified object context98 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;99}100101/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code102/// Standard jsonnet fields are always unbound103#[derive(Clone, Trace)]104pub enum MaybeUnbound {105 /// Value needs to be bound to `this`/`super`106 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),107 /// Value is object-independent108 Bound(Thunk<Val>),109}110111impl Debug for MaybeUnbound {112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {113 write!(f, "MaybeUnbound")114 }115}116impl MaybeUnbound {117 /// Attach object context to value, if required118 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {119 match self {120 Self::Unbound(v) => v.bind(sup, this),121 Self::Bound(v) => Ok(v.evaluate()?),122 }123 }124}125126/// During import, this trait will be called to create initial context for file.127/// It may initialize global variables, stdlib for example.128pub trait ContextInitializer: Trace {129 /// For which size the builder should be preallocated130 fn reserve_vars(&self) -> usize {131 0132 }133 /// Initialize default file context.134 /// Has default implementation, which calls `populate`.135 /// Prefer to always implement `populate` instead.136 fn initialize(&self, state: State, for_file: Source) -> Context {137 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());138 self.populate(for_file, &mut builder);139 builder.build()140 }141 /// For composability: extend builder. May panic if this initialization is not supported,142 /// and the context may only be created via `initialize`.143 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);144 /// Allows upcasting from abstract to concrete context initializer.145 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.146 fn as_any(&self) -> &dyn Any;147}148149/// Context initializer which adds nothing.150impl ContextInitializer for () {151 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}152 fn as_any(&self) -> &dyn Any {153 self154 }155}156157/// Dynamically reconfigurable evaluation settings158#[derive(Trace)]159pub struct EvaluationSettings {160 /// Context initializer, which will be used for imports and everything161 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`162 pub context_initializer: TraceBox<dyn ContextInitializer>,163 /// Used to resolve file locations/contents164 pub import_resolver: TraceBox<dyn ImportResolver>,165}166impl Default for EvaluationSettings {167 fn default() -> Self {168 Self {169 context_initializer: tb!(()),170 import_resolver: tb!(DummyImportResolver),171 }172 }173}174175#[derive(Trace)]176struct FileData {177 string: Option<IStr>,178 bytes: Option<IBytes>,179 parsed: Option<LocExpr>,180 evaluated: Option<Val>,181182 evaluating: bool,183}184impl FileData {185 fn new_string(data: IStr) -> Self {186 Self {187 string: Some(data),188 bytes: None,189 parsed: None,190 evaluated: None,191 evaluating: false,192 }193 }194 fn new_bytes(data: IBytes) -> Self {195 Self {196 string: None,197 bytes: Some(data),198 parsed: None,199 evaluated: None,200 evaluating: false,201 }202 }203 pub(crate) fn get_string(&mut self) -> Option<IStr> {204 if self.string.is_none() {205 self.string = Some(206 self.bytes207 .as_ref()208 .expect("either string or bytes should be set")209 .clone()210 .cast_str()?,211 );212 }213 Some(self.string.clone().expect("just set"))214 }215}216217#[derive(Default, Trace)]218pub struct EvaluationStateInternals {219 /// Internal state220 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,221 /// Settings, safe to change at runtime222 settings: RefCell<EvaluationSettings>,223}224225/// Maintains stack trace and import resolution226#[derive(Default, Clone, Trace)]227pub struct State(Cc<EvaluationStateInternals>);228229impl State {230 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise231 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {232 let mut file_cache = self.file_cache();233 let mut file = file_cache.raw_entry_mut().from_key(&path);234235 let file = match file {236 RawEntryMut::Occupied(ref mut d) => d.get_mut(),237 RawEntryMut::Vacant(v) => {238 let data = self.settings().import_resolver.load_file_contents(&path)?;239 v.insert(240 path.clone(),241 FileData::new_string(242 std::str::from_utf8(&data)243 .map_err(|_| ImportBadFileUtf8(path.clone()))?244 .into(),245 ),246 )247 .1248 }249 };250 Ok(file251 .get_string()252 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)253 }254 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise255 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {256 let mut file_cache = self.file_cache();257 let mut file = file_cache.raw_entry_mut().from_key(&path);258259 let file = match file {260 RawEntryMut::Occupied(ref mut d) => d.get_mut(),261 RawEntryMut::Vacant(v) => {262 let data = self.settings().import_resolver.load_file_contents(&path)?;263 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))264 .1265 }266 };267 if let Some(str) = &file.bytes {268 return Ok(str.clone());269 }270 if file.bytes.is_none() {271 file.bytes = Some(272 file.string273 .as_ref()274 .expect("either string or bytes should be set")275 .clone()276 .cast_bytes(),277 );278 }279 Ok(file.bytes.as_ref().expect("just set").clone())280 }281 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise282 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {283 let mut file_cache = self.file_cache();284 let mut file = file_cache.raw_entry_mut().from_key(&path);285286 let file = match file {287 RawEntryMut::Occupied(ref mut d) => d.get_mut(),288 RawEntryMut::Vacant(v) => {289 let data = self.settings().import_resolver.load_file_contents(&path)?;290 v.insert(291 path.clone(),292 FileData::new_string(293 std::str::from_utf8(&data)294 .map_err(|_| ImportBadFileUtf8(path.clone()))?295 .into(),296 ),297 )298 .1299 }300 };301 if let Some(val) = &file.evaluated {302 return Ok(val.clone());303 }304 let code = file305 .get_string()306 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;307 let file_name = Source::new(path.clone(), code.clone());308 if file.parsed.is_none() {309 file.parsed = Some(310 jrsonnet_parser::parse(311 &code,312 &ParserSettings {313 source: file_name.clone(),314 },315 )316 .map_err(|e| ImportSyntaxError {317 path: file_name.clone(),318 error: Box::new(e),319 })?,320 );321 }322 let parsed = file.parsed.as_ref().expect("just set").clone();323 if file.evaluating {324 throw!(InfiniteRecursionDetected)325 }326 file.evaluating = true;327 // Dropping file cache guard here, as evaluation may use this map too328 drop(file_cache);329 let res = evaluate(self.create_default_context(file_name), &parsed);330331 let mut file_cache = self.file_cache();332 let mut file = file_cache.raw_entry_mut().from_key(&path);333334 let RawEntryMut::Occupied(file) = &mut file else {335 unreachable!("this file was just here!")336 };337 let file = file.get_mut();338 file.evaluating = false;339 match res {340 Ok(v) => {341 file.evaluated = Some(v.clone());342 Ok(v)343 }344 Err(e) => Err(e),345 }346 }347348 /// Has same semantics as `import 'path'` called from `from` file349 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {350 let resolved = self.resolve_from(from, path)?;351 self.import_resolved(resolved)352 }353 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {354 let resolved = self.resolve(path)?;355 self.import_resolved(resolved)356 }357358 /// Creates context with all passed global variables359 pub fn create_default_context(&self, source: Source) -> Context {360 let context_initializer = &self.settings().context_initializer;361 context_initializer.initialize(self.clone(), source)362 }363364 /// Executes code creating a new stack frame365 pub fn push<T>(366 e: CallLocation<'_>,367 frame_desc: impl FnOnce() -> String,368 f: impl FnOnce() -> Result<T>,369 ) -> Result<T> {370 let _guard = check_depth()?;371372 f().with_description_src(e, frame_desc)373 }374375 /// Executes code creating a new stack frame376 pub fn push_val(377 &self,378 e: &ExprLocation,379 frame_desc: impl FnOnce() -> String,380 f: impl FnOnce() -> Result<Val>,381 ) -> Result<Val> {382 let _guard = check_depth()?;383384 f().with_description_src(e, frame_desc)385 }386 /// Executes code creating a new stack frame387 pub fn push_description<T>(388 frame_desc: impl FnOnce() -> String,389 f: impl FnOnce() -> Result<T>,390 ) -> Result<T> {391 let _guard = check_depth()?;392393 f().with_description(frame_desc)394 }395}396397/// Internals398impl State {399 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {400 self.0.file_cache.borrow_mut()401 }402 pub fn settings(&self) -> Ref<'_, EvaluationSettings> {403 self.0.settings.borrow()404 }405 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {406 self.0.settings.borrow_mut()407 }408 pub fn add_global(&self, name: IStr, value: Thunk<Val>) {409 #[derive(Trace)]410 struct GlobalsCtx {411 globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,412 inner: TraceBox<dyn ContextInitializer>,413 }414 impl ContextInitializer for GlobalsCtx {415 fn reserve_vars(&self) -> usize {416 self.inner.reserve_vars() + self.globals.borrow().len()417 }418 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {419 self.inner.populate(for_file, builder);420 for (name, val) in self.globals.borrow().iter() {421 builder.bind(name.clone(), val.clone());422 }423 }424425 fn as_any(&self) -> &dyn Any {426 self427 }428 }429 let mut settings = self.settings_mut();430 let initializer = &mut settings.context_initializer;431 match initializer.as_any().downcast_ref::<GlobalsCtx>() {432 Some(glob) => {433 glob.globals.borrow_mut().insert(name, value);434 }435 None => {436 let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));437 settings.context_initializer = tb!(GlobalsCtx {438 globals: {439 let mut out = GcHashMap::with_capacity(1);440 out.insert(name, value);441 RefCell::new(out)442 },443 inner444 })445 }446 }447 }448}449450/// Raw methods evaluate passed values but don't perform TLA execution451impl State {452 /// Parses and evaluates the given snippet453 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {454 let code = code.into();455 let source = Source::new_virtual(name.into(), code.clone());456 let parsed = jrsonnet_parser::parse(457 &code,458 &ParserSettings {459 source: source.clone(),460 },461 )462 .map_err(|e| ImportSyntaxError {463 path: source.clone(),464 error: Box::new(e),465 })?;466 evaluate(self.create_default_context(source), &parsed)467 }468}469470/// Settings utilities471impl State {472 // Only panics in case of [`ImportResolver`] contract violation473 #[allow(clippy::missing_panics_doc)]474 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {475 self.import_resolver().resolve_from(from, path.as_ref())476 }477478 // Only panics in case of [`ImportResolver`] contract violation479 #[allow(clippy::missing_panics_doc)]480 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {481 self.import_resolver().resolve(path.as_ref())482 }483 pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {484 Ref::map(self.settings(), |s| &*s.import_resolver)485 }486 pub fn set_import_resolver(&self, resolver: impl ImportResolver) {487 self.settings_mut().import_resolver = tb!(resolver);488 }489 pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {490 Ref::map(self.settings(), |s| &*s.context_initializer)491 }492 pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {493 self.settings_mut().context_initializer = tb!(initializer);494 }495}1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5 clippy::all,6 clippy::nursery,7 clippy::pedantic,8 // missing_docs,9 elided_lifetimes_in_paths,10 explicit_outlives_requirements,11 noop_method_call,12 single_use_lifetimes,13 variant_size_differences,14 rustdoc::all15)]16#![allow(17 macro_expanded_macro_exports_accessed_by_absolute_paths,18 clippy::ptr_arg,19 // Too verbose20 clippy::must_use_candidate,21 // A lot of functions pass around errors thrown by code22 clippy::missing_errors_doc,23 // A lot of pointers have interior Rc24 clippy::needless_pass_by_value,25 // Its fine26 clippy::wildcard_imports,27 clippy::enum_glob_use,28 clippy::module_name_repetitions,29 // TODO: fix individual issues, however this works as intended almost everywhere30 clippy::cast_precision_loss,31 clippy::cast_possible_wrap,32 clippy::cast_possible_truncation,33 clippy::cast_sign_loss,34 // False positives35 // https://github.com/rust-lang/rust-clippy/issues/690236 clippy::use_self,37 // https://github.com/rust-lang/rust-clippy/issues/853938 clippy::iter_with_drain,39 clippy::type_repetition_in_bounds,40 // ci is being run with nightly, but library should work on stable41 clippy::missing_const_for_fn,42)]4344// For jrsonnet-macros45extern crate self as jrsonnet_evaluator;4647mod arr;48#[cfg(feature = "async-import")]49pub mod async_import;50mod ctx;51mod dynamic;52pub mod error;53mod evaluate;54pub mod function;55pub mod gc;56mod import;57mod integrations;58pub mod manifest;59mod map;60mod obj;61pub mod stack;62pub mod stdlib;63mod tla;64pub mod trace;65pub mod typed;66pub mod val;6768use std::{69 any::Any,70 cell::{Ref, RefCell, RefMut},71 fmt::{self, Debug},72 path::Path,73};7475pub use ctx::*;76pub use dynamic::*;77pub use error::{Error, ErrorKind::*, Result, ResultExt};78pub use evaluate::*;79use function::CallLocation;80use gc::{GcHashMap, TraceBox};81use hashbrown::hash_map::RawEntryMut;82pub use import::*;83use jrsonnet_gcmodule::{Cc, Trace};84pub use jrsonnet_interner::{IBytes, IStr};85pub use jrsonnet_parser as parser;86use jrsonnet_parser::*;87pub use obj::*;88use stack::check_depth;89pub use tla::apply_tla;90pub use val::{Thunk, Val};9192/// Thunk without bound `super`/`this`93/// object inheritance may be overriden multiple times, and will be fixed only on field read94pub trait Unbound: Trace {95 /// Type of value after object context is bound96 type Bound;97 /// Create value bound to specified object context98 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;99}100101/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code102/// Standard jsonnet fields are always unbound103#[derive(Clone, Trace)]104pub enum MaybeUnbound {105 /// Value needs to be bound to `this`/`super`106 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),107 /// Value is object-independent108 Bound(Thunk<Val>),109}110111impl Debug for MaybeUnbound {112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {113 write!(f, "MaybeUnbound")114 }115}116impl MaybeUnbound {117 /// Attach object context to value, if required118 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {119 match self {120 Self::Unbound(v) => v.bind(sup, this),121 Self::Bound(v) => Ok(v.evaluate()?),122 }123 }124}125126/// During import, this trait will be called to create initial context for file.127/// It may initialize global variables, stdlib for example.128pub trait ContextInitializer: Trace {129 /// For which size the builder should be preallocated130 fn reserve_vars(&self) -> usize {131 0132 }133 /// Initialize default file context.134 /// Has default implementation, which calls `populate`.135 /// Prefer to always implement `populate` instead.136 fn initialize(&self, state: State, for_file: Source) -> Context {137 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());138 self.populate(for_file, &mut builder);139 builder.build()140 }141 /// For composability: extend builder. May panic if this initialization is not supported,142 /// and the context may only be created via `initialize`.143 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);144 /// Allows upcasting from abstract to concrete context initializer.145 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.146 fn as_any(&self) -> &dyn Any;147}148149/// Context initializer which adds nothing.150impl ContextInitializer for () {151 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}152 fn as_any(&self) -> &dyn Any {153 self154 }155}156157macro_rules! impl_context_initializer {158 ($($gen:ident)*) => {159 #[allow(non_snake_case)]160 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {161 fn reserve_vars(&self) -> usize {162 let mut out = 0;163 let ($($gen,)*) = self;164 $(out += $gen.reserve_vars();)*165 out166 }167 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {168 let ($($gen,)*) = self;169 $($gen.populate(for_file.clone(), builder);)*170 }171 fn as_any(&self) -> &dyn Any {172 self173 }174 }175 };176 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {177 impl_context_initializer!($($cur)*);178 impl_context_initializer!($($cur)* $c @ $($rest)*);179 };180 ($($cur:ident)* @) => {181 impl_context_initializer!($($cur)*);182 }183}184impl_context_initializer! {185 A B @ C D E186}187188/// Dynamically reconfigurable evaluation settings189#[derive(Trace)]190pub struct EvaluationSettings {191 /// Context initializer, which will be used for imports and everything192 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`193 pub context_initializer: TraceBox<dyn ContextInitializer>,194 /// Used to resolve file locations/contents195 pub import_resolver: TraceBox<dyn ImportResolver>,196}197impl Default for EvaluationSettings {198 fn default() -> Self {199 Self {200 context_initializer: tb!(()),201 import_resolver: tb!(DummyImportResolver),202 }203 }204}205206#[derive(Trace)]207struct FileData {208 string: Option<IStr>,209 bytes: Option<IBytes>,210 parsed: Option<LocExpr>,211 evaluated: Option<Val>,212213 evaluating: bool,214}215impl FileData {216 fn new_string(data: IStr) -> Self {217 Self {218 string: Some(data),219 bytes: None,220 parsed: None,221 evaluated: None,222 evaluating: false,223 }224 }225 fn new_bytes(data: IBytes) -> Self {226 Self {227 string: None,228 bytes: Some(data),229 parsed: None,230 evaluated: None,231 evaluating: false,232 }233 }234 pub(crate) fn get_string(&mut self) -> Option<IStr> {235 if self.string.is_none() {236 self.string = Some(237 self.bytes238 .as_ref()239 .expect("either string or bytes should be set")240 .clone()241 .cast_str()?,242 );243 }244 Some(self.string.clone().expect("just set"))245 }246}247248#[derive(Default, Trace)]249pub struct EvaluationStateInternals {250 /// Internal state251 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,252 /// Settings, safe to change at runtime253 settings: RefCell<EvaluationSettings>,254}255256/// Maintains stack trace and import resolution257#[derive(Default, Clone, Trace)]258pub struct State(Cc<EvaluationStateInternals>);259260impl State {261 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise262 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {263 let mut file_cache = self.file_cache();264 let mut file = file_cache.raw_entry_mut().from_key(&path);265266 let file = match file {267 RawEntryMut::Occupied(ref mut d) => d.get_mut(),268 RawEntryMut::Vacant(v) => {269 let data = self.settings().import_resolver.load_file_contents(&path)?;270 v.insert(271 path.clone(),272 FileData::new_string(273 std::str::from_utf8(&data)274 .map_err(|_| ImportBadFileUtf8(path.clone()))?275 .into(),276 ),277 )278 .1279 }280 };281 Ok(file282 .get_string()283 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)284 }285 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise286 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {287 let mut file_cache = self.file_cache();288 let mut file = file_cache.raw_entry_mut().from_key(&path);289290 let file = match file {291 RawEntryMut::Occupied(ref mut d) => d.get_mut(),292 RawEntryMut::Vacant(v) => {293 let data = self.settings().import_resolver.load_file_contents(&path)?;294 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))295 .1296 }297 };298 if let Some(str) = &file.bytes {299 return Ok(str.clone());300 }301 if file.bytes.is_none() {302 file.bytes = Some(303 file.string304 .as_ref()305 .expect("either string or bytes should be set")306 .clone()307 .cast_bytes(),308 );309 }310 Ok(file.bytes.as_ref().expect("just set").clone())311 }312 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise313 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {314 let mut file_cache = self.file_cache();315 let mut file = file_cache.raw_entry_mut().from_key(&path);316317 let file = match file {318 RawEntryMut::Occupied(ref mut d) => d.get_mut(),319 RawEntryMut::Vacant(v) => {320 let data = self.settings().import_resolver.load_file_contents(&path)?;321 v.insert(322 path.clone(),323 FileData::new_string(324 std::str::from_utf8(&data)325 .map_err(|_| ImportBadFileUtf8(path.clone()))?326 .into(),327 ),328 )329 .1330 }331 };332 if let Some(val) = &file.evaluated {333 return Ok(val.clone());334 }335 let code = file336 .get_string()337 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;338 let file_name = Source::new(path.clone(), code.clone());339 if file.parsed.is_none() {340 file.parsed = Some(341 jrsonnet_parser::parse(342 &code,343 &ParserSettings {344 source: file_name.clone(),345 },346 )347 .map_err(|e| ImportSyntaxError {348 path: file_name.clone(),349 error: Box::new(e),350 })?,351 );352 }353 let parsed = file.parsed.as_ref().expect("just set").clone();354 if file.evaluating {355 throw!(InfiniteRecursionDetected)356 }357 file.evaluating = true;358 // Dropping file cache guard here, as evaluation may use this map too359 drop(file_cache);360 let res = evaluate(self.create_default_context(file_name), &parsed);361362 let mut file_cache = self.file_cache();363 let mut file = file_cache.raw_entry_mut().from_key(&path);364365 let RawEntryMut::Occupied(file) = &mut file else {366 unreachable!("this file was just here!")367 };368 let file = file.get_mut();369 file.evaluating = false;370 match res {371 Ok(v) => {372 file.evaluated = Some(v.clone());373 Ok(v)374 }375 Err(e) => Err(e),376 }377 }378379 /// Has same semantics as `import 'path'` called from `from` file380 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {381 let resolved = self.resolve_from(from, path)?;382 self.import_resolved(resolved)383 }384 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {385 let resolved = self.resolve(path)?;386 self.import_resolved(resolved)387 }388389 /// Creates context with all passed global variables390 pub fn create_default_context(&self, source: Source) -> Context {391 let context_initializer = &self.settings().context_initializer;392 context_initializer.initialize(self.clone(), source)393 }394395 /// Creates context with all passed global variables, calling custom modifier396 pub fn create_default_context_with(397 &self,398 source: Source,399 context_initializer: impl ContextInitializer,400 ) -> Context {401 let default_initializer = &self.settings().context_initializer;402 let mut builder = ContextBuilder::with_capacity(403 self.clone(),404 default_initializer.reserve_vars() + context_initializer.reserve_vars(),405 );406 default_initializer.populate(source.clone(), &mut builder);407 context_initializer.populate(source, &mut builder);408409 builder.build()410 }411412 /// Executes code creating a new stack frame413 pub fn push<T>(414 e: CallLocation<'_>,415 frame_desc: impl FnOnce() -> String,416 f: impl FnOnce() -> Result<T>,417 ) -> Result<T> {418 let _guard = check_depth()?;419420 f().with_description_src(e, frame_desc)421 }422423 /// Executes code creating a new stack frame424 pub fn push_val(425 &self,426 e: &ExprLocation,427 frame_desc: impl FnOnce() -> String,428 f: impl FnOnce() -> Result<Val>,429 ) -> Result<Val> {430 let _guard = check_depth()?;431432 f().with_description_src(e, frame_desc)433 }434 /// Executes code creating a new stack frame435 pub fn push_description<T>(436 frame_desc: impl FnOnce() -> String,437 f: impl FnOnce() -> Result<T>,438 ) -> Result<T> {439 let _guard = check_depth()?;440441 f().with_description(frame_desc)442 }443}444445/// Internals446impl State {447 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {448 self.0.file_cache.borrow_mut()449 }450 pub fn settings(&self) -> Ref<'_, EvaluationSettings> {451 self.0.settings.borrow()452 }453 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {454 self.0.settings.borrow_mut()455 }456 pub fn add_global(&self, name: IStr, value: Thunk<Val>) {457 #[derive(Trace)]458 struct GlobalsCtx {459 globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,460 inner: TraceBox<dyn ContextInitializer>,461 }462 impl ContextInitializer for GlobalsCtx {463 fn reserve_vars(&self) -> usize {464 self.inner.reserve_vars() + self.globals.borrow().len()465 }466 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {467 self.inner.populate(for_file, builder);468 for (name, val) in self.globals.borrow().iter() {469 builder.bind(name.clone(), val.clone());470 }471 }472473 fn as_any(&self) -> &dyn Any {474 self475 }476 }477 let mut settings = self.settings_mut();478 let initializer = &mut settings.context_initializer;479 if let Some(global) = initializer.as_any().downcast_ref::<GlobalsCtx>() {480 global.globals.borrow_mut().insert(name, value);481 } else {482 let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));483 settings.context_initializer = tb!(GlobalsCtx {484 globals: {485 let mut out = GcHashMap::with_capacity(1);486 out.insert(name, value);487 RefCell::new(out)488 },489 inner490 });491 }492 }493}494495#[derive(Trace)]496pub struct InitialUnderscore(pub Thunk<Val>);497impl ContextInitializer for InitialUnderscore {498 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {499 builder.bind("_".into(), self.0.clone());500 }501502 fn as_any(&self) -> &dyn Any {503 self504 }505}506507/// Raw methods evaluate passed values but don't perform TLA execution508impl State {509 /// Parses and evaluates the given snippet510 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {511 let code = code.into();512 let source = Source::new_virtual(name.into(), code.clone());513 let parsed = jrsonnet_parser::parse(514 &code,515 &ParserSettings {516 source: source.clone(),517 },518 )519 .map_err(|e| ImportSyntaxError {520 path: source.clone(),521 error: Box::new(e),522 })?;523 evaluate(self.create_default_context(source), &parsed)524 }525 /// Parses and evaluates the given snippet with custom context modifier526 pub fn evaluate_snippet_with(527 &self,528 name: impl Into<IStr>,529 code: impl Into<IStr>,530 context_initializer: impl ContextInitializer,531 ) -> Result<Val> {532 let code = code.into();533 let source = Source::new_virtual(name.into(), code.clone());534 let parsed = jrsonnet_parser::parse(535 &code,536 &ParserSettings {537 source: source.clone(),538 },539 )540 .map_err(|e| ImportSyntaxError {541 path: source.clone(),542 error: Box::new(e),543 })?;544 evaluate(545 self.create_default_context_with(source, context_initializer),546 &parsed,547 )548 }549}550551/// Settings utilities552impl State {553 // Only panics in case of [`ImportResolver`] contract violation554 #[allow(clippy::missing_panics_doc)]555 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {556 self.import_resolver().resolve_from(from, path.as_ref())557 }558559 // Only panics in case of [`ImportResolver`] contract violation560 #[allow(clippy::missing_panics_doc)]561 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {562 self.import_resolver().resolve(path.as_ref())563 }564 pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {565 Ref::map(self.settings(), |s| &*s.import_resolver)566 }567 pub fn set_import_resolver(&self, resolver: impl ImportResolver) {568 self.settings_mut().import_resolver = tb!(resolver);569 }570 pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {571 Ref::map(self.settings(), |s| &*s.context_initializer)572 }573 pub fn set_context_initializer(&self, initializer: impl ContextInitializer) {574 self.settings_mut().context_initializer = tb!(initializer);575 }576}