difftreelog
feat(ir) source url
in: master
7 files changed
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod obj;19pub mod stack;20pub mod stdlib;21pub mod tla;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27 any::Any,28 cell::{RefCell, RefMut},29 clone::Clone,30 collections::hash_map::Entry,31 fmt::{self, Debug},32 marker::PhantomData,33 rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt};39pub use evaluate::ensure_sufficient_stack;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};43pub use jrsonnet_interner::{IBytes, IStr};44use jrsonnet_ir::Expr;45pub use jrsonnet_ir::{NumValue, Source, SourcePath, Span};46#[doc(hidden)]47pub use jrsonnet_macros;4849#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5152pub use error::SyntaxError;53pub use obj::*;54pub use rustc_hash;55use rustc_hash::FxHashMap;56use stack::check_depth;57pub use tla::apply_tla;58pub use val::{Thunk, Val};5960pub mod analyze;61use self::analyze::{LExpr, analyze_root};62use crate::gc::WithCapacityExt as _;6364#[allow(clippy::needless_return)]65pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {66 #[cfg(feature = "peg-parser")]67 {68 use std::sync::LazyLock;69 static USE_LEGACY_PARSER: LazyLock<bool> =70 LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7172 if *USE_LEGACY_PARSER {73 return parse_peg(code, source);74 }75 }76 #[cfg(feature = "ir-parser")]77 {78 return parse_ir(code, source);79 }80 #[cfg(feature = "peg-parser")]81 {82 return parse_peg(code, source);83 }84}8586#[cfg(feature = "ir-parser")]87fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {88 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {89 SyntaxError {90 message: e.message,91 location: e.location,92 }93 })94}9596#[cfg(feature = "peg-parser")]97fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {98 jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {99 let message = e100 .expected101 .tokens()102 .find(|t| t.starts_with("!!!"))103 .map_or_else(104 || {105 format!(106 "expected {}, got {:?}",107 e.expected,108 code.chars()109 .nth(e.location.0)110 .map_or_else(|| "EOF".into(), |c: char| c.to_string())111 )112 },113 |v| v[3..].into(),114 );115 SyntaxError {116 message,117 location: e.location,118 }119 })120}121122cc_dyn!(123 #[derive(Clone)]124 CcUnbound<V>,125 Unbound<Bound = V>126);127128/// Thunk without bound `super`/`this`129/// object inheritance may be overriden multiple times, and will be fixed only on field read130pub trait Unbound: Trace {131 /// Type of value after object context is bound132 type Bound;133 /// Create value bound to specified object context134 fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;135}136137/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code138/// Standard jsonnet fields are always unbound139#[derive(Clone, Trace)]140pub enum MaybeUnbound {141 /// Value needs to be bound to `this`/`super`142 Unbound(CcUnbound<Val>),143 /// Value is object-independent144 Bound(Thunk<Val>),145}146147impl Debug for MaybeUnbound {148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {149 write!(f, "MaybeUnbound")150 }151}152impl MaybeUnbound {153 /// Attach object context to value, if required154 pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {155 match self {156 Self::Unbound(v) => v.0.bind(sup_this),157 Self::Bound(v) => Ok(v.evaluate()?),158 }159 }160}161162cc_dyn!(CcContextInitializer, ContextInitializer);163164/// During import, this trait will be called to create initial context for file.165/// It may initialize global variables, stdlib for example.166pub trait ContextInitializer {167 /// For composability: extend builder. May panic if this initialization is not supported,168 /// and the context may only be created via `initialize`.169 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);170 /// Allows upcasting from abstract to concrete context initializer.171 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.172 fn as_any(&self) -> &dyn Any;173}174impl<T> ContextInitializer for &T175where176 T: ContextInitializer,177{178 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {179 (*self).populate(for_file, builder);180 }181182 fn as_any(&self) -> &dyn Any {183 (*self).as_any()184 }185}186187/// Context initializer which adds nothing.188impl ContextInitializer for () {189 fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}190 fn as_any(&self) -> &dyn Any {191 self192 }193}194195impl<T> ContextInitializer for Option<T>196where197 T: ContextInitializer + 'static,198{199 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {200 if let Some(ctx) = self {201 ctx.populate(for_file, builder);202 }203 }204205 fn as_any(&self) -> &dyn Any {206 self207 }208}209210macro_rules! impl_context_initializer {211 ($($gen:ident)*) => {212 #[allow(non_snake_case)]213 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {214 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {215 let ($($gen,)*) = self;216 $($gen.populate(for_file.clone(), builder);)*217 }218 fn as_any(&self) -> &dyn Any {219 self220 }221 }222 };223 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {224 impl_context_initializer!($($cur)*);225 impl_context_initializer!($($cur)* $c @ $($rest)*);226 };227 ($($cur:ident)* @) => {228 impl_context_initializer!($($cur)*);229 }230}231impl_context_initializer! {232 A @ B C D E F G233}234235#[derive(Trace)]236struct FileData {237 string: Option<IStr>,238 bytes: Option<IBytes>,239 parsed: Option<Rc<Expr>>,240 evaluated: Option<Val>,241242 evaluating: bool,243}244impl FileData {245 fn new_string(data: IStr) -> Self {246 Self {247 string: Some(data),248 bytes: None,249 parsed: None,250 evaluated: None,251 evaluating: false,252 }253 }254 fn new_bytes(data: IBytes) -> Self {255 Self {256 string: None,257 bytes: Some(data),258 parsed: None,259 evaluated: None,260 evaluating: false,261 }262 }263 pub(crate) fn get_string(&mut self) -> Option<IStr> {264 if self.string.is_none() {265 self.string = Some(266 self.bytes267 .as_ref()268 .expect("either string or bytes should be set")269 .clone()270 .cast_str()?,271 );272 }273 Some(self.string.clone().expect("just set"))274 }275}276277#[derive(Trace)]278pub struct EvaluationStateInternals {279 /// Internal state280 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,281 /// Context initializer, which will be used for imports and everything282 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`283 context_initializer: CcContextInitializer,284 /// Used to resolve file locations/contents285 import_resolver: Rc<dyn ImportResolver>,286}287288/// Maintains stack trace and import resolution289#[derive(Clone, Trace)]290pub struct State(Cc<EvaluationStateInternals>);291292thread_local! {293 pub static DEFAULT_STATE: State = State::builder().build();294 pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};295}296pub struct StateEnterGuard(PhantomData<()>);297impl Drop for StateEnterGuard {298 fn drop(&mut self) {299 STATE.with_borrow_mut(|v| *v = None);300 }301}302303pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {304 if let Some(state) = STATE.with_borrow(Clone::clone) {305 v(state)306 } else {307 let s = DEFAULT_STATE.with(Clone::clone);308 v(s)309 }310}311312impl State {313 pub fn enter(&self) -> StateEnterGuard {314 self.try_enter().expect("entered state already exists")315 }316 pub fn try_enter(&self) -> Option<StateEnterGuard> {317 STATE.with_borrow_mut(|v| {318 if v.is_none() {319 *v = Some(self.clone());320 Some(StateEnterGuard(PhantomData))321 } else {322 None323 }324 })325 }326 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise327 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {328 let mut file_cache = self.file_cache();329 let mut file = file_cache.entry(path.clone());330331 let file = match file {332 Entry::Occupied(ref mut d) => d.get_mut(),333 Entry::Vacant(v) => {334 let data = self.import_resolver().load_file_contents(&path)?;335 v.insert(FileData::new_string(336 std::str::from_utf8(&data)337 .map_err(|_| ImportBadFileUtf8(path.clone()))?338 .into(),339 ))340 }341 };342 Ok(file343 .get_string()344 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)345 }346 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise347 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {348 let mut file_cache = self.file_cache();349 let mut file = file_cache.entry(path.clone());350351 let file = match file {352 Entry::Occupied(ref mut d) => d.get_mut(),353 Entry::Vacant(v) => {354 let data = self.import_resolver().load_file_contents(&path)?;355 v.insert(FileData::new_bytes(data.as_slice().into()))356 }357 };358 if let Some(str) = &file.bytes {359 return Ok(str.clone());360 }361 if file.bytes.is_none() {362 file.bytes = Some(363 file.string364 .as_ref()365 .expect("either string or bytes should be set")366 .clone()367 .cast_bytes(),368 );369 }370 Ok(file.bytes.as_ref().expect("just set").clone())371 }372 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise373 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {374 let mut file_cache = self.file_cache();375 let mut file = file_cache.entry(path.clone());376377 let file = match file {378 Entry::Occupied(ref mut d) => d.get_mut(),379 Entry::Vacant(v) => {380 let data = self.import_resolver().load_file_contents(&path)?;381 v.insert(FileData::new_string(382 std::str::from_utf8(&data)383 .map_err(|_| ImportBadFileUtf8(path.clone()))?384 .into(),385 ))386 }387 };388 if let Some(val) = &file.evaluated {389 return Ok(val.clone());390 }391 let code = file392 .get_string()393 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;394 let file_name = Source::new(path.clone(), code.clone());395 if file.parsed.is_none() {396 file.parsed = Some(397 parse_jsonnet(&code, file_name.clone())398 .map(Rc::new)399 .map_err(|e| ImportSyntaxError {400 path: file_name.clone(),401 error: Box::new(e),402 })?,403 );404 }405 let parsed = file.parsed.as_ref().expect("just set").clone();406 if file.evaluating {407 bail!(InfiniteRecursionDetected)408 }409 file.evaluating = true;410 // Dropping file cache guard here, as evaluation may use this map too411 drop(file_cache);412 let (externals, thunks) = self.create_default_context(file_name).build();413 let report = analyze_root(&parsed, externals);414 if report.errored {415 return Err(StaticAnalysisError(report.diagnostics_list).into());416 }417 debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());418 debug_assert!(report.root_shape.captures.is_empty());419 let ctx = Context::root(thunks);420 let res = evaluate::evaluate(ctx, &report.lir);421422 let mut file_cache = self.file_cache();423 let mut file = file_cache.entry(path);424425 let Entry::Occupied(file) = &mut file else {426 unreachable!("this file was just here")427 };428 let file = file.get_mut();429 file.evaluating = false;430 match res {431 Ok(v) => {432 file.evaluated = Some(v.clone());433 Ok(v)434 }435 Err(e) => Err(e),436 }437 }438439 /// Has same semantics as `import 'path'` called from `from` file440 pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {441 let resolved = self.resolve_from(from, &path)?;442 self.import_resolved(resolved)443 }444 pub fn import(&self, path: impl AsPathLike) -> Result<Val> {445 let resolved = self.resolve_from_default(&path)?;446 self.import_resolved(resolved)447 }448449 /// Creates context with all passed global variables450 pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {451 self.create_default_context_with(source, &())452 }453454 /// Creates context with all passed global variables, calling custom modifier455 pub fn create_default_context_with(456 &self,457 source: Source,458 context_initializer: &dyn ContextInitializer,459 ) -> InitialContextBuilder {460 let default_initializer = self.context_initializer();461 let mut builder = InitialContextBuilder::new();462 default_initializer.populate(source.clone(), &mut builder);463 context_initializer.populate(source, &mut builder);464465 builder466 }467}468469/// Internals470impl State {471 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {472 self.0.file_cache.borrow_mut()473 }474}475/// Executes code creating a new stack frame, to be replaced with try{}476pub fn in_frame<T>(477 e: CallLocation<'_>,478 frame_desc: impl FnOnce() -> String,479 f: impl FnOnce() -> Result<T>,480) -> Result<T> {481 let _guard = check_depth()?;482483 f().with_description_src(e, frame_desc)484}485486/// Executes code creating a new stack frame, to be replaced with try{}487pub fn in_description_frame<T>(488 frame_desc: impl FnOnce() -> String,489 f: impl FnOnce() -> Result<T>,490) -> Result<T> {491 let _guard = check_depth()?;492493 f().with_description(frame_desc)494}495496#[derive(Trace)]497pub struct InitialUnderscore(pub Thunk<Val>);498impl ContextInitializer for InitialUnderscore {499 fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {500 builder.bind("_", self.0.clone());501 }502503 fn as_any(&self) -> &dyn Any {504 self505 }506}507508pub struct PreparedSnippet {509 lir: LExpr,510 thunks: Vec<Thunk<Val>>,511}512513/// Raw methods evaluate passed values but don't perform TLA execution514impl State {515 /// Parses and analyses the given snippet with a custom context516 /// modifier.517 pub fn prepare_snippet_with(518 &self,519 name: impl Into<IStr>,520 code: impl Into<IStr>,521 context_initializer: &dyn ContextInitializer,522 ) -> Result<PreparedSnippet> {523 let code = code.into();524 let source = Source::new_virtual(name.into(), code.clone());525 let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {526 path: source.clone(),527 error: Box::new(e),528 })?;529 let (externals, thunks) = self530 .create_default_context_with(source, context_initializer)531 .build();532 let report = analyze_root(&parsed, externals);533 if report.errored {534 return Err(StaticAnalysisError(report.diagnostics_list).into());535 }536 debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());537 debug_assert!(report.root_shape.captures.is_empty());538 Ok(PreparedSnippet {539 lir: report.lir,540 thunks,541 })542 }543 /// Parses and analyses the given snippet544 pub fn prepare_snippet(545 &self,546 name: impl Into<IStr>,547 code: impl Into<IStr>,548 ) -> Result<PreparedSnippet> {549 self.prepare_snippet_with(name, code, &())550 }551 pub fn evaluate_prepared_snippet(&self, prepared: &PreparedSnippet) -> Result<Val> {552 let ctx = Context::root(prepared.thunks.clone());553 evaluate::evaluate(ctx, &prepared.lir)554 }555 /// Parses and evaluates the given snippet with custom context modifier556 pub fn evaluate_snippet_with(557 &self,558 name: impl Into<IStr>,559 code: impl Into<IStr>,560 context_initializer: &dyn ContextInitializer,561 ) -> Result<Val> {562 let prepared = self.prepare_snippet_with(name, code, context_initializer)?;563 self.evaluate_prepared_snippet(&prepared)564 }565 /// Parses and evaluates the given snippet566 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {567 self.evaluate_snippet_with(name, code, &())568 }569}570571/// Settings utilities572impl State {573 // Only panics in case of [`ImportResolver`] contract violation574 #[allow(clippy::missing_panics_doc)]575 pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {576 self.import_resolver().resolve_from(from, path)577 }578 #[allow(clippy::missing_panics_doc)]579 pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {580 self.import_resolver().resolve_from_default(path)581 }582 pub fn import_resolver(&self) -> &dyn ImportResolver {583 &*self.0.import_resolver584 }585 pub fn context_initializer(&self) -> &dyn ContextInitializer {586 &*self.0.context_initializer.0587 }588}589590impl State {591 pub fn builder() -> StateBuilder {592 StateBuilder::default()593 }594}595596impl Default for State {597 fn default() -> Self {598 Self::builder().build()599 }600}601602#[derive(Default)]603pub struct StateBuilder {604 import_resolver: Option<Rc<dyn ImportResolver>>,605 context_initializer: Option<CcContextInitializer>,606}607impl StateBuilder {608 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {609 let _ = self.import_resolver.insert(Rc::new(import_resolver));610 self611 }612 pub fn context_initializer(613 &mut self,614 context_initializer: impl ContextInitializer + Trace,615 ) -> &mut Self {616 let _ = self617 .context_initializer618 .insert(CcContextInitializer::new(context_initializer));619 self620 }621 pub fn build(mut self) -> State {622 State(Cc::new(EvaluationStateInternals {623 file_cache: RefCell::new(FxHashMap::new()),624 context_initializer: self625 .context_initializer626 .take()627 .unwrap_or_else(|| CcContextInitializer::new(())),628 import_resolver: self629 .import_resolver630 .take()631 .unwrap_or_else(|| Rc::new(DummyImportResolver)),632 }))633 }634}1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod obj;19pub mod stack;20pub mod stdlib;21pub mod tla;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27 any::Any,28 cell::{RefCell, RefMut},29 clone::Clone,30 collections::hash_map::Entry,31 fmt::{self, Debug},32 marker::PhantomData,33 rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt, StackTraceElement};39pub use evaluate::ensure_sufficient_stack;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{Cc, Trace, cc_dyn};43pub use jrsonnet_interner::{IBytes, IStr};44use jrsonnet_ir::Expr;45pub use jrsonnet_ir::{NumValue, Source, SourcePath, SourceUrl, SourceVirtual, Span};46#[doc(hidden)]47pub use jrsonnet_macros;4849#[cfg(not(any(feature = "ir-parser", feature = "peg-parser")))]50compile_error!("at least one of `ir-parser` or `peg-parser` features must be enabled");5152pub use error::SyntaxError;53pub use obj::*;54pub use rustc_hash;55use rustc_hash::FxHashMap;56use stack::check_depth;57pub use tla::apply_tla;58pub use val::{Thunk, Val};5960pub mod analyze;61use self::analyze::{LExpr, analyze_root};62use crate::gc::WithCapacityExt as _;6364#[allow(clippy::needless_return)]65pub(crate) fn parse_jsonnet(code: &str, source: Source) -> Result<Expr, SyntaxError> {66 #[cfg(feature = "peg-parser")]67 {68 use std::sync::LazyLock;69 static USE_LEGACY_PARSER: LazyLock<bool> =70 LazyLock::new(|| std::env::var_os("JRSONNET_LEGACY_PARSER").is_some());7172 if *USE_LEGACY_PARSER {73 return parse_peg(code, source);74 }75 }76 #[cfg(feature = "ir-parser")]77 {78 return parse_ir(code, source);79 }80 #[cfg(feature = "peg-parser")]81 {82 return parse_peg(code, source);83 }84}8586#[cfg(feature = "ir-parser")]87fn parse_ir(code: &str, source: Source) -> Result<Expr, SyntaxError> {88 jrsonnet_ir_parser::parse(code, &jrsonnet_ir_parser::ParserSettings { source }).map_err(|e| {89 SyntaxError {90 message: e.message,91 location: e.location,92 }93 })94}9596#[cfg(feature = "peg-parser")]97fn parse_peg(code: &str, source: Source) -> Result<Expr, SyntaxError> {98 jrsonnet_peg_parser::parse(code, &jrsonnet_peg_parser::ParserSettings { source }).map_err(|e| {99 let message = e100 .expected101 .tokens()102 .find(|t| t.starts_with("!!!"))103 .map_or_else(104 || {105 format!(106 "expected {}, got {:?}",107 e.expected,108 code.chars()109 .nth(e.location.0)110 .map_or_else(|| "EOF".into(), |c: char| c.to_string())111 )112 },113 |v| v[3..].into(),114 );115 SyntaxError {116 message,117 location: e.location,118 }119 })120}121122cc_dyn!(123 #[derive(Clone)]124 CcUnbound<V>,125 Unbound<Bound = V>126);127128/// Thunk without bound `super`/`this`129/// object inheritance may be overriden multiple times, and will be fixed only on field read130pub trait Unbound: Trace {131 /// Type of value after object context is bound132 type Bound;133 /// Create value bound to specified object context134 fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;135}136137/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code138/// Standard jsonnet fields are always unbound139#[derive(Clone, Trace)]140pub enum MaybeUnbound {141 /// Value needs to be bound to `this`/`super`142 Unbound(CcUnbound<Val>),143 /// Value is object-independent144 Bound(Thunk<Val>),145}146147impl Debug for MaybeUnbound {148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {149 write!(f, "MaybeUnbound")150 }151}152impl MaybeUnbound {153 /// Attach object context to value, if required154 pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {155 match self {156 Self::Unbound(v) => v.0.bind(sup_this),157 Self::Bound(v) => Ok(v.evaluate()?),158 }159 }160}161162cc_dyn!(CcContextInitializer, ContextInitializer);163164/// During import, this trait will be called to create initial context for file.165/// It may initialize global variables, stdlib for example.166pub trait ContextInitializer {167 /// For composability: extend builder. May panic if this initialization is not supported,168 /// and the context may only be created via `initialize`.169 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder);170 /// Allows upcasting from abstract to concrete context initializer.171 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.172 fn as_any(&self) -> &dyn Any;173}174impl<T> ContextInitializer for &T175where176 T: ContextInitializer,177{178 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {179 (*self).populate(for_file, builder);180 }181182 fn as_any(&self) -> &dyn Any {183 (*self).as_any()184 }185}186187/// Context initializer which adds nothing.188impl ContextInitializer for () {189 fn populate(&self, _for_file: Source, _builder: &mut InitialContextBuilder) {}190 fn as_any(&self) -> &dyn Any {191 self192 }193}194195impl<T> ContextInitializer for Option<T>196where197 T: ContextInitializer + 'static,198{199 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {200 if let Some(ctx) = self {201 ctx.populate(for_file, builder);202 }203 }204205 fn as_any(&self) -> &dyn Any {206 self207 }208}209210macro_rules! impl_context_initializer {211 ($($gen:ident)*) => {212 #[allow(non_snake_case)]213 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {214 fn populate(&self, for_file: Source, builder: &mut InitialContextBuilder) {215 let ($($gen,)*) = self;216 $($gen.populate(for_file.clone(), builder);)*217 }218 fn as_any(&self) -> &dyn Any {219 self220 }221 }222 };223 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {224 impl_context_initializer!($($cur)*);225 impl_context_initializer!($($cur)* $c @ $($rest)*);226 };227 ($($cur:ident)* @) => {228 impl_context_initializer!($($cur)*);229 }230}231impl_context_initializer! {232 A @ B C D E F G233}234235#[derive(Trace)]236struct FileData {237 string: Option<IStr>,238 bytes: Option<IBytes>,239 parsed: Option<Rc<Expr>>,240 evaluated: Option<Val>,241242 evaluating: bool,243}244impl FileData {245 fn new_string(data: IStr) -> Self {246 Self {247 string: Some(data),248 bytes: None,249 parsed: None,250 evaluated: None,251 evaluating: false,252 }253 }254 fn new_bytes(data: IBytes) -> Self {255 Self {256 string: None,257 bytes: Some(data),258 parsed: None,259 evaluated: None,260 evaluating: false,261 }262 }263 pub(crate) fn get_string(&mut self) -> Option<IStr> {264 if self.string.is_none() {265 self.string = Some(266 self.bytes267 .as_ref()268 .expect("either string or bytes should be set")269 .clone()270 .cast_str()?,271 );272 }273 Some(self.string.clone().expect("just set"))274 }275}276277#[derive(Trace)]278pub struct EvaluationStateInternals {279 /// Internal state280 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,281 /// Context initializer, which will be used for imports and everything282 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`283 context_initializer: CcContextInitializer,284 /// Used to resolve file locations/contents285 import_resolver: Rc<dyn ImportResolver>,286}287288/// Maintains stack trace and import resolution289#[derive(Clone, Trace)]290pub struct State(Cc<EvaluationStateInternals>);291292thread_local! {293 pub static DEFAULT_STATE: State = State::builder().build();294 pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};295}296pub struct StateEnterGuard(PhantomData<()>);297impl Drop for StateEnterGuard {298 fn drop(&mut self) {299 STATE.with_borrow_mut(|v| *v = None);300 }301}302303pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {304 if let Some(state) = STATE.with_borrow(Clone::clone) {305 v(state)306 } else {307 let s = DEFAULT_STATE.with(Clone::clone);308 v(s)309 }310}311312impl State {313 pub fn enter(&self) -> StateEnterGuard {314 self.try_enter().expect("entered state already exists")315 }316 pub fn try_enter(&self) -> Option<StateEnterGuard> {317 STATE.with_borrow_mut(|v| {318 if v.is_none() {319 *v = Some(self.clone());320 Some(StateEnterGuard(PhantomData))321 } else {322 None323 }324 })325 }326 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise327 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {328 let mut file_cache = self.file_cache();329 let mut file = file_cache.entry(path.clone());330331 let file = match file {332 Entry::Occupied(ref mut d) => d.get_mut(),333 Entry::Vacant(v) => {334 let data = self.import_resolver().load_file_contents(&path)?;335 v.insert(FileData::new_string(336 std::str::from_utf8(&data)337 .map_err(|_| ImportBadFileUtf8(path.clone()))?338 .into(),339 ))340 }341 };342 Ok(file343 .get_string()344 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)345 }346 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise347 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {348 let mut file_cache = self.file_cache();349 let mut file = file_cache.entry(path.clone());350351 let file = match file {352 Entry::Occupied(ref mut d) => d.get_mut(),353 Entry::Vacant(v) => {354 let data = self.import_resolver().load_file_contents(&path)?;355 v.insert(FileData::new_bytes(data.as_slice().into()))356 }357 };358 if let Some(str) = &file.bytes {359 return Ok(str.clone());360 }361 if file.bytes.is_none() {362 file.bytes = Some(363 file.string364 .as_ref()365 .expect("either string or bytes should be set")366 .clone()367 .cast_bytes(),368 );369 }370 Ok(file.bytes.as_ref().expect("just set").clone())371 }372 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise373 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {374 let mut file_cache = self.file_cache();375 let mut file = file_cache.entry(path.clone());376377 let file = match file {378 Entry::Occupied(ref mut d) => d.get_mut(),379 Entry::Vacant(v) => {380 let data = self.import_resolver().load_file_contents(&path)?;381 v.insert(FileData::new_string(382 std::str::from_utf8(&data)383 .map_err(|_| ImportBadFileUtf8(path.clone()))?384 .into(),385 ))386 }387 };388 if let Some(val) = &file.evaluated {389 return Ok(val.clone());390 }391 let code = file392 .get_string()393 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;394 let file_name = Source::new(path.clone(), code.clone());395 if file.parsed.is_none() {396 file.parsed = Some(397 parse_jsonnet(&code, file_name.clone())398 .map(Rc::new)399 .map_err(|e| {400 let span = e.location.clone();401 let mut err = Error::from(ImportSyntaxError {402 path: file_name.clone(),403 error: Box::new(e),404 });405 err.trace_mut().0.push(StackTraceElement {406 location: Some(span),407 desc: "parse imported".to_string(),408 });409 err410 })?,411 );412 }413 let parsed = file.parsed.as_ref().expect("just set").clone();414 if file.evaluating {415 bail!(InfiniteRecursionDetected)416 }417 file.evaluating = true;418 // Dropping file cache guard here, as evaluation may use this map too419 drop(file_cache);420 let (externals, thunks) = self.create_default_context(file_name).build();421 let report = analyze_root(&parsed, externals);422 if report.errored {423 return Err(StaticAnalysisError(report.diagnostics_list).into());424 }425 debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());426 debug_assert!(report.root_shape.captures.is_empty());427 let ctx = Context::root(thunks);428 let res = evaluate::evaluate(ctx, &report.lir);429430 let mut file_cache = self.file_cache();431 let mut file = file_cache.entry(path);432433 let Entry::Occupied(file) = &mut file else {434 unreachable!("this file was just here")435 };436 let file = file.get_mut();437 file.evaluating = false;438 match res {439 Ok(v) => {440 file.evaluated = Some(v.clone());441 Ok(v)442 }443 Err(e) => Err(e),444 }445 }446447 /// Has same semantics as `import 'path'` called from `from` file448 pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {449 let resolved = self.resolve_from(from, &path)?;450 self.import_resolved(resolved)451 }452 pub fn import(&self, path: impl AsPathLike) -> Result<Val> {453 let resolved = self.resolve_from_default(&path)?;454 self.import_resolved(resolved)455 }456457 /// Creates context with all passed global variables458 pub fn create_default_context(&self, source: Source) -> InitialContextBuilder {459 self.create_default_context_with(source, &())460 }461462 /// Creates context with all passed global variables, calling custom modifier463 pub fn create_default_context_with(464 &self,465 source: Source,466 context_initializer: &dyn ContextInitializer,467 ) -> InitialContextBuilder {468 let default_initializer = self.context_initializer();469 let mut builder = InitialContextBuilder::new();470 default_initializer.populate(source.clone(), &mut builder);471 context_initializer.populate(source, &mut builder);472473 builder474 }475}476477/// Internals478impl State {479 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {480 self.0.file_cache.borrow_mut()481 }482}483/// Executes code creating a new stack frame, to be replaced with try{}484pub fn in_frame<T>(485 e: CallLocation<'_>,486 frame_desc: impl FnOnce() -> String,487 f: impl FnOnce() -> Result<T>,488) -> Result<T> {489 let _guard = check_depth()?;490491 f().with_description_src(e, frame_desc)492}493494/// Executes code creating a new stack frame, to be replaced with try{}495pub fn in_description_frame<T>(496 frame_desc: impl FnOnce() -> String,497 f: impl FnOnce() -> Result<T>,498) -> Result<T> {499 let _guard = check_depth()?;500501 f().with_description(frame_desc)502}503504#[derive(Trace)]505pub struct InitialUnderscore(pub Thunk<Val>);506impl ContextInitializer for InitialUnderscore {507 fn populate(&self, _for_file: Source, builder: &mut InitialContextBuilder) {508 builder.bind("_", self.0.clone());509 }510511 fn as_any(&self) -> &dyn Any {512 self513 }514}515516pub struct PreparedSnippet {517 lir: LExpr,518 thunks: Vec<Thunk<Val>>,519}520521/// Raw methods evaluate passed values but don't perform TLA execution522impl State {523 /// Parses and analyses the given snippet with a custom context524 /// modifier.525 pub fn prepare_snippet_with(526 &self,527 name: impl Into<IStr>,528 code: impl Into<IStr>,529 context_initializer: &dyn ContextInitializer,530 ) -> Result<PreparedSnippet> {531 let code = code.into();532 let source = Source::new_virtual(name.into(), code.clone());533 let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {534 path: source.clone(),535 error: Box::new(e),536 })?;537 let (externals, thunks) = self538 .create_default_context_with(source, context_initializer)539 .build();540 let report = analyze_root(&parsed, externals);541 if report.errored {542 return Err(StaticAnalysisError(report.diagnostics_list).into());543 }544 debug_assert_eq!(report.root_shape.n_locals as usize, thunks.len());545 debug_assert!(report.root_shape.captures.is_empty());546 Ok(PreparedSnippet {547 lir: report.lir,548 thunks,549 })550 }551 /// Parses and analyses the given snippet552 pub fn prepare_snippet(553 &self,554 name: impl Into<IStr>,555 code: impl Into<IStr>,556 ) -> Result<PreparedSnippet> {557 self.prepare_snippet_with(name, code, &())558 }559 pub fn evaluate_prepared_snippet(&self, prepared: &PreparedSnippet) -> Result<Val> {560 let ctx = Context::root(prepared.thunks.clone());561 evaluate::evaluate(ctx, &prepared.lir)562 }563 /// Parses and evaluates the given snippet with custom context modifier564 pub fn evaluate_snippet_with(565 &self,566 name: impl Into<IStr>,567 code: impl Into<IStr>,568 context_initializer: &dyn ContextInitializer,569 ) -> Result<Val> {570 let prepared = self.prepare_snippet_with(name, code, context_initializer)?;571 self.evaluate_prepared_snippet(&prepared)572 }573 /// Parses and evaluates the given snippet574 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {575 self.evaluate_snippet_with(name, code, &())576 }577}578579/// Settings utilities580impl State {581 // Only panics in case of [`ImportResolver`] contract violation582 #[allow(clippy::missing_panics_doc)]583 pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {584 self.import_resolver().resolve_from(from, path)585 }586 #[allow(clippy::missing_panics_doc)]587 pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {588 self.import_resolver().resolve_from_default(path)589 }590 pub fn import_resolver(&self) -> &dyn ImportResolver {591 &*self.0.import_resolver592 }593 pub fn context_initializer(&self) -> &dyn ContextInitializer {594 &*self.0.context_initializer.0595 }596}597598impl State {599 pub fn builder() -> StateBuilder {600 StateBuilder::default()601 }602}603604impl Default for State {605 fn default() -> Self {606 Self::builder().build()607 }608}609610#[derive(Default)]611pub struct StateBuilder {612 import_resolver: Option<Rc<dyn ImportResolver>>,613 context_initializer: Option<CcContextInitializer>,614}615impl StateBuilder {616 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {617 let _ = self.import_resolver.insert(Rc::new(import_resolver));618 self619 }620 pub fn context_initializer(621 &mut self,622 context_initializer: impl ContextInitializer + Trace,623 ) -> &mut Self {624 let _ = self625 .context_initializer626 .insert(CcContextInitializer::new(context_initializer));627 self628 }629 pub fn build(mut self) -> State {630 State(Cc::new(EvaluationStateInternals {631 file_cache: RefCell::new(FxHashMap::new()),632 context_initializer: self633 .context_initializer634 .take()635 .unwrap_or_else(|| CcContextInitializer::new(())),636 import_resolver: self637 .import_resolver638 .take()639 .unwrap_or_else(|| Rc::new(DummyImportResolver)),640 }))641 }642}crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -82,18 +82,24 @@
) -> Result<(), std::fmt::Error> {
if start.line == end.line {
if start.column == end.column {
- write!(out, "{}:{}", start.line, end.column.saturating_sub(1))?;
+ write!(out, "{}:{}", start.line, start.column)?;
} else {
- write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;
+ write!(
+ out,
+ "{}:{}-{}",
+ start.line,
+ start.column,
+ end.column.saturating_sub(1)
+ )?;
}
} else {
write!(
out,
"{}:{}-{}:{}",
- start.line,
- end.column.saturating_sub(1),
start.line,
- end.column
+ start.column,
+ end.line,
+ end.column.saturating_sub(1)
)?;
}
Ok(())
@@ -131,22 +137,13 @@
|| path.source_path().to_string(),
|r| self.resolver.resolve(r),
);
- let mut offset = error.location.1 as usize;
- let is_eof = if offset >= path.code().len() {
- offset = path.code().len().saturating_sub(1);
- true
- } else {
- false
- };
+ let offset = (error.location.1 as usize).min(path.code().len());
#[expect(clippy::cast_possible_truncation, reason = "code is limited by 4gb")]
- let mut location = path
+ let location = path
.map_source_locations(&[offset as u32])
.into_iter()
.next()
.unwrap();
- if is_eof {
- location.column += 1;
- }
write!(n, ":").unwrap();
print_code_location(&mut n, &location, &location).unwrap();
crates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-formatter/src/lib.rs
+++ b/crates/jrsonnet-formatter/src/lib.rs
@@ -895,10 +895,16 @@
}
}
+#[derive(Default)]
pub struct FormatOptions {
// 0 for hard tabs, otherwise number of spaces
pub indent: u8,
}
+impl FormatOptions {
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
#[allow(
clippy::result_large_err,
crates/jrsonnet-ir-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir-parser/src/lib.rs
+++ b/crates/jrsonnet-ir-parser/src/lib.rs
@@ -220,10 +220,7 @@
fn ident(p: &mut Parser<'_>) -> Result<IStr> {
if !p.at(SyntaxKind::IDENT) {
- return Err(p.error(format!(
- "expected identifier, got {}",
- p.current_desc()
- )));
+ return Err(p.error(format!("expected identifier, got {}", p.current_desc())));
}
let text = p.text();
p.eat_any();
crates/jrsonnet-ir/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir/src/lib.rs
+++ b/crates/jrsonnet-ir/src/lib.rs
@@ -15,7 +15,7 @@
pub use location::CodeLocation;
pub use source::{
Source, SourceDefaultIgnoreJpath, SourceDirectory, SourceFifo, SourceFile, SourcePath,
- SourcePathT, SourceVirtual,
+ SourcePathT, SourceUrl, SourceVirtual,
};
// It seels to be a wrong place for this kind of stuff, but as it would also be used for static analysis and
crates/jrsonnet-ir/src/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir/src/location.rs
+++ b/crates/jrsonnet-ir/src/location.rs
@@ -29,7 +29,7 @@
return [CodeLocation::default(); S];
}
let mut line = 1;
- let mut column = 1;
+ let mut column = 0;
let max_offset = *offsets.iter().max().expect("offsets is not empty");
let mut offset_map = offsets
@@ -63,7 +63,7 @@
}
if ch == '\n' {
line += 1;
- column = 1;
+ column = 0;
for idx in with_no_known_line_ending.drain(..) {
out[idx].line_end_offset = pos;
@@ -98,14 +98,14 @@
CodeLocation {
offset: 0,
line: 1,
- column: 2,
+ column: 1,
line_start_offset: 0,
line_end_offset: 11,
},
CodeLocation {
offset: 14,
line: 2,
- column: 4,
+ column: 3,
line_start_offset: 12,
line_end_offset: 67
}
crates/jrsonnet-ir/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-ir/src/source.rs
+++ b/crates/jrsonnet-ir/src/source.rs
@@ -8,6 +8,7 @@
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::{IBytes, IStr};
+use url::Url;
use crate::location::{CodeLocation, location_to_offset, offset_to_location};
@@ -186,6 +187,28 @@
any_ext_impl!(SourcePathT);
}
+#[derive(Acyclic, Hash, PartialEq, Eq, Debug)]
+pub struct SourceUrl(Url);
+impl SourceUrl {
+ pub fn new(url: Url) -> Self {
+ Self(url)
+ }
+}
+impl Display for SourceUrl {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+impl SourcePathT for SourceUrl {
+ fn is_default(&self) -> bool {
+ false
+ }
+ fn path(&self) -> Option<&Path> {
+ None
+ }
+ any_ext_impl!(SourcePathT);
+}
+
/// Represents path to the directory on the disk
///
/// See also [`SourceFile`]