difftreelog
feat importbin
in: master
9 files changed
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -2,7 +2,7 @@
use jrsonnet_evaluator::{
error::{Error::*, Result},
- throw, EvaluationState, IStr, ImportResolver,
+ throw, EvaluationState, ImportResolver,
};
use std::{
any::Any,
@@ -29,8 +29,7 @@
pub struct CallbackImportResolver {
cb: JsonnetImportCallback,
ctx: *mut c_void,
-
- out: RefCell<HashMap<PathBuf, IStr>>,
+ out: RefCell<HashMap<PathBuf, Vec<u8>>>,
}
impl ImportResolver for CallbackImportResolver {
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
@@ -75,9 +74,10 @@
Ok(found_here_buf.into())
}
- fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
+ fn load_file_contents(&self, resolved: &Path) -> Result<Vec<u8>> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
+
unsafe fn as_any(&self) -> &dyn Any {
self
}
@@ -124,12 +124,12 @@
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ 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 = String::new();
- file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
- Ok(out.into())
+ let mut out = Vec::new();
+ file.read_to_end(&mut out)
+ .map_err(|e| ImportIo(e.to_string()))?;
+ Ok(out)
}
unsafe fn as_any(&self) -> &dyn Any {
self
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -82,6 +82,8 @@
ResolvedFileNotFound(PathBuf),
#[error("imported file is not valid utf-8: {0:?}")]
ImportBadFileUtf8(PathBuf),
+ #[error("import io error: {0}")]
+ ImportIo(String),
#[error("tried to import {1} from {0}, but imports is not supported")]
ImportNotSupported(PathBuf, PathBuf),
#[error(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -700,5 +700,12 @@
import_location.pop();
Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)
}
+ ImportBin(path) => {
+ let tmp = loc.clone().0;
+ let mut import_location = tmp.to_path_buf();
+ import_location.pop();
+ let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;
+ Val::Arr(ArrValue::Bytes(bytes))
+ }
})
}
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -5,14 +5,12 @@
use fs::File;
use jrsonnet_interner::IStr;
use std::fs;
-use std::io::Read;
use std::{
any::Any,
- cell::RefCell,
- collections::HashMap,
path::{Path, PathBuf},
rc::Rc,
};
+use std::{convert::TryFrom, io::Read};
/// Implements file resolution logic for `import` and `importStr`
pub trait ImportResolver {
@@ -21,9 +19,19 @@
/// where `${vendor}` is a library path.
fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>>;
+ 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_contents(&self, resolved: &Path) -> Result<IStr>;
+ 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.
@@ -39,8 +47,7 @@
throw!(ImportNotSupported(from.into(), path.into()))
}
- fn load_file_contents(&self, _resolved: &Path) -> Result<IStr> {
- // Can be only caused by library direct consumer, not by supplied jsonnet
+ fn load_file_contents(&self, _resolved: &Path) -> Result<Vec<u8>> {
panic!("dummy resolver can't load any file")
}
@@ -79,41 +86,12 @@
throw!(ImportFileNotFound(from.to_owned(), path.to_owned()))
}
}
- fn load_file_contents(&self, id: &Path) -> Result<IStr> {
+ 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 = String::new();
- file.read_to_string(&mut out)
- .map_err(|_e| ImportBadFileUtf8(id.to_owned()))?;
- Ok(out.into())
- }
- unsafe fn as_any(&self) -> &dyn Any {
- panic!("this resolver can't be used as any")
- }
-}
-
-type ResolutionData = (PathBuf, PathBuf);
-
-/// Caches results of the underlying resolver
-pub struct CachingImportResolver {
- resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<Path>>>>,
- loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
- inner: Box<dyn ImportResolver>,
-}
-impl ImportResolver for CachingImportResolver {
- fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {
- self.resolution_cache
- .borrow_mut()
- .entry((from.to_owned(), path.to_owned()))
- .or_insert_with(|| self.inner.resolve_file(from, path))
- .clone()
- }
-
- fn load_file_contents(&self, resolved: &Path) -> Result<IStr> {
- self.loading_cache
- .borrow_mut()
- .entry(resolved.to_owned())
- .or_insert_with(|| self.inner.load_file_contents(resolved))
- .clone()
+ let mut out = Vec::new();
+ file.read_to_end(&mut out)
+ .map_err(|e| ImportIo(e.to_string()))?;
+ Ok(out)
}
unsafe fn as_any(&self) -> &dyn Any {
panic!("this resolver can't be used as any")
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4 macro_expanded_macro_exports_accessed_by_absolute_paths,5 clippy::ptr_arg6)]78// For jrsonnet-macros9extern crate self as jrsonnet_evaluator;1011mod builtin;12mod ctx;13mod dynamic;14pub mod error;15mod evaluate;16mod function;17mod import;18mod integrations;19mod map;20pub mod native;21mod obj;22pub mod trace;23pub mod typed;24mod val;2526pub use ctx::*;27pub use dynamic::*;28use error::{Error::*, LocError, Result, StackTraceElement};29pub use evaluate::*;30pub use function::parse_function_call;31use function::TlaArg;32use gc::{GcHashMap, TraceBox};33use gcmodule::{Cc, Trace};34pub use import::*;35pub use jrsonnet_interner::IStr;36use jrsonnet_parser::*;37use native::NativeCallback;38pub use obj::*;39use std::{40 cell::{Ref, RefCell, RefMut},41 collections::HashMap,42 fmt::Debug,43 path::{Path, PathBuf},44 rc::Rc,45};46use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};47pub use val::*;48pub mod gc;4950pub trait Bindable: Trace + 'static {51 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;52}5354#[derive(Clone, Trace)]55pub enum LazyBinding {56 Bindable(Cc<TraceBox<dyn Bindable>>),57 Bound(LazyVal),58}5960impl Debug for LazyBinding {61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {62 write!(f, "LazyBinding")63 }64}65impl LazyBinding {66 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {67 match self {68 Self::Bindable(v) => v.bind(this, super_obj),69 Self::Bound(v) => Ok(v.clone()),70 }71 }72}7374pub struct EvaluationSettings {75 /// Limits recursion by limiting the number of stack frames76 pub max_stack: usize,77 /// Limits amount of stack trace items preserved78 pub max_trace: usize,79 /// Used for s`td.extVar`80 pub ext_vars: HashMap<IStr, Val>,81 /// Used for ext.native82 pub ext_natives: HashMap<IStr, Cc<NativeCallback>>,83 /// TLA vars84 pub tla_vars: HashMap<IStr, TlaArg>,85 /// Global variables are inserted in default context86 pub globals: HashMap<IStr, Val>,87 /// Used to resolve file locations/contents88 pub import_resolver: Box<dyn ImportResolver>,89 /// Used in manifestification functions90 pub manifest_format: ManifestFormat,91 /// Used for bindings92 pub trace_format: Box<dyn TraceFormat>,93}94impl Default for EvaluationSettings {95 fn default() -> Self {96 Self {97 max_stack: 200,98 max_trace: 20,99 globals: Default::default(),100 ext_vars: Default::default(),101 ext_natives: Default::default(),102 tla_vars: Default::default(),103 import_resolver: Box::new(DummyImportResolver),104 manifest_format: ManifestFormat::Json(4),105 trace_format: Box::new(CompactFormat {106 padding: 4,107 resolver: trace::PathResolver::Absolute,108 }),109 }110 }111}112113#[derive(Default)]114struct EvaluationData {115 /// Used for stack overflow detection, stacktrace is populated on unwind116 stack_depth: usize,117 /// Updated every time stack entry is popt118 stack_generation: usize,119120 breakpoints: Breakpoints,121 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces122 files: HashMap<Rc<Path>, FileData>,123 str_files: HashMap<Rc<Path>, IStr>,124}125126pub struct FileData {127 source_code: IStr,128 parsed: LocExpr,129 evaluated: Option<Val>,130}131132#[allow(clippy::type_complexity)]133pub struct Breakpoint {134 loc: ExprLocation,135 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,136}137#[derive(Default)]138struct Breakpoints(Vec<Rc<Breakpoint>>);139impl Breakpoints {140 fn insert(141 &self,142 stack_depth: usize,143 stack_generation: usize,144 loc: &ExprLocation,145 result: Result<Val>,146 ) -> Result<Val> {147 if self.0.is_empty() {148 return result;149 }150 for item in self.0.iter() {151 if item.loc.belongs_to(loc) {152 let mut collected = item.collected.borrow_mut();153 let (depth, vals) = collected.entry(stack_generation).or_default();154 if stack_depth > *depth {155 vals.clear();156 }157 vals.push(result.clone());158 }159 }160 result161 }162}163164#[derive(Default)]165pub struct EvaluationStateInternals {166 /// Internal state167 data: RefCell<EvaluationData>,168 /// Settings, safe to change at runtime169 settings: RefCell<EvaluationSettings>,170}171172thread_local! {173 /// Contains the state for a currently executed file.174 /// Global state is fine here.175 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)176}177pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {178 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))179}180pub(crate) fn push_frame<T>(181 e: Option<&ExprLocation>,182 frame_desc: impl FnOnce() -> String,183 f: impl FnOnce() -> Result<T>,184) -> Result<T> {185 with_state(|s| s.push(e, frame_desc, f))186}187188#[allow(dead_code)]189pub(crate) fn push_val_frame(190 e: &ExprLocation,191 frame_desc: impl FnOnce() -> String,192 f: impl FnOnce() -> Result<Val>,193) -> Result<Val> {194 with_state(|s| s.push_val(e, frame_desc, f))195}196#[allow(dead_code)]197pub(crate) fn push_description_frame<T>(198 frame_desc: impl FnOnce() -> String,199 f: impl FnOnce() -> Result<T>,200) -> Result<T> {201 with_state(|s| s.push_description(frame_desc, f))202}203204/// Maintains stack trace and import resolution205#[derive(Default, Clone)]206pub struct EvaluationState(Rc<EvaluationStateInternals>);207208impl EvaluationState {209 /// Parses and adds file as loaded210 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {211 let parsed = parse(212 &source_code,213 &ParserSettings {214 file_name: path.clone(),215 },216 )217 .map_err(|error| ImportSyntaxError {218 error: Box::new(error),219 path: path.to_owned(),220 source_code: source_code.clone(),221 })?;222 self.add_parsed_file(path, source_code, parsed.clone())?;223224 Ok(parsed)225 }226227 pub fn reset_evaluation_state(&self, name: &Path) {228 self.data_mut()229 .files230 .get_mut(name)231 .unwrap()232 .evaluated233 .take();234 }235236 /// Adds file by source code and parsed expr237 pub fn add_parsed_file(238 &self,239 name: Rc<Path>,240 source_code: IStr,241 parsed: LocExpr,242 ) -> Result<()> {243 self.data_mut().files.insert(244 name,245 FileData {246 source_code,247 parsed,248 evaluated: None,249 },250 );251252 Ok(())253 }254 pub fn get_source(&self, name: &Path) -> Option<IStr> {255 let ro_map = &self.data().files;256 ro_map.get(name).map(|value| value.source_code.clone())257 }258 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {259 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)260 }261 pub fn map_from_source_location(262 &self,263 file: &Path,264 line: usize,265 column: usize,266 ) -> Option<usize> {267 location_to_offset(&self.get_source(file).unwrap(), line, column)268 }269 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {270 let file_path = self.resolve_file(from, path)?;271 {272 let data = self.data();273 let files = &data.files;274 if files.contains_key(&file_path as &Path) {275 drop(data);276 return self.evaluate_loaded_file_raw(&file_path);277 }278 }279 let contents = self.load_file_contents(&file_path)?;280 self.add_file(file_path.clone(), contents)?;281 self.evaluate_loaded_file_raw(&file_path)282 }283 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {284 let path = self.resolve_file(from, path)?;285 if !self.data().str_files.contains_key(&path) {286 let file_str = self.load_file_contents(&path)?;287 self.data_mut().str_files.insert(path.clone(), file_str);288 }289 Ok(self.data().str_files.get(&path).cloned().unwrap())290 }291292 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {293 let expr: LocExpr = {294 let ro_map = &self.data().files;295 let value = ro_map296 .get(name)297 .unwrap_or_else(|| panic!("file not added: {:?}", name));298 if let Some(ref evaluated) = value.evaluated {299 return Ok(evaluated.clone());300 }301 value.parsed.clone()302 };303 let value = evaluate(self.create_default_context(), &expr)?;304 {305 self.data_mut()306 .files307 .get_mut(name)308 .unwrap()309 .evaluated310 .replace(value.clone());311 }312 Ok(value)313 }314315 /// Adds standard library global variable (std) to this evaluator316 pub fn with_stdlib(&self) -> &Self {317 use jrsonnet_stdlib::STDLIB_STR;318 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();319 self.run_in_state(|| {320 self.add_parsed_file(321 std_path.clone(),322 STDLIB_STR.to_owned().into(),323 builtin::get_parsed_stdlib(),324 )325 .unwrap();326 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();327 self.settings_mut().globals.insert("std".into(), val);328 });329 self330 }331332 /// Creates context with all passed global variables333 pub fn create_default_context(&self) -> Context {334 let globals = &self.settings().globals;335 let mut new_bindings = GcHashMap::with_capacity(globals.len());336 for (name, value) in globals.iter() {337 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));338 }339 Context::new().extend_bound(new_bindings)340 }341342 /// Executes code creating a new stack frame343 pub fn push<T>(344 &self,345 e: Option<&ExprLocation>,346 frame_desc: impl FnOnce() -> String,347 f: impl FnOnce() -> Result<T>,348 ) -> Result<T> {349 {350 let mut data = self.data_mut();351 let stack_depth = &mut data.stack_depth;352 if *stack_depth > self.max_stack() {353 // Error creation uses data, so i drop guard here354 drop(data);355 throw!(StackOverflow);356 } else {357 *stack_depth += 1;358 }359 }360 let result = f();361 {362 let mut data = self.data_mut();363 data.stack_depth -= 1;364 data.stack_generation += 1;365 }366 if let Err(mut err) = result {367 err.trace_mut().0.push(StackTraceElement {368 location: e.cloned(),369 desc: frame_desc(),370 });371 return Err(err);372 }373 result374 }375376 /// Executes code creating a new stack frame377 pub fn push_val(378 &self,379 e: &ExprLocation,380 frame_desc: impl FnOnce() -> String,381 f: impl FnOnce() -> Result<Val>,382 ) -> Result<Val> {383 {384 let mut data = self.data_mut();385 let stack_depth = &mut data.stack_depth;386 if *stack_depth > self.max_stack() {387 // Error creation uses data, so i drop guard here388 drop(data);389 throw!(StackOverflow);390 } else {391 *stack_depth += 1;392 }393 }394 let mut result = f();395 {396 let mut data = self.data_mut();397 data.stack_depth -= 1;398 data.stack_generation += 1;399 result = data400 .breakpoints401 .insert(data.stack_depth, data.stack_generation, e, result);402 }403 if let Err(mut err) = result {404 err.trace_mut().0.push(StackTraceElement {405 location: Some(e.clone()),406 desc: frame_desc(),407 });408 return Err(err);409 }410 result411 }412 /// Executes code creating a new stack frame413 pub fn push_description<T>(414 &self,415 frame_desc: impl FnOnce() -> String,416 f: impl FnOnce() -> Result<T>,417 ) -> Result<T> {418 {419 let mut data = self.data_mut();420 let stack_depth = &mut data.stack_depth;421 if *stack_depth > self.max_stack() {422 // Error creation uses data, so i drop guard here423 drop(data);424 throw!(StackOverflow);425 } else {426 *stack_depth += 1;427 }428 }429 let result = f();430 {431 let mut data = self.data_mut();432 data.stack_depth -= 1;433 data.stack_generation += 1;434 }435 if let Err(mut err) = result {436 err.trace_mut().0.push(StackTraceElement {437 location: None,438 desc: frame_desc(),439 });440 return Err(err);441 }442 result443 }444445 /// Runs passed function in state (required if function needs to modify stack trace)446 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {447 EVAL_STATE.with(|v| {448 let has_state = v.borrow().is_some();449 if !has_state {450 v.borrow_mut().replace(self.clone());451 }452 let result = f();453 if !has_state {454 v.borrow_mut().take();455 }456 result457 })458 }459 pub fn run_in_state_with_breakpoint(460 &self,461 bp: Rc<Breakpoint>,462 f: impl FnOnce() -> Result<()>,463 ) -> Result<()> {464 {465 let mut data = self.data_mut();466 data.breakpoints.0.push(bp);467 }468469 let result = self.run_in_state(f);470471 {472 let mut data = self.data_mut();473 data.breakpoints.0.pop();474 }475476 result477 }478479 pub fn stringify_err(&self, e: &LocError) -> String {480 let mut out = String::new();481 self.settings()482 .trace_format483 .write_trace(&mut out, self, e)484 .unwrap();485 out486 }487488 pub fn manifest(&self, val: Val) -> Result<IStr> {489 self.run_in_state(|| {490 push_description_frame(491 || "manifestification".to_string(),492 || val.manifest(&self.manifest_format()),493 )494 })495 }496 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {497 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))498 }499 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {500 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))501 }502503 /// If passed value is function then call with set TLA504 pub fn with_tla(&self, val: Val) -> Result<Val> {505 self.run_in_state(|| {506 Ok(match val {507 Val::Func(func) => push_description_frame(508 || "during TLA call".to_owned(),509 || {510 func.evaluate(511 self.create_default_context(),512 None,513 &self.settings().tla_vars,514 true,515 )516 },517 )?,518 v => v,519 })520 })521 }522}523524/// Internals525impl EvaluationState {526 fn data(&self) -> Ref<EvaluationData> {527 self.0.data.borrow()528 }529 fn data_mut(&self) -> RefMut<EvaluationData> {530 self.0.data.borrow_mut()531 }532 pub fn settings(&self) -> Ref<EvaluationSettings> {533 self.0.settings.borrow()534 }535 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {536 self.0.settings.borrow_mut()537 }538}539540/// Raw methods evaluate passed values but don't perform TLA execution541impl EvaluationState {542 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {543 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))544 }545 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {546 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))547 }548 /// Parses and evaluates the given snippet549 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {550 let parsed = parse(551 &code,552 &ParserSettings {553 file_name: source.clone(),554 },555 )556 .map_err(|e| ImportSyntaxError {557 path: source.clone(),558 source_code: code.clone(),559 error: Box::new(e),560 })?;561 self.add_parsed_file(source, code, parsed.clone())?;562 self.evaluate_expr_raw(parsed)563 }564 /// Evaluates the parsed expression565 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {566 self.run_in_state(|| evaluate(self.create_default_context(), &code))567 }568}569570/// Settings utilities571impl EvaluationState {572 pub fn add_ext_var(&self, name: IStr, value: Val) {573 self.settings_mut().ext_vars.insert(name, value);574 }575 pub fn add_ext_str(&self, name: IStr, value: IStr) {576 self.add_ext_var(name, Val::Str(value));577 }578 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {579 let value =580 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;581 self.add_ext_var(name, value);582 Ok(())583 }584585 pub fn add_tla(&self, name: IStr, value: Val) {586 self.settings_mut()587 .tla_vars588 .insert(name, TlaArg::Val(value));589 }590 pub fn add_tla_str(&self, name: IStr, value: IStr) {591 self.settings_mut()592 .tla_vars593 .insert(name, TlaArg::String(value));594 }595 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {596 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;597 self.settings_mut()598 .tla_vars599 .insert(name, TlaArg::Code(parsed));600 Ok(())601 }602603 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {604 self.settings().import_resolver.resolve_file(from, path)605 }606 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {607 self.settings().import_resolver.load_file_contents(path)608 }609610 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {611 Ref::map(self.settings(), |s| &*s.import_resolver)612 }613 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {614 self.settings_mut().import_resolver = resolver;615 }616617 pub fn add_native(&self, name: IStr, cb: Cc<NativeCallback>) {618 self.settings_mut().ext_natives.insert(name, cb);619 }620621 pub fn manifest_format(&self) -> ManifestFormat {622 self.settings().manifest_format.clone()623 }624 pub fn set_manifest_format(&self, format: ManifestFormat) {625 self.settings_mut().manifest_format = format;626 }627628 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {629 Ref::map(self.settings(), |s| &*s.trace_format)630 }631 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {632 self.settings_mut().trace_format = format;633 }634635 pub fn max_trace(&self) -> usize {636 self.settings().max_trace637 }638 pub fn set_max_trace(&self, trace: usize) {639 self.settings_mut().max_trace = trace;640 }641642 pub fn max_stack(&self) -> usize {643 self.settings().max_stack644 }645 pub fn set_max_stack(&self, trace: usize) {646 self.settings_mut().max_stack = trace;647 }648}649650pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {651 let a = a as &T;652 let b = b as &T;653 std::ptr::eq(a, b)654}655656#[cfg(test)]657pub mod tests {658 use super::Val;659 use crate::{660 error::Error::*, gc::TraceBox, native::NativeCallbackHandler, primitive_equals,661 EvaluationState,662 };663 use gcmodule::{Cc, Trace};664 use jrsonnet_interner::IStr;665 use jrsonnet_parser::*;666 use std::{667 path::{Path, PathBuf},668 rc::Rc,669 };670671 #[test]672 #[should_panic]673 fn eval_state_stacktrace() {674 let state = EvaluationState::default();675 state.run_in_state(|| {676 state677 .push(678 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),679 || "outer".to_owned(),680 || {681 state.push(682 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),683 || "inner".to_owned(),684 || Err(RuntimeError("".into()).into()),685 )?;686 Ok(Val::Null)687 },688 )689 .unwrap();690 });691 }692693 #[test]694 fn eval_state_standard() {695 let state = EvaluationState::default();696 state.with_stdlib();697 assert!(primitive_equals(698 &state699 .evaluate_snippet_raw(700 PathBuf::from("raw.jsonnet").into(),701 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()702 )703 .unwrap(),704 &Val::Bool(true),705 )706 .unwrap());707 }708709 macro_rules! eval {710 ($str: expr) => {711 EvaluationState::default()712 .with_stdlib()713 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())714 .unwrap()715 };716 }717 macro_rules! eval_json {718 ($str: expr) => {{719 let evaluator = EvaluationState::default();720 evaluator.with_stdlib();721 evaluator.run_in_state(|| {722 evaluator723 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())724 .unwrap()725 .to_json(0)726 .unwrap()727 .replace("\n", "")728 })729 }};730 }731732 /// Asserts given code returns `true`733 macro_rules! assert_eval {734 ($str: expr) => {735 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())736 };737 }738739 /// Asserts given code returns `false`740 macro_rules! assert_eval_neg {741 ($str: expr) => {742 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())743 };744 }745 macro_rules! assert_json {746 ($str: expr, $out: expr) => {747 assert_eq!(eval_json!($str), $out.replace("\t", ""))748 };749 }750751 /// Sanity checking, before trusting to another tests752 #[test]753 fn equality_operator() {754 assert_eval!("2 == 2");755 assert_eval_neg!("2 != 2");756 assert_eval!("2 != 3");757 assert_eval_neg!("2 == 3");758 assert_eval!("'Hello' == 'Hello'");759 assert_eval_neg!("'Hello' != 'Hello'");760 assert_eval!("'Hello' != 'World'");761 assert_eval_neg!("'Hello' == 'World'");762 }763764 #[test]765 fn math_evaluation() {766 assert_eval!("2 + 2 * 2 == 6");767 assert_eval!("3 + (2 + 2 * 2) == 9");768 }769770 #[test]771 fn string_concat() {772 assert_eval!("'Hello' + 'World' == 'HelloWorld'");773 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");774 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");775 }776777 #[test]778 fn faster_join() {779 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");780 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");781 }782783 #[test]784 fn function_contexts() {785 assert_eval!(786 r#"787 local k = {788 t(name = self.h): [self.h, name],789 h: 3,790 };791 local f = {792 t: k.t(),793 h: 4,794 };795 f.t[0] == f.t[1]796 "#797 );798 }799800 #[test]801 fn local() {802 assert_eval!("local a = 2; local b = 3; a + b == 5");803 assert_eval!("local a = 1, b = a + 1; a + b == 3");804 assert_eval!("local a = 1; local a = 2; a == 2");805 }806807 #[test]808 fn object_lazyness() {809 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);810 }811812 #[test]813 fn object_inheritance() {814 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);815 }816817 #[test]818 fn object_assertion_success() {819 eval!("{assert \"a\" in self} + {a:2}");820 }821822 #[test]823 fn object_assertion_error() {824 eval!("{assert \"a\" in self}");825 }826827 #[test]828 fn lazy_args() {829 eval!("local test(a) = 2; test(error '3')");830 }831832 #[test]833 #[should_panic]834 fn tailstrict_args() {835 eval!("local test(a) = 2; test(error '3') tailstrict");836 }837838 #[test]839 #[should_panic]840 fn no_binding_error() {841 eval!("a");842 }843844 #[test]845 fn test_object() {846 assert_json!("{a:2}", r#"{"a": 2}"#);847 assert_json!("{a:2+2}", r#"{"a": 4}"#);848 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);849 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);850 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);851 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);852 assert_json!(853 r#"854 {855 name: "Alice",856 welcome: "Hello " + self.name + "!",857 }858 "#,859 r#"{"name": "Alice","welcome": "Hello Alice!"}"#860 );861 assert_json!(862 r#"863 {864 name: "Alice",865 welcome: "Hello " + self.name + "!",866 } + {867 name: "Bob"868 }869 "#,870 r#"{"name": "Bob","welcome": "Hello Bob!"}"#871 );872 }873874 #[test]875 fn functions() {876 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");877 assert_json!(878 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,879 r#""HelloDearWorld""#880 );881 }882883 #[test]884 fn local_methods() {885 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");886 assert_json!(887 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,888 r#""HelloDearWorld""#889 );890 }891892 #[test]893 fn object_locals() {894 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);895 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);896 assert_json!(897 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,898 r#"{"test": {"test": 4}}"#899 );900 }901902 #[test]903 fn object_comp() {904 assert_json!(905 r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,906 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"907 )908 }909910 #[test]911 fn direct_self() {912 println!(913 "{:#?}",914 eval!(915 r#"916 {917 local me = self,918 a: 3,919 b(): me.a,920 }921 "#922 )923 );924 }925926 #[test]927 fn indirect_self() {928 // `self` assigned to `me` was lost when being929 // referenced from field930 eval!(931 r#"{932 local me = self,933 a: 3,934 b: me.a,935 }.b"#936 );937 }938939 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly940 #[test]941 fn std_assert_ok() {942 eval!("std.assertEqual(4.5 << 2, 16)");943 }944945 #[test]946 #[should_panic]947 fn std_assert_failure() {948 eval!("std.assertEqual(4.5 << 2, 15)");949 }950951 #[test]952 fn string_is_string() {953 assert!(primitive_equals(954 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),955 &Val::Bool(false),956 )957 .unwrap());958 }959960 #[test]961 fn base64_works() {962 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);963 }964965 #[test]966 fn utf8_chars() {967 assert_json!(968 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,969 r#"{"c": 128526,"l": 1}"#970 )971 }972973 #[test]974 fn json() {975 assert_json!(976 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,977 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#978 );979 }980981 #[test]982 fn json_minified() {983 assert_json!(984 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,985 r#""{\"a\":3,\"b\":4,\"c\":6}""#986 );987 }988989 #[test]990 fn parse_json() {991 assert_json!(992 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,993 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#994 );995 }996997 #[test]998 fn test() {999 assert_json!(1000 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1001 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1002 );1003 }10041005 #[test]1006 fn sjsonnet() {1007 eval!(1008 r#"1009 local x0 = {k: 1};1010 local x1 = {k: x0.k + x0.k};1011 local x2 = {k: x1.k + x1.k};1012 local x3 = {k: x2.k + x2.k};1013 local x4 = {k: x3.k + x3.k};1014 local x5 = {k: x4.k + x4.k};1015 local x6 = {k: x5.k + x5.k};1016 local x7 = {k: x6.k + x6.k};1017 local x8 = {k: x7.k + x7.k};1018 local x9 = {k: x8.k + x8.k};1019 local x10 = {k: x9.k + x9.k};1020 local x11 = {k: x10.k + x10.k};1021 local x12 = {k: x11.k + x11.k};1022 local x13 = {k: x12.k + x12.k};1023 local x14 = {k: x13.k + x13.k};1024 local x15 = {k: x14.k + x14.k};1025 local x16 = {k: x15.k + x15.k};1026 local x17 = {k: x16.k + x16.k};1027 local x18 = {k: x17.k + x17.k};1028 local x19 = {k: x18.k + x18.k};1029 local x20 = {k: x19.k + x19.k};1030 local x21 = {k: x20.k + x20.k};1031 x21.k1032 "#1033 );1034 }10351036 // This test is commented out by default, because of huge compilation slowdown1037 // #[bench]1038 // fn bench_codegen(b: &mut Bencher) {1039 // b.iter(|| {1040 // #[allow(clippy::all)]1041 // let stdlib = {1042 // use jrsonnet_parser::*;1043 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1044 // };1045 // stdlib1046 // })1047 // }10481049 /*1050 #[bench]1051 fn bench_serialize(b: &mut Bencher) {1052 b.iter(|| {1053 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1054 env!("OUT_DIR"),1055 "/stdlib.bincode"1056 )))1057 .expect("deserialize stdlib")1058 })1059 }10601061 #[bench]1062 fn bench_parse(b: &mut Bencher) {1063 b.iter(|| {1064 jrsonnet_parser::parse(1065 jrsonnet_stdlib::STDLIB_STR,1066 &jrsonnet_parser::ParserSettings {1067 loc_data: true,1068 file_name: Rc::new(PathBuf::from("std.jsonnet")),1069 },1070 )1071 })1072 }1073 */10741075 #[test]1076 fn equality() {1077 println!(1078 "{:?}",1079 jrsonnet_parser::parse(1080 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1081 &ParserSettings {1082 file_name: PathBuf::from("equality").into(),1083 }1084 )1085 );1086 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1087 }10881089 #[test]1090 fn native_ext() -> crate::error::Result<()> {1091 use super::native::NativeCallback;1092 let evaluator = EvaluationState::default();10931094 evaluator.with_stdlib();10951096 #[derive(Trace)]1097 struct NativeAdd;1098 impl NativeCallbackHandler for NativeAdd {1099 fn call(&self, from: Rc<Path>, args: &[Val]) -> crate::error::Result<Val> {1100 assert_eq!(&from as &Path, &PathBuf::from("native_caller.jsonnet"));1101 match (&args[0], &args[1]) {1102 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1103 (_, _) => unreachable!(),1104 }1105 }1106 }1107 evaluator.settings_mut().ext_natives.insert(1108 "native_add".into(),1109 Cc::new(NativeCallback::new(1110 ParamsDesc(Rc::new(vec![1111 Param("a".into(), None),1112 Param("b".into(), None),1113 ])),1114 TraceBox(Box::new(NativeAdd)),1115 )),1116 );1117 evaluator.evaluate_snippet_raw(1118 PathBuf::from("native_caller.jsonnet").into(),1119 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1120 )?;1121 Ok(())1122 }11231124 #[test]1125 fn constant_intrinsic() -> crate::error::Result<()> {1126 assert_eval!(1127 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1128 );1129 Ok(())1130 }11311132 #[test]1133 fn standalone_super() -> crate::error::Result<()> {1134 assert_eval!(1135 r#"1136 local obj = {1137 a: 1,1138 b: 2,1139 c: 3,1140 };1141 local test = obj + {1142 fields: std.objectFields(super),1143 d: 5,1144 };1145 test.fields == ['a', 'b', 'c']1146 "#1147 );1148 Ok(())1149 }11501151 #[test]1152 fn comp_self() -> crate::error::Result<()> {1153 assert_eval!(1154 r#"1155 std.objectFields({1156 a:{1157 [name]: name for name in std.objectFields(self)1158 },1159 b: 2,1160 c: 3,1161 }.a) == ['a', 'b', 'c']1162 "#1163 );11641165 Ok(())1166 }11671168 struct TestImportResolver(IStr);1169 impl crate::import::ImportResolver for TestImportResolver {1170 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1171 Ok(PathBuf::from("/test").into())1172 }11731174 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1175 Ok(self.0.clone())1176 }11771178 unsafe fn as_any(&self) -> &dyn std::any::Any {1179 panic!()1180 }1181 }11821183 #[test]1184 fn issue_23() {1185 let state = EvaluationState::default();1186 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1187 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1188 }11891190 #[test]1191 fn issue_40() {1192 let state = EvaluationState::default();1193 state.with_stdlib();11941195 let error = state1196 .evaluate_snippet_raw(1197 PathBuf::from("issue40.jsonnet").into(),1198 r#"1199 local conf = {1200 n: ""1201 };12021203 local result = conf + {1204 assert std.isNumber(self.n): "is number"1205 };12061207 std.manifestJsonEx(result, "")1208 "#1209 .into(),1210 )1211 .unwrap_err();1212 assert_eq!(error.error().to_string(), "assert failed: is number");1213 }12141215 #[test]1216 fn test_ascii_upper_lower() {1217 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1218 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1219 }12201221 #[test]1222 fn test_member() {1223 assert_eval!(r#"!std.member("", "")"#);1224 assert_eval!(r#"std.member("abc", "a")"#);1225 assert_eval!(r#"!std.member("abc", "d")"#);1226 assert_eval!(r#"!std.member([], "")"#);1227 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1228 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1229 }12301231 #[test]1232 fn test_count() {1233 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1234 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1235 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1236 }1237}1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![warn(clippy::all, clippy::nursery)]3#![allow(4 macro_expanded_macro_exports_accessed_by_absolute_paths,5 clippy::ptr_arg6)]78// For jrsonnet-macros9extern crate self as jrsonnet_evaluator;1011mod builtin;12mod ctx;13mod dynamic;14pub mod error;15mod evaluate;16mod function;17mod import;18mod integrations;19mod map;20pub mod native;21mod obj;22pub mod trace;23pub mod typed;24mod val;2526pub use ctx::*;27pub use dynamic::*;28use error::{Error::*, LocError, Result, StackTraceElement};29pub use evaluate::*;30pub use function::parse_function_call;31use function::TlaArg;32use gc::{GcHashMap, TraceBox};33use gcmodule::{Cc, Trace};34pub use import::*;35pub use jrsonnet_interner::IStr;36use jrsonnet_parser::*;37use native::NativeCallback;38pub use obj::*;39use std::{40 cell::{Ref, RefCell, RefMut},41 collections::HashMap,42 fmt::Debug,43 path::{Path, PathBuf},44 rc::Rc,45};46use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};47pub use val::*;48pub mod gc;4950pub trait Bindable: Trace + 'static {51 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;52}5354#[derive(Clone, Trace)]55pub enum LazyBinding {56 Bindable(Cc<TraceBox<dyn Bindable>>),57 Bound(LazyVal),58}5960impl Debug for LazyBinding {61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {62 write!(f, "LazyBinding")63 }64}65impl LazyBinding {66 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {67 match self {68 Self::Bindable(v) => v.bind(this, super_obj),69 Self::Bound(v) => Ok(v.clone()),70 }71 }72}7374pub struct EvaluationSettings {75 /// Limits recursion by limiting the number of stack frames76 pub max_stack: usize,77 /// Limits amount of stack trace items preserved78 pub max_trace: usize,79 /// Used for s`td.extVar`80 pub ext_vars: HashMap<IStr, Val>,81 /// Used for ext.native82 pub ext_natives: HashMap<IStr, Cc<NativeCallback>>,83 /// TLA vars84 pub tla_vars: HashMap<IStr, TlaArg>,85 /// Global variables are inserted in default context86 pub globals: HashMap<IStr, Val>,87 /// Used to resolve file locations/contents88 pub import_resolver: Box<dyn ImportResolver>,89 /// Used in manifestification functions90 pub manifest_format: ManifestFormat,91 /// Used for bindings92 pub trace_format: Box<dyn TraceFormat>,93}94impl Default for EvaluationSettings {95 fn default() -> Self {96 Self {97 max_stack: 200,98 max_trace: 20,99 globals: Default::default(),100 ext_vars: Default::default(),101 ext_natives: Default::default(),102 tla_vars: Default::default(),103 import_resolver: Box::new(DummyImportResolver),104 manifest_format: ManifestFormat::Json(4),105 trace_format: Box::new(CompactFormat {106 padding: 4,107 resolver: trace::PathResolver::Absolute,108 }),109 }110 }111}112113#[derive(Default)]114struct EvaluationData {115 /// Used for stack overflow detection, stacktrace is populated on unwind116 stack_depth: usize,117 /// Updated every time stack entry is popt118 stack_generation: usize,119120 breakpoints: Breakpoints,121 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces122 files: GcHashMap<Rc<Path>, FileData>,123 str_files: GcHashMap<Rc<Path>, IStr>,124 bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,125}126127pub struct FileData {128 source_code: IStr,129 parsed: LocExpr,130 evaluated: Option<Val>,131}132133#[allow(clippy::type_complexity)]134pub struct Breakpoint {135 loc: ExprLocation,136 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,137}138#[derive(Default)]139struct Breakpoints(Vec<Rc<Breakpoint>>);140impl Breakpoints {141 fn insert(142 &self,143 stack_depth: usize,144 stack_generation: usize,145 loc: &ExprLocation,146 result: Result<Val>,147 ) -> Result<Val> {148 if self.0.is_empty() {149 return result;150 }151 for item in self.0.iter() {152 if item.loc.belongs_to(loc) {153 let mut collected = item.collected.borrow_mut();154 let (depth, vals) = collected.entry(stack_generation).or_default();155 if stack_depth > *depth {156 vals.clear();157 }158 vals.push(result.clone());159 }160 }161 result162 }163}164165#[derive(Default)]166pub struct EvaluationStateInternals {167 /// Internal state168 data: RefCell<EvaluationData>,169 /// Settings, safe to change at runtime170 settings: RefCell<EvaluationSettings>,171}172173thread_local! {174 /// Contains the state for a currently executed file.175 /// Global state is fine here.176 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)177}178pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {179 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))180}181pub(crate) fn push_frame<T>(182 e: Option<&ExprLocation>,183 frame_desc: impl FnOnce() -> String,184 f: impl FnOnce() -> Result<T>,185) -> Result<T> {186 with_state(|s| s.push(e, frame_desc, f))187}188189#[allow(dead_code)]190pub(crate) fn push_val_frame(191 e: &ExprLocation,192 frame_desc: impl FnOnce() -> String,193 f: impl FnOnce() -> Result<Val>,194) -> Result<Val> {195 with_state(|s| s.push_val(e, frame_desc, f))196}197#[allow(dead_code)]198pub(crate) fn push_description_frame<T>(199 frame_desc: impl FnOnce() -> String,200 f: impl FnOnce() -> Result<T>,201) -> Result<T> {202 with_state(|s| s.push_description(frame_desc, f))203}204205/// Maintains stack trace and import resolution206#[derive(Default, Clone)]207pub struct EvaluationState(Rc<EvaluationStateInternals>);208209impl EvaluationState {210 /// Parses and adds file as loaded211 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {212 let parsed = parse(213 &source_code,214 &ParserSettings {215 file_name: path.clone(),216 },217 )218 .map_err(|error| ImportSyntaxError {219 error: Box::new(error),220 path: path.to_owned(),221 source_code: source_code.clone(),222 })?;223 self.add_parsed_file(path, source_code, parsed.clone())?;224225 Ok(parsed)226 }227228 pub fn reset_evaluation_state(&self, name: &Path) {229 self.data_mut()230 .files231 .get_mut(name)232 .unwrap()233 .evaluated234 .take();235 }236237 /// Adds file by source code and parsed expr238 pub fn add_parsed_file(239 &self,240 name: Rc<Path>,241 source_code: IStr,242 parsed: LocExpr,243 ) -> Result<()> {244 self.data_mut().files.insert(245 name,246 FileData {247 source_code,248 parsed,249 evaluated: None,250 },251 );252253 Ok(())254 }255 pub fn get_source(&self, name: &Path) -> Option<IStr> {256 let ro_map = &self.data().files;257 ro_map.get(name).map(|value| value.source_code.clone())258 }259 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {260 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)261 }262 pub fn map_from_source_location(263 &self,264 file: &Path,265 line: usize,266 column: usize,267 ) -> Option<usize> {268 location_to_offset(&self.get_source(file).unwrap(), line, column)269 }270 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {271 let file_path = self.resolve_file(from, path)?;272 {273 let data = self.data();274 let files = &data.files;275 if files.contains_key(&file_path as &Path) {276 drop(data);277 return self.evaluate_loaded_file_raw(&file_path);278 }279 }280 let contents = self.load_file_str(&file_path)?;281 self.add_file(file_path.clone(), contents)?;282 self.evaluate_loaded_file_raw(&file_path)283 }284 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {285 let path = self.resolve_file(from, path)?;286 if !self.data().str_files.contains_key(&path) {287 let file_str = self.load_file_str(&path)?;288 self.data_mut().str_files.insert(path.clone(), file_str);289 }290 Ok(self.data().str_files.get(&path).cloned().unwrap())291 }292 pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {293 let path = self.resolve_file(from, path)?;294 if !self.data().bin_files.contains_key(&path) {295 let file_bin = self.load_file_bin(&path)?;296 self.data_mut().bin_files.insert(path.clone(), file_bin);297 }298 Ok(self.data().bin_files.get(&path).cloned().unwrap())299 }300301 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {302 let expr: LocExpr = {303 let ro_map = &self.data().files;304 let value = ro_map305 .get(name)306 .unwrap_or_else(|| panic!("file not added: {:?}", name));307 if let Some(ref evaluated) = value.evaluated {308 return Ok(evaluated.clone());309 }310 value.parsed.clone()311 };312 let value = evaluate(self.create_default_context(), &expr)?;313 {314 self.data_mut()315 .files316 .get_mut(name)317 .unwrap()318 .evaluated319 .replace(value.clone());320 }321 Ok(value)322 }323324 /// Adds standard library global variable (std) to this evaluator325 pub fn with_stdlib(&self) -> &Self {326 use jrsonnet_stdlib::STDLIB_STR;327 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();328 self.run_in_state(|| {329 self.add_parsed_file(330 std_path.clone(),331 STDLIB_STR.to_owned().into(),332 builtin::get_parsed_stdlib(),333 )334 .unwrap();335 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();336 self.settings_mut().globals.insert("std".into(), val);337 });338 self339 }340341 /// Creates context with all passed global variables342 pub fn create_default_context(&self) -> Context {343 let globals = &self.settings().globals;344 let mut new_bindings = GcHashMap::with_capacity(globals.len());345 for (name, value) in globals.iter() {346 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));347 }348 Context::new().extend_bound(new_bindings)349 }350351 /// Executes code creating a new stack frame352 pub fn push<T>(353 &self,354 e: Option<&ExprLocation>,355 frame_desc: impl FnOnce() -> String,356 f: impl FnOnce() -> Result<T>,357 ) -> Result<T> {358 {359 let mut data = self.data_mut();360 let stack_depth = &mut data.stack_depth;361 if *stack_depth > self.max_stack() {362 // Error creation uses data, so i drop guard here363 drop(data);364 throw!(StackOverflow);365 } else {366 *stack_depth += 1;367 }368 }369 let result = f();370 {371 let mut data = self.data_mut();372 data.stack_depth -= 1;373 data.stack_generation += 1;374 }375 if let Err(mut err) = result {376 err.trace_mut().0.push(StackTraceElement {377 location: e.cloned(),378 desc: frame_desc(),379 });380 return Err(err);381 }382 result383 }384385 /// Executes code creating a new stack frame386 pub fn push_val(387 &self,388 e: &ExprLocation,389 frame_desc: impl FnOnce() -> String,390 f: impl FnOnce() -> Result<Val>,391 ) -> Result<Val> {392 {393 let mut data = self.data_mut();394 let stack_depth = &mut data.stack_depth;395 if *stack_depth > self.max_stack() {396 // Error creation uses data, so i drop guard here397 drop(data);398 throw!(StackOverflow);399 } else {400 *stack_depth += 1;401 }402 }403 let mut result = f();404 {405 let mut data = self.data_mut();406 data.stack_depth -= 1;407 data.stack_generation += 1;408 result = data409 .breakpoints410 .insert(data.stack_depth, data.stack_generation, e, result);411 }412 if let Err(mut err) = result {413 err.trace_mut().0.push(StackTraceElement {414 location: Some(e.clone()),415 desc: frame_desc(),416 });417 return Err(err);418 }419 result420 }421 /// Executes code creating a new stack frame422 pub fn push_description<T>(423 &self,424 frame_desc: impl FnOnce() -> String,425 f: impl FnOnce() -> Result<T>,426 ) -> Result<T> {427 {428 let mut data = self.data_mut();429 let stack_depth = &mut data.stack_depth;430 if *stack_depth > self.max_stack() {431 // Error creation uses data, so i drop guard here432 drop(data);433 throw!(StackOverflow);434 } else {435 *stack_depth += 1;436 }437 }438 let result = f();439 {440 let mut data = self.data_mut();441 data.stack_depth -= 1;442 data.stack_generation += 1;443 }444 if let Err(mut err) = result {445 err.trace_mut().0.push(StackTraceElement {446 location: None,447 desc: frame_desc(),448 });449 return Err(err);450 }451 result452 }453454 /// Runs passed function in state (required if function needs to modify stack trace)455 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {456 EVAL_STATE.with(|v| {457 let has_state = v.borrow().is_some();458 if !has_state {459 v.borrow_mut().replace(self.clone());460 }461 let result = f();462 if !has_state {463 v.borrow_mut().take();464 }465 result466 })467 }468 pub fn run_in_state_with_breakpoint(469 &self,470 bp: Rc<Breakpoint>,471 f: impl FnOnce() -> Result<()>,472 ) -> Result<()> {473 {474 let mut data = self.data_mut();475 data.breakpoints.0.push(bp);476 }477478 let result = self.run_in_state(f);479480 {481 let mut data = self.data_mut();482 data.breakpoints.0.pop();483 }484485 result486 }487488 pub fn stringify_err(&self, e: &LocError) -> String {489 let mut out = String::new();490 self.settings()491 .trace_format492 .write_trace(&mut out, self, e)493 .unwrap();494 out495 }496497 pub fn manifest(&self, val: Val) -> Result<IStr> {498 self.run_in_state(|| {499 push_description_frame(500 || "manifestification".to_string(),501 || val.manifest(&self.manifest_format()),502 )503 })504 }505 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {506 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))507 }508 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {509 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))510 }511512 /// If passed value is function then call with set TLA513 pub fn with_tla(&self, val: Val) -> Result<Val> {514 self.run_in_state(|| {515 Ok(match val {516 Val::Func(func) => push_description_frame(517 || "during TLA call".to_owned(),518 || {519 func.evaluate(520 self.create_default_context(),521 None,522 &self.settings().tla_vars,523 true,524 )525 },526 )?,527 v => v,528 })529 })530 }531}532533/// Internals534impl EvaluationState {535 fn data(&self) -> Ref<EvaluationData> {536 self.0.data.borrow()537 }538 fn data_mut(&self) -> RefMut<EvaluationData> {539 self.0.data.borrow_mut()540 }541 pub fn settings(&self) -> Ref<EvaluationSettings> {542 self.0.settings.borrow()543 }544 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {545 self.0.settings.borrow_mut()546 }547}548549/// Raw methods evaluate passed values but don't perform TLA execution550impl EvaluationState {551 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {552 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))553 }554 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {555 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))556 }557 /// Parses and evaluates the given snippet558 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {559 let parsed = parse(560 &code,561 &ParserSettings {562 file_name: source.clone(),563 },564 )565 .map_err(|e| ImportSyntaxError {566 path: source.clone(),567 source_code: code.clone(),568 error: Box::new(e),569 })?;570 self.add_parsed_file(source, code, parsed.clone())?;571 self.evaluate_expr_raw(parsed)572 }573 /// Evaluates the parsed expression574 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {575 self.run_in_state(|| evaluate(self.create_default_context(), &code))576 }577}578579/// Settings utilities580impl EvaluationState {581 pub fn add_ext_var(&self, name: IStr, value: Val) {582 self.settings_mut().ext_vars.insert(name, value);583 }584 pub fn add_ext_str(&self, name: IStr, value: IStr) {585 self.add_ext_var(name, Val::Str(value));586 }587 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {588 let value =589 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;590 self.add_ext_var(name, value);591 Ok(())592 }593594 pub fn add_tla(&self, name: IStr, value: Val) {595 self.settings_mut()596 .tla_vars597 .insert(name, TlaArg::Val(value));598 }599 pub fn add_tla_str(&self, name: IStr, value: IStr) {600 self.settings_mut()601 .tla_vars602 .insert(name, TlaArg::String(value));603 }604 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {605 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;606 self.settings_mut()607 .tla_vars608 .insert(name, TlaArg::Code(parsed));609 Ok(())610 }611612 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {613 self.settings().import_resolver.resolve_file(from, path)614 }615 pub fn load_file_str(&self, path: &Path) -> Result<IStr> {616 self.settings().import_resolver.load_file_str(path)617 }618 pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {619 self.settings().import_resolver.load_file_bin(path)620 }621622 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {623 Ref::map(self.settings(), |s| &*s.import_resolver)624 }625 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {626 self.settings_mut().import_resolver = resolver;627 }628629 pub fn add_native(&self, name: IStr, cb: Cc<NativeCallback>) {630 self.settings_mut().ext_natives.insert(name, cb);631 }632633 pub fn manifest_format(&self) -> ManifestFormat {634 self.settings().manifest_format.clone()635 }636 pub fn set_manifest_format(&self, format: ManifestFormat) {637 self.settings_mut().manifest_format = format;638 }639640 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {641 Ref::map(self.settings(), |s| &*s.trace_format)642 }643 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {644 self.settings_mut().trace_format = format;645 }646647 pub fn max_trace(&self) -> usize {648 self.settings().max_trace649 }650 pub fn set_max_trace(&self, trace: usize) {651 self.settings_mut().max_trace = trace;652 }653654 pub fn max_stack(&self) -> usize {655 self.settings().max_stack656 }657 pub fn set_max_stack(&self, trace: usize) {658 self.settings_mut().max_stack = trace;659 }660}661662pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {663 let a = a as &T;664 let b = b as &T;665 std::ptr::eq(a, b)666}667668#[cfg(test)]669pub mod tests {670 use super::Val;671 use crate::{672 error::Error::*, gc::TraceBox, native::NativeCallbackHandler, primitive_equals,673 EvaluationState,674 };675 use gcmodule::{Cc, Trace};676 use jrsonnet_parser::*;677 use std::{678 path::{Path, PathBuf},679 rc::Rc,680 };681682 #[test]683 #[should_panic]684 fn eval_state_stacktrace() {685 let state = EvaluationState::default();686 state.run_in_state(|| {687 state688 .push(689 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),690 || "outer".to_owned(),691 || {692 state.push(693 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),694 || "inner".to_owned(),695 || Err(RuntimeError("".into()).into()),696 )?;697 Ok(Val::Null)698 },699 )700 .unwrap();701 });702 }703704 #[test]705 fn eval_state_standard() {706 let state = EvaluationState::default();707 state.with_stdlib();708 assert!(primitive_equals(709 &state710 .evaluate_snippet_raw(711 PathBuf::from("raw.jsonnet").into(),712 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()713 )714 .unwrap(),715 &Val::Bool(true),716 )717 .unwrap());718 }719720 macro_rules! eval {721 ($str: expr) => {722 EvaluationState::default()723 .with_stdlib()724 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())725 .unwrap()726 };727 }728 macro_rules! eval_json {729 ($str: expr) => {{730 let evaluator = EvaluationState::default();731 evaluator.with_stdlib();732 evaluator.run_in_state(|| {733 evaluator734 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())735 .unwrap()736 .to_json(0)737 .unwrap()738 .replace("\n", "")739 })740 }};741 }742743 /// Asserts given code returns `true`744 macro_rules! assert_eval {745 ($str: expr) => {746 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())747 };748 }749750 /// Asserts given code returns `false`751 macro_rules! assert_eval_neg {752 ($str: expr) => {753 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())754 };755 }756 macro_rules! assert_json {757 ($str: expr, $out: expr) => {758 assert_eq!(eval_json!($str), $out.replace("\t", ""))759 };760 }761762 /// Sanity checking, before trusting to another tests763 #[test]764 fn equality_operator() {765 assert_eval!("2 == 2");766 assert_eval_neg!("2 != 2");767 assert_eval!("2 != 3");768 assert_eval_neg!("2 == 3");769 assert_eval!("'Hello' == 'Hello'");770 assert_eval_neg!("'Hello' != 'Hello'");771 assert_eval!("'Hello' != 'World'");772 assert_eval_neg!("'Hello' == 'World'");773 }774775 #[test]776 fn math_evaluation() {777 assert_eval!("2 + 2 * 2 == 6");778 assert_eval!("3 + (2 + 2 * 2) == 9");779 }780781 #[test]782 fn string_concat() {783 assert_eval!("'Hello' + 'World' == 'HelloWorld'");784 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");785 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");786 }787788 #[test]789 fn faster_join() {790 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");791 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");792 }793794 #[test]795 fn function_contexts() {796 assert_eval!(797 r#"798 local k = {799 t(name = self.h): [self.h, name],800 h: 3,801 };802 local f = {803 t: k.t(),804 h: 4,805 };806 f.t[0] == f.t[1]807 "#808 );809 }810811 #[test]812 fn local() {813 assert_eval!("local a = 2; local b = 3; a + b == 5");814 assert_eval!("local a = 1, b = a + 1; a + b == 3");815 assert_eval!("local a = 1; local a = 2; a == 2");816 }817818 #[test]819 fn object_lazyness() {820 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);821 }822823 #[test]824 fn object_inheritance() {825 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);826 }827828 #[test]829 fn object_assertion_success() {830 eval!("{assert \"a\" in self} + {a:2}");831 }832833 #[test]834 fn object_assertion_error() {835 eval!("{assert \"a\" in self}");836 }837838 #[test]839 fn lazy_args() {840 eval!("local test(a) = 2; test(error '3')");841 }842843 #[test]844 #[should_panic]845 fn tailstrict_args() {846 eval!("local test(a) = 2; test(error '3') tailstrict");847 }848849 #[test]850 #[should_panic]851 fn no_binding_error() {852 eval!("a");853 }854855 #[test]856 fn test_object() {857 assert_json!("{a:2}", r#"{"a": 2}"#);858 assert_json!("{a:2+2}", r#"{"a": 4}"#);859 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);860 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);861 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);862 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);863 assert_json!(864 r#"865 {866 name: "Alice",867 welcome: "Hello " + self.name + "!",868 }869 "#,870 r#"{"name": "Alice","welcome": "Hello Alice!"}"#871 );872 assert_json!(873 r#"874 {875 name: "Alice",876 welcome: "Hello " + self.name + "!",877 } + {878 name: "Bob"879 }880 "#,881 r#"{"name": "Bob","welcome": "Hello Bob!"}"#882 );883 }884885 #[test]886 fn functions() {887 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");888 assert_json!(889 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,890 r#""HelloDearWorld""#891 );892 }893894 #[test]895 fn local_methods() {896 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");897 assert_json!(898 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,899 r#""HelloDearWorld""#900 );901 }902903 #[test]904 fn object_locals() {905 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);906 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);907 assert_json!(908 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,909 r#"{"test": {"test": 4}}"#910 );911 }912913 #[test]914 fn object_comp() {915 assert_json!(916 r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,917 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"918 )919 }920921 #[test]922 fn direct_self() {923 println!(924 "{:#?}",925 eval!(926 r#"927 {928 local me = self,929 a: 3,930 b(): me.a,931 }932 "#933 )934 );935 }936937 #[test]938 fn indirect_self() {939 // `self` assigned to `me` was lost when being940 // referenced from field941 eval!(942 r#"{943 local me = self,944 a: 3,945 b: me.a,946 }.b"#947 );948 }949950 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly951 #[test]952 fn std_assert_ok() {953 eval!("std.assertEqual(4.5 << 2, 16)");954 }955956 #[test]957 #[should_panic]958 fn std_assert_failure() {959 eval!("std.assertEqual(4.5 << 2, 15)");960 }961962 #[test]963 fn string_is_string() {964 assert!(primitive_equals(965 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),966 &Val::Bool(false),967 )968 .unwrap());969 }970971 #[test]972 fn base64_works() {973 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);974 }975976 #[test]977 fn utf8_chars() {978 assert_json!(979 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,980 r#"{"c": 128526,"l": 1}"#981 )982 }983984 #[test]985 fn json() {986 assert_json!(987 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,988 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#989 );990 }991992 #[test]993 fn json_minified() {994 assert_json!(995 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,996 r#""{\"a\":3,\"b\":4,\"c\":6}""#997 );998 }9991000 #[test]1001 fn parse_json() {1002 assert_json!(1003 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,1004 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#1005 );1006 }10071008 #[test]1009 fn test() {1010 assert_json!(1011 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1012 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1013 );1014 }10151016 #[test]1017 fn sjsonnet() {1018 eval!(1019 r#"1020 local x0 = {k: 1};1021 local x1 = {k: x0.k + x0.k};1022 local x2 = {k: x1.k + x1.k};1023 local x3 = {k: x2.k + x2.k};1024 local x4 = {k: x3.k + x3.k};1025 local x5 = {k: x4.k + x4.k};1026 local x6 = {k: x5.k + x5.k};1027 local x7 = {k: x6.k + x6.k};1028 local x8 = {k: x7.k + x7.k};1029 local x9 = {k: x8.k + x8.k};1030 local x10 = {k: x9.k + x9.k};1031 local x11 = {k: x10.k + x10.k};1032 local x12 = {k: x11.k + x11.k};1033 local x13 = {k: x12.k + x12.k};1034 local x14 = {k: x13.k + x13.k};1035 local x15 = {k: x14.k + x14.k};1036 local x16 = {k: x15.k + x15.k};1037 local x17 = {k: x16.k + x16.k};1038 local x18 = {k: x17.k + x17.k};1039 local x19 = {k: x18.k + x18.k};1040 local x20 = {k: x19.k + x19.k};1041 local x21 = {k: x20.k + x20.k};1042 x21.k1043 "#1044 );1045 }10461047 // This test is commented out by default, because of huge compilation slowdown1048 // #[bench]1049 // fn bench_codegen(b: &mut Bencher) {1050 // b.iter(|| {1051 // #[allow(clippy::all)]1052 // let stdlib = {1053 // use jrsonnet_parser::*;1054 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1055 // };1056 // stdlib1057 // })1058 // }10591060 /*1061 #[bench]1062 fn bench_serialize(b: &mut Bencher) {1063 b.iter(|| {1064 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1065 env!("OUT_DIR"),1066 "/stdlib.bincode"1067 )))1068 .expect("deserialize stdlib")1069 })1070 }10711072 #[bench]1073 fn bench_parse(b: &mut Bencher) {1074 b.iter(|| {1075 jrsonnet_parser::parse(1076 jrsonnet_stdlib::STDLIB_STR,1077 &jrsonnet_parser::ParserSettings {1078 loc_data: true,1079 file_name: Rc::new(PathBuf::from("std.jsonnet")),1080 },1081 )1082 })1083 }1084 */10851086 #[test]1087 fn equality() {1088 println!(1089 "{:?}",1090 jrsonnet_parser::parse(1091 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1092 &ParserSettings {1093 file_name: PathBuf::from("equality").into(),1094 }1095 )1096 );1097 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1098 }10991100 #[test]1101 fn native_ext() -> crate::error::Result<()> {1102 use super::native::NativeCallback;1103 let evaluator = EvaluationState::default();11041105 evaluator.with_stdlib();11061107 #[derive(Trace)]1108 struct NativeAdd;1109 impl NativeCallbackHandler for NativeAdd {1110 fn call(&self, from: Rc<Path>, args: &[Val]) -> crate::error::Result<Val> {1111 assert_eq!(&from as &Path, &PathBuf::from("native_caller.jsonnet"));1112 match (&args[0], &args[1]) {1113 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1114 (_, _) => unreachable!(),1115 }1116 }1117 }1118 evaluator.settings_mut().ext_natives.insert(1119 "native_add".into(),1120 Cc::new(NativeCallback::new(1121 ParamsDesc(Rc::new(vec![1122 Param("a".into(), None),1123 Param("b".into(), None),1124 ])),1125 TraceBox(Box::new(NativeAdd)),1126 )),1127 );1128 evaluator.evaluate_snippet_raw(1129 PathBuf::from("native_caller.jsonnet").into(),1130 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1131 )?;1132 Ok(())1133 }11341135 #[test]1136 fn constant_intrinsic() -> crate::error::Result<()> {1137 assert_eval!(1138 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1139 );1140 Ok(())1141 }11421143 #[test]1144 fn standalone_super() -> crate::error::Result<()> {1145 assert_eval!(1146 r#"1147 local obj = {1148 a: 1,1149 b: 2,1150 c: 3,1151 };1152 local test = obj + {1153 fields: std.objectFields(super),1154 d: 5,1155 };1156 test.fields == ['a', 'b', 'c']1157 "#1158 );1159 Ok(())1160 }11611162 #[test]1163 fn comp_self() -> crate::error::Result<()> {1164 assert_eval!(1165 r#"1166 std.objectFields({1167 a:{1168 [name]: name for name in std.objectFields(self)1169 },1170 b: 2,1171 c: 3,1172 }.a) == ['a', 'b', 'c']1173 "#1174 );11751176 Ok(())1177 }11781179 struct TestImportResolver(Vec<u8>);1180 impl crate::import::ImportResolver for TestImportResolver {1181 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1182 Ok(PathBuf::from("/test").into())1183 }11841185 fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {1186 Ok(self.0.clone())1187 }11881189 unsafe fn as_any(&self) -> &dyn std::any::Any {1190 panic!()1191 }11921193 fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {1194 panic!()1195 }1196 }11971198 #[test]1199 fn issue_23() {1200 let state = EvaluationState::default();1201 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1202 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1203 }12041205 #[test]1206 fn issue_40() {1207 let state = EvaluationState::default();1208 state.with_stdlib();12091210 let error = state1211 .evaluate_snippet_raw(1212 PathBuf::from("issue40.jsonnet").into(),1213 r#"1214 local conf = {1215 n: ""1216 };12171218 local result = conf + {1219 assert std.isNumber(self.n): "is number"1220 };12211222 std.manifestJsonEx(result, "")1223 "#1224 .into(),1225 )1226 .unwrap_err();1227 assert_eq!(error.error().to_string(), "assert failed: is number");1228 }12291230 #[test]1231 fn test_ascii_upper_lower() {1232 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1233 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1234 }12351236 #[test]1237 fn test_member() {1238 assert_eval!(r#"!std.member("", "")"#);1239 assert_eval!(r#"std.member("abc", "a")"#);1240 assert_eval!(r#"!std.member("abc", "d")"#);1241 assert_eval!(r#"!std.member([], "")"#);1242 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1243 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1244 }12451246 #[test]1247 fn test_count() {1248 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1249 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1250 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1251 }1252}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -186,6 +186,7 @@
#[derive(Debug, Clone, Trace)]
#[force_tracking]
pub enum ArrValue {
+ Bytes(#[skip_trace] Rc<[u8]>),
Lazy(Cc<Vec<LazyVal>>),
Eager(Cc<Vec<Val>>),
Extended(Box<(Self, Self)>),
@@ -197,6 +198,7 @@
pub fn len(&self) -> usize {
match self {
+ Self::Bytes(i) => i.len(),
Self::Lazy(l) => l.len(),
Self::Eager(e) => e.len(),
Self::Extended(v) => v.0.len() + v.1.len(),
@@ -209,6 +211,9 @@
pub fn get(&self, index: usize) -> Result<Option<Val>> {
match self {
+ Self::Bytes(i) => i
+ .get(index)
+ .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),
Self::Lazy(vec) => {
if let Some(v) = vec.get(index) {
Ok(Some(v.evaluate()?))
@@ -230,6 +235,9 @@
pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
match self {
+ Self::Bytes(i) => i
+ .get(index)
+ .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),
Self::Lazy(vec) => vec.get(index).cloned(),
Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
Self::Extended(v) => {
@@ -245,6 +253,13 @@
pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {
Ok(match self {
+ Self::Bytes(i) => {
+ let mut out = Vec::with_capacity(i.len());
+ for v in i.iter() {
+ out.push(Val::Num(*v as f64));
+ }
+ Cc::new(out)
+ }
Self::Lazy(vec) => {
let mut out = Vec::with_capacity(vec.len());
for item in vec.iter() {
@@ -265,6 +280,7 @@
pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
+ Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].evaluate(),
Self::Eager(e) => Ok(e[idx].clone()),
Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
@@ -273,6 +289,7 @@
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
(0..self.len()).map(move |idx| match self {
+ Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
Self::Lazy(l) => l[idx].clone(),
Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
Self::Extended(_) => self.get_lazy(idx).unwrap(),
@@ -281,6 +298,11 @@
pub fn reversed(self) -> Self {
match self {
+ Self::Bytes(b) => {
+ let mut out = b.to_vec();
+ out.reverse();
+ Self::Bytes(out.into())
+ }
Self::Lazy(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -4,10 +4,12 @@
use std::{
borrow::Cow,
cell::RefCell,
+ convert::TryFrom,
fmt::{self, Display},
hash::{BuildHasherDefault, Hash, Hasher},
ops::Deref,
rc::Rc,
+ str::Utf8Error,
};
#[derive(Clone, PartialOrd, Ord, Eq)]
@@ -85,6 +87,15 @@
}
}
+impl TryFrom<&[u8]> for IStr {
+ type Error = Utf8Error;
+
+ fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
+ let str = std::str::from_utf8(value)?;
+ Ok(str.into())
+ }
+}
+
impl From<String> for IStr {
fn from(str: String) -> Self {
(&str as &str).into()
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -306,6 +306,8 @@
Import(PathBuf),
/// importStr "file.txt"
ImportStr(PathBuf),
+ /// importBin "file.txt"
+ ImportBin(PathBuf),
/// error "I'm broken"
ErrorStmt(LocExpr),
/// a(b, c)
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -53,7 +53,7 @@
rule number() -> f64 = quiet!{a:$(uint_str() ("." uint_str())? (['e'|'E'] (s:['+'|'-'])? uint_str())?) {? a.parse().map_err(|_| "<number>") }} / expected!("<number>")
/// Reserved word followed by any non-alphanumberic
- rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
+ rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "importbin" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()
rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")
rule keyword(id: &'static str) -> ()
@@ -218,6 +218,7 @@
/ array_comp_expr(s)
/ keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}
+ / keyword("importbin") _ path:string() {Expr::ImportBin(PathBuf::from(path))}
/ keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}
/ var_expr(s)