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 borrow::Cow,48 cell::{Ref, RefCell, RefMut},49 collections::HashMap,50 fmt::{self, Debug},51 path::{Path, PathBuf},52 rc::Rc,53};5455pub use ctx::*;56pub use dynamic::*;57use error::{Error::*, LocError, Result, StackTraceElement};58pub use evaluate::*;59use function::{CallLocation, TlaArg};60use gc::{GcHashMap, TraceBox};61use hashbrown::hash_map::RawEntryMut;62pub use import::*;63use jrsonnet_gcmodule::{Cc, Trace};64pub use jrsonnet_interner::{IBytes, IStr};65pub use jrsonnet_parser as parser;66use jrsonnet_parser::*;67pub use obj::*;68use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};69pub use val::{ManifestFormat, Thunk, Val};7071pub trait Unbound: Trace {72 type Bound;73 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;74}7576#[derive(Clone, Trace)]77pub enum LazyBinding {78 Bindable(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),79 Bound(Thunk<Val>),80}8182impl Debug for LazyBinding {83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {84 write!(f, "LazyBinding")85 }86}87impl LazyBinding {88 pub fn evaluate(89 &self,90 s: State,91 sup: Option<ObjValue>,92 this: Option<ObjValue>,93 ) -> Result<Thunk<Val>> {94 match self {95 Self::Bindable(v) => v.bind(s, sup, this),96 Self::Bound(v) => Ok(v.clone()),97 }98 }99}100101102103pub trait ContextInitializer {104 fn initialize(&self, state: State, for_file: Source) -> Context;105106 107 108 109 110 111 unsafe fn as_any(&self) -> &dyn Any;112}113114115pub struct DummyContextInitializer;116impl ContextInitializer for DummyContextInitializer {117 fn initialize(&self, _state: State, _for_file: Source) -> Context {118 Context::default()119 }120 unsafe fn as_any(&self) -> &dyn Any {121 panic!("`as_any(&self)` is not supported by dummy initializer")122 }123}124125pub struct EvaluationSettings {126 127 pub max_stack: usize,128 129 pub max_trace: usize,130 131 pub tla_vars: HashMap<IStr, TlaArg>,132 133 134 pub context_initializer: Box<dyn ContextInitializer>,135 136 pub import_resolver: Box<dyn ImportResolver>,137 138 pub manifest_format: ManifestFormat,139 140 pub trace_format: Box<dyn TraceFormat>,141}142impl Default for EvaluationSettings {143 fn default() -> Self {144 Self {145 max_stack: 200,146 max_trace: 20,147 context_initializer: Box::new(DummyContextInitializer),148 tla_vars: HashMap::default(),149 import_resolver: Box::new(DummyImportResolver),150 manifest_format: ManifestFormat::Json {151 padding: 4,152 #[cfg(feature = "exp-preserve-order")]153 preserve_order: false,154 },155 trace_format: Box::new(CompactFormat {156 padding: 4,157 resolver: trace::PathResolver::Absolute,158 }),159 }160 }161}162163#[derive(Default)]164struct EvaluationData {165 166 stack_depth: usize,167 168 stack_generation: usize,169170 breakpoints: Breakpoints,171172 173 files: GcHashMap<PathBuf, FileData>,174 175 176 volatile_files: GcHashMap<String, String>,177}178struct FileData {179 string: Option<IStr>,180 bytes: Option<IBytes>,181 parsed: Option<LocExpr>,182 evaluated: Option<Val>,183184 evaluating: bool,185}186impl FileData {187 fn new_string(data: IStr) -> Self {188 Self {189 string: Some(data),190 bytes: None,191 parsed: None,192 evaluated: None,193 evaluating: false,194 }195 }196 fn new_bytes(data: IBytes) -> Self {197 Self {198 string: None,199 bytes: Some(data),200 parsed: None,201 evaluated: None,202 evaluating: false,203 }204 }205}206207#[allow(clippy::type_complexity)]208pub struct Breakpoint {209 loc: ExprLocation,210 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,211}212#[derive(Default)]213struct Breakpoints(Vec<Rc<Breakpoint>>);214impl Breakpoints {215 fn insert(216 &self,217 stack_depth: usize,218 stack_generation: usize,219 loc: &ExprLocation,220 result: Result<Val>,221 ) -> Result<Val> {222 if self.0.is_empty() {223 return result;224 }225 for item in &self.0 {226 if item.loc.belongs_to(loc) {227 let mut collected = item.collected.borrow_mut();228 let (depth, vals) = collected.entry(stack_generation).or_default();229 if stack_depth > *depth {230 vals.clear();231 }232 vals.push(result.clone());233 }234 }235 result236 }237}238239#[derive(Default)]240pub struct EvaluationStateInternals {241 242 data: RefCell<EvaluationData>,243 244 settings: RefCell<EvaluationSettings>,245}246247248#[derive(Default, Clone)]249pub struct State(Rc<EvaluationStateInternals>);250251impl State {252 pub fn import_str(&self, path: PathBuf) -> Result<IStr> {253 let mut data = self.data_mut();254 let mut file = data.files.raw_entry_mut().from_key(&path);255256 let file = match file {257 RawEntryMut::Occupied(ref mut d) => d.get_mut(),258 RawEntryMut::Vacant(v) => {259 let data = self.settings().import_resolver.load_file_contents(&path)?;260 v.insert(261 path.clone(),262 FileData::new_string(263 std::str::from_utf8(&data)264 .map_err(|_| ImportBadFileUtf8(path.clone()))?265 .into(),266 ),267 )268 .1269 }270 };271 if let Some(str) = &file.string {272 return Ok(str.clone());273 }274 if file.string.is_none() {275 file.string = Some(276 file.bytes277 .as_ref()278 .expect("either string or bytes should be set")279 .clone()280 .cast_str()281 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,282 );283 }284 Ok(file.string.as_ref().expect("just set").clone())285 }286 pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {287 let mut data = self.data_mut();288 let mut file = data.files.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 pub fn import(&self, path: PathBuf) -> Result<Val> {313 let mut data = self.data_mut();314 let mut file = data.files.raw_entry_mut().from_key(&path);315316 let file = match file {317 RawEntryMut::Occupied(ref mut d) => d.get_mut(),318 RawEntryMut::Vacant(v) => {319 let data = self.settings().import_resolver.load_file_contents(&path)?;320 v.insert(321 path.clone(),322 FileData::new_string(323 std::str::from_utf8(&data)324 .map_err(|_| ImportBadFileUtf8(path.clone()))?325 .into(),326 ),327 )328 .1329 }330 };331 if let Some(val) = &file.evaluated {332 return Ok(val.clone());333 }334 if file.string.is_none() {335 file.string = Some(336 std::str::from_utf8(337 file.bytes338 .as_ref()339 .expect("either string or bytes should be set"),340 )341 .map_err(|_| ImportBadFileUtf8(path.clone()))?342 .into(),343 );344 }345 let code = file.string.as_ref().expect("just set");346 let file_name = Source::new(path.clone()).expect("resolver should return correct name");347 if file.parsed.is_none() {348 file.parsed = Some(349 jrsonnet_parser::parse(350 code,351 &ParserSettings {352 file_name: file_name.clone(),353 },354 )355 .map_err(|e| ImportSyntaxError {356 path: file_name.clone(),357 source_code: code.clone(),358 error: Box::new(e),359 })?,360 );361 }362 let parsed = file.parsed.as_ref().expect("just set").clone();363 if file.evaluating {364 throw!(InfiniteRecursionDetected)365 }366 file.evaluating = true;367 368 drop(data);369 let res = evaluate(370 self.clone(),371 self.create_default_context(file_name),372 &parsed,373 );374375 let mut data = self.data_mut();376 let mut file = data.files.raw_entry_mut().from_key(&path);377378 let file = match file {379 RawEntryMut::Occupied(ref mut d) => d.get_mut(),380 RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),381 };382 file.evaluating = false;383 match res {384 Ok(v) => {385 file.evaluated = Some(v.clone());386 Ok(v)387 }388 Err(e) => Err(e),389 }390 }391392 pub fn get_source(&self, name: Source) -> Option<String> {393 let data = self.data();394 match name.repr() {395 Ok(real) => data396 .files397 .get(real)398 .and_then(|f| f.string.as_ref())399 .map(ToString::to_string),400 Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),401 }402 }403 pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {404 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)405 }406 pub fn map_from_source_location(407 &self,408 file: Source,409 line: usize,410 column: usize,411 ) -> Option<usize> {412 location_to_offset(413 &self.get_source(file).expect("file not found"),414 line,415 column,416 )417 }418419 420 pub fn create_default_context(&self, source: Source) -> Context {421 let context_initializer = &self.settings().context_initializer;422 context_initializer.initialize(self.clone(), source)423 }424425 426 pub fn push<T>(427 &self,428 e: CallLocation,429 frame_desc: impl FnOnce() -> String,430 f: impl FnOnce() -> Result<T>,431 ) -> Result<T> {432 {433 let mut data = self.data_mut();434 let stack_depth = &mut data.stack_depth;435 if *stack_depth > self.max_stack() {436 437 drop(data);438 throw!(StackOverflow);439 }440 *stack_depth += 1;441 }442 let result = f();443 {444 let mut data = self.data_mut();445 data.stack_depth -= 1;446 data.stack_generation += 1;447 }448 if let Err(mut err) = result {449 err.trace_mut().0.push(StackTraceElement {450 location: e.0.cloned(),451 desc: frame_desc(),452 });453 return Err(err);454 }455 result456 }457458 459 pub fn push_val(460 &self,461 e: &ExprLocation,462 frame_desc: impl FnOnce() -> String,463 f: impl FnOnce() -> Result<Val>,464 ) -> Result<Val> {465 {466 let mut data = self.data_mut();467 let stack_depth = &mut data.stack_depth;468 if *stack_depth > self.max_stack() {469 470 drop(data);471 throw!(StackOverflow);472 }473 *stack_depth += 1;474 }475 let mut result = f();476 {477 let mut data = self.data_mut();478 data.stack_depth -= 1;479 data.stack_generation += 1;480 result = data481 .breakpoints482 .insert(data.stack_depth, data.stack_generation, e, result);483 }484 if let Err(mut err) = result {485 err.trace_mut().0.push(StackTraceElement {486 location: Some(e.clone()),487 desc: frame_desc(),488 });489 return Err(err);490 }491 result492 }493 494 pub fn push_description<T>(495 &self,496 frame_desc: impl FnOnce() -> String,497 f: impl FnOnce() -> Result<T>,498 ) -> Result<T> {499 {500 let mut data = self.data_mut();501 let stack_depth = &mut data.stack_depth;502 if *stack_depth > self.max_stack() {503 504 drop(data);505 throw!(StackOverflow);506 }507 *stack_depth += 1;508 }509 let result = f();510 {511 let mut data = self.data_mut();512 data.stack_depth -= 1;513 data.stack_generation += 1;514 }515 if let Err(mut err) = result {516 err.trace_mut().0.push(StackTraceElement {517 location: None,518 desc: frame_desc(),519 });520 return Err(err);521 }522 result523 }524525 526 527 pub fn stringify_err(&self, e: &LocError) -> String {528 let mut out = String::new();529 self.settings()530 .trace_format531 .write_trace(&mut out, self, e)532 .unwrap();533 out534 }535536 pub fn manifest(&self, val: Val) -> Result<IStr> {537 self.push_description(538 || "manifestification".to_string(),539 || val.manifest(self.clone(), &self.manifest_format()),540 )541 }542 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {543 val.manifest_multi(self.clone(), &self.manifest_format())544 }545 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {546 val.manifest_stream(self.clone(), &self.manifest_format())547 }548549 550 pub fn with_tla(&self, val: Val) -> Result<Val> {551 Ok(match val {552 Val::Func(func) => self.push_description(553 || "during TLA call".to_owned(),554 || {555 func.evaluate(556 self.clone(),557 self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),558 CallLocation::native(),559 &self.settings().tla_vars,560 true,561 )562 },563 )?,564 v => v,565 })566 }567}568569570impl State {571 fn data(&self) -> Ref<EvaluationData> {572 self.0.data.borrow()573 }574 fn data_mut(&self) -> RefMut<EvaluationData> {575 self.0.data.borrow_mut()576 }577 pub fn settings(&self) -> Ref<EvaluationSettings> {578 self.0.settings.borrow()579 }580 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {581 self.0.settings.borrow_mut()582 }583}584585586impl State {587 588 pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {589 let source = Source::new_virtual(Cow::Owned(name.clone()));590 let parsed = jrsonnet_parser::parse(591 &code,592 &ParserSettings {593 file_name: source.clone(),594 },595 )596 .map_err(|e| ImportSyntaxError {597 path: source.clone(),598 source_code: code.clone().into(),599 error: Box::new(e),600 })?;601 self.data_mut().volatile_files.insert(name, code);602 evaluate(self.clone(), self.create_default_context(source), &parsed)603 }604}605606607impl State {608 pub fn add_tla(&self, name: IStr, value: Val) {609 self.settings_mut()610 .tla_vars611 .insert(name, TlaArg::Val(value));612 }613 pub fn add_tla_str(&self, name: IStr, value: IStr) {614 self.settings_mut()615 .tla_vars616 .insert(name, TlaArg::String(value));617 }618 pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {619 let source_name = format!("<top-level-arg:{}>", name);620 let source = Source::new_virtual(Cow::Owned(source_name.clone()));621 let parsed = jrsonnet_parser::parse(622 code,623 &ParserSettings {624 file_name: source.clone(),625 },626 )627 .map_err(|e| ImportSyntaxError {628 path: source,629 source_code: code.into(),630 error: Box::new(e),631 })?;632 self.data_mut()633 .volatile_files634 .insert(source_name, code.to_owned());635 self.settings_mut()636 .tla_vars637 .insert(name, TlaArg::Code(parsed));638 Ok(())639 }640641 pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {642 self.settings()643 .import_resolver644 .resolve_file(from, path.as_ref())645 }646647 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {648 Ref::map(self.settings(), |s| &*s.import_resolver)649 }650 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {651 self.settings_mut().import_resolver = resolver;652 }653 pub fn context_initializer(&self) -> Ref<dyn ContextInitializer> {654 Ref::map(self.settings(), |s| &*s.context_initializer)655 }656657 pub fn manifest_format(&self) -> ManifestFormat {658 self.settings().manifest_format.clone()659 }660 pub fn set_manifest_format(&self, format: ManifestFormat) {661 self.settings_mut().manifest_format = format;662 }663664 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {665 Ref::map(self.settings(), |s| &*s.trace_format)666 }667 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {668 self.settings_mut().trace_format = format;669 }670671 pub fn max_trace(&self) -> usize {672 self.settings().max_trace673 }674 pub fn set_max_trace(&self, trace: usize) {675 self.settings_mut().max_trace = trace;676 }677678 pub fn max_stack(&self) -> usize {679 self.settings().max_stack680 }681 pub fn set_max_stack(&self, trace: usize) {682 self.settings_mut().max_stack = trace;683 }684}