difftreelog
refactor keep source code alongside source path
in: master
18 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -16,6 +16,7 @@
error::{Error::*, Result},
throw, ImportResolver, State,
};
+use jrsonnet_parser::SourcePath;
pub type JsonnetImportCallback = unsafe extern "C" fn(
ctx: *mut c_void,
@@ -29,10 +30,10 @@
pub struct CallbackImportResolver {
cb: JsonnetImportCallback,
ctx: *mut c_void,
- out: RefCell<HashMap<PathBuf, Vec<u8>>>,
+ out: RefCell<HashMap<SourcePath, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let base = CString::new(from.to_str().unwrap()).unwrap().into_raw();
let rel = CString::new(path).unwrap().into_raw();
let found_here: *mut c_char = null_mut();
@@ -61,7 +62,7 @@
}
let found_here_raw = unsafe { CStr::from_ptr(found_here) };
- let found_here_buf = PathBuf::from(found_here_raw.to_str().unwrap());
+ let found_here_buf = SourcePath::Path(PathBuf::from(found_here_raw.to_str().unwrap()));
unsafe {
let _ = CString::from_raw(found_here);
}
@@ -74,7 +75,7 @@
Ok(found_here_buf)
}
- fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
+ fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
@@ -108,24 +109,28 @@
}
}
impl ImportResolver for NativeImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let mut new_path = from.to_owned();
new_path.push(path);
if new_path.exists() {
- Ok(new_path)
+ Ok(SourcePath::Path(new_path))
} else {
for library_path in self.library_paths.borrow().iter() {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
- return Ok(cloned);
+ return Ok(SourcePath::Path(cloned));
}
}
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()))?;
+ fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+ let path = match id {
+ SourcePath::Path(path) => path,
+ _ => unreachable!("NativeImportResolver::resolve_file may only return plain paths"),
+ };
+ let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = Vec::new();
file.read_to_end(&mut out)
.map_err(|e| ImportIo(e.to_string()))?;
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -10,9 +10,9 @@
use std::{
alloc::Layout,
+ env,
ffi::{CStr, CString},
os::raw::{c_char, c_double, c_int, c_uint},
- path::PathBuf,
};
use import::NativeImportResolver;
@@ -112,7 +112,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest(v))
{
@@ -141,10 +144,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest(v))
{
@@ -186,7 +186,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_multi(v))
{
@@ -213,10 +216,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_multi(v))
{
@@ -256,7 +256,10 @@
) -> *const c_char {
let filename = CStr::from_ptr(filename);
match vm
- .import(PathBuf::from(filename.to_str().unwrap()))
+ .import(
+ &env::current_dir().expect("cwd"),
+ filename.to_str().unwrap(),
+ )
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_stream(v))
{
@@ -283,10 +286,7 @@
let filename = CStr::from_ptr(filename);
let snippet = CStr::from_ptr(snippet);
match vm
- .evaluate_snippet(
- filename.to_str().unwrap().into(),
- snippet.to_str().unwrap().into(),
- )
+ .evaluate_snippet(filename.to_str().unwrap().into(), snippet.to_str().unwrap())
.and_then(|v| vm.with_tla(v))
.and_then(|v| vm.manifest_stream(v))
{
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -32,7 +32,7 @@
.as_any()
.downcast_ref::<jrsonnet_stdlib::ContextInitializer>()
.expect("only stdlib context initializer supported")
- .add_ext_code(name.to_str().unwrap(), value.to_str().unwrap().into())
+ .add_ext_code(name.to_str().unwrap(), value.to_str().unwrap())
.unwrap()
}
/// # Safety
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -133,14 +133,14 @@
let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
let val = if opts.input.exec {
- s.evaluate_snippet("<cmdline>".to_owned(), (&input as &str).into())?
+ s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?
} else if input == "-" {
let mut input = Vec::new();
std::io::stdin().read_to_end(&mut input)?;
- let input_str = std::str::from_utf8(&input)?.into();
+ let input_str = std::str::from_utf8(&input)?;
s.evaluate_snippet("<stdin>".to_owned(), input_str)?
} else {
- s.import(s.resolve_file(¤t_dir().expect("cwd"), &input)?)?
+ s.import(¤t_dir().expect("cwd"), &input)?
};
let val = s.with_tla(val)?;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -118,10 +118,10 @@
ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());
}
for ext in self.ext_code.iter() {
- ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
}
for ext in self.ext_code_file.iter() {
- ctx.add_ext_code(&ext.name as &str, (&ext.value as &str).into())?;
+ ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;
}
s.settings_mut().context_initializer = Box::new(ctx);
Ok(())
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,7 +2,7 @@
use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, UnaryOpType};
+use jrsonnet_parser::{BinaryOpType, ExprLocation, Source, SourcePath, UnaryOpType};
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -138,10 +138,10 @@
#[error("can't resolve {1} from {0}")]
ImportFileNotFound(PathBuf, String),
- #[error("resolved file not found: {0}")]
- ResolvedFileNotFound(PathBuf),
+ #[error("resolved file not found: {:?}", .0)]
+ ResolvedFileNotFound(SourcePath),
#[error("imported file is not valid utf-8: {0:?}")]
- ImportBadFileUtf8(PathBuf),
+ ImportBadFileUtf8(SourcePath),
#[error("import io error: {0}")]
ImportIo(String),
#[error("tried to import {1} from {0}, but imports is not supported")]
@@ -151,12 +151,11 @@
#[error(
"syntax error: expected {}, got {:?}",
.error.expected,
- .source_code.chars().nth(error.location.offset)
+ .path.code().chars().nth(error.location.offset)
.map_or_else(|| "EOF".into(), |c| c.to_string())
)]
ImportSyntaxError {
path: Source,
- source_code: IStr,
#[trace(skip)]
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
@@ -416,13 +416,13 @@
Literal(LiteralType::This) => {
Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
}
- Literal(LiteralType::Super) => {
- Val::Obj(ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+ Literal(LiteralType::Super) => Val::Obj(
+ ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
ctx.this()
.clone()
.expect("if super exists - then this should to"),
- ))
- }
+ ),
+ ),
Literal(LiteralType::Dollar) => {
Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
}
@@ -643,10 +643,10 @@
Import(_) => s.push(
CallLocation::new(loc),
|| format!("import {:?}", path.clone()),
- || s.import(resolved_path.clone()),
+ || s.import_resolved(resolved_path),
)?,
- ImportStr(_) => Val::Str(s.import_str(resolved_path)?),
- ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_bin(resolved_path)?)),
+ ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+ ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),
_ => unreachable!(),
}
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -6,6 +6,7 @@
};
use fs::File;
+use jrsonnet_parser::SourcePath;
use crate::{
error::{Error::*, Result},
@@ -17,9 +18,12 @@
/// 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: &str) -> Result<PathBuf>;
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath>;
- fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>>;
+ /// Load resolved file
+ /// This should only be called with value returned from `resolve_file`, this cannot be resolved using associated type,
+ /// as evaluator uses object instead of generic for [`ImportResolver`]
+ fn load_file_contents(&self, resolved: &SourcePath) -> Result<Vec<u8>>;
/// # Safety
///
@@ -32,11 +36,11 @@
/// Dummy resolver, can't resolve/load any file
pub struct DummyImportResolver;
impl ImportResolver for DummyImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
throw!(ImportNotSupported(from.into(), path.into()))
}
- fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
+ fn load_file_contents(&self, _resolved: &SourcePath) -> Result<Vec<u8>> {
panic!("dummy resolver can't load any file")
}
@@ -59,25 +63,35 @@
pub library_paths: Vec<PathBuf>,
}
impl ImportResolver for FileImportResolver {
- fn resolve_file(&self, from: &Path, path: &str) -> Result<PathBuf> {
+ fn resolve_file_relative(&self, from: &Path, path: &str) -> Result<SourcePath> {
let mut direct = from.to_path_buf();
direct.push(path);
if direct.exists() {
- Ok(direct.canonicalize().map_err(|e| ImportIo(e.to_string()))?)
+ Ok(SourcePath::Path(
+ 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.canonicalize().map_err(|e| ImportIo(e.to_string()))?);
+ return Ok(SourcePath::Path(
+ 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()))?;
+ fn load_file_contents(&self, id: &SourcePath) -> Result<Vec<u8>> {
+ let path = match id {
+ SourcePath::Path(path) => path,
+ _ => {
+ panic!("this resolver can only resolve to path")
+ }
+ };
+ let mut file = File::open(path).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = Vec::new();
file.read_to_end(&mut out)
.map_err(|e| ImportIo(e.to_string()))?;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -48,7 +48,7 @@
cell::{Ref, RefCell, RefMut},
collections::HashMap,
fmt::{self, Debug},
- path::{Path, PathBuf},
+ path::Path,
rc::Rc,
};
@@ -65,7 +65,7 @@
pub use jrsonnet_parser as parser;
use jrsonnet_parser::*;
pub use obj::*;
-use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};
+use trace::{CompactFormat, TraceFormat};
pub use val::{ManifestFormat, Thunk, Val};
pub trait Unbound: Trace {
@@ -170,10 +170,7 @@
breakpoints: Breakpoints,
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
- files: GcHashMap<PathBuf, FileData>,
- /// Contains tla arguments and others, which aren't needed to be obtained by name, however may be used for receiving source
- /// TODO: look into nix approach, storing source code in `Source` object
- volatile_files: GcHashMap<String, String>,
+ files: GcHashMap<SourcePath, FileData>,
}
struct FileData {
string: Option<IStr>,
@@ -249,7 +246,8 @@
pub struct State(Rc<EvaluationStateInternals>);
impl State {
- pub fn import_str(&self, path: PathBuf) -> Result<IStr> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -283,7 +281,8 @@
}
Ok(file.string.as_ref().expect("just set").clone())
}
- pub fn import_bin(&self, path: PathBuf) -> Result<IBytes> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -309,7 +308,8 @@
}
Ok(file.bytes.as_ref().expect("just set").clone())
}
- pub fn import(&self, path: PathBuf) -> Result<Val> {
+ /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise
+ pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {
let mut data = self.data_mut();
let mut file = data.files.raw_entry_mut().from_key(&path);
@@ -343,7 +343,8 @@
);
}
let code = file.string.as_ref().expect("just set");
- let file_name = Source::new(path.clone()).expect("resolver should return correct name");
+ let file_name =
+ Source::new(path.clone(), code.clone()).expect("resolver should return correct name");
if file.parsed.is_none() {
file.parsed = Some(
jrsonnet_parser::parse(
@@ -354,7 +355,6 @@
)
.map_err(|e| ImportSyntaxError {
path: file_name.clone(),
- source_code: code.clone(),
error: Box::new(e),
})?,
);
@@ -386,34 +386,11 @@
Ok(v)
}
Err(e) => Err(e),
- }
- }
-
- 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: 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: Source,
- line: usize,
- column: usize,
- ) -> Option<usize> {
- location_to_offset(
- &self.get_source(file).expect("file not found"),
- line,
- column,
- )
+ pub fn import(&self, from: &Path, path: &str) -> Result<Val> {
+ let resolved = self.resolve_file(from, path)?;
+ self.import_resolved(resolved)
}
/// Creates context with all passed global variables
@@ -554,7 +531,10 @@
|| {
func.evaluate(
self.clone(),
- self.create_default_context(Source::new_virtual(Cow::Borrowed("<tla>"))),
+ self.create_default_context(Source::new_virtual(
+ Cow::Borrowed("<tla>"),
+ IStr::empty(),
+ )),
CallLocation::native(),
&self.settings().tla_vars,
true,
@@ -568,9 +548,9 @@
/// Internals
impl State {
- fn data(&self) -> Ref<EvaluationData> {
- self.0.data.borrow()
- }
+ // fn data(&self) -> Ref<EvaluationData> {
+ // self.0.data.borrow()
+ // }
fn data_mut(&self) -> RefMut<EvaluationData> {
self.0.data.borrow_mut()
}
@@ -585,8 +565,9 @@
/// Raw methods evaluate passed values but don't perform TLA execution
impl State {
/// Parses and evaluates the given snippet
- pub fn evaluate_snippet(&self, name: String, code: String) -> Result<Val> {
- let source = Source::new_virtual(Cow::Owned(name.clone()));
+ pub fn evaluate_snippet(&self, name: String, code: impl Into<IStr>) -> Result<Val> {
+ let code = code.into();
+ let source = Source::new_virtual(Cow::Owned(name), code.clone());
let parsed = jrsonnet_parser::parse(
&code,
&ParserSettings {
@@ -595,10 +576,8 @@
)
.map_err(|e| ImportSyntaxError {
path: source.clone(),
- source_code: code.clone().into(),
error: Box::new(e),
})?;
- self.data_mut().volatile_files.insert(name, code);
evaluate(self.clone(), self.create_default_context(source), &parsed)
}
}
@@ -617,7 +596,7 @@
}
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 source = Source::new_virtual(Cow::Owned(source_name.clone()), code.into());
let parsed = jrsonnet_parser::parse(
code,
&ParserSettings {
@@ -626,22 +605,18 @@
)
.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: &str) -> Result<PathBuf> {
+ pub fn resolve_file(&self, from: &Path, path: &str) -> Result<SourcePath> {
self.settings()
.import_resolver
- .resolve_file(from, path.as_ref())
+ .resolve_file_relative(from, path.as_ref())
}
pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ /dev/null
@@ -1,124 +0,0 @@
-#[allow(clippy::module_name_repetitions)]
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct CodeLocation {
- pub offset: usize,
-
- pub line: usize,
- pub column: usize,
-
- pub line_start_offset: usize,
- pub line_end_offset: usize,
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
- let mut offset = 0;
- while line > 1 {
- let pos = file.find('\n')?;
- offset += pos + 1;
- file = &file[pos + 1..];
- line -= 1;
- }
- offset += column - 1;
- Some(offset)
-}
-
-#[allow(clippy::module_name_repetitions)]
-pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
- if offsets.is_empty() {
- return vec![];
- }
- let mut line = 1;
- let mut column = 1;
- let max_offset = *offsets.iter().max().expect("offsets is not empty");
-
- let mut offset_map = offsets
- .iter()
- .enumerate()
- .map(|(pos, offset)| (*offset, pos))
- .collect::<Vec<_>>();
- offset_map.sort_by_key(|v| v.0);
- offset_map.reverse();
-
- let mut out = vec![
- CodeLocation {
- offset: 0,
- column: 0,
- line: 0,
- line_start_offset: 0,
- line_end_offset: 0
- };
- offsets.len()
- ];
- let mut with_no_known_line_ending = vec![];
- let mut this_line_offset = 0;
- for (pos, ch) in file
- .chars()
- .enumerate()
- .chain(std::iter::once((file.len(), ' ')))
- {
- column += 1;
- match offset_map.last() {
- 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;
- out[out_idx].line = line;
- out[out_idx].column = column;
- out[out_idx].line_start_offset = this_line_offset;
- offset_map.pop();
- }
- _ => {}
- }
- if ch == '\n' {
- line += 1;
- column = 1;
-
- for idx in with_no_known_line_ending.drain(..) {
- out[idx].line_end_offset = pos;
- }
- this_line_offset = pos + 1;
-
- if pos == max_offset as usize + 1 {
- break;
- }
- }
- }
- let file_end = file.chars().count();
- for idx in with_no_known_line_ending {
- out[idx].line_end_offset = file_end;
- }
-
- out
-}
-
-#[cfg(test)]
-pub mod tests {
- use super::{offset_to_location, CodeLocation};
-
- #[test]
- fn test() {
- assert_eq!(
- offset_to_location(
- "hello world\n_______________________________________________________",
- &[0, 14]
- ),
- vec![
- CodeLocation {
- offset: 0,
- line: 1,
- column: 2,
- line_start_offset: 0,
- line_end_offset: 11,
- },
- CodeLocation {
- offset: 14,
- line: 2,
- column: 4,
- line_start_offset: 12,
- line_end_offset: 67
- }
- ]
- )
- }
-}
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,9 +1,6 @@
-mod location;
-
use std::path::{Path, PathBuf};
-use jrsonnet_parser::Source;
-pub use location::*;
+use jrsonnet_parser::{CodeLocation, Source};
use crate::{error::Error, LocError, State};
@@ -84,31 +81,27 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
- if let Error::ImportSyntaxError {
- path,
- source_code,
- error,
- } = error.error()
- {
+ if let Error::ImportSyntaxError { path, error } = error.error() {
use std::fmt::Write;
writeln!(out)?;
- let mut n = match path.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let mut n = match path.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => path.short_display().to_string(),
};
let mut offset = error.location.offset;
- let is_eof = if offset >= source_code.len() {
- offset = source_code.len().saturating_sub(1);
+ let is_eof = if offset >= path.code().len() {
+ offset = path.code().len().saturating_sub(1);
true
} else {
false
};
- let mut location = offset_to_location(source_code, &[offset as u32])
+ let mut location = path
+ .map_source_locations(&[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -129,13 +122,12 @@
use std::fmt::Write;
#[allow(clippy::option_if_let_else)]
if let Some(location) = location {
- let mut resolved_path = match location.0.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let mut resolved_path = match location.0.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => location.0.short_display().to_string(),
};
// TODO: Process all trace elements first
- let location =
- s.map_source_locations(location.0.clone(), &[location.1, location.2]);
+ let location = location.0.map_source_locations(&[location.1, location.2]);
write!(resolved_path, ":").unwrap();
print_code_location(&mut resolved_path, &location[0], &location[1]).unwrap();
write!(resolved_path, ":").unwrap();
@@ -176,7 +168,7 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
@@ -184,10 +176,10 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- 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(),
+ let start_end = source.0.map_source_locations(&[source.1, source.2]);
+ let resolved_path = match source.0.path() {
+ Some(r) => r.display().to_string(),
+ None => source.0.short_display().to_string(),
};
write!(
@@ -213,19 +205,15 @@
fn write_trace(
&self,
out: &mut dyn std::fmt::Write,
- s: &State,
+ _s: &State,
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
- if let Error::ImportSyntaxError {
- path,
- source_code,
- error,
- } = error.error()
- {
+ if let Error::ImportSyntaxError { path, error } = error.error() {
writeln!(out)?;
let offset = error.location.offset;
- let location = offset_to_location(source_code, &[offset as u32])
+ let location = path
+ .map_source_locations(&[offset as u32])
.into_iter()
.next()
.unwrap();
@@ -234,7 +222,7 @@
self.print_snippet(
out,
- source_code,
+ path.code(),
path,
&location,
&end_location,
@@ -246,10 +234,10 @@
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
- let start_end = s.map_source_locations(source.0.clone(), &[source.1, source.2]);
+ let start_end = source.0.map_source_locations(&[source.1, source.2]);
self.print_snippet(
out,
- &s.get_source(source.0.clone()).unwrap(),
+ &source.0.code(),
&source.0,
&start_end[0],
&start_end[1],
@@ -284,9 +272,9 @@
.take(end.line_end_offset - end.line_start_offset)
.collect();
- let origin = match origin.repr() {
- Ok(r) => self.resolver.resolve(r),
- Err(v) => v.to_string(),
+ let origin = match origin.path() {
+ Some(r) => self.resolver.resolve(r),
+ None => origin.short_display().to_string(),
};
let snippet = Snippet {
opt: FormatOptions {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -33,6 +33,10 @@
impl IStr {
#[must_use]
+ pub fn empty() -> Self {
+ "".into()
+ }
+ #[must_use]
pub fn as_str(&self) -> &str {
self as &str
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -7,9 +7,11 @@
pub use expr::*;
pub use jrsonnet_interner::IStr;
pub use peg;
+mod location;
mod source;
mod unescape;
-pub use source::Source;
+pub use location::CodeLocation;
+pub use source::{Source, SourcePath};
pub struct ParserSettings {
pub file_name: Source,
crates/jrsonnet-parser/src/location.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-parser/src/location.rs
@@ -0,0 +1,124 @@
+#[allow(clippy::module_name_repetitions)]
+#[derive(Clone, PartialEq, Eq, Debug)]
+pub struct CodeLocation {
+ pub offset: usize,
+
+ pub line: usize,
+ pub column: usize,
+
+ pub line_start_offset: usize,
+ pub line_end_offset: usize,
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
+ let mut offset = 0;
+ while line > 1 {
+ let pos = file.find('\n')?;
+ offset += pos + 1;
+ file = &file[pos + 1..];
+ line -= 1;
+ }
+ offset += column - 1;
+ Some(offset)
+}
+
+#[allow(clippy::module_name_repetitions)]
+pub fn offset_to_location(file: &str, offsets: &[u32]) -> Vec<CodeLocation> {
+ if offsets.is_empty() {
+ return vec![];
+ }
+ let mut line = 1;
+ let mut column = 1;
+ let max_offset = *offsets.iter().max().expect("offsets is not empty");
+
+ let mut offset_map = offsets
+ .iter()
+ .enumerate()
+ .map(|(pos, offset)| (*offset, pos))
+ .collect::<Vec<_>>();
+ offset_map.sort_by_key(|v| v.0);
+ offset_map.reverse();
+
+ let mut out = vec![
+ CodeLocation {
+ offset: 0,
+ column: 0,
+ line: 0,
+ line_start_offset: 0,
+ line_end_offset: 0
+ };
+ offsets.len()
+ ];
+ let mut with_no_known_line_ending = vec![];
+ let mut this_line_offset = 0;
+ for (pos, ch) in file
+ .chars()
+ .enumerate()
+ .chain(std::iter::once((file.len(), ' ')))
+ {
+ column += 1;
+ match offset_map.last() {
+ 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;
+ out[out_idx].line = line;
+ out[out_idx].column = column;
+ out[out_idx].line_start_offset = this_line_offset;
+ offset_map.pop();
+ }
+ _ => {}
+ }
+ if ch == '\n' {
+ line += 1;
+ column = 1;
+
+ for idx in with_no_known_line_ending.drain(..) {
+ out[idx].line_end_offset = pos;
+ }
+ this_line_offset = pos + 1;
+
+ if pos == max_offset as usize + 1 {
+ break;
+ }
+ }
+ }
+ let file_end = file.chars().count();
+ for idx in with_no_known_line_ending {
+ out[idx].line_end_offset = file_end;
+ }
+
+ out
+}
+
+#[cfg(test)]
+pub mod tests {
+ use super::{offset_to_location, CodeLocation};
+
+ #[test]
+ fn test() {
+ assert_eq!(
+ offset_to_location(
+ "hello world\n_______________________________________________________",
+ &[0, 14]
+ ),
+ vec![
+ CodeLocation {
+ offset: 0,
+ line: 1,
+ column: 2,
+ line_start_offset: 0,
+ line_end_offset: 11,
+ },
+ CodeLocation {
+ offset: 14,
+ line: 2,
+ column: 4,
+ line_start_offset: 12,
+ line_end_offset: 67
+ }
+ ]
+ )
+ }
+}
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -6,21 +6,42 @@
};
use jrsonnet_gcmodule::{Trace, Tracer};
+use jrsonnet_interner::IStr;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
+use crate::location::{location_to_offset, offset_to_location, CodeLocation};
+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(PartialEq, Eq, Debug, Hash)]
-enum Inner {
- Real(PathBuf),
+#[derive(PartialEq, Eq, Debug, Hash, Clone)]
+pub enum SourcePath {
+ /// This file is located on disk
+ Path(PathBuf),
+ /// This file is located somewhere else (I.e http), but it can refer to relative paths, and is egilible for caching
+ Custom(String),
+ /// This file is only located in memory, and can't be cached
Virtual(Cow<'static, str>),
}
+impl Trace for SourcePath {
+ fn trace(&self, _tracer: &mut Tracer) {}
+ fn is_type_tracked() -> bool {
+ false
+ }
+}
+
+impl SourcePath {
+ /// Should import resolver be able to read file by this path?
+ pub fn can_load(&self) -> bool {
+ matches!(self, Self::Path(_) | Self::Custom(_))
+ }
+}
+
/// Either real file, or virtual
/// Hash of FileName always have same value as raw Path, to make it possible to use with raw_entry_mut
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct Source(Rc<Inner>);
+pub struct Source(Rc<(SourcePath, IStr)>);
static_assertions::assert_eq_size!(Source, *const ());
impl Trace for Source {
@@ -33,55 +54,63 @@
impl Source {
/// Fails when path contains inner /../ or /./ references, or not absolute
- pub fn new(path: PathBuf) -> Option<Self> {
- if !path.is_absolute()
- || path
- .components()
- .any(|c| matches!(c, Component::CurDir | Component::ParentDir))
- {
- return None;
+ pub fn new(path: SourcePath, code: IStr) -> Option<Self> {
+ if let SourcePath::Path(path) = &path {
+ if !path.is_absolute()
+ || path
+ .components()
+ .any(|c| matches!(c, Component::CurDir | Component::ParentDir))
+ {
+ return None;
+ }
}
- Some(Self(Rc::new(Inner::Real(path))))
+ Some(Self(Rc::new((path, code))))
}
- pub fn new_virtual(n: Cow<'static, str>) -> Self {
- Self(Rc::new(Inner::Virtual(n)))
+ pub fn new_virtual(n: Cow<'static, str>, code: IStr) -> Self {
+ Self(Rc::new((SourcePath::Virtual(n), code)))
}
pub fn short_display(&self) -> ShortDisplay {
ShortDisplay(self.clone())
}
- /// Returns None if file is virtual
+ /// Returns Some if this file is loaded from FS
pub fn path(&self) -> Option<&Path> {
- match self.inner() {
- Inner::Real(r) => Some(r),
- Inner::Virtual(_) => None,
+ match self.source_path() {
+ SourcePath::Path(r) => Some(r),
+ SourcePath::Custom(_) => None,
+ SourcePath::Virtual(_) => None,
}
}
- pub fn repr(&self) -> Result<&Path, &str> {
- match self.inner() {
- Inner::Real(r) => Ok(r),
- Inner::Virtual(v) => Err(v.as_ref()),
- }
+ pub fn code(&self) -> &str {
+ &self.0 .1
+ }
+
+ pub fn source_path(&self) -> &SourcePath {
+ &self.0 .0 as &SourcePath
}
- fn inner(&self) -> &Inner {
- &self.0 as &Inner
+ pub fn map_source_locations(&self, locs: &[u32]) -> Vec<CodeLocation> {
+ offset_to_location(&self.0 .1, locs)
+ }
+ pub fn map_from_source_location(&self, line: usize, column: usize) -> Option<usize> {
+ location_to_offset(&self.0 .1, line, column)
}
}
pub struct ShortDisplay(Source);
impl fmt::Display for ShortDisplay {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match &self.0 .0 as &Inner {
- Inner::Real(r) => {
+ match &self.0 .0 .0 as &SourcePath {
+ SourcePath::Path(r) => {
write!(
f,
"{}",
r.file_name().expect("path is valid").to_string_lossy()
)
}
- Inner::Virtual(n) => write!(f, "{}", n),
+ SourcePath::Custom(r) => write!(f, "{}", r),
+ SourcePath::Virtual(n) => write!(f, "{}", n),
}
}
}
crates/jrsonnet-stdlib/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/build.rs
+++ b/crates/jrsonnet-stdlib/build.rs
@@ -7,7 +7,10 @@
let parsed = parse(
include_str!("./src/std.jsonnet"),
&ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+ file_name: Source::new_virtual(
+ Cow::Borrowed("<std>"),
+ include_str!("./src/std.jsonnet").into(),
+ ),
},
)
.expect("parse");
crates/jrsonnet-stdlib/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/expr.rs
+++ b/crates/jrsonnet-stdlib/src/expr.rs
@@ -15,7 +15,7 @@
jrsonnet_parser::parse(
STDLIB_STR,
&ParserSettings {
- file_name: Source::new_virtual(Cow::Borrowed("<std>")),
+ file_name: Source::new_virtual(Cow::Borrowed("<std>"), STDLIB_STR.into()),
},
)
.unwrap()
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth1use std::{2 borrow::Cow,3 cell::{Ref, RefCell, RefMut},4 collections::HashMap,5 rc::Rc,6};78use jrsonnet_evaluator::{9 error::{Error::*, Result},10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11 gc::TraceBox,12 tb,13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::ArrValue,15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44 let mut builder = ObjValueBuilder::new();4546 let expr = expr::stdlib_expr();47 let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48 .expect("stdlib.jsonnet should have no errors")49 .as_obj()50 .expect("stdlib.jsonnet should evaluate to object");5152 builder.with_super(eval);5354 for (name, builtin) in [55 ("length".into(), builtin_length::INST),56 // Types57 ("type".into(), builtin_type::INST),58 ("isString".into(), builtin_is_string::INST),59 ("isNumber".into(), builtin_is_number::INST),60 ("isBoolean".into(), builtin_is_boolean::INST),61 ("isObject".into(), builtin_is_object::INST),62 ("isArray".into(), builtin_is_array::INST),63 ("isFunction".into(), builtin_is_function::INST),64 // Arrays65 ("makeArray".into(), builtin_make_array::INST),66 ("slice".into(), builtin_slice::INST),67 ("map".into(), builtin_map::INST),68 ("flatMap".into(), builtin_flatmap::INST),69 ("filter".into(), builtin_filter::INST),70 ("foldl".into(), builtin_foldl::INST),71 ("foldr".into(), builtin_foldr::INST),72 ("range".into(), builtin_range::INST),73 ("join".into(), builtin_join::INST),74 ("reverse".into(), builtin_reverse::INST),75 ("any".into(), builtin_any::INST),76 ("all".into(), builtin_all::INST),77 ("member".into(), builtin_member::INST),78 ("count".into(), builtin_count::INST),79 // Math80 ("modulo".into(), builtin_modulo::INST),81 ("floor".into(), builtin_floor::INST),82 ("ceil".into(), builtin_ceil::INST),83 ("log".into(), builtin_log::INST),84 ("pow".into(), builtin_pow::INST),85 ("sqrt".into(), builtin_sqrt::INST),86 ("sin".into(), builtin_sin::INST),87 ("cos".into(), builtin_cos::INST),88 ("tan".into(), builtin_tan::INST),89 ("asin".into(), builtin_asin::INST),90 ("acos".into(), builtin_acos::INST),91 ("atan".into(), builtin_atan::INST),92 ("exp".into(), builtin_exp::INST),93 ("mantissa".into(), builtin_mantissa::INST),94 ("exponent".into(), builtin_exponent::INST),95 // Operator96 ("mod".into(), builtin_mod::INST),97 ("primitiveEquals".into(), builtin_primitive_equals::INST),98 ("equals".into(), builtin_equals::INST),99 ("format".into(), builtin_format::INST),100 // Sort101 ("sort".into(), builtin_sort::INST),102 // Hash103 ("md5".into(), builtin_md5::INST),104 // Encoding105 ("encodeUTF8".into(), builtin_encode_utf8::INST),106 ("decodeUTF8".into(), builtin_decode_utf8::INST),107 ("base64".into(), builtin_base64::INST),108 ("base64Decode".into(), builtin_base64_decode::INST),109 (110 "base64DecodeBytes".into(),111 builtin_base64_decode_bytes::INST,112 ),113 // Objects114 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),115 ("objectHasEx".into(), builtin_object_has_ex::INST),116 // Manifest117 ("escapeStringJson".into(), builtin_escape_string_json::INST),118 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120 // Parsing121 ("parseJson".into(), builtin_parse_json::INST),122 ("parseYaml".into(), builtin_parse_yaml::INST),123 // Misc124 ("codepoint".into(), builtin_codepoint::INST),125 ("substr".into(), builtin_substr::INST),126 ("char".into(), builtin_char::INST),127 ("strReplace".into(), builtin_str_replace::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),131 ]132 .iter()133 .cloned()134 {135 builder136 .member(name)137 .hide()138 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))139 .expect("no conflict");140 }141142 builder143 .member("extVar".into())144 .hide()145 .value(146 s.clone(),147 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {148 settings: settings.clone()149 })))),150 )151 .expect("no conflict");152 builder153 .member("native".into())154 .hide()155 .value(156 s.clone(),157 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {158 settings: settings.clone()159 })))),160 )161 .expect("no conflict");162 builder163 .member("trace".into())164 .hide()165 .value(166 s.clone(),167 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),168 )169 .expect("no conflict");170171 builder172 .member("id".into())173 .hide()174 .value(s, Val::Func(FuncVal::Id))175 .expect("no conflict");176177 builder.build()178}179180pub trait TracePrinter {181 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);182}183184pub struct StdTracePrinter;185impl TracePrinter for StdTracePrinter {186 fn print_trace(&self, s: State, loc: CallLocation, value: IStr) {187 eprint!("TRACE:");188 if let Some(loc) = loc.0 {189 let locs = s.map_source_locations(loc.0.clone(), &[loc.1]);190 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);191 }192 eprintln!(" {}", value);193 }194}195196pub struct Settings {197 /// Used for `std.extVar`198 pub ext_vars: HashMap<IStr, TlaArg>,199 /// Used for `std.native`200 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,201 /// Used for `std.trace`202 pub trace_printer: Box<dyn TracePrinter>,203}204205impl Default for Settings {206 fn default() -> Self {207 Self {208 ext_vars: Default::default(),209 ext_natives: Default::default(),210 trace_printer: Box::new(StdTracePrinter),211 }212 }213}214215pub fn extvar_source(name: &str) -> Source {216 let source_name = format!("<extvar:{}>", name);217 Source::new_virtual(Cow::Owned(source_name))218}219220pub struct ContextInitializer {221 // When we don't need to support legacy-this-file, we can reuse same context for all files222 #[cfg(not(feature = "legacy-this-file"))]223 context: Context,224 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it225 #[cfg(feature = "legacy-this-file")]226 stdlib_obj: ObjValue,227 settings: Rc<RefCell<Settings>>,228}229impl ContextInitializer {230 pub fn new(s: State) -> Self {231 let settings = Rc::new(RefCell::new(Settings::default()));232 Self {233 #[cfg(not(feature = "legacy-this-file"))]234 context: {235 let mut context = ContextBuilder::with_capacity(1);236 context.bind(237 "std".into(),238 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),239 );240 context.build()241 },242 #[cfg(feature = "legacy-this-file")]243 stdlib_obj: stdlib_uncached(s, settings.clone()),244 settings,245 }246 }247 pub fn settings(&self) -> Ref<Settings> {248 self.settings.borrow()249 }250 pub fn settings_mut(&self) -> RefMut<Settings> {251 self.settings.borrow_mut()252 }253 pub fn add_ext_var(&self, name: IStr, value: Val) {254 self.settings_mut()255 .ext_vars256 .insert(name, TlaArg::Val(value));257 }258 pub fn add_ext_str(&self, name: IStr, value: IStr) {259 self.settings_mut()260 .ext_vars261 .insert(name, TlaArg::String(value));262 }263 pub fn add_ext_code(&self, name: &str, code: String) -> Result<()> {264 let source = extvar_source(name);265 let parsed = jrsonnet_parser::parse(266 &code,267 &jrsonnet_parser::ParserSettings {268 file_name: source.clone(),269 },270 )271 .map_err(|e| ImportSyntaxError {272 path: source,273 source_code: code.clone().into(),274 error: Box::new(e),275 })?;276 // self.data_mut().volatile_files.insert(source_name, code);277 self.settings_mut()278 .ext_vars279 .insert(name.into(), TlaArg::Code(parsed));280 Ok(())281 }282 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {283 self.settings_mut().ext_natives.insert(name, cb);284 }285}286impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {287 #[cfg(not(feature = "legacy-this-file"))]288 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {289 self.context.clone()290 }291 #[cfg(feature = "legacy-this-file")]292 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {293 let mut builder = ObjValueBuilder::new();294 builder.with_super(self.stdlib_obj.clone());295 builder296 .member("thisFile".into())297 .hide()298 .value(299 s,300 Val::Str(match source.repr() {301 Ok(p) => p.display().to_string().into(),302 // Virtual files end up as empty strings in std.thisFile303 Err(_e) => "".into(),304 }),305 )306 .expect("this object builder is empty");307 let stdlib_with_this_file = builder.build();308309 let mut context = ContextBuilder::with_capacity(1);310 context.bind(311 "std".into(),312 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),313 );314 context.build()315 }316 unsafe fn as_any(&self) -> &dyn std::any::Any {317 self318 }319}320321#[builtin]322fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {323 use Either4::*;324 Ok(match x {325 A(x) => x.chars().count(),326 B(x) => x.len(),327 C(x) => x.len(),328 D(f) => f.params_len(),329 })330}331332#[builtin]333const fn builtin_codepoint(str: char) -> Result<u32> {334 Ok(str as u32)335}336337#[builtin]338fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {339 Ok(str.chars().skip(from as usize).take(len as usize).collect())340}341342#[builtin(fields(343 settings: Rc<RefCell<Settings>>,344))]345fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {346 let ctx = s.create_default_context(extvar_source(&x));347 Ok(Any(this348 .settings349 .borrow()350 .ext_vars351 .get(&x)352 .cloned()353 .ok_or(UndefinedExternalVariable(x))?354 .evaluate_arg(s.clone(), ctx, true)?355 .evaluate(s)?))356}357358#[builtin(fields(359 settings: Rc<RefCell<Settings>>,360))]361fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {362 Ok(Any(this363 .settings364 .borrow()365 .ext_natives366 .get(&name)367 .cloned()368 .map_or(Val::Null, |v| {369 Val::Func(FuncVal::Builtin(v.clone()))370 })))371}372373#[builtin]374fn builtin_char(n: u32) -> Result<char> {375 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)376}377378#[builtin(fields(379 settings: Rc<RefCell<Settings>>,380))]381fn builtin_trace(382 this: &builtin_trace,383 s: State,384 loc: CallLocation,385 str: IStr,386 rest: Thunk<Val>,387) -> Result<Any> {388 this.settings389 .borrow()390 .trace_printer391 .print_trace(s.clone(), loc, str);392 Ok(Any(rest.evaluate(s)?))393}394395#[builtin]396fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {397 Ok(str.replace(&from as &str, &to as &str))398}399400#[builtin]401fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {402 use Either2::*;403 Ok(VecVal(Cc::new(match maxsplits {404 A(n) => str405 .splitn(n + 1, &c as &str)406 .map(|s| Val::Str(s.into()))407 .collect(),408 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),409 })))410}411412#[builtin]413fn builtin_ascii_upper(str: IStr) -> Result<String> {414 Ok(str.to_ascii_uppercase())415}416417#[builtin]418fn builtin_ascii_lower(str: IStr) -> Result<String> {419 Ok(str.to_ascii_lowercase())420}1use std::{2 borrow::Cow,3 cell::{Ref, RefCell, RefMut},4 collections::HashMap,5 rc::Rc,6};78use jrsonnet_evaluator::{9 error::{Error::*, Result},10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},11 gc::TraceBox,12 tb,13 typed::{Any, Either, Either2, Either4, VecVal, M1},14 val::ArrValue,15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,16};17use jrsonnet_gcmodule::Cc;18use jrsonnet_macros::builtin;19use jrsonnet_parser::Source;2021mod expr;22mod types;23pub use types::*;24mod arrays;25pub use arrays::*;26mod math;27pub use math::*;28mod operator;29pub use operator::*;30mod sort;31pub use sort::*;32mod hash;33pub use hash::*;34mod encoding;35pub use encoding::*;36mod objects;37pub use objects::*;38mod manifest;39pub use manifest::*;40mod parse;41pub use parse::*;4243pub fn stdlib_uncached(s: State, settings: Rc<RefCell<Settings>>) -> ObjValue {44 let mut builder = ObjValueBuilder::new();4546 let expr = expr::stdlib_expr();47 let eval = jrsonnet_evaluator::evaluate(s.clone(), Context::default(), &expr)48 .expect("stdlib.jsonnet should have no errors")49 .as_obj()50 .expect("stdlib.jsonnet should evaluate to object");5152 builder.with_super(eval);5354 for (name, builtin) in [55 ("length".into(), builtin_length::INST),56 // Types57 ("type".into(), builtin_type::INST),58 ("isString".into(), builtin_is_string::INST),59 ("isNumber".into(), builtin_is_number::INST),60 ("isBoolean".into(), builtin_is_boolean::INST),61 ("isObject".into(), builtin_is_object::INST),62 ("isArray".into(), builtin_is_array::INST),63 ("isFunction".into(), builtin_is_function::INST),64 // Arrays65 ("makeArray".into(), builtin_make_array::INST),66 ("slice".into(), builtin_slice::INST),67 ("map".into(), builtin_map::INST),68 ("flatMap".into(), builtin_flatmap::INST),69 ("filter".into(), builtin_filter::INST),70 ("foldl".into(), builtin_foldl::INST),71 ("foldr".into(), builtin_foldr::INST),72 ("range".into(), builtin_range::INST),73 ("join".into(), builtin_join::INST),74 ("reverse".into(), builtin_reverse::INST),75 ("any".into(), builtin_any::INST),76 ("all".into(), builtin_all::INST),77 ("member".into(), builtin_member::INST),78 ("count".into(), builtin_count::INST),79 // Math80 ("modulo".into(), builtin_modulo::INST),81 ("floor".into(), builtin_floor::INST),82 ("ceil".into(), builtin_ceil::INST),83 ("log".into(), builtin_log::INST),84 ("pow".into(), builtin_pow::INST),85 ("sqrt".into(), builtin_sqrt::INST),86 ("sin".into(), builtin_sin::INST),87 ("cos".into(), builtin_cos::INST),88 ("tan".into(), builtin_tan::INST),89 ("asin".into(), builtin_asin::INST),90 ("acos".into(), builtin_acos::INST),91 ("atan".into(), builtin_atan::INST),92 ("exp".into(), builtin_exp::INST),93 ("mantissa".into(), builtin_mantissa::INST),94 ("exponent".into(), builtin_exponent::INST),95 // Operator96 ("mod".into(), builtin_mod::INST),97 ("primitiveEquals".into(), builtin_primitive_equals::INST),98 ("equals".into(), builtin_equals::INST),99 ("format".into(), builtin_format::INST),100 // Sort101 ("sort".into(), builtin_sort::INST),102 // Hash103 ("md5".into(), builtin_md5::INST),104 // Encoding105 ("encodeUTF8".into(), builtin_encode_utf8::INST),106 ("decodeUTF8".into(), builtin_decode_utf8::INST),107 ("base64".into(), builtin_base64::INST),108 ("base64Decode".into(), builtin_base64_decode::INST),109 (110 "base64DecodeBytes".into(),111 builtin_base64_decode_bytes::INST,112 ),113 // Objects114 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),115 ("objectHasEx".into(), builtin_object_has_ex::INST),116 // Manifest117 ("escapeStringJson".into(), builtin_escape_string_json::INST),118 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),119 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),120 // Parsing121 ("parseJson".into(), builtin_parse_json::INST),122 ("parseYaml".into(), builtin_parse_yaml::INST),123 // Misc124 ("codepoint".into(), builtin_codepoint::INST),125 ("substr".into(), builtin_substr::INST),126 ("char".into(), builtin_char::INST),127 ("strReplace".into(), builtin_str_replace::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),131 ]132 .iter()133 .cloned()134 {135 builder136 .member(name)137 .hide()138 .value(s.clone(), Val::Func(FuncVal::StaticBuiltin(builtin)))139 .expect("no conflict");140 }141142 builder143 .member("extVar".into())144 .hide()145 .value(146 s.clone(),147 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_ext_var {148 settings: settings.clone()149 })))),150 )151 .expect("no conflict");152 builder153 .member("native".into())154 .hide()155 .value(156 s.clone(),157 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_native {158 settings: settings.clone()159 })))),160 )161 .expect("no conflict");162 builder163 .member("trace".into())164 .hide()165 .value(166 s.clone(),167 Val::Func(FuncVal::Builtin(Cc::new(tb!(builtin_trace { settings })))),168 )169 .expect("no conflict");170171 builder172 .member("id".into())173 .hide()174 .value(s, Val::Func(FuncVal::Id))175 .expect("no conflict");176177 builder.build()178}179180pub trait TracePrinter {181 fn print_trace(&self, s: State, loc: CallLocation, value: IStr);182}183184pub struct StdTracePrinter;185impl TracePrinter for StdTracePrinter {186 fn print_trace(&self, _s: State, loc: CallLocation, value: IStr) {187 eprint!("TRACE:");188 if let Some(loc) = loc.0 {189 let locs = loc.0.map_source_locations(&[loc.1]);190 eprint!(" {}:{}", loc.0.short_display(), locs[0].line);191 }192 eprintln!(" {}", value);193 }194}195196pub struct Settings {197 /// Used for `std.extVar`198 pub ext_vars: HashMap<IStr, TlaArg>,199 /// Used for `std.native`200 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,201 /// Used for `std.trace`202 pub trace_printer: Box<dyn TracePrinter>,203}204205impl Default for Settings {206 fn default() -> Self {207 Self {208 ext_vars: Default::default(),209 ext_natives: Default::default(),210 trace_printer: Box::new(StdTracePrinter),211 }212 }213}214215pub fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {216 let source_name = format!("<extvar:{}>", name);217 Source::new_virtual(Cow::Owned(source_name), code.into())218}219220pub struct ContextInitializer {221 // When we don't need to support legacy-this-file, we can reuse same context for all files222 #[cfg(not(feature = "legacy-this-file"))]223 context: Context,224 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it225 #[cfg(feature = "legacy-this-file")]226 stdlib_obj: ObjValue,227 settings: Rc<RefCell<Settings>>,228}229impl ContextInitializer {230 pub fn new(s: State) -> Self {231 let settings = Rc::new(RefCell::new(Settings::default()));232 Self {233 #[cfg(not(feature = "legacy-this-file"))]234 context: {235 let mut context = ContextBuilder::with_capacity(1);236 context.bind(237 "std".into(),238 Thunk::evaluated(Val::Obj(stdlib_uncached(s, settings.clone()))),239 );240 context.build()241 },242 #[cfg(feature = "legacy-this-file")]243 stdlib_obj: stdlib_uncached(s, settings.clone()),244 settings,245 }246 }247 pub fn settings(&self) -> Ref<Settings> {248 self.settings.borrow()249 }250 pub fn settings_mut(&self) -> RefMut<Settings> {251 self.settings.borrow_mut()252 }253 pub fn add_ext_var(&self, name: IStr, value: Val) {254 self.settings_mut()255 .ext_vars256 .insert(name, TlaArg::Val(value));257 }258 pub fn add_ext_str(&self, name: IStr, value: IStr) {259 self.settings_mut()260 .ext_vars261 .insert(name, TlaArg::String(value));262 }263 pub fn add_ext_code(&self, name: &str, code: impl Into<IStr>) -> Result<()> {264 let code = code.into();265 let source = extvar_source(name, code.clone());266 let parsed = jrsonnet_parser::parse(267 &code,268 &jrsonnet_parser::ParserSettings {269 file_name: source.clone(),270 },271 )272 .map_err(|e| ImportSyntaxError {273 path: source,274 error: Box::new(e),275 })?;276 // self.data_mut().volatile_files.insert(source_name, code);277 self.settings_mut()278 .ext_vars279 .insert(name.into(), TlaArg::Code(parsed));280 Ok(())281 }282 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {283 self.settings_mut().ext_natives.insert(name, cb);284 }285}286impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {287 #[cfg(not(feature = "legacy-this-file"))]288 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {289 self.context.clone()290 }291 #[cfg(feature = "legacy-this-file")]292 fn initialize(&self, s: State, source: Source) -> jrsonnet_evaluator::Context {293 let mut builder = ObjValueBuilder::new();294 builder.with_super(self.stdlib_obj.clone());295 builder296 .member("thisFile".into())297 .hide()298 .value(299 s,300 Val::Str(301 source302 .path()303 .map(|p| p.display().to_string())304 .unwrap_or_else(String::new)305 .into(),306 ),307 )308 .expect("this object builder is empty");309 let stdlib_with_this_file = builder.build();310311 let mut context = ContextBuilder::with_capacity(1);312 context.bind(313 "std".into(),314 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),315 );316 context.build()317 }318 unsafe fn as_any(&self) -> &dyn std::any::Any {319 self320 }321}322323#[builtin]324fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {325 use Either4::*;326 Ok(match x {327 A(x) => x.chars().count(),328 B(x) => x.len(),329 C(x) => x.len(),330 D(f) => f.params_len(),331 })332}333334#[builtin]335const fn builtin_codepoint(str: char) -> Result<u32> {336 Ok(str as u32)337}338339#[builtin]340fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {341 Ok(str.chars().skip(from as usize).take(len as usize).collect())342}343344#[builtin(fields(345 settings: Rc<RefCell<Settings>>,346))]347fn builtin_ext_var(this: &builtin_ext_var, s: State, x: IStr) -> Result<Any> {348 let ctx = s.create_default_context(extvar_source(&x, ""));349 Ok(Any(this350 .settings351 .borrow()352 .ext_vars353 .get(&x)354 .cloned()355 .ok_or(UndefinedExternalVariable(x))?356 .evaluate_arg(s.clone(), ctx, true)?357 .evaluate(s)?))358}359360#[builtin(fields(361 settings: Rc<RefCell<Settings>>,362))]363fn builtin_native(this: &builtin_native, name: IStr) -> Result<Any> {364 Ok(Any(this365 .settings366 .borrow()367 .ext_natives368 .get(&name)369 .cloned()370 .map_or(Val::Null, |v| {371 Val::Func(FuncVal::Builtin(v.clone()))372 })))373}374375#[builtin]376fn builtin_char(n: u32) -> Result<char> {377 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)378}379380#[builtin(fields(381 settings: Rc<RefCell<Settings>>,382))]383fn builtin_trace(384 this: &builtin_trace,385 s: State,386 loc: CallLocation,387 str: IStr,388 rest: Thunk<Val>,389) -> Result<Any> {390 this.settings391 .borrow()392 .trace_printer393 .print_trace(s.clone(), loc, str);394 Ok(Any(rest.evaluate(s)?))395}396397#[builtin]398fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {399 Ok(str.replace(&from as &str, &to as &str))400}401402#[builtin]403fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {404 use Either2::*;405 Ok(VecVal(Cc::new(match maxsplits {406 A(n) => str407 .splitn(n + 1, &c as &str)408 .map(|s| Val::Str(s.into()))409 .collect(),410 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),411 })))412}413414#[builtin]415fn builtin_ascii_upper(str: IStr) -> Result<String> {416 Ok(str.to_ascii_uppercase())417}418419#[builtin]420fn builtin_ascii_lower(str: IStr) -> Result<String> {421 Ok(str.to_ascii_lowercase())422}