difftreelog
refactor move push_frame out of State struct
in: master
14 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -16,10 +16,11 @@
error::{suggest_object_fields, ErrorKind::*},
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal},
+ in_frame,
typed::Typed,
val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk, ThunkValue},
Context, Error, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
- ResultExt, State, Unbound, Val,
+ ResultExt, Unbound, Val,
};
pub mod destructure;
pub mod operator;
@@ -71,7 +72,7 @@
pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {
Ok(match field_name {
FieldName::Fixed(n) => Some(n.clone()),
- FieldName::Dyn(expr) => State::push(
+ FieldName::Dyn(expr) => in_frame(
CallLocation::new(&expr.span()),
|| "evaluating field name".to_string(),
|| {
@@ -374,7 +375,7 @@
if tailstrict {
body()?
} else {
- State::push(loc, || format!("function <{}> call", f.name()), body)?
+ in_frame(loc, || format!("function <{}> call", f.name()), body)?
}
}
v => bail!(OnlyFunctionsCanBeCalledGot(v.value_type())),
@@ -384,13 +385,13 @@
pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {
let value = &assertion.0;
let msg = &assertion.1;
- let assertion_result = State::push(
+ let assertion_result = in_frame(
CallLocation::new(&value.span()),
|| "assertion condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), value)?),
)?;
if !assertion_result {
- State::push(
+ in_frame(
CallLocation::new(&value.span()),
|| "assertion failure".to_owned(),
|| {
@@ -457,7 +458,7 @@
}
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
- Var(name) => State::push(
+ Var(name) => in_frame(
CallLocation::new(&loc),
|| format!("variable <{name}> access"),
|| ctx.binding(name.clone())?.evaluate(),
@@ -645,7 +646,7 @@
evaluate_assert(ctx.clone(), assert)?;
evaluate(ctx, returned)?
}
- ErrorStmt(e) => State::push(
+ ErrorStmt(e) => in_frame(
CallLocation::new(&loc),
|| "error statement".to_owned(),
|| bail!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),
@@ -655,7 +656,7 @@
cond_then,
cond_else,
} => {
- if State::push(
+ if in_frame(
CallLocation::new(&loc),
|| "if condition".to_owned(),
|| bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),
@@ -676,7 +677,7 @@
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
- Ok(Some(State::push(
+ Ok(Some(in_frame(
loc,
|| format!("slice {desc}"),
|| T::from_untyped(evaluate(ctx.clone(), value)?),
@@ -703,7 +704,7 @@
let s = ctx.state();
let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
match i {
- Import(_) => State::push(
+ Import(_) => in_frame(
CallLocation::new(&loc),
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,6 +1,5 @@
use std::{
any::Any,
- cell::RefCell,
env::current_dir,
fs,
io::{ErrorKind, Read},
@@ -41,8 +40,10 @@
/// this cannot be resolved using associated type, as evaluator uses object instead of generic for [`ImportResolver`]
fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
- /// For downcasts
+ // For downcasts, will be removed after trait_upcasting_coercion
+ // stabilization.
fn as_any(&self) -> &dyn Any;
+ fn as_any_mut(&mut self) -> &mut dyn Any;
}
/// Dummy resolver, can't resolve/load any file
@@ -56,6 +57,9 @@
fn as_any(&self) -> &dyn Any {
self
}
+ fn as_any_mut(&mut self) -> &mut dyn Any {
+ self
+ }
}
#[allow(clippy::use_self)]
impl Default for Box<dyn ImportResolver> {
@@ -69,17 +73,15 @@
pub struct FileImportResolver {
/// Library directories to search for file.
/// Referred to as `jpath` in original jsonnet implementation.
- library_paths: RefCell<Vec<PathBuf>>,
+ library_paths: Vec<PathBuf>,
}
impl FileImportResolver {
- pub fn new(jpath: Vec<PathBuf>) -> Self {
- Self {
- library_paths: RefCell::new(jpath),
- }
+ pub fn new(library_paths: Vec<PathBuf>) -> Self {
+ Self { library_paths }
}
/// Dynamically add new jpath, used by bindings
- pub fn add_jpath(&self, path: PathBuf) {
- self.library_paths.borrow_mut().push(path);
+ pub fn add_jpath(&mut self, path: PathBuf) {
+ self.library_paths.push(path);
}
}
@@ -132,7 +134,7 @@
if let Some(direct) = check_path(&direct)? {
return Ok(direct);
}
- for library_path in self.library_paths.borrow().iter() {
+ for library_path in &self.library_paths {
let mut cloned = library_path.clone();
cloned.push(path);
if let Some(cloned) = check_path(&cloned)? {
@@ -165,11 +167,15 @@
Ok(out)
}
+ fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ self.resolve_from(&SourcePath::default(), path)
+ }
+
fn as_any(&self) -> &dyn Any {
self
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
- self.resolve_from(&SourcePath::default(), path)
+ fn as_any_mut(&mut self) -> &mut dyn Any {
+ self
}
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -11,8 +11,8 @@
};
use crate::{
- arr::ArrValue, runtime_error, val::NumValue, Error as JrError, ObjValue, ObjValueBuilder,
- Result, State, Val,
+ arr::ArrValue, in_description_frame, runtime_error, val::NumValue, Error as JrError, ObjValue,
+ ObjValueBuilder, Result, Val,
};
impl<'de> Deserialize<'de> for Val {
@@ -173,8 +173,7 @@
let mut seq = serializer.serialize_seq(Some(arr.len()))?;
for (i, element) in arr.iter().enumerate() {
let mut serde_error = None;
- // TODO: rewrite using try{} after stabilization
- State::push_description(
+ in_description_frame(
|| format!("array index [{i}]"),
|| {
let e = element?;
@@ -199,7 +198,7 @@
) {
let mut serde_error = None;
// TODO: rewrite using try{} after stabilization
- State::push_description(
+ in_description_frame(
|| format!("object field {field:?}"),
|| {
let v = value?;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8#[cfg(feature = "async-import")]9pub mod async_import;10mod ctx;11mod dynamic;12pub mod error;13mod evaluate;14pub mod function;15pub mod gc;16mod import;17mod integrations;18pub mod manifest;19mod map;20mod obj;21pub mod stack;22pub mod stdlib;23mod tla;24pub mod trace;25pub mod typed;26pub mod val;2728use std::{29 any::Any,30 cell::{RefCell, RefMut},31 fmt::{self, Debug},32 path::Path,33};3435pub use ctx::*;36pub use dynamic::*;37pub use error::{Error, ErrorKind::*, Result, ResultExt};38pub use evaluate::*;39use function::CallLocation;40use gc::{GcHashMap, TraceBox};41use hashbrown::hash_map::RawEntryMut;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath, Span};49pub use obj::*;50use stack::check_depth;51pub use tla::apply_tla;52pub use val::{Thunk, Val};5354/// Thunk without bound `super`/`this`55/// object inheritance may be overriden multiple times, and will be fixed only on field read56pub trait Unbound: Trace {57 /// Type of value after object context is bound58 type Bound;59 /// Create value bound to specified object context60 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;61}6263/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code64/// Standard jsonnet fields are always unbound65#[derive(Clone, Trace)]66pub enum MaybeUnbound {67 /// Value needs to be bound to `this`/`super`68 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),69 /// Value is object-independent70 Bound(Thunk<Val>),71}7273impl Debug for MaybeUnbound {74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {75 write!(f, "MaybeUnbound")76 }77}78impl MaybeUnbound {79 /// Attach object context to value, if required80 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {81 match self {82 Self::Unbound(v) => v.bind(sup, this),83 Self::Bound(v) => Ok(v.evaluate()?),84 }85 }86}8788/// During import, this trait will be called to create initial context for file.89/// It may initialize global variables, stdlib for example.90pub trait ContextInitializer: Trace {91 /// For which size the builder should be preallocated92 fn reserve_vars(&self) -> usize {93 094 }95 /// Initialize default file context.96 /// Has default implementation, which calls `populate`.97 /// Prefer to always implement `populate` instead.98 fn initialize(&self, state: State, for_file: Source) -> Context {99 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());100 self.populate(for_file, &mut builder);101 builder.build()102 }103 /// For composability: extend builder. May panic if this initialization is not supported,104 /// and the context may only be created via `initialize`.105 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);106 /// Allows upcasting from abstract to concrete context initializer.107 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.108 fn as_any(&self) -> &dyn Any;109}110111/// Context initializer which adds nothing.112impl ContextInitializer for () {113 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}114 fn as_any(&self) -> &dyn Any {115 self116 }117}118119impl<T> ContextInitializer for Option<T>120where121 T: ContextInitializer,122{123 fn initialize(&self, state: State, for_file: Source) -> Context {124 if let Some(ctx) = self {125 ctx.initialize(state, for_file)126 } else {127 ().initialize(state, for_file)128 }129 }130131 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {132 if let Some(ctx) = self {133 ctx.populate(for_file, builder);134 }135 }136137 fn as_any(&self) -> &dyn Any {138 self139 }140}141142macro_rules! impl_context_initializer {143 ($($gen:ident)*) => {144 #[allow(non_snake_case)]145 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {146 fn reserve_vars(&self) -> usize {147 let mut out = 0;148 let ($($gen,)*) = self;149 $(out += $gen.reserve_vars();)*150 out151 }152 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {153 let ($($gen,)*) = self;154 $($gen.populate(for_file.clone(), builder);)*155 }156 fn as_any(&self) -> &dyn Any {157 self158 }159 }160 };161 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {162 impl_context_initializer!($($cur)*);163 impl_context_initializer!($($cur)* $c @ $($rest)*);164 };165 ($($cur:ident)* @) => {166 impl_context_initializer!($($cur)*);167 }168}169impl_context_initializer! {170 A @ B C D E F G171}172173#[derive(Trace)]174struct FileData {175 string: Option<IStr>,176 bytes: Option<IBytes>,177 parsed: Option<LocExpr>,178 evaluated: Option<Val>,179180 evaluating: bool,181}182impl FileData {183 fn new_string(data: IStr) -> Self {184 Self {185 string: Some(data),186 bytes: None,187 parsed: None,188 evaluated: None,189 evaluating: false,190 }191 }192 fn new_bytes(data: IBytes) -> Self {193 Self {194 string: None,195 bytes: Some(data),196 parsed: None,197 evaluated: None,198 evaluating: false,199 }200 }201 pub(crate) fn get_string(&mut self) -> Option<IStr> {202 if self.string.is_none() {203 self.string = Some(204 self.bytes205 .as_ref()206 .expect("either string or bytes should be set")207 .clone()208 .cast_str()?,209 );210 }211 Some(self.string.clone().expect("just set"))212 }213}214215#[derive(Trace)]216pub struct EvaluationStateInternals {217 /// Internal state218 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,219 /// Context initializer, which will be used for imports and everything220 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`221 context_initializer: TraceBox<dyn ContextInitializer>,222 /// Used to resolve file locations/contents223 import_resolver: TraceBox<dyn ImportResolver>,224}225226/// Maintains stack trace and import resolution227#[derive(Clone, Trace)]228pub struct State(Cc<EvaluationStateInternals>);229230impl State {231 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise232 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {233 let mut file_cache = self.file_cache();234 let mut file = file_cache.raw_entry_mut().from_key(&path);235236 let file = match file {237 RawEntryMut::Occupied(ref mut d) => d.get_mut(),238 RawEntryMut::Vacant(v) => {239 let data = self.import_resolver().load_file_contents(&path)?;240 v.insert(241 path.clone(),242 FileData::new_string(243 std::str::from_utf8(&data)244 .map_err(|_| ImportBadFileUtf8(path.clone()))?245 .into(),246 ),247 )248 .1249 }250 };251 Ok(file252 .get_string()253 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)254 }255 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise256 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {257 let mut file_cache = self.file_cache();258 let mut file = file_cache.raw_entry_mut().from_key(&path);259260 let file = match file {261 RawEntryMut::Occupied(ref mut d) => d.get_mut(),262 RawEntryMut::Vacant(v) => {263 let data = self.import_resolver().load_file_contents(&path)?;264 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))265 .1266 }267 };268 if let Some(str) = &file.bytes {269 return Ok(str.clone());270 }271 if file.bytes.is_none() {272 file.bytes = Some(273 file.string274 .as_ref()275 .expect("either string or bytes should be set")276 .clone()277 .cast_bytes(),278 );279 }280 Ok(file.bytes.as_ref().expect("just set").clone())281 }282 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise283 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {284 let mut file_cache = self.file_cache();285 let mut file = file_cache.raw_entry_mut().from_key(&path);286287 let file = match file {288 RawEntryMut::Occupied(ref mut d) => d.get_mut(),289 RawEntryMut::Vacant(v) => {290 let data = self.import_resolver().load_file_contents(&path)?;291 v.insert(292 path.clone(),293 FileData::new_string(294 std::str::from_utf8(&data)295 .map_err(|_| ImportBadFileUtf8(path.clone()))?296 .into(),297 ),298 )299 .1300 }301 };302 if let Some(val) = &file.evaluated {303 return Ok(val.clone());304 }305 let code = file306 .get_string()307 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;308 let file_name = Source::new(path.clone(), code.clone());309 if file.parsed.is_none() {310 file.parsed = Some(311 jrsonnet_parser::parse(312 &code,313 &ParserSettings {314 source: file_name.clone(),315 },316 )317 .map_err(|e| ImportSyntaxError {318 path: file_name.clone(),319 error: Box::new(e),320 })?,321 );322 }323 let parsed = file.parsed.as_ref().expect("just set").clone();324 if file.evaluating {325 bail!(InfiniteRecursionDetected)326 }327 file.evaluating = true;328 // Dropping file cache guard here, as evaluation may use this map too329 drop(file_cache);330 let res = evaluate(self.create_default_context(file_name), &parsed);331332 let mut file_cache = self.file_cache();333 let mut file = file_cache.raw_entry_mut().from_key(&path);334335 let RawEntryMut::Occupied(file) = &mut file else {336 unreachable!("this file was just here!")337 };338 let file = file.get_mut();339 file.evaluating = false;340 match res {341 Ok(v) => {342 file.evaluated = Some(v.clone());343 Ok(v)344 }345 Err(e) => Err(e),346 }347 }348349 /// Has same semantics as `import 'path'` called from `from` file350 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {351 let resolved = self.resolve_from(from, path)?;352 self.import_resolved(resolved)353 }354 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {355 let resolved = self.resolve(path)?;356 self.import_resolved(resolved)357 }358359 /// Creates context with all passed global variables360 pub fn create_default_context(&self, source: Source) -> Context {361 self.context_initializer().initialize(self.clone(), source)362 }363364 /// Creates context with all passed global variables, calling custom modifier365 pub fn create_default_context_with(366 &self,367 source: Source,368 context_initializer: impl ContextInitializer,369 ) -> Context {370 let default_initializer = self.context_initializer();371 let mut builder = ContextBuilder::with_capacity(372 self.clone(),373 default_initializer.reserve_vars() + context_initializer.reserve_vars(),374 );375 default_initializer.populate(source.clone(), &mut builder);376 context_initializer.populate(source, &mut builder);377378 builder.build()379 }380381 /// Executes code creating a new stack frame382 pub fn push<T>(383 e: CallLocation<'_>,384 frame_desc: impl FnOnce() -> String,385 f: impl FnOnce() -> Result<T>,386 ) -> Result<T> {387 let _guard = check_depth()?;388389 f().with_description_src(e, frame_desc)390 }391392 /// Executes code creating a new stack frame393 pub fn push_val(394 &self,395 e: &Span,396 frame_desc: impl FnOnce() -> String,397 f: impl FnOnce() -> Result<Val>,398 ) -> Result<Val> {399 let _guard = check_depth()?;400401 f().with_description_src(e, frame_desc)402 }403 /// Executes code creating a new stack frame404 pub fn push_description<T>(405 frame_desc: impl FnOnce() -> String,406 f: impl FnOnce() -> Result<T>,407 ) -> Result<T> {408 let _guard = check_depth()?;409410 f().with_description(frame_desc)411 }412}413414/// Internals415impl State {416 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {417 self.0.file_cache.borrow_mut()418 }419}420421#[derive(Trace)]422pub struct InitialUnderscore(pub Thunk<Val>);423impl ContextInitializer for InitialUnderscore {424 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {425 builder.bind("_", self.0.clone());426 }427428 fn as_any(&self) -> &dyn Any {429 self430 }431}432433/// Raw methods evaluate passed values but don't perform TLA execution434impl State {435 /// Parses and evaluates the given snippet436 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {437 let code = code.into();438 let source = Source::new_virtual(name.into(), code.clone());439 let parsed = jrsonnet_parser::parse(440 &code,441 &ParserSettings {442 source: source.clone(),443 },444 )445 .map_err(|e| ImportSyntaxError {446 path: source.clone(),447 error: Box::new(e),448 })?;449 evaluate(self.create_default_context(source), &parsed)450 }451 /// Parses and evaluates the given snippet with custom context modifier452 pub fn evaluate_snippet_with(453 &self,454 name: impl Into<IStr>,455 code: impl Into<IStr>,456 context_initializer: impl ContextInitializer,457 ) -> Result<Val> {458 let code = code.into();459 let source = Source::new_virtual(name.into(), code.clone());460 let parsed = jrsonnet_parser::parse(461 &code,462 &ParserSettings {463 source: source.clone(),464 },465 )466 .map_err(|e| ImportSyntaxError {467 path: source.clone(),468 error: Box::new(e),469 })?;470 evaluate(471 self.create_default_context_with(source, context_initializer),472 &parsed,473 )474 }475}476477/// Settings utilities478impl State {479 // Only panics in case of [`ImportResolver`] contract violation480 #[allow(clippy::missing_panics_doc)]481 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {482 self.import_resolver().resolve_from(from, path.as_ref())483 }484485 // Only panics in case of [`ImportResolver`] contract violation486 #[allow(clippy::missing_panics_doc)]487 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {488 self.import_resolver().resolve(path.as_ref())489 }490 pub fn import_resolver(&self) -> &dyn ImportResolver {491 &*self.0.import_resolver492 }493 pub fn context_initializer(&self) -> &dyn ContextInitializer {494 &*self.0.context_initializer495 }496}497498impl State {499 pub fn builder() -> StateBuilder {500 StateBuilder::default()501 }502}503504impl Default for State {505 fn default() -> Self {506 Self::builder().build()507 }508}509510#[derive(Default)]511pub struct StateBuilder {512 import_resolver: Option<TraceBox<dyn ImportResolver>>,513 context_initializer: Option<TraceBox<dyn ContextInitializer>>,514}515impl StateBuilder {516 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {517 let _ = self.import_resolver.insert(tb!(import_resolver));518 self519 }520 pub fn context_initializer(521 &mut self,522 context_initializer: impl ContextInitializer,523 ) -> &mut Self {524 let _ = self.context_initializer.insert(tb!(context_initializer));525 self526 }527 pub fn build(mut self) -> State {528 State(Cc::new(EvaluationStateInternals {529 file_cache: RefCell::new(GcHashMap::new()),530 context_initializer: self.context_initializer.take().unwrap_or_else(|| tb!(())),531 import_resolver: self532 .import_resolver533 .take()534 .unwrap_or_else(|| tb!(DummyImportResolver)),535 }))536 }537}1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8#[cfg(feature = "async-import")]9pub mod async_import;10mod ctx;11mod dynamic;12pub mod error;13mod evaluate;14pub mod function;15pub mod gc;16mod import;17mod integrations;18pub mod manifest;19mod map;20mod obj;21pub mod stack;22pub mod stdlib;23mod tla;24pub mod trace;25pub mod typed;26pub mod val;2728use std::{29 any::Any,30 cell::{RefCell, RefMut},31 fmt::{self, Debug},32 path::Path,33};3435pub use ctx::*;36pub use dynamic::*;37pub use error::{Error, ErrorKind::*, Result, ResultExt};38pub use evaluate::*;39use function::CallLocation;40use gc::{GcHashMap, TraceBox};41use hashbrown::hash_map::RawEntryMut;42pub use import::*;43use jrsonnet_gcmodule::{Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath};49pub use obj::*;50use stack::check_depth;51pub use tla::apply_tla;52pub use val::{Thunk, Val};5354/// Thunk without bound `super`/`this`55/// object inheritance may be overriden multiple times, and will be fixed only on field read56pub trait Unbound: Trace {57 /// Type of value after object context is bound58 type Bound;59 /// Create value bound to specified object context60 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;61}6263/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code64/// Standard jsonnet fields are always unbound65#[derive(Clone, Trace)]66pub enum MaybeUnbound {67 /// Value needs to be bound to `this`/`super`68 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),69 /// Value is object-independent70 Bound(Thunk<Val>),71}7273impl Debug for MaybeUnbound {74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {75 write!(f, "MaybeUnbound")76 }77}78impl MaybeUnbound {79 /// Attach object context to value, if required80 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {81 match self {82 Self::Unbound(v) => v.bind(sup, this),83 Self::Bound(v) => Ok(v.evaluate()?),84 }85 }86}8788/// During import, this trait will be called to create initial context for file.89/// It may initialize global variables, stdlib for example.90pub trait ContextInitializer: Trace {91 /// For which size the builder should be preallocated92 fn reserve_vars(&self) -> usize {93 094 }95 /// Initialize default file context.96 /// Has default implementation, which calls `populate`.97 /// Prefer to always implement `populate` instead.98 fn initialize(&self, state: State, for_file: Source) -> Context {99 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());100 self.populate(for_file, &mut builder);101 builder.build()102 }103 /// For composability: extend builder. May panic if this initialization is not supported,104 /// and the context may only be created via `initialize`.105 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);106 /// Allows upcasting from abstract to concrete context initializer.107 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.108 fn as_any(&self) -> &dyn Any;109}110111/// Context initializer which adds nothing.112impl ContextInitializer for () {113 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}114 fn as_any(&self) -> &dyn Any {115 self116 }117}118119impl<T> ContextInitializer for Option<T>120where121 T: ContextInitializer,122{123 fn initialize(&self, state: State, for_file: Source) -> Context {124 if let Some(ctx) = self {125 ctx.initialize(state, for_file)126 } else {127 ().initialize(state, for_file)128 }129 }130131 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {132 if let Some(ctx) = self {133 ctx.populate(for_file, builder);134 }135 }136137 fn as_any(&self) -> &dyn Any {138 self139 }140}141142macro_rules! impl_context_initializer {143 ($($gen:ident)*) => {144 #[allow(non_snake_case)]145 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {146 fn reserve_vars(&self) -> usize {147 let mut out = 0;148 let ($($gen,)*) = self;149 $(out += $gen.reserve_vars();)*150 out151 }152 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {153 let ($($gen,)*) = self;154 $($gen.populate(for_file.clone(), builder);)*155 }156 fn as_any(&self) -> &dyn Any {157 self158 }159 }160 };161 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {162 impl_context_initializer!($($cur)*);163 impl_context_initializer!($($cur)* $c @ $($rest)*);164 };165 ($($cur:ident)* @) => {166 impl_context_initializer!($($cur)*);167 }168}169impl_context_initializer! {170 A @ B C D E F G171}172173#[derive(Trace)]174struct FileData {175 string: Option<IStr>,176 bytes: Option<IBytes>,177 parsed: Option<LocExpr>,178 evaluated: Option<Val>,179180 evaluating: bool,181}182impl FileData {183 fn new_string(data: IStr) -> Self {184 Self {185 string: Some(data),186 bytes: None,187 parsed: None,188 evaluated: None,189 evaluating: false,190 }191 }192 fn new_bytes(data: IBytes) -> Self {193 Self {194 string: None,195 bytes: Some(data),196 parsed: None,197 evaluated: None,198 evaluating: false,199 }200 }201 pub(crate) fn get_string(&mut self) -> Option<IStr> {202 if self.string.is_none() {203 self.string = Some(204 self.bytes205 .as_ref()206 .expect("either string or bytes should be set")207 .clone()208 .cast_str()?,209 );210 }211 Some(self.string.clone().expect("just set"))212 }213}214215#[derive(Trace)]216pub struct EvaluationStateInternals {217 /// Internal state218 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,219 /// Context initializer, which will be used for imports and everything220 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`221 context_initializer: TraceBox<dyn ContextInitializer>,222 /// Used to resolve file locations/contents223 import_resolver: TraceBox<dyn ImportResolver>,224}225226/// Maintains stack trace and import resolution227#[derive(Clone, Trace)]228pub struct State(Cc<EvaluationStateInternals>);229230impl State {231 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise232 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {233 let mut file_cache = self.file_cache();234 let mut file = file_cache.raw_entry_mut().from_key(&path);235236 let file = match file {237 RawEntryMut::Occupied(ref mut d) => d.get_mut(),238 RawEntryMut::Vacant(v) => {239 let data = self.import_resolver().load_file_contents(&path)?;240 v.insert(241 path.clone(),242 FileData::new_string(243 std::str::from_utf8(&data)244 .map_err(|_| ImportBadFileUtf8(path.clone()))?245 .into(),246 ),247 )248 .1249 }250 };251 Ok(file252 .get_string()253 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)254 }255 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise256 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {257 let mut file_cache = self.file_cache();258 let mut file = file_cache.raw_entry_mut().from_key(&path);259260 let file = match file {261 RawEntryMut::Occupied(ref mut d) => d.get_mut(),262 RawEntryMut::Vacant(v) => {263 let data = self.import_resolver().load_file_contents(&path)?;264 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))265 .1266 }267 };268 if let Some(str) = &file.bytes {269 return Ok(str.clone());270 }271 if file.bytes.is_none() {272 file.bytes = Some(273 file.string274 .as_ref()275 .expect("either string or bytes should be set")276 .clone()277 .cast_bytes(),278 );279 }280 Ok(file.bytes.as_ref().expect("just set").clone())281 }282 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise283 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {284 let mut file_cache = self.file_cache();285 let mut file = file_cache.raw_entry_mut().from_key(&path);286287 let file = match file {288 RawEntryMut::Occupied(ref mut d) => d.get_mut(),289 RawEntryMut::Vacant(v) => {290 let data = self.import_resolver().load_file_contents(&path)?;291 v.insert(292 path.clone(),293 FileData::new_string(294 std::str::from_utf8(&data)295 .map_err(|_| ImportBadFileUtf8(path.clone()))?296 .into(),297 ),298 )299 .1300 }301 };302 if let Some(val) = &file.evaluated {303 return Ok(val.clone());304 }305 let code = file306 .get_string()307 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;308 let file_name = Source::new(path.clone(), code.clone());309 if file.parsed.is_none() {310 file.parsed = Some(311 jrsonnet_parser::parse(312 &code,313 &ParserSettings {314 source: file_name.clone(),315 },316 )317 .map_err(|e| ImportSyntaxError {318 path: file_name.clone(),319 error: Box::new(e),320 })?,321 );322 }323 let parsed = file.parsed.as_ref().expect("just set").clone();324 if file.evaluating {325 bail!(InfiniteRecursionDetected)326 }327 file.evaluating = true;328 // Dropping file cache guard here, as evaluation may use this map too329 drop(file_cache);330 let res = evaluate(self.create_default_context(file_name), &parsed);331332 let mut file_cache = self.file_cache();333 let mut file = file_cache.raw_entry_mut().from_key(&path);334335 let RawEntryMut::Occupied(file) = &mut file else {336 unreachable!("this file was just here!")337 };338 let file = file.get_mut();339 file.evaluating = false;340 match res {341 Ok(v) => {342 file.evaluated = Some(v.clone());343 Ok(v)344 }345 Err(e) => Err(e),346 }347 }348349 /// Has same semantics as `import 'path'` called from `from` file350 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {351 let resolved = self.resolve_from(from, path)?;352 self.import_resolved(resolved)353 }354 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {355 let resolved = self.resolve(path)?;356 self.import_resolved(resolved)357 }358359 /// Creates context with all passed global variables360 pub fn create_default_context(&self, source: Source) -> Context {361 self.context_initializer().initialize(self.clone(), source)362 }363364 /// Creates context with all passed global variables, calling custom modifier365 pub fn create_default_context_with(366 &self,367 source: Source,368 context_initializer: impl ContextInitializer,369 ) -> Context {370 let default_initializer = self.context_initializer();371 let mut builder = ContextBuilder::with_capacity(372 self.clone(),373 default_initializer.reserve_vars() + context_initializer.reserve_vars(),374 );375 default_initializer.populate(source.clone(), &mut builder);376 context_initializer.populate(source, &mut builder);377378 builder.build()379 }380}381382/// Internals383impl State {384 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {385 self.0.file_cache.borrow_mut()386 }387}388/// Executes code creating a new stack frame, to be replaced with try{}389pub fn in_frame<T>(390 e: CallLocation<'_>,391 frame_desc: impl FnOnce() -> String,392 f: impl FnOnce() -> Result<T>,393) -> Result<T> {394 let _guard = check_depth()?;395396 f().with_description_src(e, frame_desc)397}398399/// Executes code creating a new stack frame, to be replaced with try{}400pub fn in_description_frame<T>(401 frame_desc: impl FnOnce() -> String,402 f: impl FnOnce() -> Result<T>,403) -> Result<T> {404 let _guard = check_depth()?;405406 f().with_description(frame_desc)407}408409#[derive(Trace)]410pub struct InitialUnderscore(pub Thunk<Val>);411impl ContextInitializer for InitialUnderscore {412 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {413 builder.bind("_", self.0.clone());414 }415416 fn as_any(&self) -> &dyn Any {417 self418 }419}420421/// Raw methods evaluate passed values but don't perform TLA execution422impl State {423 /// Parses and evaluates the given snippet424 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {425 let code = code.into();426 let source = Source::new_virtual(name.into(), code.clone());427 let parsed = jrsonnet_parser::parse(428 &code,429 &ParserSettings {430 source: source.clone(),431 },432 )433 .map_err(|e| ImportSyntaxError {434 path: source.clone(),435 error: Box::new(e),436 })?;437 evaluate(self.create_default_context(source), &parsed)438 }439 /// Parses and evaluates the given snippet with custom context modifier440 pub fn evaluate_snippet_with(441 &self,442 name: impl Into<IStr>,443 code: impl Into<IStr>,444 context_initializer: impl ContextInitializer,445 ) -> Result<Val> {446 let code = code.into();447 let source = Source::new_virtual(name.into(), code.clone());448 let parsed = jrsonnet_parser::parse(449 &code,450 &ParserSettings {451 source: source.clone(),452 },453 )454 .map_err(|e| ImportSyntaxError {455 path: source.clone(),456 error: Box::new(e),457 })?;458 evaluate(459 self.create_default_context_with(source, context_initializer),460 &parsed,461 )462 }463}464465/// Settings utilities466impl State {467 // Only panics in case of [`ImportResolver`] contract violation468 #[allow(clippy::missing_panics_doc)]469 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {470 self.import_resolver().resolve_from(from, path.as_ref())471 }472473 // Only panics in case of [`ImportResolver`] contract violation474 #[allow(clippy::missing_panics_doc)]475 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {476 self.import_resolver().resolve(path.as_ref())477 }478 pub fn import_resolver(&self) -> &dyn ImportResolver {479 &*self.0.import_resolver480 }481 pub fn context_initializer(&self) -> &dyn ContextInitializer {482 &*self.0.context_initializer483 }484}485486impl State {487 pub fn builder() -> StateBuilder {488 StateBuilder::default()489 }490}491492impl Default for State {493 fn default() -> Self {494 Self::builder().build()495 }496}497498#[derive(Default)]499pub struct StateBuilder {500 import_resolver: Option<TraceBox<dyn ImportResolver>>,501 context_initializer: Option<TraceBox<dyn ContextInitializer>>,502}503impl StateBuilder {504 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {505 let _ = self.import_resolver.insert(tb!(import_resolver));506 self507 }508 pub fn context_initializer(509 &mut self,510 context_initializer: impl ContextInitializer,511 ) -> &mut Self {512 let _ = self.context_initializer.insert(tb!(context_initializer));513 self514 }515 pub fn build(mut self) -> State {516 State(Cc::new(EvaluationStateInternals {517 file_cache: RefCell::new(GcHashMap::new()),518 context_initializer: self.context_initializer.take().unwrap_or_else(|| tb!(())),519 import_resolver: self520 .import_resolver521 .take()522 .unwrap_or_else(|| tb!(DummyImportResolver)),523 }))524 }525}crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,6 +1,6 @@
use std::{borrow::Cow, fmt::Write, ptr};
-use crate::{bail, Result, ResultExt, State, Val};
+use crate::{bail, in_description_frame, Result, ResultExt, Val};
pub trait ManifestFormat {
fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()>;
@@ -242,7 +242,7 @@
Minify | ToString => {}
};
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_json_ex_buf(&item, buf, cur_padding, options),
)?;
@@ -304,7 +304,7 @@
escape_string_json_buf(&key, buf);
buf.push_str(options.key_val_sep);
- State::push_description(
+ in_description_frame(
|| format!("field <{key}> manifestification"),
|| manifest_json_ex_buf(&value, buf, cur_padding, options),
)?;
@@ -412,7 +412,7 @@
for (i, v) in arr.iter().enumerate() {
let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
out.push_str("---\n");
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| self.inner.manifest_buf(v, out),
)?;
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -17,10 +17,11 @@
error::{suggest_object_fields, Error, ErrorKind::*},
function::{CallLocation, FuncVal},
gc::{GcHashMap, GcHashSet, TraceBox},
+ in_frame,
operator::evaluate_add_op,
tb,
val::{ArrValue, ThunkValue},
- MaybeUnbound, Result, State, Thunk, Unbound, Val,
+ MaybeUnbound, Result, Thunk, Unbound, Val,
};
#[cfg(not(feature = "exp-preserve-order"))]
@@ -969,7 +970,7 @@
let location = member.location.clone();
let old = receiver.0.map.insert(name.clone(), member);
if old.is_some() {
- State::push(
+ in_frame(
CallLocation(location.as_ref()),
|| format!("field <{}> initializtion", name.clone()),
|| bail!(DuplicateFieldName(name.clone())),
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -3,12 +3,12 @@
use format::{format_arr, format_obj};
-use crate::{function::CallLocation, Result, State, Val};
+use crate::{function::CallLocation, in_frame, Result, Val};
pub mod format;
pub fn std_format(str: &str, vals: Val) -> Result<String> {
- State::push(
+ in_frame(
CallLocation::native(),
|| format!("std.format of {str}"),
|| {
crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -3,12 +3,12 @@
use crate::{
function::{ArgsLike, CallLocation},
- Result, State, Val,
+ in_description_frame, 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(
+ in_description_frame(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -8,7 +8,7 @@
use crate::{
error::{Error, ErrorKind, Result},
- State, Val,
+ in_description_frame, Val,
};
#[derive(Debug, Error, Clone, Trace)]
@@ -89,7 +89,7 @@
path: impl Fn() -> ValuePathItem,
item: impl Fn() -> Result<()>,
) -> Result<()> {
- State::push_description(error_reason, || match item() {
+ in_description_frame(error_reason, || match item() {
Ok(()) => Ok(()),
Err(mut e) => {
if let ErrorKind::TypeError(e) = &mut e.error_mut() {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -235,6 +235,7 @@
use crate::{PoolMap, POOL};
+ /// Type-erased interned string pool
pub enum PoolState {}
/// Dump current interned string pool, to be restored by
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -290,7 +290,7 @@
cfg_attrs,
} => {
let name = name.as_ref().map_or("<unnamed>", String::as_str);
- let eval = quote! {jrsonnet_evaluator::State::push_description(
+ let eval = quote! {jrsonnet_evaluator::in_description_frame(
|| format!("argument <{}> evaluation", #name),
|| <#ty>::from_untyped(value.evaluate()?),
)?};
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -1,10 +1,10 @@
use std::borrow::Cow;
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{escape_string_json_buf, ManifestFormat},
val::ArrValue,
- IStr, ObjValue, Result, ResultExt, State, Val,
+ IStr, ObjValue, Result, ResultExt, Val,
};
pub struct TomlFormat<'s> {
@@ -124,7 +124,7 @@
buf.push_str(&options.padding);
}
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_value(&e, true, buf, "", options),
)?;
@@ -161,7 +161,7 @@
escape_key_toml_buf(&k, buf);
buf.push_str(" = ");
- State::push_description(
+ in_description_frame(
|| format!("field <{k}> manifestification"),
|| manifest_value(&v, true, buf, "", options),
)?;
crates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,9 +1,9 @@
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{ManifestFormat, ToStringFormat},
typed::{ComplexValType, Either2, Typed, ValType},
val::ArrValue,
- Either, ObjValue, Result, ResultExt, State, Val,
+ Either, ObjValue, Result, ResultExt, Val,
};
pub struct XmlJsonmlFormat {
@@ -70,7 +70,7 @@
Ok(Self::Tag {
tag,
attrs,
- children: State::push_description(
+ children: in_description_frame(
|| "parsing children".to_owned(),
|| {
Typed::from_untyped(Val::Arr(arr.slice(
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -1,9 +1,9 @@
use std::{borrow::Cow, fmt::Write};
use jrsonnet_evaluator::{
- bail,
+ bail, in_description_frame,
manifest::{escape_string_json_buf, ManifestFormat},
- Result, ResultExt, State, Val,
+ Result, ResultExt, Val,
};
pub struct YamlFormat<'s> {
@@ -178,7 +178,7 @@
if extra_padding {
cur_padding.push_str(&options.padding);
}
- State::push_description(
+ in_description_frame(
|| format!("elem <{i}> manifestification"),
|| manifest_yaml_ex_buf(&item, buf, cur_padding, options),
)?;
@@ -225,7 +225,7 @@
}
_ => buf.push(' '),
}
- State::push_description(
+ in_description_frame(
|| format!("field <{key}> manifestification"),
|| manifest_yaml_ex_buf(&value, buf, cur_padding, options),
)?;