12#![cfg_attr(feature = "nightly", feature(thread_local))]3#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5 clippy::all,6 clippy::nursery,7 clippy::pedantic,8 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 20 clippy::must_use_candidate,21 22 clippy::missing_errors_doc,23 24 clippy::needless_pass_by_value,25 26 clippy::wildcard_imports,27 clippy::enum_glob_use,28 clippy::module_name_repetitions,29 30 clippy::cast_precision_loss,31 clippy::cast_possible_wrap,32 clippy::cast_possible_truncation,33 clippy::cast_sign_loss,34 35 36 clippy::use_self,37 38 clippy::iter_with_drain,39 40 clippy::missing_const_for_fn,41)]424344extern crate self as jrsonnet_evaluator;4546mod ctx;47mod dynamic;48pub mod error;49mod evaluate;50pub mod function;51pub mod gc;52mod import;53mod integrations;54mod map;55mod obj;56pub mod stack;57pub mod stdlib;58mod tla;59pub mod trace;60pub mod typed;61pub mod val;6263use std::{64 any::Any,65 cell::{Ref, RefCell, RefMut},66 fmt::{self, Debug},67 path::Path,68};6970pub use ctx::*;71pub use dynamic::*;72pub use error::{Error::*, LocError, Result, ResultExt};73pub use evaluate::*;74use function::CallLocation;75use gc::{GcHashMap, TraceBox};76use hashbrown::hash_map::RawEntryMut;77pub use import::*;78use jrsonnet_gcmodule::{Cc, Trace};79pub use jrsonnet_interner::{IBytes, IStr};80pub use jrsonnet_parser as parser;81use jrsonnet_parser::*;82pub use obj::*;83use stack::check_depth;84pub use tla::apply_tla;85pub use val::{ManifestFormat, Thunk, Val};86878889pub trait Unbound: Trace {90 91 type Bound;92 93 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;94}95969798#[derive(Clone, Trace)]99pub enum MaybeUnbound {100 101 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),102 103 Bound(Thunk<Val>),104}105106impl Debug for MaybeUnbound {107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {108 write!(f, "MaybeUnbound")109 }110}111impl MaybeUnbound {112 113 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {114 match self {115 Self::Unbound(v) => v.bind(sup, this),116 Self::Bound(v) => Ok(v.evaluate()?),117 }118 }119}120121122123pub trait ContextInitializer: Trace {124 125 fn initialize(&self, state: State, for_file: Source) -> Context;126 127 128 fn as_any(&self) -> &dyn Any;129}130131132#[derive(Trace)]133pub struct DummyContextInitializer;134impl ContextInitializer for DummyContextInitializer {135 fn initialize(&self, state: State, _for_file: Source) -> Context {136 ContextBuilder::new(state).build()137 }138 fn as_any(&self) -> &dyn Any {139 self140 }141}142143144#[derive(Trace)]145pub struct EvaluationSettings {146 147 148 pub context_initializer: TraceBox<dyn ContextInitializer>,149 150 pub import_resolver: TraceBox<dyn ImportResolver>,151}152impl Default for EvaluationSettings {153 fn default() -> Self {154 Self {155 context_initializer: tb!(DummyContextInitializer),156 import_resolver: tb!(DummyImportResolver),157 }158 }159}160161#[derive(Trace)]162struct FileData {163 string: Option<IStr>,164 bytes: Option<IBytes>,165 parsed: Option<LocExpr>,166 evaluated: Option<Val>,167168 evaluating: bool,169}170impl FileData {171 fn new_string(data: IStr) -> Self {172 Self {173 string: Some(data),174 bytes: None,175 parsed: None,176 evaluated: None,177 evaluating: false,178 }179 }180 fn new_bytes(data: IBytes) -> Self {181 Self {182 string: None,183 bytes: Some(data),184 parsed: None,185 evaluated: None,186 evaluating: false,187 }188 }189}190191#[derive(Default, Trace)]192pub struct EvaluationStateInternals {193 194 file_cache: RefCell<GcHashMap<SourcePath, FileData>>,195 196 settings: RefCell<EvaluationSettings>,197}198199200#[derive(Default, Clone, Trace)]201pub struct State(Cc<EvaluationStateInternals>);202203impl State {204 205 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {206 let mut file_cache = self.file_cache();207 let mut file = file_cache.raw_entry_mut().from_key(&path);208209 let file = match file {210 RawEntryMut::Occupied(ref mut d) => d.get_mut(),211 RawEntryMut::Vacant(v) => {212 let data = self.settings().import_resolver.load_file_contents(&path)?;213 v.insert(214 path.clone(),215 FileData::new_string(216 std::str::from_utf8(&data)217 .map_err(|_| ImportBadFileUtf8(path.clone()))?218 .into(),219 ),220 )221 .1222 }223 };224 if let Some(str) = &file.string {225 return Ok(str.clone());226 }227 if file.string.is_none() {228 file.string = Some(229 file.bytes230 .as_ref()231 .expect("either string or bytes should be set")232 .clone()233 .cast_str()234 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,235 );236 }237 Ok(file.string.as_ref().expect("just set").clone())238 }239 240 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {241 let mut file_cache = self.file_cache();242 let mut file = file_cache.raw_entry_mut().from_key(&path);243244 let file = match file {245 RawEntryMut::Occupied(ref mut d) => d.get_mut(),246 RawEntryMut::Vacant(v) => {247 let data = self.settings().import_resolver.load_file_contents(&path)?;248 v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))249 .1250 }251 };252 if let Some(str) = &file.bytes {253 return Ok(str.clone());254 }255 if file.bytes.is_none() {256 file.bytes = Some(257 file.string258 .as_ref()259 .expect("either string or bytes should be set")260 .clone()261 .cast_bytes(),262 );263 }264 Ok(file.bytes.as_ref().expect("just set").clone())265 }266 267 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {268 let mut file_cache = self.file_cache();269 let mut file = file_cache.raw_entry_mut().from_key(&path);270271 let file = match file {272 RawEntryMut::Occupied(ref mut d) => d.get_mut(),273 RawEntryMut::Vacant(v) => {274 let data = self.settings().import_resolver.load_file_contents(&path)?;275 v.insert(276 path.clone(),277 FileData::new_string(278 std::str::from_utf8(&data)279 .map_err(|_| ImportBadFileUtf8(path.clone()))?280 .into(),281 ),282 )283 .1284 }285 };286 if let Some(val) = &file.evaluated {287 return Ok(val.clone());288 }289 if file.string.is_none() {290 file.string = Some(291 std::str::from_utf8(292 file.bytes293 .as_ref()294 .expect("either string or bytes should be set"),295 )296 .map_err(|_| ImportBadFileUtf8(path.clone()))?297 .into(),298 );299 }300 let code = file.string.as_ref().expect("just set");301 let file_name = Source::new(path.clone(), code.clone());302 if file.parsed.is_none() {303 file.parsed = Some(304 jrsonnet_parser::parse(305 code,306 &ParserSettings {307 source: file_name.clone(),308 },309 )310 .map_err(|e| ImportSyntaxError {311 path: file_name.clone(),312 error: Box::new(e),313 })?,314 );315 }316 let parsed = file.parsed.as_ref().expect("just set").clone();317 if file.evaluating {318 throw!(InfiniteRecursionDetected)319 }320 file.evaluating = true;321 322 drop(file_cache);323 let res = evaluate(self.create_default_context(file_name), &parsed);324325 let mut file_cache = self.file_cache();326 let mut file = file_cache.raw_entry_mut().from_key(&path);327328 let RawEntryMut::Occupied(file) = &mut file else {329 unreachable!("this file was just here!")330 };331 let file = file.get_mut();332 file.evaluating = false;333 match res {334 Ok(v) => {335 file.evaluated = Some(v.clone());336 Ok(v)337 }338 Err(e) => Err(e),339 }340 }341342 343 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {344 let resolved = self.resolve_from(from, path)?;345 self.import_resolved(resolved)346 }347 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {348 let resolved = self.resolve(path)?;349 self.import_resolved(resolved)350 }351352 353 pub fn create_default_context(&self, source: Source) -> Context {354 let context_initializer = &self.settings().context_initializer;355 context_initializer.initialize(self.clone(), source)356 }357358 359 pub fn push<T>(360 e: CallLocation<'_>,361 frame_desc: impl FnOnce() -> String,362 f: impl FnOnce() -> Result<T>,363 ) -> Result<T> {364 let _guard = check_depth()?;365366 f().with_description_src(e, frame_desc)367 }368369 370 pub fn push_val(371 &self,372 e: &ExprLocation,373 frame_desc: impl FnOnce() -> String,374 f: impl FnOnce() -> Result<Val>,375 ) -> Result<Val> {376 let _guard = check_depth()?;377378 f().with_description_src(e, frame_desc)379 }380 381 pub fn push_description<T>(382 frame_desc: impl FnOnce() -> String,383 f: impl FnOnce() -> Result<T>,384 ) -> Result<T> {385 let _guard = check_depth()?;386387 f().with_description(frame_desc)388 }389}390391392impl State {393 fn file_cache(&self) -> RefMut<'_, GcHashMap<SourcePath, FileData>> {394 self.0.file_cache.borrow_mut()395 }396 pub fn settings(&self) -> Ref<'_, EvaluationSettings> {397 self.0.settings.borrow()398 }399 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {400 self.0.settings.borrow_mut()401 }402}403404405impl State {406 407 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {408 let code = code.into();409 let source = Source::new_virtual(name.into(), code.clone());410 let parsed = jrsonnet_parser::parse(411 &code,412 &ParserSettings {413 source: source.clone(),414 },415 )416 .map_err(|e| ImportSyntaxError {417 path: source.clone(),418 error: Box::new(e),419 })?;420 evaluate(self.create_default_context(source), &parsed)421 }422}423424425impl State {426 427 #[allow(clippy::missing_panics_doc)]428 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {429 self.import_resolver().resolve_from(from, path.as_ref())430 }431432 433 #[allow(clippy::missing_panics_doc)]434 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {435 self.import_resolver().resolve(path.as_ref())436 }437 pub fn import_resolver(&self) -> Ref<'_, dyn ImportResolver> {438 Ref::map(self.settings(), |s| &*s.import_resolver)439 }440 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {441 self.settings_mut().import_resolver = TraceBox(resolver);442 }443 pub fn context_initializer(&self) -> Ref<'_, dyn ContextInitializer> {444 Ref::map(self.settings(), |s| &*s.context_initializer)445 }446}