difftreelog
refactor saner imports from TLA/std.extVars
in: master
13 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -14,7 +14,7 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- ImportResolver,
+ AsPathLike, ImportResolver, ResolvePath,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::{SourceDirectory, SourceFile, SourcePath};
@@ -38,7 +38,7 @@
out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
let base = if let Some(p) = from.downcast_ref::<SourceFile>() {
let mut o = p.path().to_owned();
o.pop();
@@ -51,7 +51,11 @@
unreachable!("can't resolve this path");
};
let base = unsafe { crate::unparse_path(&base) };
- let rel = CString::new(path).unwrap();
+ let rel = path.as_path();
+ let rel = match rel {
+ ResolvePath::Str(s) => CString::new(s.as_bytes()).unwrap(),
+ ResolvePath::Path(p) => unsafe { crate::unparse_path(p) },
+ };
let found_here: *mut c_char = null_mut();
let mut buf = null_mut();
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -28,7 +28,7 @@
rustc_hash::FxHashMap,
stack::set_stack_depth_limit,
trace::{CompactFormat, PathResolver, TraceFormat},
- FileImportResolver, IStr, ImportResolver, Result, State, Val,
+ AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_parser::SourcePath;
@@ -62,18 +62,18 @@
}
}
-unsafe fn unparse_path(input: &Path) -> Cow<'_, CStr> {
+unsafe fn unparse_path(input: &Path) -> CString {
#[cfg(target_family = "unix")]
{
use std::os::unix::ffi::OsStrExt;
let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");
- Cow::Owned(str)
+ str
}
#[cfg(not(target_family = "unix"))]
{
let str = input.as_os_str().to_str().expect("bad utf-8");
let cstr = CString::new(str).expect("input has NUL inside");
- Cow::Owned(cstr)
+ cstr
}
}
@@ -93,16 +93,12 @@
self.inner.borrow().load_file_contents(resolved)
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from(from, path)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.inner.borrow().resolve_from_default(path)
- }
-
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- self.inner.borrow().resolve(path)
}
}
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -3,7 +3,6 @@
use std::{ffi::CStr, os::raw::c_char};
use jrsonnet_evaluator::{function::TlaArg, IStr};
-use jrsonnet_parser::{ParserSettings, Source};
use crate::VM;
@@ -84,14 +83,7 @@
let code = unsafe { CStr::from_ptr(code) };
let name: IStr = name.to_str().expect("name is not utf-8").into();
- let code: IStr = code.to_str().expect("code is not utf-8").into();
- let code = jrsonnet_parser::parse(
- &code,
- &ParserSettings {
- source: Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.clone()),
- },
- )
- .expect("can't parse TLA code");
+ let code: String = code.to_str().expect("code is not utf-8").to_owned();
- vm.tla_args.insert(name, TlaArg::Code(code));
+ vm.tla_args.insert(name, TlaArg::InlineCode(code));
}
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -182,7 +182,7 @@
let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(&input)?
+ s.import(input.as_str())?
};
let tla = opts.tla.tla_opts()?;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,7 @@
-use std::{fs::read_to_string, str::FromStr};
+use std::str::FromStr;
use clap::Parser;
-use jrsonnet_evaluator::{trace::PathResolver, Result};
+use jrsonnet_evaluator::{function::TlaArg, trace::PathResolver, Result};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone)]
@@ -54,25 +54,20 @@
#[derive(Clone)]
pub struct ExtFile {
pub name: String,
- pub value: String,
+ pub path: String,
}
impl FromStr for ExtFile {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
- let out: Vec<&str> = s.split('=').collect();
- if out.len() != 2 {
+ let Some((name, path)) = s.split_once('=') else {
return Err("bad ext-file syntax".to_owned());
- }
- let file = read_to_string(out[1]);
- match file {
- Ok(content) => Ok(Self {
- name: out[0].into(),
- value: content,
- }),
- Err(e) => Err(format!("{e}")),
- }
+ };
+ Ok(Self {
+ name: name.into(),
+ path: path.into(),
+ })
}
}
@@ -110,16 +105,27 @@
}
let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());
for ext in &self.ext_str {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().into()),
+ );
}
for ext in &self.ext_str_file {
- ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.path.clone()),
+ );
}
for ext in &self.ext_code {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut().ext_vars.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
+ );
}
for ext in &self.ext_code_file {
- ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
+ ctx.settings_mut()
+ .ext_vars
+ .insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
}
Ok(Some(ctx))
}
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,12 +1,5 @@
use clap::Parser;
-use jrsonnet_evaluator::{
- error::{ErrorKind, Result},
- function::TlaArg,
- gc::WithCapacityExt as _,
- rustc_hash::FxHashMap,
- IStr,
-};
-use jrsonnet_parser::{ParserSettings, Source};
+use jrsonnet_evaluator::{IStr, error::Result, function::TlaArg, gc::WithCapacityExt as _, rustc_hash::FxHashMap};
use crate::{ExtFile, ExtStr};
@@ -35,37 +28,27 @@
impl TlaOpts {
pub fn tla_opts(&self) -> Result<FxHashMap<IStr, TlaArg>> {
let mut out = FxHashMap::new();
- for (name, value) in self
- .tla_str
- .iter()
- .map(|c| (&c.name, &c.value))
- .chain(self.tla_str_file.iter().map(|c| (&c.name, &c.value)))
- {
- out.insert(name.into(), TlaArg::String(value.into()));
+ for ext in &self.tla_str {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::String(ext.value.as_str().into()),
+ );
}
- for (name, code) in self
- .tla_code
- .iter()
- .map(|c| (&c.name, &c.value))
- .chain(self.tla_code_file.iter().map(|c| (&c.name, &c.value)))
- {
- let source = Source::new_virtual(format!("<top-level-arg:{name}>").into(), code.into());
+ for ext in &self.tla_str_file {
out.insert(
- (name as &str).into(),
- TlaArg::Code(
- jrsonnet_parser::parse(
- code,
- &ParserSettings {
- source: source.clone(),
- },
- )
- .map_err(|e| ErrorKind::ImportSyntaxError {
- path: source,
- error: Box::new(e),
- })?,
- ),
+ ext.name.as_str().into(),
+ TlaArg::ImportStr(ext.name.as_str().into()),
+ );
+ }
+ for ext in &self.tla_code {
+ out.insert(
+ ext.name.as_str().into(),
+ TlaArg::InlineCode(ext.value.clone()),
);
}
+ for ext in &self.tla_code_file {
+ out.insert(ext.name.as_str().into(), TlaArg::Import(ext.path.clone()));
+ }
Ok(out)
}
}
crates/jrsonnet-evaluator/src/async_import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/async_import.rs
+++ b/crates/jrsonnet-evaluator/src/async_import.rs
@@ -1,7 +1,6 @@
-use std::{any::Any, cell::RefCell, future::Future, path::Path};
+use std::{any::Any, cell::RefCell, future::Future};
use jrsonnet_gcmodule::Acyclic;
-use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, FieldName, ForSpecData,
IfSpecData, LocExpr, Member, ObjBody, Param, ParamsDesc, ParserSettings, SliceDesc, Source,
@@ -9,10 +8,10 @@
};
use rustc_hash::FxHashMap;
-use crate::{bail, FileData, ImportResolver, State};
+use crate::{AsPathLike, FileData, ImportResolver, ResolvePathOwned, State};
pub struct Import {
- path: IStr,
+ path: ResolvePathOwned,
expression: bool,
}
@@ -137,7 +136,7 @@
Expr::Import(v) | Expr::ImportStr(v) | Expr::ImportBin(v) => {
if let Expr::Str(s) = &*v.expr() {
out.0.push(Import {
- path: s.clone(),
+ path: ResolvePathOwned::Str(s.to_string()),
expression: matches!(&*expr.expr(), Expr::Import(_)),
});
}
@@ -229,16 +228,14 @@
fn resolve_from(
&self,
from: &SourcePath,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>>;
fn resolve_from_default(
&self,
- path: &str,
+ path: &dyn AsPathLike,
) -> impl Future<Output = Result<SourcePath, Self::Error>> {
async { self.resolve_from(&SourcePath::default(), path).await }
}
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> impl Future<Output = Result<SourcePath, Self::Error>>;
/// Load resolved file
/// This should only be called with value returned
@@ -253,31 +250,25 @@
#[derive(Acyclic)]
struct ResolvedImportResolver {
- resolved: RefCell<FxHashMap<(SourcePath, IStr), (SourcePath, bool)>>,
+ resolved: RefCell<FxHashMap<(SourcePath, ResolvePathOwned), (SourcePath, bool)>>,
}
impl ImportResolver for ResolvedImportResolver {
fn load_file_contents(&self, _resolved: &SourcePath) -> crate::Result<Vec<u8>> {
unreachable!("all files should be loaded at this point");
}
- fn resolve_from(&self, from: &SourcePath, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
Ok(self
.resolved
.borrow()
- .get(&(from.clone(), path.into()))
+ .get(&(from.clone(), path.as_path().to_owned()))
.expect("all imports should be resolved at this point")
.0
.clone())
}
- fn resolve_from_default(&self, path: &str) -> crate::Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> crate::Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
-
- fn resolve(&self, path: &Path) -> crate::Result<SourcePath> {
- bail!(crate::error::ErrorKind::AbsoluteImportNotSupported(
- path.to_owned()
- ))
}
}
@@ -288,7 +279,7 @@
}
#[allow(clippy::future_not_send)]
-pub async fn async_import<H>(s: State, handler: H, path: impl AsRef<Path>) -> Result<(), H::Error>
+pub async fn async_import<H>(s: State, handler: H, path: &dyn AsPathLike) -> Result<(), H::Error>
where
H: AsyncImportResolver,
{
@@ -299,7 +290,7 @@
let mut resolved_map = resolved.resolved.borrow_mut();
let mut queue = vec![Job::LoadFile {
- path: handler.resolve(path.as_ref()).await?,
+ path: handler.resolve_from_default(path).await?,
parse: true,
}];
while let Some(job) = queue.pop() {
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,6 @@
cmp::Ordering,
convert::Infallible,
fmt::{Debug, Display},
- path::PathBuf,
};
use jrsonnet_gcmodule::Trace;
@@ -16,7 +15,7 @@
stdlib::format::FormatError,
typed::TypeLocError,
val::ConvertNumValueError,
- ObjValue,
+ ObjValue, ResolvePathOwned,
};
pub(crate) fn format_found(list: &[IStr], what: &str) -> String {
@@ -180,9 +179,7 @@
StandaloneSuper,
#[error("can't resolve {1} from {0}")]
- ImportFileNotFound(SourcePath, String),
- #[error("can't resolve absolute {0}")]
- AbsoluteImportFileNotFound(PathBuf),
+ ImportFileNotFound(SourcePath, ResolvePathOwned),
#[error("resolved file not found: {:?}", .0)]
ResolvedFileNotFound(SourcePath),
#[error("can't import {0}: is a directory")]
@@ -192,9 +189,7 @@
#[error("import io error: {0}")]
ImportIo(String),
#[error("tried to import {1} from {0}, but imports are not supported")]
- ImportNotSupported(SourcePath, String),
- #[error("tried to import {0}, but absolute imports are not supported")]
- AbsoluteImportNotSupported(PathBuf),
+ ImportNotSupported(SourcePath, ResolvePathOwned),
#[error("can't import from virtual file")]
CantImportFromVirtualFile,
#[error(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -682,7 +682,7 @@
};
let tmp = loc.clone().0;
let s = ctx.state();
- let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;
+ let resolved_path = s.resolve_from(tmp.source_path(), path)?;
match i {
Import(_) => in_frame(
CallLocation::new(&loc),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -2,7 +2,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, LocExpr};
+use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
@@ -41,22 +41,34 @@
#[derive(Clone, Trace)]
pub enum TlaArg {
String(IStr),
- Code(LocExpr),
Val(Val),
Lazy(Thunk<Val>),
+ Import(String),
+ ImportStr(String),
+ InlineCode(String),
}
impl ArgLike for TlaArg {
- fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+ fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
match self {
Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
- Self::Code(code) => Ok(if tailstrict {
- Thunk::evaluated(evaluate(ctx, code)?)
- } else {
- let code = code.clone();
- Thunk!(move || evaluate(ctx, &code))
- }),
Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
Self::Lazy(lazy) => Ok(lazy.clone()),
+ Self::Import(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
+ Self::ImportStr(p) => {
+ let resolved = ctx.state().resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || ctx
+ .state()
+ .import_resolved_str(resolved)
+ .map(Val::string)))
+ }
+ Self::InlineCode(p) => {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
+ }
}
}
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,7 +1,8 @@
use std::{
any::Any,
+ borrow::Cow,
env::current_dir,
- fs,
+ fmt, fs,
io::{ErrorKind, Read},
path::{Path, PathBuf},
};
@@ -9,12 +10,85 @@
use fs::File;
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_interner::IBytes;
-use jrsonnet_parser::{SourceDirectory, SourceFifo, SourceFile, SourcePath};
+use jrsonnet_parser::{IStr, SourceDirectory, SourceFifo, SourceFile, SourcePath};
use crate::{
bail,
error::{ErrorKind::*, Result},
};
+#[derive(Clone, Debug, Acyclic, Eq, Hash, PartialEq)]
+pub enum ResolvePathOwned {
+ Str(String),
+ Path(PathBuf),
+}
+impl fmt::Display for ResolvePathOwned {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ ResolvePathOwned::Str(s) => write!(f, "{s}"),
+ ResolvePathOwned::Path(p) => write!(f, "{}", p.display()),
+ }
+ }
+}
+#[derive(Clone, Copy)]
+pub enum ResolvePath<'s> {
+ Str(&'s str),
+ Path(&'s Path),
+}
+impl ResolvePath<'_> {
+ pub fn to_owned(self) -> ResolvePathOwned {
+ match self {
+ ResolvePath::Str(s) => ResolvePathOwned::Str(s.to_owned()),
+ ResolvePath::Path(p) => ResolvePathOwned::Path(p.to_owned()),
+ }
+ }
+}
+impl AsRef<Path> for ResolvePath<'_> {
+ fn as_ref(&self) -> &Path {
+ match self {
+ ResolvePath::Str(s) => s.as_ref(),
+ ResolvePath::Path(p) => p,
+ }
+ }
+}
+pub trait AsPathLike {
+ fn as_path(&self) -> ResolvePath<'_>;
+}
+impl<T> AsPathLike for &T
+where
+ T: AsPathLike + ?Sized,
+{
+ fn as_path(&self) -> ResolvePath<'_> {
+ (*self).as_path()
+ }
+}
+impl AsPathLike for str {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for IStr {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Str(self)
+ }
+}
+impl AsPathLike for Cow<'_, Path> {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self.as_ref())
+ }
+}
+impl AsPathLike for Path {
+ fn as_path(&self) -> ResolvePath<'_> {
+ ResolvePath::Path(self)
+ }
+}
+impl AsPathLike for ResolvePathOwned {
+ fn as_path(&self) -> ResolvePath<'_> {
+ match self {
+ ResolvePathOwned::Str(s) => ResolvePath::Str(s),
+ ResolvePathOwned::Path(path_buf) => ResolvePath::Path(path_buf),
+ }
+ }
+}
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver: Acyclic + Any {
@@ -24,15 +98,11 @@
///
/// `from` should only be returned from [`ImportResolver::resolve`], or from other defined file, any other value
/// may result in panic
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
- bail!(ImportNotSupported(from.clone(), path.into()))
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ bail!(ImportNotSupported(from.clone(), path.as_path().to_owned()))
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
- }
- /// Resolves absolute path, doesn't supports jpath and other fancy things
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- bail!(AbsoluteImportNotSupported(path.to_owned()))
}
/// Load resolved file
@@ -105,7 +175,8 @@
}
impl ImportResolver for FileImportResolver {
- fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {
+ fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {
+ let path = path.as_path();
let mut direct = if let Some(f) = from.downcast_ref::<SourceFile>() {
let mut o = f.path().to_owned();
o.pop();
@@ -130,12 +201,6 @@
}
}
bail!(ImportFileNotFound(from.clone(), path.to_owned()))
- }
- fn resolve(&self, path: &Path) -> Result<SourcePath> {
- let Some(source) = check_path(path)? else {
- bail!(AbsoluteImportFileNotFound(path.to_owned()))
- };
- Ok(source)
}
fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
@@ -155,7 +220,7 @@
Ok(out)
}
- fn resolve_from_default(&self, path: &str) -> Result<SourcePath> {
+ fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {
self.resolve_from(&SourcePath::default(), path)
}
}
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 map;19mod obj;20pub mod stack;21pub mod stdlib;22mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28 any::Any,29 cell::{RefCell, RefMut},30 collections::hash_map::Entry,31 fmt::{self, Debug},32 path::Path,33 rc::Rc,34};3536pub use ctx::*;37pub use dynamic::*;38pub use error::{Error, ErrorKind::*, Result, ResultExt};39pub use evaluate::*;40use function::CallLocation;41pub use import::*;42use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};43pub use jrsonnet_interner::{IBytes, IStr};44#[doc(hidden)]45pub use jrsonnet_macros;46pub use jrsonnet_parser as parser;47use jrsonnet_parser::{LocExpr, ParserSettings, Source, SourcePath};48pub use obj::*;49pub use rustc_hash;50use rustc_hash::FxHashMap;51use stack::check_depth;52pub use tla::apply_tla;53pub use val::{Thunk, Val};5455use crate::gc::WithCapacityExt as _;5657cc_dyn!(58 #[derive(Clone)]59 CcUnbound<V>,60 Unbound<Bound = V>61);6263/// Thunk without bound `super`/`this`64/// object inheritance may be overriden multiple times, and will be fixed only on field read65pub trait Unbound: Trace {66 /// Type of value after object context is bound67 type Bound;68 /// Create value bound to specified object context69 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;70}7172/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code73/// Standard jsonnet fields are always unbound74#[derive(Clone, Trace)]75pub enum MaybeUnbound {76 /// Value needs to be bound to `this`/`super`77 Unbound(CcUnbound<Val>),78 /// Value is object-independent79 Bound(Thunk<Val>),80}8182impl Debug for MaybeUnbound {83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {84 write!(f, "MaybeUnbound")85 }86}87impl MaybeUnbound {88 /// Attach object context to value, if required89 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {90 match self {91 Self::Unbound(v) => v.0.bind(sup, this),92 Self::Bound(v) => Ok(v.evaluate()?),93 }94 }95}9697cc_dyn!(CcContextInitializer, ContextInitializer);9899/// During import, this trait will be called to create initial context for file.100/// It may initialize global variables, stdlib for example.101pub trait ContextInitializer: Trace {102 /// For which size the builder should be preallocated103 fn reserve_vars(&self) -> usize {104 0105 }106 /// Initialize default file context.107 /// Has default implementation, which calls `populate`.108 /// Prefer to always implement `populate` instead.109 fn initialize(&self, state: State, for_file: Source) -> Context {110 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());111 self.populate(for_file, &mut builder);112 builder.build()113 }114 /// For composability: extend builder. May panic if this initialization is not supported,115 /// and the context may only be created via `initialize`.116 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);117 /// Allows upcasting from abstract to concrete context initializer.118 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.119 fn as_any(&self) -> &dyn Any;120}121122/// Context initializer which adds nothing.123impl ContextInitializer for () {124 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}125 fn as_any(&self) -> &dyn Any {126 self127 }128}129130impl<T> ContextInitializer for Option<T>131where132 T: ContextInitializer,133{134 fn initialize(&self, state: State, for_file: Source) -> Context {135 if let Some(ctx) = self {136 ctx.initialize(state, for_file)137 } else {138 ().initialize(state, for_file)139 }140 }141142 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {143 if let Some(ctx) = self {144 ctx.populate(for_file, builder);145 }146 }147148 fn as_any(&self) -> &dyn Any {149 self150 }151}152153macro_rules! impl_context_initializer {154 ($($gen:ident)*) => {155 #[allow(non_snake_case)]156 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {157 fn reserve_vars(&self) -> usize {158 let mut out = 0;159 let ($($gen,)*) = self;160 $(out += $gen.reserve_vars();)*161 out162 }163 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {164 let ($($gen,)*) = self;165 $($gen.populate(for_file.clone(), builder);)*166 }167 fn as_any(&self) -> &dyn Any {168 self169 }170 }171 };172 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {173 impl_context_initializer!($($cur)*);174 impl_context_initializer!($($cur)* $c @ $($rest)*);175 };176 ($($cur:ident)* @) => {177 impl_context_initializer!($($cur)*);178 }179}180impl_context_initializer! {181 A @ B C D E F G182}183184#[derive(Trace)]185struct FileData {186 string: Option<IStr>,187 bytes: Option<IBytes>,188 parsed: Option<LocExpr>,189 evaluated: Option<Val>,190191 evaluating: bool,192}193impl FileData {194 fn new_string(data: IStr) -> Self {195 Self {196 string: Some(data),197 bytes: None,198 parsed: None,199 evaluated: None,200 evaluating: false,201 }202 }203 fn new_bytes(data: IBytes) -> Self {204 Self {205 string: None,206 bytes: Some(data),207 parsed: None,208 evaluated: None,209 evaluating: false,210 }211 }212 pub(crate) fn get_string(&mut self) -> Option<IStr> {213 if self.string.is_none() {214 self.string = Some(215 self.bytes216 .as_ref()217 .expect("either string or bytes should be set")218 .clone()219 .cast_str()?,220 );221 }222 Some(self.string.clone().expect("just set"))223 }224}225226#[derive(Trace)]227pub struct EvaluationStateInternals {228 /// Internal state229 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,230 /// Context initializer, which will be used for imports and everything231 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`232 context_initializer: CcContextInitializer,233 /// Used to resolve file locations/contents234 import_resolver: Rc<dyn ImportResolver>,235}236237/// Maintains stack trace and import resolution238#[derive(Clone, Trace)]239pub struct State(Cc<EvaluationStateInternals>);240241impl State {242 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise243 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {244 let mut file_cache = self.file_cache();245 let mut file = file_cache.entry(path.clone());246247 let file = match file {248 Entry::Occupied(ref mut d) => d.get_mut(),249 Entry::Vacant(v) => {250 let data = self.import_resolver().load_file_contents(&path)?;251 v.insert(FileData::new_string(252 std::str::from_utf8(&data)253 .map_err(|_| ImportBadFileUtf8(path.clone()))?254 .into(),255 ))256 }257 };258 Ok(file259 .get_string()260 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)261 }262 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise263 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {264 let mut file_cache = self.file_cache();265 let mut file = file_cache.entry(path.clone());266267 let file = match file {268 Entry::Occupied(ref mut d) => d.get_mut(),269 Entry::Vacant(v) => {270 let data = self.import_resolver().load_file_contents(&path)?;271 v.insert(FileData::new_bytes(data.as_slice().into()))272 }273 };274 if let Some(str) = &file.bytes {275 return Ok(str.clone());276 }277 if file.bytes.is_none() {278 file.bytes = Some(279 file.string280 .as_ref()281 .expect("either string or bytes should be set")282 .clone()283 .cast_bytes(),284 );285 }286 Ok(file.bytes.as_ref().expect("just set").clone())287 }288 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise289 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {290 let mut file_cache = self.file_cache();291 let mut file = file_cache.entry(path.clone());292293 let file = match file {294 Entry::Occupied(ref mut d) => d.get_mut(),295 Entry::Vacant(v) => {296 let data = self.import_resolver().load_file_contents(&path)?;297 v.insert(FileData::new_string(298 std::str::from_utf8(&data)299 .map_err(|_| ImportBadFileUtf8(path.clone()))?300 .into(),301 ))302 }303 };304 if let Some(val) = &file.evaluated {305 return Ok(val.clone());306 }307 let code = file308 .get_string()309 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;310 let file_name = Source::new(path.clone(), code.clone());311 if file.parsed.is_none() {312 file.parsed = Some(313 jrsonnet_parser::parse(314 &code,315 &ParserSettings {316 source: file_name.clone(),317 },318 )319 .map_err(|e| ImportSyntaxError {320 path: file_name.clone(),321 error: Box::new(e),322 })?,323 );324 }325 let parsed = file.parsed.as_ref().expect("just set").clone();326 if file.evaluating {327 bail!(InfiniteRecursionDetected)328 }329 file.evaluating = true;330 // Dropping file cache guard here, as evaluation may use this map too331 drop(file_cache);332 let res = evaluate(self.create_default_context(file_name), &parsed);333334 let mut file_cache = self.file_cache();335 let mut file = file_cache.entry(path.clone());336337 let Entry::Occupied(file) = &mut file else {338 unreachable!("this file was just here")339 };340 let file = file.get_mut();341 file.evaluating = false;342 match res {343 Ok(v) => {344 file.evaluated = Some(v.clone());345 Ok(v)346 }347 Err(e) => Err(e),348 }349 }350351 /// Has same semantics as `import 'path'` called from `from` file352 pub fn import_from(&self, from: &SourcePath, path: &str) -> Result<Val> {353 let resolved = self.resolve_from(from, path)?;354 self.import_resolved(resolved)355 }356 pub fn import(&self, path: impl AsRef<Path>) -> Result<Val> {357 let resolved = self.resolve(path)?;358 self.import_resolved(resolved)359 }360361 /// Creates context with all passed global variables362 pub fn create_default_context(&self, source: Source) -> Context {363 self.context_initializer().initialize(self.clone(), source)364 }365366 /// Creates context with all passed global variables, calling custom modifier367 pub fn create_default_context_with(368 &self,369 source: Source,370 context_initializer: impl ContextInitializer,371 ) -> Context {372 let default_initializer = self.context_initializer();373 let mut builder = ContextBuilder::with_capacity(374 self.clone(),375 default_initializer.reserve_vars() + context_initializer.reserve_vars(),376 );377 default_initializer.populate(source.clone(), &mut builder);378 context_initializer.populate(source, &mut builder);379380 builder.build()381 }382}383384/// Internals385impl State {386 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {387 self.0.file_cache.borrow_mut()388 }389}390/// Executes code creating a new stack frame, to be replaced with try{}391pub fn in_frame<T>(392 e: CallLocation<'_>,393 frame_desc: impl FnOnce() -> String,394 f: impl FnOnce() -> Result<T>,395) -> Result<T> {396 let _guard = check_depth()?;397398 f().with_description_src(e, frame_desc)399}400401/// Executes code creating a new stack frame, to be replaced with try{}402pub fn in_description_frame<T>(403 frame_desc: impl FnOnce() -> String,404 f: impl FnOnce() -> Result<T>,405) -> Result<T> {406 let _guard = check_depth()?;407408 f().with_description(frame_desc)409}410411#[derive(Trace)]412pub struct InitialUnderscore(pub Thunk<Val>);413impl ContextInitializer for InitialUnderscore {414 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {415 builder.bind("_", self.0.clone());416 }417418 fn as_any(&self) -> &dyn Any {419 self420 }421}422423/// Raw methods evaluate passed values but don't perform TLA execution424impl State {425 /// Parses and evaluates the given snippet426 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {427 let code = code.into();428 let source = Source::new_virtual(name.into(), code.clone());429 let parsed = jrsonnet_parser::parse(430 &code,431 &ParserSettings {432 source: source.clone(),433 },434 )435 .map_err(|e| ImportSyntaxError {436 path: source.clone(),437 error: Box::new(e),438 })?;439 evaluate(self.create_default_context(source), &parsed)440 }441 /// Parses and evaluates the given snippet with custom context modifier442 pub fn evaluate_snippet_with(443 &self,444 name: impl Into<IStr>,445 code: impl Into<IStr>,446 context_initializer: impl ContextInitializer,447 ) -> Result<Val> {448 let code = code.into();449 let source = Source::new_virtual(name.into(), code.clone());450 let parsed = jrsonnet_parser::parse(451 &code,452 &ParserSettings {453 source: source.clone(),454 },455 )456 .map_err(|e| ImportSyntaxError {457 path: source.clone(),458 error: Box::new(e),459 })?;460 evaluate(461 self.create_default_context_with(source, context_initializer),462 &parsed,463 )464 }465}466467/// Settings utilities468impl State {469 // Only panics in case of [`ImportResolver`] contract violation470 #[allow(clippy::missing_panics_doc)]471 pub fn resolve_from(&self, from: &SourcePath, path: &str) -> Result<SourcePath> {472 self.import_resolver().resolve_from(from, path.as_ref())473 }474475 // Only panics in case of [`ImportResolver`] contract violation476 #[allow(clippy::missing_panics_doc)]477 pub fn resolve(&self, path: impl AsRef<Path>) -> Result<SourcePath> {478 self.import_resolver().resolve(path.as_ref())479 }480 pub fn import_resolver(&self) -> &dyn ImportResolver {481 &*self.0.import_resolver482 }483 pub fn context_initializer(&self) -> &dyn ContextInitializer {484 &*self.0.context_initializer.0485 }486}487488impl State {489 pub fn builder() -> StateBuilder {490 StateBuilder::default()491 }492}493494impl Default for State {495 fn default() -> Self {496 Self::builder().build()497 }498}499500#[derive(Default)]501pub struct StateBuilder {502 import_resolver: Option<Rc<dyn ImportResolver>>,503 context_initializer: Option<CcContextInitializer>,504}505impl StateBuilder {506 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {507 let _ = self.import_resolver.insert(Rc::new(import_resolver));508 self509 }510 pub fn context_initializer(511 &mut self,512 context_initializer: impl ContextInitializer,513 ) -> &mut Self {514 let _ = self515 .context_initializer516 .insert(CcContextInitializer::new(context_initializer));517 self518 }519 pub fn build(mut self) -> State {520 State(Cc::new(EvaluationStateInternals {521 file_cache: RefCell::new(FxHashMap::new()),522 context_initializer: self523 .context_initializer524 .take()525 .unwrap_or_else(|| CcContextInitializer::new(())),526 import_resolver: self527 .import_resolver528 .take()529 .unwrap_or_else(|| Rc::new(DummyImportResolver)),530 }))531 }532}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -12,7 +12,7 @@
pub use encoding::*;
pub use hash::*;
use jrsonnet_evaluator::{
- error::{ErrorKind::*, Result},
+ error::Result,
function::{CallLocation, FuncVal, TlaArg},
trace::PathResolver,
val::NumValue,
@@ -377,23 +377,11 @@
.ext_vars
.insert(name, TlaArg::String(value));
}
- pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {
- let code = code.into();
- let source = extvar_source(name, code.clone());
- let parsed = jrsonnet_parser::parse(
- &code,
- &jrsonnet_parser::ParserSettings {
- source: source.clone(),
- },
- )
- .map_err(|e| ImportSyntaxError {
- path: source,
- error: Box::new(e),
- })?;
+ pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {
// self.data_mut().volatile_files.insert(source_name, code);
self.settings_mut()
.ext_vars
- .insert(name.into(), TlaArg::Code(parsed));
+ .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));
Ok(())
}
pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {