difftreelog
refactor rework layout and path handling
in: master
32 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -10,7 +10,6 @@
os::raw::{c_char, c_int},
path::{Path, PathBuf},
ptr::null_mut,
- rc::Rc,
};
use jrsonnet_evaluator::{
@@ -33,7 +32,7 @@
out: RefCell<HashMap<PathBuf, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<PathBuf> {
let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
let rel = CString::new(path.to_str().unwrap()).unwrap().into_raw();
let found_here: *mut c_char = null_mut();
@@ -73,7 +72,7 @@
unsafe { CString::from_raw(result_ptr) };
}
- Ok(found_here_buf.into())
+ Ok(found_here_buf)
}
fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
@@ -109,7 +108,7 @@
}
}
impl ImportResolver for NativeImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ fn resolve_file(&self, from: &Path, path: &Path) -> Result<PathBuf> {
let mut new_path = from.to_owned();
new_path.push(path);
if new_path.exists() {
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -111,7 +111,7 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))
+ .import(PathBuf::from(filename.to_str().unwrap()))
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest(v))
{
@@ -140,8 +140,8 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet_raw(
- PathBuf::from(filename.to_str().unwrap()).into(),
+ .evaluate_snippet(
+ filename.to_str().unwrap().into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
@@ -185,7 +185,7 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))
+ .import(PathBuf::from(filename.to_str().unwrap()))
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_multi(v))
{
@@ -212,8 +212,8 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet_raw(
- PathBuf::from(filename.to_str().unwrap()).into(),
+ .evaluate_snippet(
+ filename.to_str().unwrap().into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
@@ -255,7 +255,7 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .evaluate_file_raw_nocwd(&PathBuf::from(filename.to_str().unwrap()))
+ .import(PathBuf::from(filename.to_str().unwrap()))
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_stream(v))
{
@@ -282,8 +282,8 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet_raw(
- PathBuf::from(filename.to_str().unwrap()).into(),
+ .evaluate_snippet(
+ filename.to_str().unwrap().into(),
snippet.to_str().unwrap().into(),
)
.and_then(|v| vm.with_tla(v))
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,8 +1,6 @@
use std::{
ffi::{c_void, CStr},
os::raw::{c_char, c_int},
- path::Path,
- rc::Rc,
};
use gcmodule::Cc;
@@ -28,7 +26,7 @@
cb: JsonnetNativeCallback,
}
impl NativeCallbackHandler for JsonnetNativeCallbackHandler {
- fn call(&self, s: State, _from: Option<Rc<Path>>, args: &[Val]) -> Result<Val, LocError> {
+ fn call(&self, s: State, args: &[Val]) -> Result<Val, LocError> {
let mut n_args = Vec::new();
for a in args {
n_args.push(Some(Box::new(a.clone())));
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -120,17 +120,14 @@
opts.manifest.configure(s)?;
let val = if opts.input.exec {
- s.evaluate_snippet_raw(
- PathBuf::from("<cmdline>").into(),
- (&opts.input.input as &str).into(),
- )?
+ s.evaluate_snippet("<cmdline>".to_owned(), (&opts.input.input as &str).into())?
} else if opts.input.input == "-" {
let mut input = Vec::new();
std::io::stdin().read_to_end(&mut input)?;
let input_str = std::str::from_utf8(&input)?.into();
- s.evaluate_snippet_raw(PathBuf::from("<stdin>").into(), input_str)?
+ s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.evaluate_file_raw(&PathBuf::from(opts.input.input))?
+ s.import(s.resolve_file(&PathBuf::new(), &opts.input.input)?)?
};
let val = s.with_tla(val)?;
crates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -108,10 +108,10 @@
s.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
}
for ext in self.ext_code.iter() {
- s.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
+ s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
}
for ext in self.ext_code_file.iter() {
- s.add_ext_code((&ext.name as &str).into(), (&ext.value as &str).into())?;
+ s.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
}
Ok(())
}
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -55,10 +55,10 @@
s.add_tla_str((&tla.name as &str).into(), (&tla.value as &str).into())
}
for tla in self.tla_code.iter() {
- s.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
+ s.add_tla_code((&tla.name as &str).into(), &tla.value as &str)?;
}
for tla in self.tla_code_file.iter() {
- s.add_tla_code((&tla.name as &str).into(), (&tla.value as &str).into())?;
+ s.add_tla_code((&tla.name as &str).into(), &tla.value as &str)?;
}
Ok(())
}
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -28,6 +28,8 @@
jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.2" }
jrsonnet-macros = { path = "../jrsonnet-macros", version = "0.4.2" }
pathdiff = "0.2.1"
+hashbrown = "0.12.1"
+static_assertions = "1.1.0"
md5 = "0.7.0"
base64 = "0.13.0"
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,19 +1,14 @@
-use std::{
- env,
- fs::File,
- io::Write,
- path::{Path, PathBuf},
-};
+use std::{borrow::Cow, env, fs::File, io::Write, path::Path};
use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings};
+use jrsonnet_parser::{parse, ParserSettings, Source};
use jrsonnet_stdlib::STDLIB_STR;
fn main() {
let parsed = parse(
STDLIB_STR,
&ParserSettings {
- file_name: PathBuf::from("std.jsonnet").into(),
+ file_name: Source::new_virtual(Cow::Borrowed("<std>")),
},
)
.expect("parse");
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,12 +1,8 @@
-use std::{
- fmt::Debug,
- path::{Path, PathBuf},
- rc::Rc,
-};
+use std::{fmt::Debug, path::PathBuf};
use gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -85,7 +81,7 @@
StandaloneSuper,
#[error("can't resolve {1} from {0}")]
- ImportFileNotFound(PathBuf, PathBuf),
+ ImportFileNotFound(PathBuf, String),
#[error("resolved file not found: {0}")]
ResolvedFileNotFound(PathBuf),
#[error("imported file is not valid utf-8: {0:?}")]
@@ -94,6 +90,8 @@
ImportIo(String),
#[error("tried to import {1} from {0}, but imports is not supported")]
ImportNotSupported(PathBuf, PathBuf),
+ #[error("can't import from virtual file")]
+ CantImportFromVirtualFile,
#[error(
"syntax error: expected {}, got {:?}",
.error.expected,
@@ -101,8 +99,7 @@
.map_or_else(|| "EOF".into(), |c| c.to_string())
)]
ImportSyntaxError {
- #[skip_trace]
- path: Rc<Path>,
+ path: Source,
source_code: IStr,
#[skip_trace]
error: Box<jrsonnet_parser::ParseError>,
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -452,7 +452,7 @@
Ok(Some(v)) => Ok(v),
Ok(None) => throw!(NoSuchField(key.clone())),
Err(e) if matches!(e.error(), MagicThisFileUsed) => {
- Ok(Val::Str(loc.0.to_string_lossy().into()))
+ Ok(Val::Str(loc.0.full_path().into()))
}
Err(e) => Err(e),
},
@@ -617,28 +617,27 @@
std_slice(indexable.into_indexable()?, start, end, step)?
}
- Import(path) => {
+ i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {
let tmp = loc.clone().0;
- let mut import_location = tmp.to_path_buf();
- import_location.pop();
- s.push(
+ let import_location = tmp
+ .path()
+ .map(|p| {
+ let mut p = p.to_owned();
+ p.pop();
+ p
+ })
+ .unwrap_or_default();
+ let path = s.resolve_file(&import_location, path as &str)?;
+ match i {
+ Import(_) => s.push(
CallLocation::new(loc),
- || format!("import {:?}", path),
- || s.import_file(&import_location, path),
- )?
- }
- ImportStr(path) => {
- let tmp = loc.clone().0;
- let mut import_location = tmp.to_path_buf();
- import_location.pop();
- Val::Str(s.import_file_str(&import_location, path)?)
+ || format!("import {:?}", path.clone()),
+ || s.import(path.clone()),
+ )?,
+ ImportStr(_) => Val::Str(s.import_str(path)?),
+ ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(path)?)),
+ _ => unreachable!(),
}
- ImportBin(path) => {
- let tmp = loc.clone().0;
- let mut import_location = tmp.to_path_buf();
- import_location.pop();
- let bytes = s.import_file_bin(&import_location, path)?;
- Val::Arr(ArrValue::Bytes(bytes))
}
})
}
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -47,6 +47,7 @@
}
}
+#[derive(Clone)]
pub enum TlaArg {
String(IStr),
Code(LocExpr),
crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -1,4 +1,4 @@
-use std::{borrow::Cow, path::Path, rc::Rc};
+use std::borrow::Cow;
use gcmodule::Trace;
@@ -51,16 +51,16 @@
&self.params
}
- fn call(&self, s: State, ctx: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
+ fn call(&self, s: State, ctx: Context, _loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
let args = parse_builtin_call(s.clone(), ctx, &self.params, args, true)?;
let mut out_args = Vec::with_capacity(self.params.len());
for p in &self.params {
out_args.push(args[&p.name].evaluate(s.clone())?);
}
- self.handler.call(s, loc.0.map(|l| l.0.clone()), &out_args)
+ self.handler.call(s, &out_args)
}
}
pub trait NativeCallbackHandler: Trace {
- fn call(&self, s: State, from: Option<Rc<Path>>, args: &[Val]) -> Result<Val>;
+ fn call(&self, s: State, args: &[Val]) -> Result<Val>;
}
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -1,13 +1,14 @@
/// Macros to help deal with Gc
use std::{
borrow::{Borrow, BorrowMut},
- collections::{HashMap, HashSet},
+ collections::HashSet,
hash::BuildHasherDefault,
ops::{Deref, DerefMut},
};
use gcmodule::{Trace, Tracer};
-use rustc_hash::{FxHashMap, FxHashSet};
+use hashbrown::HashMap;
+use rustc_hash::{FxHashSet, FxHasher};
/// Replacement for box, which assumes that the underlying type is [`Trace`]
/// Used in places, where `Cc<dyn Trait>` should be used instead, but it can't, because `CoerceUnsiced` is not stable
@@ -115,14 +116,13 @@
}
}
-#[derive(Clone)]
-pub struct GcHashMap<K, V>(pub FxHashMap<K, V>);
+pub struct GcHashMap<K, V>(pub HashMap<K, V, BuildHasherDefault<FxHasher>>);
impl<K, V> GcHashMap<K, V> {
pub fn new() -> Self {
Self(HashMap::default())
}
pub fn with_capacity(capacity: usize) -> Self {
- Self(FxHashMap::with_capacity_and_hasher(
+ Self(HashMap::with_capacity_and_hasher(
capacity,
BuildHasherDefault::default(),
))
@@ -141,7 +141,7 @@
}
}
impl<K, V> Deref for GcHashMap<K, V> {
- type Target = FxHashMap<K, V>;
+ type Target = HashMap<K, V, BuildHasherDefault<FxHasher>>;
fn deref(&self) -> &Self::Target {
&self.0
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,14 +1,11 @@
use std::{
any::Any,
- convert::TryFrom,
fs,
io::Read,
path::{Path, PathBuf},
- rc::Rc,
};
use fs::File;
-use jrsonnet_interner::IStr;
use crate::{
error::{Error::*, Result},
@@ -20,21 +17,10 @@
/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
/// both to `/home/user/manifests/b.libjsonnet` and to `/home/user/${vendor}/b.libjsonnet`
/// where `${vendor}` is a library path.
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
+ fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf>;
fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
- /// Reads file from filesystem, should be used only with path received from `resolve_file`
- fn load_file_str(&self, resolved: &Path) -> Result<IStr> {
- Ok(IStr::try_from(&self.load_file_contents(resolved)? as &[u8])
- .map_err(|_| ImportBadFileUtf8(resolved.to_path_buf()))?)
- }
-
- /// Reads file from filesystem, should be used only with path received from `resolve_file`
- fn load_file_bin(&self, resolved: &Path) -> Result<Rc<[u8]>> {
- Ok(self.load_file_contents(resolved)?.into())
- }
-
/// # Safety
///
/// For use only in bindings, should not be used elsewhere.
@@ -46,7 +32,7 @@
/// Dummy resolver, can't resolve/load any file
pub struct DummyImportResolver;
impl ImportResolver for DummyImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
throw!(ImportNotSupported(from.into(), path.into()))
}
@@ -73,22 +59,23 @@
pub library_paths: Vec<PathBuf>,
}
impl ImportResolver for FileImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
+ fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
let mut direct = from.to_path_buf();
direct.push(path);
if direct.exists() {
- Ok(direct.into())
+ Ok(direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?)
} else {
for library_path in &self.library_paths {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(cloned.into());
+ return Ok(cloned.canonicalize().map_err(|e| ImportIo(e.to_string()))?);
}
}
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
+
fn load_file_contents(&self, id: &Path) -> Result<Vec<u8>> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.to_owned()))?;
let mut out = Vec::new();
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -38,6 +38,7 @@
pub mod val;
use std::{
+ borrow::Cow,
cell::{Ref, RefCell, RefMut},
collections::HashMap,
fmt::{self, Debug},
@@ -52,7 +53,9 @@
use function::{builtin::Builtin, CallLocation, TlaArg};
use gc::{GcHashMap, TraceBox};
use gcmodule::{Cc, Trace, Weak};
+use hashbrown::hash_map::RawEntryMut;
pub use import::*;
+use jrsonnet_interner::IBytes;
pub use jrsonnet_interner::IStr;
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
@@ -96,7 +99,7 @@
/// Limits amount of stack trace items preserved
pub max_trace: usize,
/// Used for s`td.extVar`
- pub ext_vars: HashMap<IStr, Val>,
+ pub ext_vars: HashMap<IStr, TlaArg>,
/// Used for ext.native
pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
/// TLA vars
@@ -141,16 +144,39 @@
stack_generation: usize,
breakpoints: Breakpoints,
+
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
- files: GcHashMap<Rc<Path>, FileData>,
- str_files: GcHashMap<Rc<Path>, IStr>,
- bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,
+ files: GcHashMap<PathBuf, FileData>,
+ /// Contains tla arguments and others, which aren't needed to be obtained by name
+ volatile_files: GcHashMap<String, String>,
}
+struct FileData {
+ string: Option<IStr>,
+ bytes: Option<IBytes>,
+ parsed: Option<LocExpr>,
+ evaluated: Option<Val>,
-pub struct FileData {
- source_code: IStr,
- parsed: LocExpr,
- evaluated: Option<Val>,
+ evaluating: bool,
+}
+impl FileData {
+ fn new_string(data: IStr) -> Self {
+ Self {
+ string: Some(data),
+ bytes: None,
+ parsed: None,
+ evaluated: None,
+ evaluating: false,
+ }
+ }
+ fn new_bytes(data: IBytes) -> Self {
+ Self {
+ string: None,
+ bytes: Some(data),
+ parsed: None,
+ evaluated: None,
+ evaluating: false,
+ }
+ }
}
#[allow(clippy::type_complexity)]
@@ -198,61 +224,159 @@
pub struct State(Rc<EvaluationStateInternals>);
impl State {
- /// Parses and adds file as loaded
- pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {
- let parsed = parse(
- &source_code,
- &ParserSettings {
- file_name: path.clone(),
- },
- )
- .map_err(|error| ImportSyntaxError {
- error: Box::new(error),
- path: path.clone(),
- source_code: source_code.clone(),
- })?;
- self.add_parsed_file(path, source_code, parsed.clone())?;
+ pub fn import_str(&self, path: PathBuf) -> Result<IStr> {
+ let mut data = self.data_mut();
+ let mut file = data.files.raw_entry_mut().from_key(&path);
- Ok(parsed)
+ let file = match file {
+ RawEntryMut::Occupied(ref mut d) => d.get_mut(),
+ RawEntryMut::Vacant(v) => {
+ let data = self.settings().import_resolver.load_file_contents(&path)?;
+ v.insert(
+ path.clone(),
+ FileData::new_string(
+ std::str::from_utf8(&data)
+ .map_err(|_| ImportBadFileUtf8(path.clone()))?
+ .into(),
+ ),
+ )
+ .1
+ }
+ };
+ if let Some(str) = &file.string {
+ return Ok(str.clone());
+ }
+ if file.string.is_none() {
+ file.string = Some(
+ file.bytes
+ .as_ref()
+ .expect("either string or bytes should be set")
+ .clone()
+ .cast_str()
+ .ok_or_else(|| ImportBadFileUtf8(path.clone()))?,
+ );
+ }
+ Ok(file.string.as_ref().expect("just set").clone())
}
+ pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {
+ let mut data = self.data_mut();
+ let mut file = data.files.raw_entry_mut().from_key(&path);
- pub fn reset_evaluation_state(&self, name: &Path) {
- self.data_mut()
- .files
- .get_mut(name)
- .expect("file not found")
- .evaluated
- .take();
+ let file = match file {
+ RawEntryMut::Occupied(ref mut d) => d.get_mut(),
+ RawEntryMut::Vacant(v) => {
+ let data = self.settings().import_resolver.load_file_contents(&path)?;
+ v.insert(path.clone(), FileData::new_bytes(data.as_slice().into()))
+ .1
+ }
+ };
+ if let Some(str) = &file.bytes {
+ return Ok(str.clone());
+ }
+ if file.bytes.is_none() {
+ file.bytes = Some(
+ file.string
+ .as_ref()
+ .expect("either string or bytes should be set")
+ .clone()
+ .cast_bytes(),
+ );
+ }
+ Ok(file.bytes.as_ref().expect("just set").clone())
}
+ pub fn import(&self, path: PathBuf) -> Result<Val> {
+ let mut data = self.data_mut();
+ let mut file = data.files.raw_entry_mut().from_key(&path);
- /// Adds file by source code and parsed expr
- pub fn add_parsed_file(
- &self,
- name: Rc<Path>,
- source_code: IStr,
- parsed: LocExpr,
- ) -> Result<()> {
- self.data_mut().files.insert(
- name,
- FileData {
- source_code,
- parsed,
- evaluated: None,
- },
- );
+ let file = match file {
+ RawEntryMut::Occupied(ref mut d) => d.get_mut(),
+ RawEntryMut::Vacant(v) => {
+ let data = self.settings().import_resolver.load_file_contents(&path)?;
+ v.insert(
+ path.clone(),
+ FileData::new_string(
+ std::str::from_utf8(&data)
+ .map_err(|_| ImportBadFileUtf8(path.clone()))?
+ .into(),
+ ),
+ )
+ .1
+ }
+ };
+ if let Some(val) = &file.evaluated {
+ return Ok(val.clone());
+ }
+ if file.string.is_none() {
+ file.string = Some(
+ std::str::from_utf8(
+ file.bytes
+ .as_ref()
+ .expect("either string or bytes should be set"),
+ )
+ .map_err(|_| ImportBadFileUtf8(path.clone()))?
+ .into(),
+ );
+ }
+ let code = file.string.as_ref().expect("just set");
+ let file_name = Source::new(path.clone()).expect("resolver should return correct name");
+ if file.parsed.is_none() {
+ file.parsed = Some(
+ jrsonnet_parser::parse(
+ code,
+ &ParserSettings {
+ file_name: file_name.clone(),
+ },
+ )
+ .map_err(|e| ImportSyntaxError {
+ path: file_name,
+ source_code: code.clone(),
+ error: Box::new(e),
+ })?,
+ );
+ }
+ let parsed = file.parsed.as_ref().expect("just set").clone();
+ if file.evaluating {
+ throw!(InfiniteRecursionDetected)
+ }
+ file.evaluating = true;
+ // Dropping file here, as it borrows data, which may be used in evaluation
+ drop(data);
+ let res = evaluate(self.clone(), self.create_default_context(), &parsed);
+
+ let mut data = self.data_mut();
+ let mut file = data.files.raw_entry_mut().from_key(&path);
- Ok(())
+ let file = match file {
+ RawEntryMut::Occupied(ref mut d) => d.get_mut(),
+ RawEntryMut::Vacant(_) => unreachable!("this file was just here!"),
+ };
+ file.evaluating = false;
+ match res {
+ Ok(v) => {
+ file.evaluated = Some(v.clone());
+ Ok(v)
+ }
+ Err(e) => Err(e),
+ }
}
- pub fn get_source(&self, name: &Path) -> Option<IStr> {
- let ro_map = &self.data().files;
- ro_map.get(name).map(|value| value.source_code.clone())
+
+ pub fn get_source(&self, name: Source) -> Option<String> {
+ let data = self.data();
+ match name.repr() {
+ Ok(real) => data
+ .files
+ .get(real)
+ .and_then(|f| f.string.as_ref())
+ .map(ToString::to_string),
+ Err(e) => data.volatile_files.get(e).map(ToOwned::to_owned),
+ }
}
- pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {
+ pub fn map_source_locations(&self, file: Source, locs: &[u32]) -> Vec<CodeLocation> {
offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)
}
pub fn map_from_source_location(
&self,
- file: &Path,
+ file: Source,
line: usize,
column: usize,
) -> Option<usize> {
@@ -262,74 +386,14 @@
column,
)
}
- pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
- let file_path = self.resolve_file(from, path)?;
- {
- let data = self.data();
- let files = &data.files;
- if files.contains_key(&file_path as &Path) {
- drop(data);
- return self.evaluate_loaded_file_raw(&file_path);
- }
- }
- let contents = self.load_file_str(&file_path)?;
- self.add_file(file_path.clone(), contents)?;
- self.evaluate_loaded_file_raw(&file_path)
- }
- pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {
- let path = self.resolve_file(from, path)?;
- if !self.data().str_files.contains_key(&path) {
- let file_str = self.load_file_str(&path)?;
- self.data_mut().str_files.insert(path.clone(), file_str);
- }
- Ok(self.data().str_files.get(&path).cloned().unwrap())
- }
- pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {
- let path = self.resolve_file(from, path)?;
- if !self.data().bin_files.contains_key(&path) {
- let file_bin = self.load_file_bin(&path)?;
- self.data_mut().bin_files.insert(path.clone(), file_bin);
- }
- Ok(self.data().bin_files.get(&path).cloned().unwrap())
- }
-
- fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {
- let expr: LocExpr = {
- let ro_map = &self.data().files;
- let value = ro_map
- .get(name)
- .unwrap_or_else(|| panic!("file not added: {:?}", name));
- if let Some(ref evaluated) = value.evaluated {
- return Ok(evaluated.clone());
- }
- value.parsed.clone()
- };
- let value = evaluate(self.clone(), self.create_default_context(), &expr)?;
- {
- self.data_mut()
- .files
- .get_mut(name)
- .unwrap()
- .evaluated
- .replace(value.clone());
- }
- Ok(value)
- }
-
/// Adds standard library global variable (std) to this evaluator
pub fn with_stdlib(&self) -> &Self {
- use jrsonnet_stdlib::STDLIB_STR;
- let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();
-
- self.add_parsed_file(
- std_path.clone(),
- STDLIB_STR.to_owned().into(),
- stdlib::get_parsed_stdlib(),
+ let val = evaluate(
+ self.clone(),
+ self.create_default_context(),
+ &stdlib::get_parsed_stdlib(),
)
- .expect("stdlib is correct");
- let val = self
- .evaluate_loaded_file_raw(&std_path)
- .expect("stdlib is correct");
+ .expect("std should not fail");
self.settings_mut().globals.insert("std".into(), val);
self
}
@@ -506,46 +570,55 @@
/// Raw methods evaluate passed values but don't perform TLA execution
impl State {
- pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {
- self.import_file(&std::env::current_dir().expect("cwd"), name)
- }
- pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {
- self.import_file(&PathBuf::from("."), name)
- }
/// Parses and evaluates the given snippet
- pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {
- let parsed = parse(
+ pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {
+ let source = Source::new_virtual(Cow::Owned(name.clone()));
+ let parsed = jrsonnet_parser::parse(
&code,
&ParserSettings {
file_name: source.clone(),
},
)
.map_err(|e| ImportSyntaxError {
- path: source.clone(),
- source_code: code.clone(),
+ path: source,
+ source_code: code.clone().into(),
error: Box::new(e),
})?;
- self.add_parsed_file(source, code, parsed.clone())?;
- self.evaluate_expr_raw(parsed)
+ self.data_mut().volatile_files.insert(name, code);
+ evaluate(self.clone(), self.create_default_context(), &parsed)
}
- /// Evaluates the parsed expression
- pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {
- evaluate(self.clone(), self.create_default_context(), &code)
- }
}
/// Settings utilities
impl State {
pub fn add_ext_var(&self, name: IStr, value: Val) {
- self.settings_mut().ext_vars.insert(name, value);
+ self.settings_mut()
+ .ext_vars
+ .insert(name, TlaArg::Val(value));
}
pub fn add_ext_str(&self, name: IStr, value: IStr) {
- self.add_ext_var(name, Val::Str(value));
+ self.settings_mut()
+ .ext_vars
+ .insert(name, TlaArg::String(value));
}
- pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {
- let value =
- self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;
- self.add_ext_var(name, value);
+ pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {
+ let source_name = format!("<extvar:{}>", name);
+ let source = Source::new_virtual(Cow::Owned(source_name.clone()));
+ let parsed = jrsonnet_parser::parse(
+ &code,
+ &ParserSettings {
+ file_name: source.clone(),
+ },
+ )
+ .map_err(|e| ImportSyntaxError {
+ path: source,
+ source_code: code.clone().into(),
+ error: Box::new(e),
+ })?;
+ self.data_mut().volatile_files.insert(source_name, code);
+ self.settings_mut()
+ .ext_vars
+ .insert(name.into(), TlaArg::Code(parsed));
Ok(())
}
@@ -559,22 +632,33 @@
.tla_vars
.insert(name, TlaArg::String(value));
}
- pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
- let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;
+ pub fn add_tla_code(&self, name: IStr, code: &str) -> Result<()> {
+ let source_name = format!("<top-level-arg:{}>", name);
+ let source = Source::new_virtual(Cow::Owned(source_name.clone()));
+ let parsed = jrsonnet_parser::parse(
+ code,
+ &ParserSettings {
+ file_name: source.clone(),
+ },
+ )
+ .map_err(|e| ImportSyntaxError {
+ path: source,
+ source_code: code.into(),
+ error: Box::new(e),
+ })?;
+ self.data_mut()
+ .volatile_files
+ .insert(source_name, code.to_owned());
self.settings_mut()
.tla_vars
.insert(name, TlaArg::Code(parsed));
Ok(())
}
- pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
- self.settings().import_resolver.resolve_file(from, path)
- }
- pub fn load_file_str(&self, path: &Path) -> Result<IStr> {
- self.settings().import_resolver.load_file_str(path)
- }
- pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {
- self.settings().import_resolver.load_file_bin(path)
+ pub fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ self.settings()
+ .import_resolver
+ .resolve_file(from, path.as_ref())
}
pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -14,6 +14,15 @@
pub struct LayeredHashMap(Cc<LayeredHashMapInternals>);
impl LayeredHashMap {
+ pub fn iter_keys(self, mut handler: impl FnMut(IStr)) {
+ for (k, _) in self.0.current.iter() {
+ handler(k.clone());
+ }
+ if let Some(parent) = self.0.parent.clone() {
+ parent.iter_keys(handler);
+ }
+ }
+
pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
Self(Cc::new(LayeredHashMapInternals {
parent: Some(self),
crates/jrsonnet-evaluator/src/stdlib/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/expr.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/expr.rs
@@ -1,6 +1,6 @@
-use std::path::PathBuf;
+use std::borrow::Cow;
-use jrsonnet_parser::{LocExpr, ParserSettings};
+use jrsonnet_parser::{LocExpr, ParserSettings, Source};
thread_local! {
/// To avoid parsing again when issued from the same thread
@@ -16,7 +16,7 @@
jrsonnet_parser::parse(
jrsonnet_stdlib::STDLIB_STR,
&ParserSettings {
- file_name: PathBuf::from("std.jsonnet").into(),
+ file_name: Source::new_virtual(Cow::Borrowed("<std>")),
},
)
.unwrap()
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -5,17 +5,17 @@
use format::{format_arr, format_obj};
use gcmodule::Cc;
-use jrsonnet_interner::IStr;
+use jrsonnet_interner::{IBytes, IStr};
use serde::Deserialize;
use serde_yaml::DeserializingQuirks;
use crate::{
error::{Error::*, Result},
- function::{builtin::StaticBuiltin, CallLocation, FuncVal},
+ function::{builtin::StaticBuiltin, ArgLike, CallLocation, FuncVal},
operator::evaluate_mod_op,
stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},
throw,
- typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, Typed, VecVal, M1},
+ typed::{Any, BoundedUsize, Either2, Either4, PositiveF64, Typed, VecVal, M1},
val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},
Either, ObjValue, State, Val,
};
@@ -365,12 +365,16 @@
#[jrsonnet_macros::builtin]
fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {
+ let ctx = s.create_default_context();
Ok(Any(s
+ .clone()
.settings()
.ext_vars
.get(&x)
.cloned()
- .ok_or(UndefinedExternalVariable(x))?))
+ .ok_or(UndefinedExternalVariable(x))?
+ .evaluate_arg(s.clone(), ctx, true)?
+ .evaluate(s)?))
}
#[jrsonnet_macros::builtin]
@@ -489,15 +493,15 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {
- Ok(Bytes(str.bytes().collect::<Vec<u8>>().into()))
+fn builtin_encode_utf8(str: IStr) -> Result<IBytes> {
+ Ok(str.cast_bytes())
}
#[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {
- Ok(std::str::from_utf8(&arr.0)
- .map_err(|_| RuntimeError("bad utf8".into()))?
- .into())
+fn builtin_decode_utf8(arr: IBytes) -> Result<IStr> {
+ Ok(arr
+ .cast_str()
+ .ok_or_else(|| RuntimeError("bad utf8".into()))?)
}
#[jrsonnet_macros::builtin]
@@ -509,33 +513,28 @@
fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {
eprint!("TRACE:");
if let Some(loc) = loc.0 {
- let locs = s.map_source_locations(&loc.0, &[loc.1]);
- eprint!(
- " {}:{}",
- loc.0.file_name().unwrap().to_str().unwrap(),
- locs[0].line
- );
+ let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);
+ eprint!(" {}:{}", loc.0.short_display(), locs[0].line);
}
eprintln!(" {}", str);
Ok(rest) as Result<Any>
}
#[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {
+fn builtin_base64(input: Either![IBytes, IStr]) -> Result<String> {
use Either2::*;
Ok(match input {
- A(a) => base64::encode(a.0),
+ A(a) => base64::encode(a.as_slice()),
B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
})
}
#[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {
- Ok(Bytes(
- base64::decode(&input.as_bytes())
- .map_err(|_| RuntimeError("bad base64".into()))?
- .into(),
- ))
+fn builtin_base64_decode_bytes(input: IStr) -> Result<IBytes> {
+ Ok(base64::decode(&input.as_bytes())
+ .map_err(|_| RuntimeError("bad base64".into()))?
+ .as_slice()
+ .into())
}
#[jrsonnet_macros::builtin]
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -24,7 +24,7 @@
}
#[allow(clippy::module_name_repetitions)]
-pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
+pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
if offsets.is_empty() {
return vec![];
}
@@ -59,7 +59,7 @@
{
column += 1;
match offset_map.last() {
- Some(x) if x.0 == pos => {
+ Some(x) if x.0 == pos as u32 => {
let out_idx = x.1;
with_no_known_line_ending.push(out_idx);
out[out_idx].offset = pos;
@@ -79,7 +79,7 @@
}
this_line_offset = pos + 1;
- if pos == max_offset + 1 {
+ if pos == max_offset as usize + 1 {
break;
}
}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -2,6 +2,7 @@
use std::path::{Path, PathBuf};
+use jrsonnet_parser::Source;
pub use location::*;
use crate::{error::Error, LocError, State};
@@ -56,7 +57,7 @@
) -> Result<(), std::fmt::Error> {
if start.line == end.line {
if start.column == end.column {
- write!(out, "{}:{}", start.line, end.column - 1)?;
+ write!(out, "{}:{}", start.line, end.column.saturating_sub(1))?;
} else {
write!(out, "{}:{}-{}", start.line, start.column - 1, end.column)?;
}
@@ -96,7 +97,10 @@
use std::fmt::Write;
writeln!(out)?;
- let mut n = self.resolver.resolve(path);
+ let mut n = match path.repr() {
+ Ok(r) => self.resolver.resolve(r),
+ Err(v) => v.to_string(),
+ };
let mut offset = error.location.offset;
let is_eof = if offset >= source_code.len() {
offset = source_code.len().saturating_sub(1);
@@ -104,7 +108,7 @@
} else {
false
};
- let mut location = offset_to_location(source_code, &[offset])
+ let mut location = offset_to_location(source_code, &[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -125,9 +129,13 @@
use std::fmt::Write;
#[allow(clippy::option_if_let_else)]
if let Some(location) = location {
- let mut resolved_path = self.resolver.resolve(&location.0);
+ let mut resolved_path = match location.0.repr() {
+ Ok(r) => self.resolver.resolve(r),
+ Err(v) => v.to_string(),
+ };
// TODO: Process all trace elements first
- let location = s.map_source_locations(&location.0, &[location.1, location.2]);
+ let location =
+ s.map_source_locations(location.0.clone(), &[location.1, location.2]);
write!(resolved_path, ":").unwrap();
print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
write!(resolved_path, ":").unwrap();
@@ -176,15 +184,16 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- let start_end = s.map_source_locations(&source.0, &[source.1, source.2]);
+ let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
+ let resolved_path = match source.0.repr() {
+ Ok(r) => r.display().to_string(),
+ Err(v) => v.to_string(),
+ };
write!(
out,
" at {} ({}:{}:{})",
- desc,
- source.0.to_str().unwrap(),
- start_end[0].line,
- start_end[0].column,
+ desc, resolved_path, start_end[0].line, start_end[0].column,
)?;
} else {
write!(out, " during {}", desc)?;
@@ -216,7 +225,7 @@
{
writeln!(out)?;
let offset = error.location.offset;
- let location = offset_to_location(source_code, &[offset])
+ let location = offset_to_location(source_code, &[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -237,10 +246,10 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- let start_end = s.map_source_locations(&source.0, &[source.1, source.2]);
+ let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
self.print_snippet(
out,
- &s.get_source(&source.0).unwrap(),
+ &s.get_source(source.0.clone()).unwrap(),
&source.0,
&start_end[0],
&start_end[1],
@@ -259,7 +268,7 @@
&self,
out: &mut dyn std::fmt::Write,
source: &str,
- origin: &Path,
+ origin: &Source,
start: &CodeLocation,
end: &CodeLocation,
desc: &str,
@@ -275,7 +284,10 @@
.take(end.line_end_offset - end.line_start_offset)
.collect();
- let origin = self.resolver.resolve(origin);
+ let origin = match origin.repr() {
+ Ok(r) => self.resolver.resolve(r),
+ Err(v) => v.to_string(),
+ };
let snippet = Snippet {
opt: FormatOptions {
color: true,
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,7 +1,7 @@
-use std::{ops::Deref, rc::Rc};
+use std::ops::Deref;
use gcmodule::Cc;
-use jrsonnet_interner::IStr;
+use jrsonnet_interner::{IBytes, IStr};
pub use jrsonnet_macros::Typed;
use jrsonnet_types::{ComplexValType, ValType};
@@ -303,19 +303,17 @@
}
/// Specialization
-pub struct Bytes(pub Rc<[u8]>);
-
-impl Typed for Bytes {
+impl Typed for IBytes {
const TYPE: &'static ComplexValType =
&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));
fn into_untyped(value: Self, _: State) -> Result<Val> {
- Ok(Val::Arr(ArrValue::Bytes(value.0)))
+ Ok(Val::Arr(ArrValue::Bytes(value)))
}
fn from_untyped(value: Val, s: State) -> Result<Self> {
if let Val::Arr(ArrValue::Bytes(bytes)) = value {
- return Ok(Self(bytes));
+ return Ok(bytes);
}
<Self as Typed>::TYPE.check(s.clone(), &value)?;
match value {
@@ -325,7 +323,7 @@
let r = e?;
out.push(u8::from_untyped(r, s.clone())?);
}
- Ok(Self(out.into()))
+ Ok(out.as_slice().into())
}
_ => unreachable!(),
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_types::ValType;67use crate::{8 cc_ptr_eq,9 error::{Error::*, LocError},10 function::FuncVal,11 gc::{GcHashMap, TraceBox},12 stdlib::manifest::{13 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,14 },15 throw, ObjValue, Result, State, Unbound, WeakObjValue,16};1718pub trait ThunkValue: Trace {19 type Output;20 fn get(self: Box<Self>, s: State) -> Result<Self::Output>;21}2223#[derive(Trace)]24enum ThunkInner<T> {25 Computed(T),26 Errored(LocError),27 Waiting(TraceBox<dyn ThunkValue<Output = T>>),28 Pending,29}3031#[allow(clippy::module_name_repetitions)]32#[derive(Clone, Trace)]33pub struct Thunk<T>(Cc<RefCell<ThunkInner<T>>>);34impl<T> Thunk<T>35where36 T: Clone + Trace,37{38 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {39 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))40 }41 pub fn evaluated(val: T) -> Self {42 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))43 }44 pub fn force(&self, s: State) -> Result<()> {45 self.evaluate(s)?;46 Ok(())47 }48 pub fn evaluate(&self, s: State) -> Result<T> {49 match &*self.0.borrow() {50 ThunkInner::Computed(v) => return Ok(v.clone()),51 ThunkInner::Errored(e) => return Err(e.clone()),52 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),53 ThunkInner::Waiting(..) => (),54 };55 let value = if let ThunkInner::Waiting(value) =56 std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)57 {58 value59 } else {60 unreachable!()61 };62 let new_value = match value.0.get(s) {63 Ok(v) => v,64 Err(e) => {65 *self.0.borrow_mut() = ThunkInner::Errored(e.clone());66 return Err(e);67 }68 };69 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());70 Ok(new_value)71 }72}7374type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);7576#[derive(Trace, Clone)]77pub struct CachedUnbound<I, T>78where79 I: Unbound<Bound = T>,80{81 cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,82 value: I,83}84impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {85 pub fn new(value: I) -> Self {86 Self {87 cache: Cc::new(RefCell::new(GcHashMap::new())),88 value,89 }90 }91}92impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {93 type Bound = T;94 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {95 let cache_key = (96 sup.as_ref().map(|s| s.clone().downgrade()),97 this.as_ref().map(|t| t.clone().downgrade()),98 );99 {100 if let Some(t) = self.cache.borrow().get(&cache_key) {101 return Ok(t.clone());102 }103 }104 let bound = self.value.bind(s, sup, this)?;105106 {107 let mut cache = self.cache.borrow_mut();108 cache.insert(cache_key, bound.clone());109 }110111 Ok(bound)112 }113}114115impl<T: Debug> Debug for Thunk<T> {116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {117 write!(f, "Lazy")118 }119}120impl<T> PartialEq for Thunk<T> {121 fn eq(&self, other: &Self) -> bool {122 cc_ptr_eq(&self.0, &other.0)123 }124}125126#[derive(Clone)]127pub enum ManifestFormat {128 YamlStream(Box<ManifestFormat>),129 Yaml {130 padding: usize,131 #[cfg(feature = "exp-preserve-order")]132 preserve_order: bool,133 },134 Json {135 padding: usize,136 #[cfg(feature = "exp-preserve-order")]137 preserve_order: bool,138 },139 ToString,140 String,141}142impl ManifestFormat {143 #[cfg(feature = "exp-preserve-order")]144 fn preserve_order(&self) -> bool {145 match self {146 ManifestFormat::YamlStream(s) => s.preserve_order(),147 ManifestFormat::Yaml { preserve_order, .. } => *preserve_order,148 ManifestFormat::Json { preserve_order, .. } => *preserve_order,149 ManifestFormat::ToString => false,150 ManifestFormat::String => false,151 }152 }153}154155#[derive(Debug, Clone, Trace)]156pub struct Slice {157 pub(crate) inner: ArrValue,158 pub(crate) from: u32,159 pub(crate) to: u32,160 pub(crate) step: u32,161}162impl Slice {163 const fn from(&self) -> usize {164 self.from as usize165 }166 const fn to(&self) -> usize {167 self.to as usize168 }169 const fn step(&self) -> usize {170 self.step as usize171 }172 const fn len(&self) -> usize {173 // TODO: use div_ceil174 let diff = self.to() - self.from();175 let rem = diff % self.step();176 let div = diff / self.step();177178 if rem == 0 {179 div180 } else {181 div + 1182 }183 }184}185186#[derive(Debug, Clone, Trace)]187#[force_tracking]188pub enum ArrValue {189 Bytes(#[skip_trace] Rc<[u8]>),190 Lazy(Cc<Vec<Thunk<Val>>>),191 Eager(Cc<Vec<Val>>),192 Extended(Box<(Self, Self)>),193 Range(i32, i32),194 Slice(Box<Slice>),195 Reversed(Box<Self>),196}197impl ArrValue {198 pub fn new_eager() -> Self {199 Self::Eager(Cc::new(Vec::new()))200 }201202 /// # Panics203 /// If a > b204 pub fn new_range(a: i32, b: i32) -> Self {205 assert!(a <= b);206 Self::Range(a, b)207 }208209 /// # Panics210 /// If passed numbers are incorrect211 #[must_use]212 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {213 let len = self.len();214 let from = from.unwrap_or(0);215 let to = to.unwrap_or(len).min(len);216 let step = step.unwrap_or(1);217 assert!(from < to);218 assert!(step > 0);219220 Self::Slice(Box::new(Slice {221 inner: self,222 from: from as u32,223 to: to as u32,224 step: step as u32,225 }))226 }227228 pub fn len(&self) -> usize {229 match self {230 Self::Bytes(i) => i.len(),231 Self::Lazy(l) => l.len(),232 Self::Eager(e) => e.len(),233 Self::Extended(v) => v.0.len() + v.1.len(),234 Self::Range(a, b) => a.abs_diff(*b) as usize + 1,235 Self::Reversed(i) => i.len(),236 Self::Slice(s) => s.len(),237 }238 }239240 pub fn is_empty(&self) -> bool {241 self.len() == 0242 }243244 pub fn get(&self, s: State, index: usize) -> Result<Option<Val>> {245 match self {246 Self::Bytes(i) => i247 .get(index)248 .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),249 Self::Lazy(vec) => {250 if let Some(v) = vec.get(index) {251 Ok(Some(v.evaluate(s)?))252 } else {253 Ok(None)254 }255 }256 Self::Eager(vec) => Ok(vec.get(index).cloned()),257 Self::Extended(v) => {258 let a_len = v.0.len();259 if a_len > index {260 v.0.get(s, index)261 } else {262 v.1.get(s, index - a_len)263 }264 }265 Self::Range(a, _) => {266 if index >= self.len() {267 return Ok(None);268 }269 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))270 }271 Self::Reversed(v) => {272 let len = v.len();273 if index >= len {274 return Ok(None);275 }276 v.get(s, len - index - 1)277 }278 Self::Slice(v) => {279 let index = v.from() + index * v.step();280 if index >= v.to() {281 return Ok(None);282 }283 v.inner.get(s, index as usize)284 }285 }286 }287288 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {289 match self {290 Self::Bytes(i) => i291 .get(index)292 .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),293 Self::Lazy(vec) => vec.get(index).cloned(),294 Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),295 Self::Extended(v) => {296 let a_len = v.0.len();297 if a_len > index {298 v.0.get_lazy(index)299 } else {300 v.1.get_lazy(index - a_len)301 }302 }303 Self::Range(a, _) => {304 if index >= self.len() {305 return None;306 }307 Some(Thunk::evaluated(Val::Num(308 ((*a as isize) + index as isize) as f64,309 )))310 }311 Self::Reversed(v) => {312 let len = v.len();313 if index >= len {314 return None;315 }316 v.get_lazy(len - index - 1)317 }318 Self::Slice(s) => {319 let index = s.from() + index * s.step();320 if index >= s.to() {321 return None;322 }323 s.inner.get_lazy(index as usize)324 }325 }326 }327328 pub fn evaluated(&self, s: State) -> Result<Cc<Vec<Val>>> {329 Ok(match self {330 Self::Bytes(i) => {331 let mut out = Vec::with_capacity(i.len());332 for v in i.iter() {333 out.push(Val::Num(f64::from(*v)));334 }335 Cc::new(out)336 }337 Self::Lazy(vec) => {338 let mut out = Vec::with_capacity(vec.len());339 for item in vec.iter() {340 out.push(item.evaluate(s.clone())?);341 }342 Cc::new(out)343 }344 Self::Eager(vec) => vec.clone(),345 Self::Extended(_v) => {346 let mut out = Vec::with_capacity(self.len());347 for item in self.iter(s) {348 out.push(item?);349 }350 Cc::new(out)351 }352 Self::Range(a, b) => {353 let mut out = Vec::with_capacity(self.len());354 for i in *a..*b {355 out.push(Val::Num(f64::from(i)));356 }357 Cc::new(out)358 }359 Self::Reversed(r) => {360 let mut r = r.evaluated(s)?;361 Cc::update_with(&mut r, |v| v.reverse());362 r363 }364 Self::Slice(v) => {365 let mut out = Vec::with_capacity(v.inner.len());366 for v in v367 .inner368 .iter_lazy()369 .skip(v.from())370 .take(v.to() - v.from())371 .step_by(v.step())372 {373 out.push(v.evaluate(s.clone())?);374 }375 Cc::new(out)376 }377 })378 }379380 pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {381 (0..self.len()).map(move |idx| match self {382 Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),383 Self::Lazy(l) => l[idx].evaluate(s.clone()),384 Self::Eager(e) => Ok(e[idx].clone()),385 Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {386 self.get(s.clone(), idx).map(|e| e.expect("idx < len"))387 }388 })389 }390391 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {392 (0..self.len()).map(move |idx| match self {393 Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),394 Self::Lazy(l) => l[idx].clone(),395 Self::Eager(e) => Thunk::evaluated(e[idx].clone()),396 Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {397 self.get_lazy(idx).expect("idx < len")398 }399 })400 }401402 #[must_use]403 pub fn reversed(self) -> Self {404 Self::Reversed(Box::new(self))405 }406407 pub fn map(self, s: State, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {408 let mut out = Vec::with_capacity(self.len());409410 for value in self.iter(s) {411 out.push(mapper(value?)?);412 }413414 Ok(Self::Eager(Cc::new(out)))415 }416417 pub fn filter(self, s: State, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {418 let mut out = Vec::with_capacity(self.len());419420 for value in self.iter(s) {421 let value = value?;422 if filter(&value)? {423 out.push(value);424 }425 }426427 Ok(Self::Eager(Cc::new(out)))428 }429430 pub fn ptr_eq(a: &Self, b: &Self) -> bool {431 match (a, b) {432 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),433 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),434 _ => false,435 }436 }437}438439impl From<Vec<Thunk<Val>>> for ArrValue {440 fn from(v: Vec<Thunk<Val>>) -> Self {441 Self::Lazy(Cc::new(v))442 }443}444445impl From<Vec<Val>> for ArrValue {446 fn from(v: Vec<Val>) -> Self {447 Self::Eager(Cc::new(v))448 }449}450451#[allow(clippy::module_name_repetitions)]452pub enum IndexableVal {453 Str(IStr),454 Arr(ArrValue),455}456457#[derive(Debug, Clone, Trace)]458pub enum Val {459 Bool(bool),460 Null,461 Str(IStr),462 Num(f64),463 Arr(ArrValue),464 Obj(ObjValue),465 Func(FuncVal),466}467468impl Val {469 pub const fn as_bool(&self) -> Option<bool> {470 match self {471 Val::Bool(v) => Some(*v),472 _ => None,473 }474 }475 pub const fn as_null(&self) -> Option<()> {476 match self {477 Val::Null => Some(()),478 _ => None,479 }480 }481 pub fn as_str(&self) -> Option<IStr> {482 match self {483 Val::Str(s) => Some(s.clone()),484 _ => None,485 }486 }487 pub const fn as_num(&self) -> Option<f64> {488 match self {489 Val::Num(n) => Some(*n),490 _ => None,491 }492 }493 pub fn as_arr(&self) -> Option<ArrValue> {494 match self {495 Val::Arr(a) => Some(a.clone()),496 _ => None,497 }498 }499 pub fn as_obj(&self) -> Option<ObjValue> {500 match self {501 Val::Obj(o) => Some(o.clone()),502 _ => None,503 }504 }505 pub fn as_func(&self) -> Option<FuncVal> {506 match self {507 Val::Func(f) => Some(f.clone()),508 _ => None,509 }510 }511512 /// Creates `Val::Num` after checking for numeric overflow.513 /// As numbers are `f64`, we can just check for their finity.514 pub fn new_checked_num(num: f64) -> Result<Self> {515 if num.is_finite() {516 Ok(Self::Num(num))517 } else {518 throw!(RuntimeError("overflow".into()))519 }520 }521522 pub const fn value_type(&self) -> ValType {523 match self {524 Self::Str(..) => ValType::Str,525 Self::Num(..) => ValType::Num,526 Self::Arr(..) => ValType::Arr,527 Self::Obj(..) => ValType::Obj,528 Self::Bool(_) => ValType::Bool,529 Self::Null => ValType::Null,530 Self::Func(..) => ValType::Func,531 }532 }533534 pub fn to_string(&self, s: State) -> Result<IStr> {535 Ok(match self {536 Self::Bool(true) => "true".into(),537 Self::Bool(false) => "false".into(),538 Self::Null => "null".into(),539 Self::Str(s) => s.clone(),540 v => manifest_json_ex(541 s,542 v,543 &ManifestJsonOptions {544 padding: "",545 mtype: ManifestType::ToString,546 newline: "\n",547 key_val_sep: ": ",548 #[cfg(feature = "exp-preserve-order")]549 preserve_order: false,550 },551 )?552 .into(),553 })554 }555556 /// Expects value to be object, outputs (key, manifested value) pairs557 pub fn manifest_multi(&self, s: State, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {558 let obj = match self {559 Self::Obj(obj) => obj,560 _ => throw!(MultiManifestOutputIsNotAObject),561 };562 let keys = obj.fields(563 #[cfg(feature = "exp-preserve-order")]564 ty.preserve_order(),565 );566 let mut out = Vec::with_capacity(keys.len());567 for key in keys {568 let value = obj569 .get(s.clone(), key.clone())?570 .expect("item in object")571 .manifest(s.clone(), ty)?;572 out.push((key, value));573 }574 Ok(out)575 }576577 /// Expects value to be array, outputs manifested values578 pub fn manifest_stream(&self, s: State, ty: &ManifestFormat) -> Result<Vec<IStr>> {579 let arr = match self {580 Self::Arr(a) => a,581 _ => throw!(StreamManifestOutputIsNotAArray),582 };583 let mut out = Vec::with_capacity(arr.len());584 for i in arr.iter(s.clone()) {585 out.push(i?.manifest(s.clone(), ty)?);586 }587 Ok(out)588 }589590 pub fn manifest(&self, s: State, ty: &ManifestFormat) -> Result<IStr> {591 Ok(match ty {592 ManifestFormat::YamlStream(format) => {593 let arr = match self {594 Self::Arr(a) => a,595 _ => throw!(StreamManifestOutputIsNotAArray),596 };597 let mut out = String::new();598599 match format as &ManifestFormat {600 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),601 ManifestFormat::String => throw!(StreamManifestCannotNestString),602 _ => {}603 };604605 if !arr.is_empty() {606 for v in arr.iter(s.clone()) {607 out.push_str("---\n");608 out.push_str(&v?.manifest(s.clone(), format)?);609 out.push('\n');610 }611 out.push_str("...");612 }613614 out.into()615 }616 ManifestFormat::Yaml {617 padding,618 #[cfg(feature = "exp-preserve-order")]619 preserve_order,620 } => self.to_yaml(621 s,622 *padding,623 #[cfg(feature = "exp-preserve-order")]624 *preserve_order,625 )?,626 ManifestFormat::Json {627 padding,628 #[cfg(feature = "exp-preserve-order")]629 preserve_order,630 } => self.to_json(631 s,632 *padding,633 #[cfg(feature = "exp-preserve-order")]634 *preserve_order,635 )?,636 ManifestFormat::ToString => self.to_string(s)?,637 ManifestFormat::String => match self {638 Self::Str(s) => s.clone(),639 _ => throw!(StringManifestOutputIsNotAString),640 },641 })642 }643644 /// For manifestification645 pub fn to_json(646 &self,647 s: State,648 padding: usize,649 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,650 ) -> Result<IStr> {651 manifest_json_ex(652 s,653 self,654 &ManifestJsonOptions {655 padding: &" ".repeat(padding),656 mtype: if padding == 0 {657 ManifestType::Minify658 } else {659 ManifestType::Manifest660 },661 newline: "\n",662 key_val_sep: ": ",663 #[cfg(feature = "exp-preserve-order")]664 preserve_order,665 },666 )667 .map(Into::into)668 }669670 /// Calls `std.manifestJson`671 pub fn to_std_json(672 &self,673 s: State,674 padding: usize,675 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,676 ) -> Result<Rc<str>> {677 manifest_json_ex(678 s,679 self,680 &ManifestJsonOptions {681 padding: &" ".repeat(padding),682 mtype: ManifestType::Std,683 newline: "\n",684 key_val_sep: ": ",685 #[cfg(feature = "exp-preserve-order")]686 preserve_order,687 },688 )689 .map(Into::into)690 }691692 pub fn to_yaml(693 &self,694 s: State,695 padding: usize,696 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,697 ) -> Result<IStr> {698 let padding = &" ".repeat(padding);699 manifest_yaml_ex(700 s,701 self,702 &ManifestYamlOptions {703 padding,704 arr_element_padding: padding,705 quote_keys: false,706 #[cfg(feature = "exp-preserve-order")]707 preserve_order,708 },709 )710 .map(Into::into)711 }712 pub fn into_indexable(self) -> Result<IndexableVal> {713 Ok(match self {714 Val::Str(s) => IndexableVal::Str(s),715 Val::Arr(arr) => IndexableVal::Arr(arr),716 _ => throw!(ValueIsNotIndexable(self.value_type())),717 })718 }719}720721const fn is_function_like(val: &Val) -> bool {722 matches!(val, Val::Func(_))723}724725/// Native implementation of `std.primitiveEquals`726pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {727 Ok(match (val_a, val_b) {728 (Val::Bool(a), Val::Bool(b)) => a == b,729 (Val::Null, Val::Null) => true,730 (Val::Str(a), Val::Str(b)) => a == b,731 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,732 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(733 "primitiveEquals operates on primitive types, got array".into(),734 )),735 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(736 "primitiveEquals operates on primitive types, got object".into(),737 )),738 (a, b) if is_function_like(a) && is_function_like(b) => {739 throw!(RuntimeError("cannot test equality of functions".into()))740 }741 (_, _) => false,742 })743}744745/// Native implementation of `std.equals`746pub fn equals(s: State, val_a: &Val, val_b: &Val) -> Result<bool> {747 if val_a.value_type() != val_b.value_type() {748 return Ok(false);749 }750 match (val_a, val_b) {751 (Val::Arr(a), Val::Arr(b)) => {752 if ArrValue::ptr_eq(a, b) {753 return Ok(true);754 }755 if a.len() != b.len() {756 return Ok(false);757 }758 for (a, b) in a.iter(s.clone()).zip(b.iter(s.clone())) {759 if !equals(s.clone(), &a?, &b?)? {760 return Ok(false);761 }762 }763 Ok(true)764 }765 (Val::Obj(a), Val::Obj(b)) => {766 if ObjValue::ptr_eq(a, b) {767 return Ok(true);768 }769 let fields = a.fields(770 #[cfg(feature = "exp-preserve-order")]771 false,772 );773 if fields774 != b.fields(775 #[cfg(feature = "exp-preserve-order")]776 false,777 ) {778 return Ok(false);779 }780 for field in fields {781 if !equals(782 s.clone(),783 &a.get(s.clone(), field.clone())?.expect("field exists"),784 &b.get(s.clone(), field)?.expect("field exists"),785 )? {786 return Ok(false);787 }788 }789 Ok(true)790 }791 (a, b) => Ok(primitive_equals(a, b)?),792 }793}1use std::{cell::RefCell, fmt::Debug, rc::Rc};23use gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5use jrsonnet_types::ValType;67use crate::{8 cc_ptr_eq,9 error::{Error::*, LocError},10 function::FuncVal,11 gc::{GcHashMap, TraceBox},12 stdlib::manifest::{13 manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,14 },15 throw, ObjValue, Result, State, Unbound, WeakObjValue,16};1718pub trait ThunkValue: Trace {19 type Output;20 fn get(self: Box<Self>, s: State) -> Result<Self::Output>;21}2223#[derive(Trace)]24enum ThunkInner<T> {25 Computed(T),26 Errored(LocError),27 Waiting(TraceBox<dyn ThunkValue<Output = T>>),28 Pending,29}3031#[allow(clippy::module_name_repetitions)]32#[derive(Clone, Trace)]33pub struct Thunk<T>(Cc<RefCell<ThunkInner<T>>>);3435impl<T> Thunk<T>36where37 T: Clone + Trace,38{39 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {40 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))41 }42 pub fn evaluated(val: T) -> Self {43 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))44 }45 pub fn force(&self, s: State) -> Result<()> {46 self.evaluate(s)?;47 Ok(())48 }49 pub fn evaluate(&self, s: State) -> Result<T> {50 match &*self.0.borrow() {51 ThunkInner::Computed(v) => return Ok(v.clone()),52 ThunkInner::Errored(e) => return Err(e.clone()),53 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),54 ThunkInner::Waiting(..) => (),55 };56 let value = if let ThunkInner::Waiting(value) =57 std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending)58 {59 value60 } else {61 unreachable!()62 };63 let new_value = match value.0.get(s) {64 Ok(v) => v,65 Err(e) => {66 *self.0.borrow_mut() = ThunkInner::Errored(e.clone());67 return Err(e);68 }69 };70 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());71 Ok(new_value)72 }73}7475type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);7677#[derive(Trace, Clone)]78pub struct CachedUnbound<I, T>79where80 I: Unbound<Bound = T>,81{82 cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,83 value: I,84}85impl<I: Unbound<Bound = T>, T: Trace> CachedUnbound<I, T> {86 pub fn new(value: I) -> Self {87 Self {88 cache: Cc::new(RefCell::new(GcHashMap::new())),89 value,90 }91 }92}93impl<I: Unbound<Bound = T>, T: Clone + Trace> Unbound for CachedUnbound<I, T> {94 type Bound = T;95 fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {96 let cache_key = (97 sup.as_ref().map(|s| s.clone().downgrade()),98 this.as_ref().map(|t| t.clone().downgrade()),99 );100 {101 if let Some(t) = self.cache.borrow().get(&cache_key) {102 return Ok(t.clone());103 }104 }105 let bound = self.value.bind(s, sup, this)?;106107 {108 let mut cache = self.cache.borrow_mut();109 cache.insert(cache_key, bound.clone());110 }111112 Ok(bound)113 }114}115116impl<T: Debug> Debug for Thunk<T> {117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {118 write!(f, "Lazy")119 }120}121impl<T> PartialEq for Thunk<T> {122 fn eq(&self, other: &Self) -> bool {123 cc_ptr_eq(&self.0, &other.0)124 }125}126127#[derive(Clone)]128pub enum ManifestFormat {129 YamlStream(Box<ManifestFormat>),130 Yaml {131 padding: usize,132 #[cfg(feature = "exp-preserve-order")]133 preserve_order: bool,134 },135 Json {136 padding: usize,137 #[cfg(feature = "exp-preserve-order")]138 preserve_order: bool,139 },140 ToString,141 String,142}143impl ManifestFormat {144 #[cfg(feature = "exp-preserve-order")]145 fn preserve_order(&self) -> bool {146 match self {147 ManifestFormat::YamlStream(s) => s.preserve_order(),148 ManifestFormat::Yaml { preserve_order, .. } => *preserve_order,149 ManifestFormat::Json { preserve_order, .. } => *preserve_order,150 ManifestFormat::ToString => false,151 ManifestFormat::String => false,152 }153 }154}155156#[derive(Debug, Clone, Trace)]157pub struct Slice {158 pub(crate) inner: ArrValue,159 pub(crate) from: u32,160 pub(crate) to: u32,161 pub(crate) step: u32,162}163impl Slice {164 const fn from(&self) -> usize {165 self.from as usize166 }167 const fn to(&self) -> usize {168 self.to as usize169 }170 const fn step(&self) -> usize {171 self.step as usize172 }173 const fn len(&self) -> usize {174 // TODO: use div_ceil175 let diff = self.to() - self.from();176 let rem = diff % self.step();177 let div = diff / self.step();178179 if rem == 0 {180 div181 } else {182 div + 1183 }184 }185}186187#[derive(Debug, Clone, Trace)]188#[force_tracking]189pub enum ArrValue {190 Bytes(#[skip_trace] IBytes),191 Lazy(Cc<Vec<Thunk<Val>>>),192 Eager(Cc<Vec<Val>>),193 Extended(Box<(Self, Self)>),194 Range(i32, i32),195 Slice(Box<Slice>),196 Reversed(Box<Self>),197}198199#[cfg(target_pointer_width = "64")]200static_assertions::assert_eq_size!(ArrValue, [u8; 16]);201202impl ArrValue {203 pub fn new_eager() -> Self {204 Self::Eager(Cc::new(Vec::new()))205 }206207 /// # Panics208 /// If a > b209 pub fn new_range(a: i32, b: i32) -> Self {210 assert!(a <= b);211 Self::Range(a, b)212 }213214 /// # Panics215 /// If passed numbers are incorrect216 #[must_use]217 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {218 let len = self.len();219 let from = from.unwrap_or(0);220 let to = to.unwrap_or(len).min(len);221 let step = step.unwrap_or(1);222 assert!(from < to);223 assert!(step > 0);224225 Self::Slice(Box::new(Slice {226 inner: self,227 from: from as u32,228 to: to as u32,229 step: step as u32,230 }))231 }232233 pub fn len(&self) -> usize {234 match self {235 Self::Bytes(i) => i.len(),236 Self::Lazy(l) => l.len(),237 Self::Eager(e) => e.len(),238 Self::Extended(v) => v.0.len() + v.1.len(),239 Self::Range(a, b) => a.abs_diff(*b) as usize + 1,240 Self::Reversed(i) => i.len(),241 Self::Slice(s) => s.len(),242 }243 }244245 pub fn is_empty(&self) -> bool {246 self.len() == 0247 }248249 pub fn get(&self, s: State, index: usize) -> Result<Option<Val>> {250 match self {251 Self::Bytes(i) => i252 .get(index)253 .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),254 Self::Lazy(vec) => {255 if let Some(v) = vec.get(index) {256 Ok(Some(v.evaluate(s)?))257 } else {258 Ok(None)259 }260 }261 Self::Eager(vec) => Ok(vec.get(index).cloned()),262 Self::Extended(v) => {263 let a_len = v.0.len();264 if a_len > index {265 v.0.get(s, index)266 } else {267 v.1.get(s, index - a_len)268 }269 }270 Self::Range(a, _) => {271 if index >= self.len() {272 return Ok(None);273 }274 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))275 }276 Self::Reversed(v) => {277 let len = v.len();278 if index >= len {279 return Ok(None);280 }281 v.get(s, len - index - 1)282 }283 Self::Slice(v) => {284 let index = v.from() + index * v.step();285 if index >= v.to() {286 return Ok(None);287 }288 v.inner.get(s, index as usize)289 }290 }291 }292293 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {294 match self {295 Self::Bytes(i) => i296 .get(index)297 .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),298 Self::Lazy(vec) => vec.get(index).cloned(),299 Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),300 Self::Extended(v) => {301 let a_len = v.0.len();302 if a_len > index {303 v.0.get_lazy(index)304 } else {305 v.1.get_lazy(index - a_len)306 }307 }308 Self::Range(a, _) => {309 if index >= self.len() {310 return None;311 }312 Some(Thunk::evaluated(Val::Num(313 ((*a as isize) + index as isize) as f64,314 )))315 }316 Self::Reversed(v) => {317 let len = v.len();318 if index >= len {319 return None;320 }321 v.get_lazy(len - index - 1)322 }323 Self::Slice(s) => {324 let index = s.from() + index * s.step();325 if index >= s.to() {326 return None;327 }328 s.inner.get_lazy(index as usize)329 }330 }331 }332333 pub fn evaluated(&self, s: State) -> Result<Cc<Vec<Val>>> {334 Ok(match self {335 Self::Bytes(i) => {336 let mut out = Vec::with_capacity(i.len());337 for v in i.iter() {338 out.push(Val::Num(f64::from(*v)));339 }340 Cc::new(out)341 }342 Self::Lazy(vec) => {343 let mut out = Vec::with_capacity(vec.len());344 for item in vec.iter() {345 out.push(item.evaluate(s.clone())?);346 }347 Cc::new(out)348 }349 Self::Eager(vec) => vec.clone(),350 Self::Extended(_v) => {351 let mut out = Vec::with_capacity(self.len());352 for item in self.iter(s) {353 out.push(item?);354 }355 Cc::new(out)356 }357 Self::Range(a, b) => {358 let mut out = Vec::with_capacity(self.len());359 for i in *a..*b {360 out.push(Val::Num(f64::from(i)));361 }362 Cc::new(out)363 }364 Self::Reversed(r) => {365 let mut r = r.evaluated(s)?;366 Cc::update_with(&mut r, |v| v.reverse());367 r368 }369 Self::Slice(v) => {370 let mut out = Vec::with_capacity(v.inner.len());371 for v in v372 .inner373 .iter_lazy()374 .skip(v.from())375 .take(v.to() - v.from())376 .step_by(v.step())377 {378 out.push(v.evaluate(s.clone())?);379 }380 Cc::new(out)381 }382 })383 }384385 pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {386 (0..self.len()).map(move |idx| match self {387 Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),388 Self::Lazy(l) => l[idx].evaluate(s.clone()),389 Self::Eager(e) => Ok(e[idx].clone()),390 Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {391 self.get(s.clone(), idx).map(|e| e.expect("idx < len"))392 }393 })394 }395396 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {397 (0..self.len()).map(move |idx| match self {398 Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),399 Self::Lazy(l) => l[idx].clone(),400 Self::Eager(e) => Thunk::evaluated(e[idx].clone()),401 Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {402 self.get_lazy(idx).expect("idx < len")403 }404 })405 }406407 #[must_use]408 pub fn reversed(self) -> Self {409 Self::Reversed(Box::new(self))410 }411412 pub fn map(self, s: State, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {413 let mut out = Vec::with_capacity(self.len());414415 for value in self.iter(s) {416 out.push(mapper(value?)?);417 }418419 Ok(Self::Eager(Cc::new(out)))420 }421422 pub fn filter(self, s: State, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {423 let mut out = Vec::with_capacity(self.len());424425 for value in self.iter(s) {426 let value = value?;427 if filter(&value)? {428 out.push(value);429 }430 }431432 Ok(Self::Eager(Cc::new(out)))433 }434435 pub fn ptr_eq(a: &Self, b: &Self) -> bool {436 match (a, b) {437 (Self::Lazy(a), Self::Lazy(b)) => cc_ptr_eq(a, b),438 (Self::Eager(a), Self::Eager(b)) => cc_ptr_eq(a, b),439 _ => false,440 }441 }442}443444impl From<Vec<Thunk<Val>>> for ArrValue {445 fn from(v: Vec<Thunk<Val>>) -> Self {446 Self::Lazy(Cc::new(v))447 }448}449450impl From<Vec<Val>> for ArrValue {451 fn from(v: Vec<Val>) -> Self {452 Self::Eager(Cc::new(v))453 }454}455456#[allow(clippy::module_name_repetitions)]457pub enum IndexableVal {458 Str(IStr),459 Arr(ArrValue),460}461462#[derive(Debug, Clone, Trace)]463pub enum Val {464 Bool(bool),465 Null,466 Str(IStr),467 Num(f64),468 Arr(ArrValue),469 Obj(ObjValue),470 Func(FuncVal),471}472473#[cfg(target_pointer_width = "64")]474static_assertions::assert_eq_size!(Val, [u8; 32]);475476impl Val {477 pub const fn as_bool(&self) -> Option<bool> {478 match self {479 Val::Bool(v) => Some(*v),480 _ => None,481 }482 }483 pub const fn as_null(&self) -> Option<()> {484 match self {485 Val::Null => Some(()),486 _ => None,487 }488 }489 pub fn as_str(&self) -> Option<IStr> {490 match self {491 Val::Str(s) => Some(s.clone()),492 _ => None,493 }494 }495 pub const fn as_num(&self) -> Option<f64> {496 match self {497 Val::Num(n) => Some(*n),498 _ => None,499 }500 }501 pub fn as_arr(&self) -> Option<ArrValue> {502 match self {503 Val::Arr(a) => Some(a.clone()),504 _ => None,505 }506 }507 pub fn as_obj(&self) -> Option<ObjValue> {508 match self {509 Val::Obj(o) => Some(o.clone()),510 _ => None,511 }512 }513 pub fn as_func(&self) -> Option<FuncVal> {514 match self {515 Val::Func(f) => Some(f.clone()),516 _ => None,517 }518 }519520 /// Creates `Val::Num` after checking for numeric overflow.521 /// As numbers are `f64`, we can just check for their finity.522 pub fn new_checked_num(num: f64) -> Result<Self> {523 if num.is_finite() {524 Ok(Self::Num(num))525 } else {526 throw!(RuntimeError("overflow".into()))527 }528 }529530 pub const fn value_type(&self) -> ValType {531 match self {532 Self::Str(..) => ValType::Str,533 Self::Num(..) => ValType::Num,534 Self::Arr(..) => ValType::Arr,535 Self::Obj(..) => ValType::Obj,536 Self::Bool(_) => ValType::Bool,537 Self::Null => ValType::Null,538 Self::Func(..) => ValType::Func,539 }540 }541542 pub fn to_string(&self, s: State) -> Result<IStr> {543 Ok(match self {544 Self::Bool(true) => "true".into(),545 Self::Bool(false) => "false".into(),546 Self::Null => "null".into(),547 Self::Str(s) => s.clone(),548 v => manifest_json_ex(549 s,550 v,551 &ManifestJsonOptions {552 padding: "",553 mtype: ManifestType::ToString,554 newline: "\n",555 key_val_sep: ": ",556 #[cfg(feature = "exp-preserve-order")]557 preserve_order: false,558 },559 )?560 .into(),561 })562 }563564 /// Expects value to be object, outputs (key, manifested value) pairs565 pub fn manifest_multi(&self, s: State, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {566 let obj = match self {567 Self::Obj(obj) => obj,568 _ => throw!(MultiManifestOutputIsNotAObject),569 };570 let keys = obj.fields(571 #[cfg(feature = "exp-preserve-order")]572 ty.preserve_order(),573 );574 let mut out = Vec::with_capacity(keys.len());575 for key in keys {576 let value = obj577 .get(s.clone(), key.clone())?578 .expect("item in object")579 .manifest(s.clone(), ty)?;580 out.push((key, value));581 }582 Ok(out)583 }584585 /// Expects value to be array, outputs manifested values586 pub fn manifest_stream(&self, s: State, ty: &ManifestFormat) -> Result<Vec<IStr>> {587 let arr = match self {588 Self::Arr(a) => a,589 _ => throw!(StreamManifestOutputIsNotAArray),590 };591 let mut out = Vec::with_capacity(arr.len());592 for i in arr.iter(s.clone()) {593 out.push(i?.manifest(s.clone(), ty)?);594 }595 Ok(out)596 }597598 pub fn manifest(&self, s: State, ty: &ManifestFormat) -> Result<IStr> {599 Ok(match ty {600 ManifestFormat::YamlStream(format) => {601 let arr = match self {602 Self::Arr(a) => a,603 _ => throw!(StreamManifestOutputIsNotAArray),604 };605 let mut out = String::new();606607 match format as &ManifestFormat {608 ManifestFormat::YamlStream(_) => throw!(StreamManifestOutputCannotBeRecursed),609 ManifestFormat::String => throw!(StreamManifestCannotNestString),610 _ => {}611 };612613 if !arr.is_empty() {614 for v in arr.iter(s.clone()) {615 out.push_str("---\n");616 out.push_str(&v?.manifest(s.clone(), format)?);617 out.push('\n');618 }619 out.push_str("...");620 }621622 out.into()623 }624 ManifestFormat::Yaml {625 padding,626 #[cfg(feature = "exp-preserve-order")]627 preserve_order,628 } => self.to_yaml(629 s,630 *padding,631 #[cfg(feature = "exp-preserve-order")]632 *preserve_order,633 )?,634 ManifestFormat::Json {635 padding,636 #[cfg(feature = "exp-preserve-order")]637 preserve_order,638 } => self.to_json(639 s,640 *padding,641 #[cfg(feature = "exp-preserve-order")]642 *preserve_order,643 )?,644 ManifestFormat::ToString => self.to_string(s)?,645 ManifestFormat::String => match self {646 Self::Str(s) => s.clone(),647 _ => throw!(StringManifestOutputIsNotAString),648 },649 })650 }651652 /// For manifestification653 pub fn to_json(654 &self,655 s: State,656 padding: usize,657 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,658 ) -> Result<IStr> {659 manifest_json_ex(660 s,661 self,662 &ManifestJsonOptions {663 padding: &" ".repeat(padding),664 mtype: if padding == 0 {665 ManifestType::Minify666 } else {667 ManifestType::Manifest668 },669 newline: "\n",670 key_val_sep: ": ",671 #[cfg(feature = "exp-preserve-order")]672 preserve_order,673 },674 )675 .map(Into::into)676 }677678 /// Calls `std.manifestJson`679 pub fn to_std_json(680 &self,681 s: State,682 padding: usize,683 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,684 ) -> Result<Rc<str>> {685 manifest_json_ex(686 s,687 self,688 &ManifestJsonOptions {689 padding: &" ".repeat(padding),690 mtype: ManifestType::Std,691 newline: "\n",692 key_val_sep: ": ",693 #[cfg(feature = "exp-preserve-order")]694 preserve_order,695 },696 )697 .map(Into::into)698 }699700 pub fn to_yaml(701 &self,702 s: State,703 padding: usize,704 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,705 ) -> Result<IStr> {706 let padding = &" ".repeat(padding);707 manifest_yaml_ex(708 s,709 self,710 &ManifestYamlOptions {711 padding,712 arr_element_padding: padding,713 quote_keys: false,714 #[cfg(feature = "exp-preserve-order")]715 preserve_order,716 },717 )718 .map(Into::into)719 }720 pub fn into_indexable(self) -> Result<IndexableVal> {721 Ok(match self {722 Val::Str(s) => IndexableVal::Str(s),723 Val::Arr(arr) => IndexableVal::Arr(arr),724 _ => throw!(ValueIsNotIndexable(self.value_type())),725 })726 }727}728729const fn is_function_like(val: &Val) -> bool {730 matches!(val, Val::Func(_))731}732733/// Native implementation of `std.primitiveEquals`734pub fn primitive_equals(val_a: &Val, val_b: &Val) -> Result<bool> {735 Ok(match (val_a, val_b) {736 (Val::Bool(a), Val::Bool(b)) => a == b,737 (Val::Null, Val::Null) => true,738 (Val::Str(a), Val::Str(b)) => a == b,739 (Val::Num(a), Val::Num(b)) => (a - b).abs() <= f64::EPSILON,740 (Val::Arr(_), Val::Arr(_)) => throw!(RuntimeError(741 "primitiveEquals operates on primitive types, got array".into(),742 )),743 (Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(744 "primitiveEquals operates on primitive types, got object".into(),745 )),746 (a, b) if is_function_like(a) && is_function_like(b) => {747 throw!(RuntimeError("cannot test equality of functions".into()))748 }749 (_, _) => false,750 })751}752753/// Native implementation of `std.equals`754pub fn equals(s: State, val_a: &Val, val_b: &Val) -> Result<bool> {755 if val_a.value_type() != val_b.value_type() {756 return Ok(false);757 }758 match (val_a, val_b) {759 (Val::Arr(a), Val::Arr(b)) => {760 if ArrValue::ptr_eq(a, b) {761 return Ok(true);762 }763 if a.len() != b.len() {764 return Ok(false);765 }766 for (a, b) in a.iter(s.clone()).zip(b.iter(s.clone())) {767 if !equals(s.clone(), &a?, &b?)? {768 return Ok(false);769 }770 }771 Ok(true)772 }773 (Val::Obj(a), Val::Obj(b)) => {774 if ObjValue::ptr_eq(a, b) {775 return Ok(true);776 }777 let fields = a.fields(778 #[cfg(feature = "exp-preserve-order")]779 false,780 );781 if fields782 != b.fields(783 #[cfg(feature = "exp-preserve-order")]784 false,785 ) {786 return Ok(false);787 }788 for field in fields {789 if !equals(790 s.clone(),791 &a.get(s.clone(), field.clone())?.expect("field exists"),792 &b.get(s.clone(), field)?.expect("field exists"),793 )? {794 return Ok(false);795 }796 }797 Ok(true)798 }799 (a, b) => Ok(primitive_equals(a, b)?),800 }801}crates/jrsonnet-evaluator/tests/as_native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/as_native.rs
+++ b/crates/jrsonnet-evaluator/tests/as_native.rs
@@ -1,5 +1,3 @@
-use std::path::PathBuf;
-
use jrsonnet_evaluator::{error::Result, State};
mod common;
@@ -9,7 +7,7 @@
let s = State::default();
s.with_stdlib();
- let val = s.evaluate_snippet_raw(PathBuf::new().into(), r#"function(a, b) a + b"#.into())?;
+ let val = s.evaluate_snippet("snip".to_owned(), r#"function(a, b) a + b"#.into())?;
let func = val.as_func().expect("this is function");
let native = func.into_native::<((u32, u32), u32)>();
crates/jrsonnet-evaluator/tests/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/builtin.rs
+++ b/crates/jrsonnet-evaluator/tests/builtin.rs
@@ -1,7 +1,5 @@
mod common;
-use std::path::PathBuf;
-
use gcmodule::Cc;
use jrsonnet_evaluator::{
error::Result,
@@ -27,7 +25,7 @@
CallLocation::native(),
&(),
)?,
- s.clone(),
+ s,
)?;
ensure_eq!(v, 1);
@@ -48,8 +46,8 @@
Val::Func(FuncVal::StaticBuiltin(native_add::INST)),
);
- let v = s.evaluate_snippet_raw(
- PathBuf::new().into(),
+ let v = s.evaluate_snippet(
+ "snip".to_owned(),
"
assert nativeAdd(1, 2) == 3;
assert nativeAdd(100, 200) == 300;
@@ -57,7 +55,7 @@
"
.into(),
)?;
- ensure_val_eq!(s.clone(), v, Val::Null);
+ ensure_val_eq!(s, v, Val::Null);
Ok(())
}
@@ -82,8 +80,8 @@
Val::Func(FuncVal::StaticBuiltin(curry_add::INST)),
);
- let v = s.evaluate_snippet_raw(
- PathBuf::new().into(),
+ let v = s.evaluate_snippet(
+ "snip".to_owned(),
"
local a = curryAdd(1);
local b = curryAdd(4);
@@ -97,6 +95,6 @@
"
.into(),
)?;
- ensure_val_eq!(s.clone(), v, Val::Null);
+ ensure_val_eq!(s, v, Val::Null);
Ok(())
}
crates/jrsonnet-evaluator/tests/golden.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/golden.rs
+++ b/crates/jrsonnet-evaluator/tests/golden.rs
@@ -20,7 +20,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- let v = match s.evaluate_file_raw(file) {
+ let v = match s.import(file.to_owned()) {
Ok(v) => v,
Err(e) => return s.stringify_err(&e),
};
crates/jrsonnet-evaluator/tests/golden/issue23.jsonnet.goldendiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/golden/issue23.jsonnet.golden
+++ b/crates/jrsonnet-evaluator/tests/golden/issue23.jsonnet.golden
@@ -1,202 +1,2 @@
-stack overflow, try to reduce recursion, or set --max-stack to bigger value
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
- issue23.jsonnet:1:1-26: import "issue23.jsonnet"
\ No newline at end of file
+infinite recursion detected
+ issue23.jsonnet:1:1-26: import "/home/lach/build/jrsonnet/crates/jrsonnet-evaluator/tests/golden/issue23.jsonnet"
\ No newline at end of file
crates/jrsonnet-evaluator/tests/golden/missing_binding.jsonnet.goldendiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/golden/missing_binding.jsonnet.golden
+++ b/crates/jrsonnet-evaluator/tests/golden/missing_binding.jsonnet.golden
@@ -1,2 +1,3 @@
-variable is not defined: a
+runtime error: variable is not defined: a
+There is variable(s) with similar names present: std, test
missing_binding.jsonnet:1:1-3: variable <a> access
\ No newline at end of file
crates/jrsonnet-evaluator/tests/sanity.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/sanity.rs
+++ b/crates/jrsonnet-evaluator/tests/sanity.rs
@@ -1,5 +1,3 @@
-use std::path::PathBuf;
-
use jrsonnet_evaluator::{error::Result, throw_runtime, State, Val};
mod common;
@@ -9,10 +7,10 @@
let s = State::default();
s.with_stdlib();
- let v = s.evaluate_snippet_raw(PathBuf::new().into(), "assert 1 == 1: 'fail'; null".into())?;
- ensure_val_eq!(s.clone(), v, Val::Null);
- let v = s.evaluate_snippet_raw(PathBuf::new().into(), "std.assertEqual(1, 1)".into())?;
- ensure_val_eq!(s.clone(), v, Val::Bool(true));
+ let v = s.evaluate_snippet("snip".to_owned(), "assert 1 == 1: 'fail'; null".into())?;
+ ensure_val_eq!(s, v, Val::Null);
+ let v = s.evaluate_snippet("snip".to_owned(), "std.assertEqual(1, 1)".into())?;
+ ensure_val_eq!(s, v, Val::Bool(true));
Ok(())
}
@@ -23,9 +21,7 @@
s.with_stdlib();
{
- let e = match s
- .evaluate_snippet_raw(PathBuf::new().into(), "assert 1 == 2: 'fail'; null".into())
- {
+ let e = match s.evaluate_snippet("snip".to_owned(), "assert 1 == 2: 'fail'; null".into()) {
Ok(_) => throw_runtime!("assertion should fail"),
Err(e) => e,
};
@@ -33,8 +29,7 @@
ensure!(e.starts_with("assert failed: fail\n"));
}
{
- let e = match s.evaluate_snippet_raw(PathBuf::new().into(), "std.assertEqual(1, 2)".into())
- {
+ let e = match s.evaluate_snippet("snip".to_owned(), "std.assertEqual(1, 2)".into()) {
Ok(_) => throw_runtime!("assertion should fail"),
Err(e) => e,
};
crates/jrsonnet-evaluator/tests/suite.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/suite.rs
+++ b/crates/jrsonnet-evaluator/tests/suite.rs
@@ -20,7 +20,7 @@
common::with_test(&s);
s.set_import_resolver(Box::new(FileImportResolver::default()));
- match s.evaluate_file_raw(file) {
+ match s.import(file.to_owned()) {
Ok(Val::Bool(true)) => {}
Ok(Val::Bool(false)) => panic!("test {} returned false", file.display()),
Ok(_) => panic!("test {} returned wrong type as result", file.display()),
crates/jrsonnet-evaluator/tests/typed_obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/typed_obj.rs
+++ b/crates/jrsonnet-evaluator/tests/typed_obj.rs
@@ -1,6 +1,6 @@
mod common;
-use std::{fmt::Debug, path::PathBuf};
+use std::fmt::Debug;
use jrsonnet_evaluator::{error::Result, typed::Typed, State};
@@ -25,11 +25,11 @@
let s = State::default();
s.with_stdlib();
let a = A::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{a: 1, b: 2}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{a: 1, b: 2}".into())?,
s.clone(),
)?;
ensure_eq!(a, A { a: 1, b: 2 });
- test_roundtrip(a.clone(), s.clone())?;
+ test_roundtrip(a, s)?;
Ok(())
}
@@ -45,7 +45,7 @@
let s = State::default();
s.with_stdlib();
let b = B::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{a: 1, c: 2}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{a: 1, c: 2}".into())?,
s.clone(),
)?;
ensure_eq!(b, B { a: 1, b: 2 });
@@ -53,7 +53,7 @@
&B::into_untyped(b.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"a": 1, "c": 2}"#,
);
- test_roundtrip(b.clone(), s.clone())?;
+ test_roundtrip(b, s)?;
Ok(())
}
@@ -77,8 +77,8 @@
let s = State::default();
s.with_stdlib();
let obj = Object::from_untyped(
- s.evaluate_snippet_raw(
- PathBuf::new().into(),
+ s.evaluate_snippet(
+ "snip".to_owned(),
"{apiVersion: 'ver', kind: 'kind', b: 2}".into(),
)?,
s.clone(),
@@ -97,7 +97,7 @@
&Object::into_untyped(obj.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"apiVersion": "ver", "b": 2, "kind": "kind"}"#,
);
- test_roundtrip(obj.clone(), s.clone())?;
+ test_roundtrip(obj, s)?;
Ok(())
}
@@ -112,7 +112,7 @@
let s = State::default();
s.with_stdlib();
let c = C::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{a: 1, b: 2}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{a: 1, b: 2}".into())?,
s.clone(),
)?;
ensure_eq!(c, C { a: Some(1), b: 2 });
@@ -120,7 +120,7 @@
&C::into_untyped(c.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"a": 1, "b": 2}"#,
);
- test_roundtrip(c.clone(), s.clone())?;
+ test_roundtrip(c, s)?;
Ok(())
}
@@ -129,7 +129,7 @@
let s = State::default();
s.with_stdlib();
let c = C::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{b: 2}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{b: 2}".into())?,
s.clone(),
)?;
ensure_eq!(c, C { a: None, b: 2 });
@@ -137,7 +137,7 @@
&C::into_untyped(c.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"b": 2}"#,
);
- test_roundtrip(c.clone(), s.clone())?;
+ test_roundtrip(c, s)?;
Ok(())
}
@@ -158,7 +158,7 @@
let s = State::default();
s.with_stdlib();
let d = D::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{b: 2, v:1}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{b: 2, v:1}".into())?,
s.clone(),
)?;
ensure_eq!(
@@ -172,7 +172,7 @@
&D::into_untyped(d.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"b": 2, "v": 1}"#,
);
- test_roundtrip(d.clone(), s.clone())?;
+ test_roundtrip(d, s)?;
Ok(())
}
@@ -181,7 +181,7 @@
let s = State::default();
s.with_stdlib();
let d = D::from_untyped(
- s.evaluate_snippet_raw(PathBuf::new().into(), "{b: 2, v: '1'}".into())?,
+ s.evaluate_snippet("snip".to_owned(), "{b: 2, v: '1'}".into())?,
s.clone(),
)?;
ensure_eq!(d, D { e: None, b: 2 });
@@ -189,6 +189,6 @@
&D::into_untyped(d.clone(), s.clone())?.to_string(s.clone())? as &str,
r#"{"b": 2}"#,
);
- test_roundtrip(d.clone(), s.clone())?;
+ test_roundtrip(d, s)?;
Ok(())
}
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -127,6 +127,11 @@
unsafe { Inner::assume_utf8(&self.0) };
IStr(self.0.clone())
}
+
+ #[must_use]
+ pub fn as_slice(&self) -> &[u8] {
+ self.0.as_slice()
+ }
}
impl Deref for IBytes {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -362,7 +362,7 @@
#[cfg(test)]
pub mod tests {
- use std::path::PathBuf;
+ use std::borrow::Cow;
use BinaryOpType::*;
@@ -374,7 +374,7 @@
parse(
$s,
&ParserSettings {
- file_name: Source::new(PathBuf::from("test.jsonnet")).unwrap(),
+ file_name: Source::new_virtual(Cow::Borrowed("<test>")),
},
)
.unwrap()
@@ -385,11 +385,7 @@
($expr:expr, $from:expr, $to:expr$(,)?) => {
LocExpr(
std::rc::Rc::new($expr),
- ExprLocation(
- Source::new(PathBuf::from("test.jsonnet")).unwrap(),
- $from,
- $to,
- ),
+ ExprLocation(Source::new_virtual(Cow::Borrowed("<test>")), $from, $to),
)
};
}
@@ -727,12 +723,10 @@
fn add_location_info_to_all_sub_expressions() {
use Expr::*;
- let file_name = Source::new(PathBuf::from("test.jsonnet")).unwrap();
+ let file_name = Source::new_virtual(Cow::Borrowed("<test>"));
let expr = parse(
"{} { local x = 1, x: x } + {}",
- &ParserSettings {
- file_name: file_name.clone(),
- },
+ &ParserSettings { file_name },
)
.unwrap();
assert_eq!(