1#![warn(clippy::all, clippy::nursery, clippy::pedantic)]2#![allow(3 macro_expanded_macro_exports_accessed_by_absolute_paths,4 clippy::ptr_arg,5 6 clippy::must_use_candidate,7 8 clippy::missing_errors_doc,9 10 clippy::needless_pass_by_value,11 12 clippy::wildcard_imports,13 clippy::enum_glob_use,14 clippy::module_name_repetitions,15 16 clippy::cast_precision_loss,17 clippy::cast_possible_wrap,18 clippy::cast_possible_truncation,19 clippy::cast_sign_loss,20 21 22 clippy::use_self,23 24 clippy::iter_with_drain,25)]262728extern crate self as jrsonnet_evaluator;2930mod ctx;31mod dynamic;32pub mod error;33mod evaluate;34pub mod function;35pub mod gc;36mod import;37mod integrations;38mod map;39mod obj;40pub mod stdlib;41pub mod trace;42pub mod typed;43pub mod val;4445use std::{46 any::Any,47 cell::{Ref, RefCell, RefMut},48 collections::HashMap,49 fmt::{self, Debug},50 path::Path,51 rc::Rc,52};5354pub use ctx::*;55pub use dynamic::*;56use error::{Error::*, LocError, Result, StackTraceElement};57pub use evaluate::*;58use function::{CallLocation, TlaArg};59use gc::{GcHashMap, TraceBox};60use hashbrown::hash_map::RawEntryMut;61pub use import::*;62use jrsonnet_gcmodule::{Cc, Trace};63pub use jrsonnet_interner::{IBytes, IStr};64pub use jrsonnet_parser as parser;65use jrsonnet_parser::*;66pub use obj::*;67use trace::{CompactFormat, TraceFormat};68pub use val::{ManifestFormat, Thunk, Val};6970pub trait Unbound: Trace {71 type Bound;72 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;73}7475#[derive(Clone, Trace)]76pub enum LazyBinding {77 Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),78 Bound(Thunk<Val>),79}8081impl Debug for LazyBinding {82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {83 write!(f, "LazyBinding")84 }85}86impl LazyBinding {87 pub fn evaluate(88 &self,89 s: State,90 sup: Option<ObjValue>,91 this: Option<ObjValue>,92 ) -> Result<Thunk<Val>> {93 match self {94 Self::Bindable(v) => v.bind(s, sup, this),95 Self::Bound(v) => Ok(v.clone()),96 }97 }98}99100101102pub trait ContextInitializer {103 fn initialize(&self, state: State, for_file: Source) -> Context;104105 fn as_any(&self) -> &dyn Any;106}107108109pub struct DummyContextInitializer;110impl ContextInitializer for DummyContextInitializer {111 fn initialize(&self, _state: State, _for_file: Source) -> Context {112 Context::default()113 }114 fn as_any(&self) -> &dyn Any {115 self116 }117}118119pub struct EvaluationSettings {120 121 pub max_stack: usize,122 123 pub max_trace: usize,124 125 pub tla_vars: HashMap<IStr, TlaArg>,126 127 128 pub context_initializer: Box<dyn ContextInitializer>,129 130 pub import_resolver: Box<dyn ImportResolver>,131 132 pub manifest_format: ManifestFormat,133 134 pub trace_format: Box<dyn TraceFormat>,135}136impl Default for EvaluationSettings {137 fn default() -> Self {138 Self {139 max_stack: 200,140 max_trace: 20,141 context_initializer: Box::new(DummyContextInitializer),142 tla_vars: HashMap::default(),143 import_resolver: Box::new(DummyImportResolver),144 manifest_format: ManifestFormat::Json {145 padding: 4,146 #[cfg(feature = "exp-preserve-order")]147 preserve_order: false,148 },149 trace_format: Box::new(CompactFormat {150 padding: 4,151 resolver: trace::PathResolver::Absolute,152 }),153 }154 }155}156157#[derive(Default)]158struct EvaluationData {159 160 stack_depth: usize,161 162 stack_generation: usize,163164 breakpoints: Breakpoints,165166 167 files: GcHashMap<SourcePath, FileData>,168}169struct FileData {170 string: Option<IStr>,171 bytes: Option<IBytes>,172 parsed: Option<LocExpr>,173 evaluated: Option<Val>,174175 evaluating: bool,176}177impl FileData {178 fn new_string(data: IStr) -> Self {179 Self {180 string: Some(data),181 bytes: None,182 parsed: None,183 evaluated: None,184 evaluating: false,185 }186 }187 fn new_bytes(data: IBytes) -> Self {188 Self {189 string: None,190 bytes: Some(data),191 parsed: None,192 evaluated: None,193 evaluating: false,194 }195 }196}197198#[allow(clippy::type_complexity)]199pub struct Breakpoint {200 loc: ExprLocation,201 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,202}203#[derive(Default)]204struct Breakpoints(Vec<Rc<Breakpoint>>);205impl Breakpoints {206 fn insert(207 &self,208 stack_depth: usize,209 stack_generation: usize,210 loc: &ExprLocation,211 result: Result<Val>,212 ) -> Result<Val> {213 if self.0.is_empty() {214 return result;215 }216 for item in &self.0 {217 if item.loc.belongs_to(loc) {218 let mut collected = item.collected.borrow_mut();219 let (depth, vals) = collected.entry(stack_generation).or_default();220 if stack_depth > *depth {221 vals.clear();222 }223 vals.push(result.clone());224 }225 }226 result227 }228}229230#[derive(Default)]231pub struct EvaluationStateInternals {232 233 data: RefCell<EvaluationData>,234 235 settings: RefCell<EvaluationSettings>,236}237238239#[derive(Default, Clone)]240pub struct State(Rc<EvaluationStateInternals>);241242impl State {243 244 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {245 let mut data = self.data_mut();246 let mut file = data.files.raw_entry_mut().from_key(&path);247248 let file = match file {249 RawEntryMut::Occupied(ref mut d) => d.get_mut(),250 RawEntryMut::Vacant(v) => {251 let data = self.settings().import_resolver.load_file_contents(&path)?;252 v.insert(253 path.clone(),254 FileData::new_string(255 std::str::from_utf8(&data)256 .map_err(|_| ImportBadFileUtf8(path.clone()))?257 .into(),258 ),259 )260 .1261 }262 };263 if let Some(str) = &file.string {264 return Ok(str.clone());265 }266 if file.string.is_none() {267 file.string = Some(268 file.bytes269 .as_ref()270 .expect("either string or bytes should be set")271 .clone()272 .cast_str()273 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,274 );275 }276 Ok(file.string.as_ref().expect("just set").clone())277 }278 279 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {280 let mut data = self.data_mut();281 let mut file = data.files.raw_entry_mut().from_key(&path);282283 let file = match file {284 RawEntryMut::Occupied(ref mut d) => d.get_mut(),285 RawEntryMut::Vacant(v) => {286 let data = self.settings().import_resolver.load_file_contents(&path)?;287 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))288 .1289 }290 };291 if let Some(str) = &file.bytes {292 return Ok(str.clone());293 }294 if file.bytes.is_none() {295 file.bytes = Some(296 file.string297 .as_ref()298 .expect("either string or bytes should be set")299 .clone()300 .cast_bytes(),301 );302 }303 Ok(file.bytes.as_ref().expect("just set").clone())304 }305 306 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {307 let mut data = self.data_mut();308 let mut file = data.files.raw_entry_mut().from_key(&path);309310 let file = match file {311 RawEntryMut::Occupied(ref mut d) => d.get_mut(),312 RawEntryMut::Vacant(v) => {313 let data = self.settings().import_resolver.load_file_contents(&path)?;314 v.insert(315 path.clone(),316 FileData::new_string(317 std::str::from_utf8(&data)318 .map_err(|_| ImportBadFileUtf8(path.clone()))?319 .into(),320 ),321 )322 .1323 }324 };325 if let Some(val) = &file.evaluated {326 return Ok(val.clone());327 }328 if file.string.is_none() {329 file.string = Some(330 std::str::from_utf8(331 file.bytes332 .as_ref()333 .expect("either string or bytes should be set"),334 )335 .map_err(|_| ImportBadFileUtf8(path.clone()))?336 .into(),337 );338 }339 let code = file.string.as_ref().expect("just set");340 let file_name = Source::new(path.clone(), code.clone());341 if file.parsed.is_none() {342 file.parsed = Some(343 jrsonnet_parser::parse(344 code,345 &ParserSettings {346 file_name: file_name.clone(),347 },348 )349 .map_err(|e| ImportSyntaxError {350 path: file_name.clone(),351 error: Box::new(e),352 })?,353 );354 }355 let parsed = file.parsed.as_ref().expect("just set").clone();356 if file.evaluating {357 throw!(InfiniteRecursionDetected)358 }359 file.evaluating = true;360 361 drop(data);362 let res = evaluate(363 self.clone(),364 self.create_default_context(file_name),365 &parsed,366 );367368 let mut data = self.data_mut();369 let mut file = data.files.raw_entry_mut().from_key(&path);370371 let file = match file {372 RawEntryMut::Occupied(ref mut d) => d.get_mut(),373 RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),374 };375 file.evaluating = false;376 match res {377 Ok(v) => {378 file.evaluated = Some(v.clone());379 Ok(v)380 }381 Err(e) => Err(e),382 }383 }384385 386 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {387 let resolved = self.resolve_from(from, path)?;388 self.import_resolved(resolved)389 }390 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {391 let resolved = self.resolve(path)?;392 self.import_resolved(resolved)393 }394395 396 pub fn create_default_context(&self, source: Source) -> Context {397 let context_initializer = &self.settings().context_initializer;398 context_initializer.initialize(self.clone(), source)399 }400401 402 pub fn push<T>(403 &self,404 e: CallLocation,405 frame_desc: impl FnOnce() -> String,406 f: impl FnOnce() -> Result<T>,407 ) -> Result<T> {408 {409 let mut data = self.data_mut();410 let stack_depth = &mut data.stack_depth;411 if *stack_depth > self.max_stack() {412 413 drop(data);414 throw!(StackOverflow);415 }416 *stack_depth += 1;417 }418 let result = f();419 {420 let mut data = self.data_mut();421 data.stack_depth -= 1;422 data.stack_generation += 1;423 }424 if let Err(mut err) = result {425 err.trace_mut().0.push(StackTraceElement {426 location: e.0.cloned(),427 desc: frame_desc(),428 });429 return Err(err);430 }431 result432 }433434 435 pub fn push_val(436 &self,437 e: &ExprLocation,438 frame_desc: impl FnOnce() -> String,439 f: impl FnOnce() -> Result<Val>,440 ) -> Result<Val> {441 {442 let mut data = self.data_mut();443 let stack_depth = &mut data.stack_depth;444 if *stack_depth > self.max_stack() {445 446 drop(data);447 throw!(StackOverflow);448 }449 *stack_depth += 1;450 }451 let mut result = f();452 {453 let mut data = self.data_mut();454 data.stack_depth -= 1;455 data.stack_generation += 1;456 result = data457 .breakpoints458 .insert(data.stack_depth, data.stack_generation, e, result);459 }460 if let Err(mut err) = result {461 err.trace_mut().0.push(StackTraceElement {462 location: Some(e.clone()),463 desc: frame_desc(),464 });465 return Err(err);466 }467 result468 }469 470 pub fn push_description<T>(471 &self,472 frame_desc: impl FnOnce() -> String,473 f: impl FnOnce() -> Result<T>,474 ) -> Result<T> {475 {476 let mut data = self.data_mut();477 let stack_depth = &mut data.stack_depth;478 if *stack_depth > self.max_stack() {479 480 drop(data);481 throw!(StackOverflow);482 }483 *stack_depth += 1;484 }485 let result = f();486 {487 let mut data = self.data_mut();488 data.stack_depth -= 1;489 data.stack_generation += 1;490 }491 if let Err(mut err) = result {492 err.trace_mut().0.push(StackTraceElement {493 location: None,494 desc: frame_desc(),495 });496 return Err(err);497 }498 result499 }500501 502 503 pub fn stringify_err(&self, e: &LocError) -> String {504 let mut out = String::new();505 self.settings()506 .trace_format507 .write_trace(&mut out, self, e)508 .unwrap();509 out510 }511512 pub fn manifest(&self, val: Val) -> Result<IStr> {513 self.push_description(514 || "manifestification".to_string(),515 || val.manifest(self.clone(), &self.manifest_format()),516 )517 }518 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {519 val.manifest_multi(self.clone(), &self.manifest_format())520 }521 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {522 val.manifest_stream(self.clone(), &self.manifest_format())523 }524525 526 pub fn with_tla(&self, val: Val) -> Result<Val> {527 Ok(match val {528 Val::Func(func) => self.push_description(529 || "during TLA call".to_owned(),530 || {531 func.evaluate(532 self.clone(),533 self.create_default_context(Source::new_virtual(534 "<tla>".into(),535 IStr::empty(),536 )),537 CallLocation::native(),538 &self.settings().tla_vars,539 true,540 )541 },542 )?,543 v => v,544 })545 }546}547548549impl State {550 551 552 553 fn data_mut(&self) -> RefMut<EvaluationData> {554 self.0.data.borrow_mut()555 }556 pub fn settings(&self) -> Ref<EvaluationSettings> {557 self.0.settings.borrow()558 }559 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {560 self.0.settings.borrow_mut()561 }562}563564565impl State {566 567 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {568 let code = code.into();569 let source = Source::new_virtual(name.into(), code.clone());570 let parsed = jrsonnet_parser::parse(571 &code,572 &ParserSettings {573 file_name: source.clone(),574 },575 )576 .map_err(|e| ImportSyntaxError {577 path: source.clone(),578 error: Box::new(e),579 })?;580 evaluate(self.clone(), self.create_default_context(source), &parsed)581 }582}583584585impl State {586 pub fn add_tla(&self, name: IStr, value: Val) {587 self.settings_mut()588 .tla_vars589 .insert(name, TlaArg::Val(value));590 }591 pub fn add_tla_str(&self, name: IStr, value: IStr) {592 self.settings_mut()593 .tla_vars594 .insert(name, TlaArg::String(value));595 }596 pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {597 let source_name = format!("<top-level-arg:{}>", name);598 let source = Source::new_virtual(source_name.into(), code.into());599 let parsed = jrsonnet_parser::parse(600 code,601 &ParserSettings {602 file_name: source.clone(),603 },604 )605 .map_err(|e| ImportSyntaxError {606 path: source,607 error: Box::new(e),608 })?;609 self.settings_mut()610 .tla_vars611 .insert(name, TlaArg::Code(parsed));612 Ok(())613 }614615 616 #[allow(clippy::missing_panics_doc)]617 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {618 self.import_resolver().resolve_from(from, path.as_ref())619 }620621 622 #[allow(clippy::missing_panics_doc)]623 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {624 self.import_resolver().resolve(path.as_ref())625 }626 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {627 Ref::map(self.settings(), |s| &*s.import_resolver)628 }629 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {630 self.settings_mut().import_resolver = resolver;631 }632 pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {633 Ref::map(self.settings(), |s| &*s.context_initializer)634 }635636 pub fn manifest_format(&self) -> ManifestFormat {637 self.settings().manifest_format.clone()638 }639 pub fn set_manifest_format(&self, format: ManifestFormat) {640 self.settings_mut().manifest_format = format;641 }642643 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {644 Ref::map(self.settings(), |s| &*s.trace_format)645 }646 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {647 self.settings_mut().trace_format = format;648 }649650 pub fn max_trace(&self) -> usize {651 self.settings().max_trace652 }653 pub fn set_max_trace(&self, trace: usize) {654 self.settings_mut().max_trace = trace;655 }656657 pub fn max_stack(&self) -> usize {658 self.settings().max_stack659 }660 pub fn set_max_stack(&self, trace: usize) {661 self.settings_mut().max_stack = trace;662 }663}