difftreelog
Merge remote-tracking branch 'origin/feature/importbin' into gcmodule
in: master
11 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/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,5 +1,5 @@
use crate::function::{CallLocation, StaticBuiltin};
-use crate::typed::{Any, PositiveF64, VecVal, M1};
+use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
equals,
@@ -447,17 +447,15 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {
- Ok(VecVal(
- str.bytes()
- .map(|b| Val::Num(b as f64))
- .collect::<Vec<Val>>(),
- ))
+fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {
+ Ok(Bytes(str.bytes().map(|b| b).collect::<Vec<u8>>().into()))
}
#[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {
- Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)
+fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {
+ Ok(std::str::from_utf8(&arr.0)
+ .map_err(|_| RuntimeError("bad utf8".into()))?
+ .into())
}
#[jrsonnet_macros::builtin]
@@ -483,17 +481,21 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![Vec<u8>, IStr]) -> Result<String> {
+fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {
use Either2::*;
Ok(match input {
- A(a) => base64::encode(a),
+ A(a) => base64::encode(a.0),
B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
})
}
#[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {
- Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)
+fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {
+ Ok(Bytes(
+ base64::decode(&input.as_bytes())
+ .map_err(|_| RuntimeError("bad base64".into()))?
+ .into(),
+ ))
}
#[jrsonnet_macros::builtin]
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
@@ -701,5 +701,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#![warn(clippy::all, clippy::nursery)]2#![allow(3 macro_expanded_macro_exports_accessed_by_absolute_paths,4 clippy::ptr_arg5)]67// For jrsonnet-macros8extern crate self as jrsonnet_evaluator;910mod builtin;11mod ctx;12mod dynamic;13pub mod error;14mod evaluate;15pub mod function;16mod import;17mod integrations;18mod map;19pub mod native;20mod obj;21pub mod trace;22pub mod typed;23mod val;2425pub use jrsonnet_parser as parser;2627pub use ctx::*;28pub use dynamic::*;29use error::{Error::*, LocError, Result, StackTraceElement};30pub use evaluate::*;31use function::{Builtin, CallLocation, TlaArg};32use gc::{GcHashMap, TraceBox};33use gcmodule::{Cc, Trace, Weak};34pub use import::*;35pub use jrsonnet_interner::IStr;36use jrsonnet_parser::*;37pub use obj::*;38use std::{39 cell::{Ref, RefCell, RefMut},40 collections::HashMap,41 fmt::Debug,42 path::{Path, PathBuf},43 rc::Rc,44};45use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};46pub use val::*;47pub mod gc;4849pub trait Bindable: Trace + 'static {50 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;51}5253#[derive(Clone, Trace)]54pub enum LazyBinding {55 Bindable(Cc<TraceBox<dyn Bindable>>),56 Bound(LazyVal),57}5859impl Debug for LazyBinding {60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {61 write!(f, "LazyBinding")62 }63}64impl LazyBinding {65 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {66 match self {67 Self::Bindable(v) => v.bind(this, super_obj),68 Self::Bound(v) => Ok(v.clone()),69 }70 }71}7273pub struct EvaluationSettings {74 /// Limits recursion by limiting the number of stack frames75 pub max_stack: usize,76 /// Limits amount of stack trace items preserved77 pub max_trace: usize,78 /// Used for s`td.extVar`79 pub ext_vars: HashMap<IStr, Val>,80 /// Used for ext.native81 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,82 /// TLA vars83 pub tla_vars: HashMap<IStr, TlaArg>,84 /// Global variables are inserted in default context85 pub globals: HashMap<IStr, Val>,86 /// Used to resolve file locations/contents87 pub import_resolver: Box<dyn ImportResolver>,88 /// Used in manifestification functions89 pub manifest_format: ManifestFormat,90 /// Used for bindings91 pub trace_format: Box<dyn TraceFormat>,92}93impl Default for EvaluationSettings {94 fn default() -> Self {95 Self {96 max_stack: 200,97 max_trace: 20,98 globals: Default::default(),99 ext_vars: Default::default(),100 ext_natives: Default::default(),101 tla_vars: Default::default(),102 import_resolver: Box::new(DummyImportResolver),103 manifest_format: ManifestFormat::Json(4),104 trace_format: Box::new(CompactFormat {105 padding: 4,106 resolver: trace::PathResolver::Absolute,107 }),108 }109 }110}111112#[derive(Default)]113struct EvaluationData {114 /// Used for stack overflow detection, stacktrace is populated on unwind115 stack_depth: usize,116 /// Updated every time stack entry is popt117 stack_generation: usize,118119 breakpoints: Breakpoints,120 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces121 files: HashMap<Rc<Path>, FileData>,122 str_files: HashMap<Rc<Path>, IStr>,123}124125pub struct FileData {126 source_code: IStr,127 parsed: LocExpr,128 evaluated: Option<Val>,129}130131#[allow(clippy::type_complexity)]132pub struct Breakpoint {133 loc: ExprLocation,134 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,135}136#[derive(Default)]137struct Breakpoints(Vec<Rc<Breakpoint>>);138impl Breakpoints {139 fn insert(140 &self,141 stack_depth: usize,142 stack_generation: usize,143 loc: &ExprLocation,144 result: Result<Val>,145 ) -> Result<Val> {146 if self.0.is_empty() {147 return result;148 }149 for item in self.0.iter() {150 if item.loc.belongs_to(loc) {151 let mut collected = item.collected.borrow_mut();152 let (depth, vals) = collected.entry(stack_generation).or_default();153 if stack_depth > *depth {154 vals.clear();155 }156 vals.push(result.clone());157 }158 }159 result160 }161}162163#[derive(Default)]164pub struct EvaluationStateInternals {165 /// Internal state166 data: RefCell<EvaluationData>,167 /// Settings, safe to change at runtime168 settings: RefCell<EvaluationSettings>,169}170171thread_local! {172 /// Contains the state for a currently executed file.173 /// Global state is fine here.174 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)175}176177pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {178 EVAL_STATE.with(|s| {179 f(s.borrow().as_ref().expect(180 "missing evaluation state, some functions should be called inside of run_in_state call",181 ))182 })183}184pub fn push_frame<T>(185 e: CallLocation,186 frame_desc: impl FnOnce() -> String,187 f: impl FnOnce() -> Result<T>,188) -> Result<T> {189 with_state(|s| s.push(e, frame_desc, f))190}191192#[allow(dead_code)]193pub fn push_val_frame(194 e: &ExprLocation,195 frame_desc: impl FnOnce() -> String,196 f: impl FnOnce() -> Result<Val>,197) -> Result<Val> {198 with_state(|s| s.push_val(e, frame_desc, f))199}200#[allow(dead_code)]201pub fn push_description_frame<T>(202 frame_desc: impl FnOnce() -> String,203 f: impl FnOnce() -> Result<T>,204) -> Result<T> {205 with_state(|s| s.push_description(frame_desc, f))206}207208/// Maintains stack trace and import resolution209#[derive(Default, Clone)]210pub struct EvaluationState(Rc<EvaluationStateInternals>);211212impl EvaluationState {213 /// Parses and adds file as loaded214 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {215 let parsed = parse(216 &source_code,217 &ParserSettings {218 file_name: path.clone(),219 },220 )221 .map_err(|error| ImportSyntaxError {222 error: Box::new(error),223 path: path.to_owned(),224 source_code: source_code.clone(),225 })?;226 self.add_parsed_file(path, source_code, parsed.clone())?;227228 Ok(parsed)229 }230231 pub fn reset_evaluation_state(&self, name: &Path) {232 self.data_mut()233 .files234 .get_mut(name)235 .unwrap()236 .evaluated237 .take();238 }239240 /// Adds file by source code and parsed expr241 pub fn add_parsed_file(242 &self,243 name: Rc<Path>,244 source_code: IStr,245 parsed: LocExpr,246 ) -> Result<()> {247 self.data_mut().files.insert(248 name,249 FileData {250 source_code,251 parsed,252 evaluated: None,253 },254 );255256 Ok(())257 }258 pub fn get_source(&self, name: &Path) -> Option<IStr> {259 let ro_map = &self.data().files;260 ro_map.get(name).map(|value| value.source_code.clone())261 }262 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {263 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)264 }265 pub fn map_from_source_location(266 &self,267 file: &Path,268 line: usize,269 column: usize,270 ) -> Option<usize> {271 location_to_offset(&self.get_source(file).unwrap(), line, column)272 }273 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {274 let file_path = self.resolve_file(from, path)?;275 {276 let data = self.data();277 let files = &data.files;278 if files.contains_key(&file_path as &Path) {279 drop(data);280 return self.evaluate_loaded_file_raw(&file_path);281 }282 }283 let contents = self.load_file_contents(&file_path)?;284 self.add_file(file_path.clone(), contents)?;285 self.evaluate_loaded_file_raw(&file_path)286 }287 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {288 let path = self.resolve_file(from, path)?;289 if !self.data().str_files.contains_key(&path) {290 let file_str = self.load_file_contents(&path)?;291 self.data_mut().str_files.insert(path.clone(), file_str);292 }293 Ok(self.data().str_files.get(&path).cloned().unwrap())294 }295296 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {297 let expr: LocExpr = {298 let ro_map = &self.data().files;299 let value = ro_map300 .get(name)301 .unwrap_or_else(|| panic!("file not added: {:?}", name));302 if let Some(ref evaluated) = value.evaluated {303 return Ok(evaluated.clone());304 }305 value.parsed.clone()306 };307 let value = evaluate(self.create_default_context(), &expr)?;308 {309 self.data_mut()310 .files311 .get_mut(name)312 .unwrap()313 .evaluated314 .replace(value.clone());315 }316 Ok(value)317 }318319 /// Adds standard library global variable (std) to this evaluator320 pub fn with_stdlib(&self) -> &Self {321 use jrsonnet_stdlib::STDLIB_STR;322 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();323 self.run_in_state(|| {324 self.add_parsed_file(325 std_path.clone(),326 STDLIB_STR.to_owned().into(),327 builtin::get_parsed_stdlib(),328 )329 .unwrap();330 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();331 self.settings_mut().globals.insert("std".into(), val);332 });333 self334 }335336 /// Creates context with all passed global variables337 pub fn create_default_context(&self) -> Context {338 let globals = &self.settings().globals;339 let mut new_bindings = GcHashMap::with_capacity(globals.len());340 for (name, value) in globals.iter() {341 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));342 }343 Context::new().extend_bound(new_bindings)344 }345346 /// Executes code creating a new stack frame347 pub fn push<T>(348 &self,349 e: CallLocation,350 frame_desc: impl FnOnce() -> String,351 f: impl FnOnce() -> Result<T>,352 ) -> Result<T> {353 {354 let mut data = self.data_mut();355 let stack_depth = &mut data.stack_depth;356 if *stack_depth > self.max_stack() {357 // Error creation uses data, so i drop guard here358 drop(data);359 throw!(StackOverflow);360 } else {361 *stack_depth += 1;362 }363 }364 let result = f();365 {366 let mut data = self.data_mut();367 data.stack_depth -= 1;368 data.stack_generation += 1;369 }370 if let Err(mut err) = result {371 err.trace_mut().0.push(StackTraceElement {372 location: e.0.cloned(),373 desc: frame_desc(),374 });375 return Err(err);376 }377 result378 }379380 /// Executes code creating a new stack frame381 pub fn push_val(382 &self,383 e: &ExprLocation,384 frame_desc: impl FnOnce() -> String,385 f: impl FnOnce() -> Result<Val>,386 ) -> Result<Val> {387 {388 let mut data = self.data_mut();389 let stack_depth = &mut data.stack_depth;390 if *stack_depth > self.max_stack() {391 // Error creation uses data, so i drop guard here392 drop(data);393 throw!(StackOverflow);394 } else {395 *stack_depth += 1;396 }397 }398 let mut result = f();399 {400 let mut data = self.data_mut();401 data.stack_depth -= 1;402 data.stack_generation += 1;403 result = data404 .breakpoints405 .insert(data.stack_depth, data.stack_generation, e, result);406 }407 if let Err(mut err) = result {408 err.trace_mut().0.push(StackTraceElement {409 location: Some(e.clone()),410 desc: frame_desc(),411 });412 return Err(err);413 }414 result415 }416 /// Executes code creating a new stack frame417 pub fn push_description<T>(418 &self,419 frame_desc: impl FnOnce() -> String,420 f: impl FnOnce() -> Result<T>,421 ) -> Result<T> {422 {423 let mut data = self.data_mut();424 let stack_depth = &mut data.stack_depth;425 if *stack_depth > self.max_stack() {426 // Error creation uses data, so i drop guard here427 drop(data);428 throw!(StackOverflow);429 } else {430 *stack_depth += 1;431 }432 }433 let result = f();434 {435 let mut data = self.data_mut();436 data.stack_depth -= 1;437 data.stack_generation += 1;438 }439 if let Err(mut err) = result {440 err.trace_mut().0.push(StackTraceElement {441 location: None,442 desc: frame_desc(),443 });444 return Err(err);445 }446 result447 }448449 /// Runs passed function in state (required if function needs to modify stack trace)450 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {451 EVAL_STATE.with(|v| {452 let has_state = v.borrow().is_some();453 if !has_state {454 v.borrow_mut().replace(self.clone());455 }456 let result = f();457 if !has_state {458 v.borrow_mut().take();459 }460 result461 })462 }463 pub fn run_in_state_with_breakpoint(464 &self,465 bp: Rc<Breakpoint>,466 f: impl FnOnce() -> Result<()>,467 ) -> Result<()> {468 {469 let mut data = self.data_mut();470 data.breakpoints.0.push(bp);471 }472473 let result = self.run_in_state(f);474475 {476 let mut data = self.data_mut();477 data.breakpoints.0.pop();478 }479480 result481 }482483 pub fn stringify_err(&self, e: &LocError) -> String {484 let mut out = String::new();485 self.settings()486 .trace_format487 .write_trace(&mut out, self, e)488 .unwrap();489 out490 }491492 pub fn manifest(&self, val: Val) -> Result<IStr> {493 self.run_in_state(|| {494 push_description_frame(495 || "manifestification".to_string(),496 || val.manifest(&self.manifest_format()),497 )498 })499 }500 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {501 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))502 }503 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {504 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))505 }506507 /// If passed value is function then call with set TLA508 pub fn with_tla(&self, val: Val) -> Result<Val> {509 self.run_in_state(|| {510 Ok(match val {511 Val::Func(func) => push_description_frame(512 || "during TLA call".to_owned(),513 || {514 func.evaluate(515 self.create_default_context(),516 CallLocation::native(),517 &self.settings().tla_vars,518 true,519 )520 },521 )?,522 v => v,523 })524 })525 }526}527528/// Internals529impl EvaluationState {530 fn data(&self) -> Ref<EvaluationData> {531 self.0.data.borrow()532 }533 fn data_mut(&self) -> RefMut<EvaluationData> {534 self.0.data.borrow_mut()535 }536 pub fn settings(&self) -> Ref<EvaluationSettings> {537 self.0.settings.borrow()538 }539 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {540 self.0.settings.borrow_mut()541 }542}543544/// Raw methods evaluate passed values but don't perform TLA execution545impl EvaluationState {546 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {547 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))548 }549 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {550 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))551 }552 /// Parses and evaluates the given snippet553 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {554 let parsed = parse(555 &code,556 &ParserSettings {557 file_name: source.clone(),558 },559 )560 .map_err(|e| ImportSyntaxError {561 path: source.clone(),562 source_code: code.clone(),563 error: Box::new(e),564 })?;565 self.add_parsed_file(source, code, parsed.clone())?;566 self.evaluate_expr_raw(parsed)567 }568 /// Evaluates the parsed expression569 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {570 self.run_in_state(|| evaluate(self.create_default_context(), &code))571 }572}573574/// Settings utilities575impl EvaluationState {576 pub fn add_ext_var(&self, name: IStr, value: Val) {577 self.settings_mut().ext_vars.insert(name, value);578 }579 pub fn add_ext_str(&self, name: IStr, value: IStr) {580 self.add_ext_var(name, Val::Str(value));581 }582 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {583 let value =584 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;585 self.add_ext_var(name, value);586 Ok(())587 }588589 pub fn add_tla(&self, name: IStr, value: Val) {590 self.settings_mut()591 .tla_vars592 .insert(name, TlaArg::Val(value));593 }594 pub fn add_tla_str(&self, name: IStr, value: IStr) {595 self.settings_mut()596 .tla_vars597 .insert(name, TlaArg::String(value));598 }599 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {600 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;601 self.settings_mut()602 .tla_vars603 .insert(name, TlaArg::Code(parsed));604 Ok(())605 }606607 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {608 self.settings().import_resolver.resolve_file(from, path)609 }610 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {611 self.settings().import_resolver.load_file_contents(path)612 }613614 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {615 Ref::map(self.settings(), |s| &*s.import_resolver)616 }617 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {618 self.settings_mut().import_resolver = resolver;619 }620621 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {622 self.settings_mut().ext_natives.insert(name, cb);623 }624625 pub fn manifest_format(&self) -> ManifestFormat {626 self.settings().manifest_format.clone()627 }628 pub fn set_manifest_format(&self, format: ManifestFormat) {629 self.settings_mut().manifest_format = format;630 }631632 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {633 Ref::map(self.settings(), |s| &*s.trace_format)634 }635 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {636 self.settings_mut().trace_format = format;637 }638639 pub fn max_trace(&self) -> usize {640 self.settings().max_trace641 }642 pub fn set_max_trace(&self, trace: usize) {643 self.settings_mut().max_trace = trace;644 }645646 pub fn max_stack(&self) -> usize {647 self.settings().max_stack648 }649 pub fn set_max_stack(&self, trace: usize) {650 self.settings_mut().max_stack = trace;651 }652}653654pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {655 let a = a as &T;656 let b = b as &T;657 std::ptr::eq(a, b)658}659660fn weak_raw<T>(a: Weak<T>) -> *const () {661 unsafe { std::mem::transmute(a) }662}663fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {664 std::ptr::eq(weak_raw(a), weak_raw(b))665}666667#[test]668fn weak_unsafe() {669 let a = Cc::new(1);670 let b = Cc::new(2);671672 let aw1 = a.clone().downgrade();673 let aw2 = a.clone().downgrade();674 let aw3 = a.clone().downgrade();675676 let bw = b.clone().downgrade();677678 assert!(weak_ptr_eq(aw1, aw2));679 assert!(!weak_ptr_eq(aw3, bw));680}681682#[cfg(test)]683pub mod tests {684 use super::Val;685 use crate::{686 error::Error::*, function::BuiltinParam, gc::TraceBox, native::NativeCallbackHandler,687 primitive_equals, EvaluationState,688 };689 use gcmodule::{Cc, Trace};690 use jrsonnet_interner::IStr;691 use jrsonnet_parser::*;692 use std::{693 path::{Path, PathBuf},694 rc::Rc,695 };696697 #[test]698 #[should_panic]699 fn eval_state_stacktrace() {700 let state = EvaluationState::default();701 state.run_in_state(|| {702 state703 .push(704 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),705 || "outer".to_owned(),706 || {707 state.push(708 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),709 || "inner".to_owned(),710 || Err(RuntimeError("".into()).into()),711 )?;712 Ok(Val::Null)713 },714 )715 .unwrap();716 });717 }718719 #[test]720 fn eval_state_standard() {721 let state = EvaluationState::default();722 state.with_stdlib();723 assert!(primitive_equals(724 &state725 .evaluate_snippet_raw(726 PathBuf::from("raw.jsonnet").into(),727 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()728 )729 .unwrap(),730 &Val::Bool(true),731 )732 .unwrap());733 }734735 macro_rules! eval {736 ($str: expr) => {{737 let evaluator = EvaluationState::default();738 evaluator.with_stdlib();739 evaluator.run_in_state(|| {740 evaluator741 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())742 .unwrap()743 })744 }};745 }746 macro_rules! eval_json {747 ($str: expr) => {{748 let evaluator = EvaluationState::default();749 evaluator.with_stdlib();750 evaluator.run_in_state(|| {751 evaluator752 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())753 .unwrap()754 .to_json(0)755 .unwrap()756 .replace("\n", "")757 })758 }};759 }760761 /// Asserts given code returns `true`762 macro_rules! assert_eval {763 ($str: expr) => {764 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())765 };766 }767768 /// Asserts given code returns `false`769 macro_rules! assert_eval_neg {770 ($str: expr) => {771 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())772 };773 }774 macro_rules! assert_json {775 ($str: expr, $out: expr) => {776 assert_eq!(eval_json!($str), $out.replace("\t", ""))777 };778 }779780 /// Sanity checking, before trusting to another tests781 #[test]782 fn equality_operator() {783 assert_eval!("2 == 2");784 assert_eval_neg!("2 != 2");785 assert_eval!("2 != 3");786 assert_eval_neg!("2 == 3");787 assert_eval!("'Hello' == 'Hello'");788 assert_eval_neg!("'Hello' != 'Hello'");789 assert_eval!("'Hello' != 'World'");790 assert_eval_neg!("'Hello' == 'World'");791 }792793 #[test]794 fn math_evaluation() {795 assert_eval!("2 + 2 * 2 == 6");796 assert_eval!("3 + (2 + 2 * 2) == 9");797 }798799 #[test]800 fn string_concat() {801 assert_eval!("'Hello' + 'World' == 'HelloWorld'");802 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");803 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");804 }805806 #[test]807 fn faster_join() {808 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");809 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");810 }811812 #[test]813 fn function_contexts() {814 assert_eval!(815 r#"816 local k = {817 t(name = self.h): [self.h, name],818 h: 3,819 };820 local f = {821 t: k.t(),822 h: 4,823 };824 f.t[0] == f.t[1]825 "#826 );827 }828829 #[test]830 fn local() {831 assert_eval!("local a = 2; local b = 3; a + b == 5");832 assert_eval!("local a = 1, b = a + 1; a + b == 3");833 assert_eval!("local a = 1; local a = 2; a == 2");834 }835836 #[test]837 fn object_lazyness() {838 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);839 }840841 #[test]842 fn object_inheritance() {843 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);844 }845846 #[test]847 fn object_assertion_success() {848 eval!("{assert \"a\" in self} + {a:2}");849 }850851 #[test]852 fn object_assertion_error() {853 eval!("{assert \"a\" in self}");854 }855856 #[test]857 fn lazy_args() {858 eval!("local test(a) = 2; test(error '3')");859 }860861 #[test]862 #[should_panic]863 fn tailstrict_args() {864 eval!("local test(a) = 2; test(error '3') tailstrict");865 }866867 #[test]868 #[should_panic]869 fn no_binding_error() {870 eval!("a");871 }872873 #[test]874 fn test_object() {875 assert_json!("{a:2}", r#"{"a": 2}"#);876 assert_json!("{a:2+2}", r#"{"a": 4}"#);877 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);878 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);879 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);880 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);881 assert_json!(882 r#"883 {884 name: "Alice",885 welcome: "Hello " + self.name + "!",886 }887 "#,888 r#"{"name": "Alice","welcome": "Hello Alice!"}"#889 );890 assert_json!(891 r#"892 {893 name: "Alice",894 welcome: "Hello " + self.name + "!",895 } + {896 name: "Bob"897 }898 "#,899 r#"{"name": "Bob","welcome": "Hello Bob!"}"#900 );901 }902903 #[test]904 fn functions() {905 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");906 assert_json!(907 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,908 r#""HelloDearWorld""#909 );910 }911912 #[test]913 fn local_methods() {914 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");915 assert_json!(916 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,917 r#""HelloDearWorld""#918 );919 }920921 #[test]922 fn object_locals() {923 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);924 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);925 assert_json!(926 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,927 r#"{"test": {"test": 4}}"#928 );929 }930931 #[test]932 fn object_comp() {933 assert_json!(934 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}"#,935 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"936 )937 }938939 #[test]940 fn direct_self() {941 println!(942 "{:#?}",943 eval!(944 r#"945 {946 local me = self,947 a: 3,948 b(): me.a,949 }950 "#951 )952 );953 }954955 #[test]956 fn indirect_self() {957 // `self` assigned to `me` was lost when being958 // referenced from field959 eval!(960 r#"{961 local me = self,962 a: 3,963 b: me.a,964 }.b"#965 );966 }967968 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly969 #[test]970 fn std_assert_ok() {971 eval!("std.assertEqual(4.5 << 2, 16)");972 }973974 #[test]975 #[should_panic]976 fn std_assert_failure() {977 eval!("std.assertEqual(4.5 << 2, 15)");978 }979980 #[test]981 fn string_is_string() {982 assert!(primitive_equals(983 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),984 &Val::Bool(false),985 )986 .unwrap());987 }988989 #[test]990 fn base64_works() {991 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);992 }993994 #[test]995 fn utf8_chars() {996 assert_json!(997 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,998 r#"{"c": 128526,"l": 1}"#999 )1000 }10011002 #[test]1003 fn json() {1004 assert_json!(1005 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,1006 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#1007 );1008 }10091010 #[test]1011 fn json_minified() {1012 assert_json!(1013 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,1014 r#""{\"a\":3,\"b\":4,\"c\":6}""#1015 );1016 }10171018 #[test]1019 fn parse_json() {1020 assert_json!(1021 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,1022 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#1023 );1024 }10251026 #[test]1027 fn test() {1028 assert_json!(1029 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1030 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1031 );1032 }10331034 #[test]1035 fn sjsonnet() {1036 eval!(1037 r#"1038 local x0 = {k: 1};1039 local x1 = {k: x0.k + x0.k};1040 local x2 = {k: x1.k + x1.k};1041 local x3 = {k: x2.k + x2.k};1042 local x4 = {k: x3.k + x3.k};1043 local x5 = {k: x4.k + x4.k};1044 local x6 = {k: x5.k + x5.k};1045 local x7 = {k: x6.k + x6.k};1046 local x8 = {k: x7.k + x7.k};1047 local x9 = {k: x8.k + x8.k};1048 local x10 = {k: x9.k + x9.k};1049 local x11 = {k: x10.k + x10.k};1050 local x12 = {k: x11.k + x11.k};1051 local x13 = {k: x12.k + x12.k};1052 local x14 = {k: x13.k + x13.k};1053 local x15 = {k: x14.k + x14.k};1054 local x16 = {k: x15.k + x15.k};1055 local x17 = {k: x16.k + x16.k};1056 local x18 = {k: x17.k + x17.k};1057 local x19 = {k: x18.k + x18.k};1058 local x20 = {k: x19.k + x19.k};1059 local x21 = {k: x20.k + x20.k};1060 x21.k1061 "#1062 );1063 }10641065 // This test is commented out by default, because of huge compilation slowdown1066 // #[bench]1067 // fn bench_codegen(b: &mut Bencher) {1068 // b.iter(|| {1069 // #[allow(clippy::all)]1070 // let stdlib = {1071 // use jrsonnet_parser::*;1072 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1073 // };1074 // stdlib1075 // })1076 // }10771078 /*1079 #[bench]1080 fn bench_serialize(b: &mut Bencher) {1081 b.iter(|| {1082 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1083 env!("OUT_DIR"),1084 "/stdlib.bincode"1085 )))1086 .expect("deserialize stdlib")1087 })1088 }10891090 #[bench]1091 fn bench_parse(b: &mut Bencher) {1092 b.iter(|| {1093 jrsonnet_parser::parse(1094 jrsonnet_stdlib::STDLIB_STR,1095 &jrsonnet_parser::ParserSettings {1096 loc_data: true,1097 file_name: Rc::new(PathBuf::from("std.jsonnet")),1098 },1099 )1100 })1101 }1102 */11031104 #[test]1105 fn equality() {1106 println!(1107 "{:?}",1108 jrsonnet_parser::parse(1109 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1110 &ParserSettings {1111 file_name: PathBuf::from("equality").into(),1112 }1113 )1114 );1115 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1116 }11171118 #[test]1119 fn native_ext() -> crate::error::Result<()> {1120 use super::native::NativeCallback;1121 let evaluator = EvaluationState::default();11221123 evaluator.with_stdlib();11241125 #[derive(Trace)]1126 struct NativeAdd;1127 impl NativeCallbackHandler for NativeAdd {1128 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {1129 assert_eq!(1130 &from.unwrap() as &Path,1131 &PathBuf::from("native_caller.jsonnet")1132 );1133 match (&args[0], &args[1]) {1134 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1135 (_, _) => unreachable!(),1136 }1137 }1138 }1139 evaluator.settings_mut().ext_natives.insert(1140 "native_add".into(),1141 #[allow(deprecated)]1142 Cc::new(TraceBox(Box::new(NativeCallback::new(1143 vec![1144 BuiltinParam {1145 name: "a".into(),1146 has_default: false,1147 },1148 BuiltinParam {1149 name: "b".into(),1150 has_default: false,1151 },1152 ],1153 TraceBox(Box::new(NativeAdd)),1154 )))),1155 );1156 evaluator.evaluate_snippet_raw(1157 PathBuf::from("native_caller.jsonnet").into(),1158 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1159 )?;1160 Ok(())1161 }11621163 #[test]1164 fn constant_intrinsic() -> crate::error::Result<()> {1165 assert_eval!(1166 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1167 );1168 Ok(())1169 }11701171 #[test]1172 fn standalone_super() -> crate::error::Result<()> {1173 assert_eval!(1174 r#"1175 local obj = {1176 a: 1,1177 b: 2,1178 c: 3,1179 };1180 local test = obj + {1181 fields: std.objectFields(super),1182 d: 5,1183 };1184 test.fields == ['a', 'b', 'c']1185 "#1186 );1187 Ok(())1188 }11891190 #[test]1191 fn comp_self() -> crate::error::Result<()> {1192 assert_eval!(1193 r#"1194 std.objectFields({1195 a:{1196 [name]: name for name in std.objectFields(self)1197 },1198 b: 2,1199 c: 3,1200 }.a) == ['a', 'b', 'c']1201 "#1202 );12031204 Ok(())1205 }12061207 struct TestImportResolver(IStr);1208 impl crate::import::ImportResolver for TestImportResolver {1209 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1210 Ok(PathBuf::from("/test").into())1211 }12121213 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1214 Ok(self.0.clone())1215 }12161217 unsafe fn as_any(&self) -> &dyn std::any::Any {1218 panic!()1219 }1220 }12211222 #[test]1223 fn issue_23() {1224 let state = EvaluationState::default();1225 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1226 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1227 }12281229 #[test]1230 fn issue_40() {1231 let state = EvaluationState::default();1232 state.with_stdlib();12331234 let error = state1235 .evaluate_snippet_raw(1236 PathBuf::from("issue40.jsonnet").into(),1237 r#"1238 local conf = {1239 n: ""1240 };12411242 local result = conf + {1243 assert std.isNumber(self.n): "is number"1244 };12451246 std.manifestJsonEx(result, "")1247 "#1248 .into(),1249 )1250 .unwrap_err();1251 assert_eq!(error.error().to_string(), "assert failed: is number");1252 }12531254 #[test]1255 fn test_ascii_upper_lower() {1256 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1257 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1258 }12591260 #[test]1261 fn test_member() {1262 assert_eval!(r#"!std.member("", "")"#);1263 assert_eval!(r#"std.member("abc", "a")"#);1264 assert_eval!(r#"!std.member("abc", "d")"#);1265 assert_eval!(r#"!std.member([], "")"#);1266 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1267 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1268 }12691270 #[test]1271 fn test_count() {1272 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1273 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1274 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1275 }12761277 mod derive_typed {1278 use crate::{typed::Typed, EvaluationState};1279 use std::path::PathBuf;12801281 #[derive(Typed, PartialEq, Debug)]1282 struct MyTyped {1283 a: u32,1284 #[typed(rename = "b")]1285 c: String,1286 }12871288 #[test]1289 fn test() {1290 let es = EvaluationState::default();1291 let val = eval!("{a: 14, b: 'Hello, world!'}");1292 let typed = es.run_in_state(|| MyTyped::try_from(val).unwrap());12931294 assert_eq!(1295 typed,1296 MyTyped {1297 a: 14,1298 c: "Hello, world!".to_string()1299 }1300 );1301 es.settings_mut().globals.insert(1302 "mytyped".into(),1303 es.run_in_state(|| typed.try_into()).unwrap(),1304 );13051306 let v = es1307 .evaluate_snippet_raw(1308 PathBuf::from("raw.jsonnet").into(),1309 "1310 mytyped == {a: 14, b: 'Hello, world!'}1311 "1312 .into(),1313 )1314 .unwrap()1315 .as_bool()1316 .unwrap();1317 assert!(v)1318 }1319 }1320}1#![warn(clippy::all, clippy::nursery)]2#![allow(3 macro_expanded_macro_exports_accessed_by_absolute_paths,4 clippy::ptr_arg5)]67// For jrsonnet-macros8extern crate self as jrsonnet_evaluator;910mod builtin;11mod ctx;12mod dynamic;13pub mod error;14mod evaluate;15pub mod function;16mod import;17mod integrations;18mod map;19pub mod native;20mod obj;21pub mod trace;22pub mod typed;23mod val;2425pub use jrsonnet_parser as parser;2627pub use ctx::*;28pub use dynamic::*;29use error::{Error::*, LocError, Result, StackTraceElement};30pub use evaluate::*;31use function::{Builtin, CallLocation, TlaArg};32use gc::{GcHashMap, TraceBox};33use gcmodule::{Cc, Trace, Weak};34pub use import::*;35pub use jrsonnet_interner::IStr;36use jrsonnet_parser::*;37pub use obj::*;38use std::{39 cell::{Ref, RefCell, RefMut},40 collections::HashMap,41 fmt::Debug,42 path::{Path, PathBuf},43 rc::Rc,44};45use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};46pub use val::*;47pub mod gc;4849pub trait Bindable: Trace + 'static {50 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;51}5253#[derive(Clone, Trace)]54pub enum LazyBinding {55 Bindable(Cc<TraceBox<dyn Bindable>>),56 Bound(LazyVal),57}5859impl Debug for LazyBinding {60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {61 write!(f, "LazyBinding")62 }63}64impl LazyBinding {65 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {66 match self {67 Self::Bindable(v) => v.bind(this, super_obj),68 Self::Bound(v) => Ok(v.clone()),69 }70 }71}7273pub struct EvaluationSettings {74 /// Limits recursion by limiting the number of stack frames75 pub max_stack: usize,76 /// Limits amount of stack trace items preserved77 pub max_trace: usize,78 /// Used for s`td.extVar`79 pub ext_vars: HashMap<IStr, Val>,80 /// Used for ext.native81 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,82 /// TLA vars83 pub tla_vars: HashMap<IStr, TlaArg>,84 /// Global variables are inserted in default context85 pub globals: HashMap<IStr, Val>,86 /// Used to resolve file locations/contents87 pub import_resolver: Box<dyn ImportResolver>,88 /// Used in manifestification functions89 pub manifest_format: ManifestFormat,90 /// Used for bindings91 pub trace_format: Box<dyn TraceFormat>,92}93impl Default for EvaluationSettings {94 fn default() -> Self {95 Self {96 max_stack: 200,97 max_trace: 20,98 globals: Default::default(),99 ext_vars: Default::default(),100 ext_natives: Default::default(),101 tla_vars: Default::default(),102 import_resolver: Box::new(DummyImportResolver),103 manifest_format: ManifestFormat::Json(4),104 trace_format: Box::new(CompactFormat {105 padding: 4,106 resolver: trace::PathResolver::Absolute,107 }),108 }109 }110}111112#[derive(Default)]113struct EvaluationData {114 /// Used for stack overflow detection, stacktrace is populated on unwind115 stack_depth: usize,116 /// Updated every time stack entry is popt117 stack_generation: usize,118119 breakpoints: Breakpoints,120 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces121 files: GcHashMap<Rc<Path>, FileData>,122 str_files: GcHashMap<Rc<Path>, IStr>,123 bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,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}177178pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {179 EVAL_STATE.with(|s| {180 f(s.borrow().as_ref().expect(181 "missing evaluation state, some functions should be called inside of run_in_state call",182 ))183 })184}185pub fn push_frame<T>(186 e: CallLocation,187 frame_desc: impl FnOnce() -> String,188 f: impl FnOnce() -> Result<T>,189) -> Result<T> {190 with_state(|s| s.push(e, frame_desc, f))191}192193#[allow(dead_code)]194pub fn push_val_frame(195 e: &ExprLocation,196 frame_desc: impl FnOnce() -> String,197 f: impl FnOnce() -> Result<Val>,198) -> Result<Val> {199 with_state(|s| s.push_val(e, frame_desc, f))200}201#[allow(dead_code)]202pub fn push_description_frame<T>(203 frame_desc: impl FnOnce() -> String,204 f: impl FnOnce() -> Result<T>,205) -> Result<T> {206 with_state(|s| s.push_description(frame_desc, f))207}208209/// Maintains stack trace and import resolution210#[derive(Default, Clone)]211pub struct EvaluationState(Rc<EvaluationStateInternals>);212213impl EvaluationState {214 /// Parses and adds file as loaded215 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {216 let parsed = parse(217 &source_code,218 &ParserSettings {219 file_name: path.clone(),220 },221 )222 .map_err(|error| ImportSyntaxError {223 error: Box::new(error),224 path: path.to_owned(),225 source_code: source_code.clone(),226 })?;227 self.add_parsed_file(path, source_code, parsed.clone())?;228229 Ok(parsed)230 }231232 pub fn reset_evaluation_state(&self, name: &Path) {233 self.data_mut()234 .files235 .get_mut(name)236 .unwrap()237 .evaluated238 .take();239 }240241 /// Adds file by source code and parsed expr242 pub fn add_parsed_file(243 &self,244 name: Rc<Path>,245 source_code: IStr,246 parsed: LocExpr,247 ) -> Result<()> {248 self.data_mut().files.insert(249 name,250 FileData {251 source_code,252 parsed,253 evaluated: None,254 },255 );256257 Ok(())258 }259 pub fn get_source(&self, name: &Path) -> Option<IStr> {260 let ro_map = &self.data().files;261 ro_map.get(name).map(|value| value.source_code.clone())262 }263 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {264 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)265 }266 pub fn map_from_source_location(267 &self,268 file: &Path,269 line: usize,270 column: usize,271 ) -> Option<usize> {272 location_to_offset(&self.get_source(file).unwrap(), line, column)273 }274 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {275 let file_path = self.resolve_file(from, path)?;276 {277 let data = self.data();278 let files = &data.files;279 if files.contains_key(&file_path as &Path) {280 drop(data);281 return self.evaluate_loaded_file_raw(&file_path);282 }283 }284 let contents = self.load_file_str(&file_path)?;285 self.add_file(file_path.clone(), contents)?;286 self.evaluate_loaded_file_raw(&file_path)287 }288 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {289 let path = self.resolve_file(from, path)?;290 if !self.data().str_files.contains_key(&path) {291 let file_str = self.load_file_str(&path)?;292 self.data_mut().str_files.insert(path.clone(), file_str);293 }294 Ok(self.data().str_files.get(&path).cloned().unwrap())295 }296 pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {297 let path = self.resolve_file(from, path)?;298 if !self.data().bin_files.contains_key(&path) {299 let file_bin = self.load_file_bin(&path)?;300 self.data_mut().bin_files.insert(path.clone(), file_bin);301 }302 Ok(self.data().bin_files.get(&path).cloned().unwrap())303 }304305 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {306 let expr: LocExpr = {307 let ro_map = &self.data().files;308 let value = ro_map309 .get(name)310 .unwrap_or_else(|| panic!("file not added: {:?}", name));311 if let Some(ref evaluated) = value.evaluated {312 return Ok(evaluated.clone());313 }314 value.parsed.clone()315 };316 let value = evaluate(self.create_default_context(), &expr)?;317 {318 self.data_mut()319 .files320 .get_mut(name)321 .unwrap()322 .evaluated323 .replace(value.clone());324 }325 Ok(value)326 }327328 /// Adds standard library global variable (std) to this evaluator329 pub fn with_stdlib(&self) -> &Self {330 use jrsonnet_stdlib::STDLIB_STR;331 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();332 self.run_in_state(|| {333 self.add_parsed_file(334 std_path.clone(),335 STDLIB_STR.to_owned().into(),336 builtin::get_parsed_stdlib(),337 )338 .unwrap();339 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();340 self.settings_mut().globals.insert("std".into(), val);341 });342 self343 }344345 /// Creates context with all passed global variables346 pub fn create_default_context(&self) -> Context {347 let globals = &self.settings().globals;348 let mut new_bindings = GcHashMap::with_capacity(globals.len());349 for (name, value) in globals.iter() {350 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));351 }352 Context::new().extend_bound(new_bindings)353 }354355 /// Executes code creating a new stack frame356 pub fn push<T>(357 &self,358 e: CallLocation,359 frame_desc: impl FnOnce() -> String,360 f: impl FnOnce() -> Result<T>,361 ) -> Result<T> {362 {363 let mut data = self.data_mut();364 let stack_depth = &mut data.stack_depth;365 if *stack_depth > self.max_stack() {366 // Error creation uses data, so i drop guard here367 drop(data);368 throw!(StackOverflow);369 } else {370 *stack_depth += 1;371 }372 }373 let result = f();374 {375 let mut data = self.data_mut();376 data.stack_depth -= 1;377 data.stack_generation += 1;378 }379 if let Err(mut err) = result {380 err.trace_mut().0.push(StackTraceElement {381 location: e.0.cloned(),382 desc: frame_desc(),383 });384 return Err(err);385 }386 result387 }388389 /// Executes code creating a new stack frame390 pub fn push_val(391 &self,392 e: &ExprLocation,393 frame_desc: impl FnOnce() -> String,394 f: impl FnOnce() -> Result<Val>,395 ) -> Result<Val> {396 {397 let mut data = self.data_mut();398 let stack_depth = &mut data.stack_depth;399 if *stack_depth > self.max_stack() {400 // Error creation uses data, so i drop guard here401 drop(data);402 throw!(StackOverflow);403 } else {404 *stack_depth += 1;405 }406 }407 let mut result = f();408 {409 let mut data = self.data_mut();410 data.stack_depth -= 1;411 data.stack_generation += 1;412 result = data413 .breakpoints414 .insert(data.stack_depth, data.stack_generation, e, result);415 }416 if let Err(mut err) = result {417 err.trace_mut().0.push(StackTraceElement {418 location: Some(e.clone()),419 desc: frame_desc(),420 });421 return Err(err);422 }423 result424 }425 /// Executes code creating a new stack frame426 pub fn push_description<T>(427 &self,428 frame_desc: impl FnOnce() -> String,429 f: impl FnOnce() -> Result<T>,430 ) -> Result<T> {431 {432 let mut data = self.data_mut();433 let stack_depth = &mut data.stack_depth;434 if *stack_depth > self.max_stack() {435 // Error creation uses data, so i drop guard here436 drop(data);437 throw!(StackOverflow);438 } else {439 *stack_depth += 1;440 }441 }442 let result = f();443 {444 let mut data = self.data_mut();445 data.stack_depth -= 1;446 data.stack_generation += 1;447 }448 if let Err(mut err) = result {449 err.trace_mut().0.push(StackTraceElement {450 location: None,451 desc: frame_desc(),452 });453 return Err(err);454 }455 result456 }457458 /// Runs passed function in state (required if function needs to modify stack trace)459 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {460 EVAL_STATE.with(|v| {461 let has_state = v.borrow().is_some();462 if !has_state {463 v.borrow_mut().replace(self.clone());464 }465 let result = f();466 if !has_state {467 v.borrow_mut().take();468 }469 result470 })471 }472 pub fn run_in_state_with_breakpoint(473 &self,474 bp: Rc<Breakpoint>,475 f: impl FnOnce() -> Result<()>,476 ) -> Result<()> {477 {478 let mut data = self.data_mut();479 data.breakpoints.0.push(bp);480 }481482 let result = self.run_in_state(f);483484 {485 let mut data = self.data_mut();486 data.breakpoints.0.pop();487 }488489 result490 }491492 pub fn stringify_err(&self, e: &LocError) -> String {493 let mut out = String::new();494 self.settings()495 .trace_format496 .write_trace(&mut out, self, e)497 .unwrap();498 out499 }500501 pub fn manifest(&self, val: Val) -> Result<IStr> {502 self.run_in_state(|| {503 push_description_frame(504 || "manifestification".to_string(),505 || val.manifest(&self.manifest_format()),506 )507 })508 }509 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {510 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))511 }512 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {513 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))514 }515516 /// If passed value is function then call with set TLA517 pub fn with_tla(&self, val: Val) -> Result<Val> {518 self.run_in_state(|| {519 Ok(match val {520 Val::Func(func) => push_description_frame(521 || "during TLA call".to_owned(),522 || {523 func.evaluate(524 self.create_default_context(),525 CallLocation::native(),526 &self.settings().tla_vars,527 true,528 )529 },530 )?,531 v => v,532 })533 })534 }535}536537/// Internals538impl EvaluationState {539 fn data(&self) -> Ref<EvaluationData> {540 self.0.data.borrow()541 }542 fn data_mut(&self) -> RefMut<EvaluationData> {543 self.0.data.borrow_mut()544 }545 pub fn settings(&self) -> Ref<EvaluationSettings> {546 self.0.settings.borrow()547 }548 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {549 self.0.settings.borrow_mut()550 }551}552553/// Raw methods evaluate passed values but don't perform TLA execution554impl EvaluationState {555 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {556 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))557 }558 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {559 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))560 }561 /// Parses and evaluates the given snippet562 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {563 let parsed = parse(564 &code,565 &ParserSettings {566 file_name: source.clone(),567 },568 )569 .map_err(|e| ImportSyntaxError {570 path: source.clone(),571 source_code: code.clone(),572 error: Box::new(e),573 })?;574 self.add_parsed_file(source, code, parsed.clone())?;575 self.evaluate_expr_raw(parsed)576 }577 /// Evaluates the parsed expression578 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {579 self.run_in_state(|| evaluate(self.create_default_context(), &code))580 }581}582583/// Settings utilities584impl EvaluationState {585 pub fn add_ext_var(&self, name: IStr, value: Val) {586 self.settings_mut().ext_vars.insert(name, value);587 }588 pub fn add_ext_str(&self, name: IStr, value: IStr) {589 self.add_ext_var(name, Val::Str(value));590 }591 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {592 let value =593 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;594 self.add_ext_var(name, value);595 Ok(())596 }597598 pub fn add_tla(&self, name: IStr, value: Val) {599 self.settings_mut()600 .tla_vars601 .insert(name, TlaArg::Val(value));602 }603 pub fn add_tla_str(&self, name: IStr, value: IStr) {604 self.settings_mut()605 .tla_vars606 .insert(name, TlaArg::String(value));607 }608 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {609 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;610 self.settings_mut()611 .tla_vars612 .insert(name, TlaArg::Code(parsed));613 Ok(())614 }615616 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {617 self.settings().import_resolver.resolve_file(from, path)618 }619 pub fn load_file_str(&self, path: &Path) -> Result<IStr> {620 self.settings().import_resolver.load_file_str(path)621 }622 pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {623 self.settings().import_resolver.load_file_bin(path)624 }625626 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {627 Ref::map(self.settings(), |s| &*s.import_resolver)628 }629 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {630 self.settings_mut().import_resolver = resolver;631 }632633 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {634 self.settings_mut().ext_natives.insert(name, cb);635 }636637 pub fn manifest_format(&self) -> ManifestFormat {638 self.settings().manifest_format.clone()639 }640 pub fn set_manifest_format(&self, format: ManifestFormat) {641 self.settings_mut().manifest_format = format;642 }643644 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {645 Ref::map(self.settings(), |s| &*s.trace_format)646 }647 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {648 self.settings_mut().trace_format = format;649 }650651 pub fn max_trace(&self) -> usize {652 self.settings().max_trace653 }654 pub fn set_max_trace(&self, trace: usize) {655 self.settings_mut().max_trace = trace;656 }657658 pub fn max_stack(&self) -> usize {659 self.settings().max_stack660 }661 pub fn set_max_stack(&self, trace: usize) {662 self.settings_mut().max_stack = trace;663 }664}665666pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {667 let a = a as &T;668 let b = b as &T;669 std::ptr::eq(a, b)670}671672fn weak_raw<T>(a: Weak<T>) -> *const () {673 unsafe { std::mem::transmute(a) }674}675fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {676 std::ptr::eq(weak_raw(a), weak_raw(b))677}678679#[test]680fn weak_unsafe() {681 let a = Cc::new(1);682 let b = Cc::new(2);683684 let aw1 = a.clone().downgrade();685 let aw2 = a.clone().downgrade();686 let aw3 = a.clone().downgrade();687688 let bw = b.clone().downgrade();689690 assert!(weak_ptr_eq(aw1, aw2));691 assert!(!weak_ptr_eq(aw3, bw));692}693694#[cfg(test)]695pub mod tests {696 use super::Val;697 use crate::{698 error::Error::*, function::BuiltinParam, gc::TraceBox, native::NativeCallbackHandler,699 primitive_equals, EvaluationState,700 };701 use gcmodule::{Cc, Trace};702 use jrsonnet_parser::*;703 use std::{704 path::{Path, PathBuf},705 rc::Rc,706 };707708 #[test]709 #[should_panic]710 fn eval_state_stacktrace() {711 let state = EvaluationState::default();712 state.run_in_state(|| {713 state714 .push(715 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),716 || "outer".to_owned(),717 || {718 state.push(719 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),720 || "inner".to_owned(),721 || Err(RuntimeError("".into()).into()),722 )?;723 Ok(Val::Null)724 },725 )726 .unwrap();727 });728 }729730 #[test]731 fn eval_state_standard() {732 let state = EvaluationState::default();733 state.with_stdlib();734 assert!(primitive_equals(735 &state736 .evaluate_snippet_raw(737 PathBuf::from("raw.jsonnet").into(),738 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()739 )740 .unwrap(),741 &Val::Bool(true),742 )743 .unwrap());744 }745746 macro_rules! eval {747 ($str: expr) => {{748 let evaluator = EvaluationState::default();749 evaluator.with_stdlib();750 evaluator.run_in_state(|| {751 evaluator752 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())753 .unwrap()754 })755 }};756 }757 macro_rules! eval_json {758 ($str: expr) => {{759 let evaluator = EvaluationState::default();760 evaluator.with_stdlib();761 evaluator.run_in_state(|| {762 evaluator763 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())764 .unwrap()765 .to_json(0)766 .unwrap()767 .replace("\n", "")768 })769 }};770 }771772 /// Asserts given code returns `true`773 macro_rules! assert_eval {774 ($str: expr) => {775 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())776 };777 }778779 /// Asserts given code returns `false`780 macro_rules! assert_eval_neg {781 ($str: expr) => {782 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())783 };784 }785 macro_rules! assert_json {786 ($str: expr, $out: expr) => {787 assert_eq!(eval_json!($str), $out.replace("\t", ""))788 };789 }790791 /// Sanity checking, before trusting to another tests792 #[test]793 fn equality_operator() {794 assert_eval!("2 == 2");795 assert_eval_neg!("2 != 2");796 assert_eval!("2 != 3");797 assert_eval_neg!("2 == 3");798 assert_eval!("'Hello' == 'Hello'");799 assert_eval_neg!("'Hello' != 'Hello'");800 assert_eval!("'Hello' != 'World'");801 assert_eval_neg!("'Hello' == 'World'");802 }803804 #[test]805 fn math_evaluation() {806 assert_eval!("2 + 2 * 2 == 6");807 assert_eval!("3 + (2 + 2 * 2) == 9");808 }809810 #[test]811 fn string_concat() {812 assert_eval!("'Hello' + 'World' == 'HelloWorld'");813 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");814 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");815 }816817 #[test]818 fn faster_join() {819 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");820 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");821 }822823 #[test]824 fn function_contexts() {825 assert_eval!(826 r#"827 local k = {828 t(name = self.h): [self.h, name],829 h: 3,830 };831 local f = {832 t: k.t(),833 h: 4,834 };835 f.t[0] == f.t[1]836 "#837 );838 }839840 #[test]841 fn local() {842 assert_eval!("local a = 2; local b = 3; a + b == 5");843 assert_eval!("local a = 1, b = a + 1; a + b == 3");844 assert_eval!("local a = 1; local a = 2; a == 2");845 }846847 #[test]848 fn object_lazyness() {849 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);850 }851852 #[test]853 fn object_inheritance() {854 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);855 }856857 #[test]858 fn object_assertion_success() {859 eval!("{assert \"a\" in self} + {a:2}");860 }861862 #[test]863 fn object_assertion_error() {864 eval!("{assert \"a\" in self}");865 }866867 #[test]868 fn lazy_args() {869 eval!("local test(a) = 2; test(error '3')");870 }871872 #[test]873 #[should_panic]874 fn tailstrict_args() {875 eval!("local test(a) = 2; test(error '3') tailstrict");876 }877878 #[test]879 #[should_panic]880 fn no_binding_error() {881 eval!("a");882 }883884 #[test]885 fn test_object() {886 assert_json!("{a:2}", r#"{"a": 2}"#);887 assert_json!("{a:2+2}", r#"{"a": 4}"#);888 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);889 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);890 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);891 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);892 assert_json!(893 r#"894 {895 name: "Alice",896 welcome: "Hello " + self.name + "!",897 }898 "#,899 r#"{"name": "Alice","welcome": "Hello Alice!"}"#900 );901 assert_json!(902 r#"903 {904 name: "Alice",905 welcome: "Hello " + self.name + "!",906 } + {907 name: "Bob"908 }909 "#,910 r#"{"name": "Bob","welcome": "Hello Bob!"}"#911 );912 }913914 #[test]915 fn functions() {916 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");917 assert_json!(918 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,919 r#""HelloDearWorld""#920 );921 }922923 #[test]924 fn local_methods() {925 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");926 assert_json!(927 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,928 r#""HelloDearWorld""#929 );930 }931932 #[test]933 fn object_locals() {934 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);935 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);936 assert_json!(937 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,938 r#"{"test": {"test": 4}}"#939 );940 }941942 #[test]943 fn object_comp() {944 assert_json!(945 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}"#,946 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"947 )948 }949950 #[test]951 fn direct_self() {952 println!(953 "{:#?}",954 eval!(955 r#"956 {957 local me = self,958 a: 3,959 b(): me.a,960 }961 "#962 )963 );964 }965966 #[test]967 fn indirect_self() {968 // `self` assigned to `me` was lost when being969 // referenced from field970 eval!(971 r#"{972 local me = self,973 a: 3,974 b: me.a,975 }.b"#976 );977 }978979 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly980 #[test]981 fn std_assert_ok() {982 eval!("std.assertEqual(4.5 << 2, 16)");983 }984985 #[test]986 #[should_panic]987 fn std_assert_failure() {988 eval!("std.assertEqual(4.5 << 2, 15)");989 }990991 #[test]992 fn string_is_string() {993 assert!(primitive_equals(994 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),995 &Val::Bool(false),996 )997 .unwrap());998 }9991000 #[test]1001 fn base64_works() {1002 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);1003 }10041005 #[test]1006 fn utf8_chars() {1007 assert_json!(1008 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,1009 r#"{"c": 128526,"l": 1}"#1010 )1011 }10121013 #[test]1014 fn json() {1015 assert_json!(1016 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,1017 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#1018 );1019 }10201021 #[test]1022 fn json_minified() {1023 assert_json!(1024 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,1025 r#""{\"a\":3,\"b\":4,\"c\":6}""#1026 );1027 }10281029 #[test]1030 fn parse_json() {1031 assert_json!(1032 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,1033 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#1034 );1035 }10361037 #[test]1038 fn test() {1039 assert_json!(1040 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1041 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1042 );1043 }10441045 #[test]1046 fn sjsonnet() {1047 eval!(1048 r#"1049 local x0 = {k: 1};1050 local x1 = {k: x0.k + x0.k};1051 local x2 = {k: x1.k + x1.k};1052 local x3 = {k: x2.k + x2.k};1053 local x4 = {k: x3.k + x3.k};1054 local x5 = {k: x4.k + x4.k};1055 local x6 = {k: x5.k + x5.k};1056 local x7 = {k: x6.k + x6.k};1057 local x8 = {k: x7.k + x7.k};1058 local x9 = {k: x8.k + x8.k};1059 local x10 = {k: x9.k + x9.k};1060 local x11 = {k: x10.k + x10.k};1061 local x12 = {k: x11.k + x11.k};1062 local x13 = {k: x12.k + x12.k};1063 local x14 = {k: x13.k + x13.k};1064 local x15 = {k: x14.k + x14.k};1065 local x16 = {k: x15.k + x15.k};1066 local x17 = {k: x16.k + x16.k};1067 local x18 = {k: x17.k + x17.k};1068 local x19 = {k: x18.k + x18.k};1069 local x20 = {k: x19.k + x19.k};1070 local x21 = {k: x20.k + x20.k};1071 x21.k1072 "#1073 );1074 }10751076 // This test is commented out by default, because of huge compilation slowdown1077 // #[bench]1078 // fn bench_codegen(b: &mut Bencher) {1079 // b.iter(|| {1080 // #[allow(clippy::all)]1081 // let stdlib = {1082 // use jrsonnet_parser::*;1083 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1084 // };1085 // stdlib1086 // })1087 // }10881089 /*1090 #[bench]1091 fn bench_serialize(b: &mut Bencher) {1092 b.iter(|| {1093 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1094 env!("OUT_DIR"),1095 "/stdlib.bincode"1096 )))1097 .expect("deserialize stdlib")1098 })1099 }11001101 #[bench]1102 fn bench_parse(b: &mut Bencher) {1103 b.iter(|| {1104 jrsonnet_parser::parse(1105 jrsonnet_stdlib::STDLIB_STR,1106 &jrsonnet_parser::ParserSettings {1107 loc_data: true,1108 file_name: Rc::new(PathBuf::from("std.jsonnet")),1109 },1110 )1111 })1112 }1113 */11141115 #[test]1116 fn equality() {1117 println!(1118 "{:?}",1119 jrsonnet_parser::parse(1120 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1121 &ParserSettings {1122 file_name: PathBuf::from("equality").into(),1123 }1124 )1125 );1126 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1127 }11281129 #[test]1130 fn native_ext() -> crate::error::Result<()> {1131 use super::native::NativeCallback;1132 let evaluator = EvaluationState::default();11331134 evaluator.with_stdlib();11351136 #[derive(Trace)]1137 struct NativeAdd;1138 impl NativeCallbackHandler for NativeAdd {1139 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {1140 assert_eq!(1141 &from.unwrap() as &Path,1142 &PathBuf::from("native_caller.jsonnet")1143 );1144 match (&args[0], &args[1]) {1145 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1146 (_, _) => unreachable!(),1147 }1148 }1149 }1150 evaluator.settings_mut().ext_natives.insert(1151 "native_add".into(),1152 #[allow(deprecated)]1153 Cc::new(TraceBox(Box::new(NativeCallback::new(1154 vec![1155 BuiltinParam {1156 name: "a".into(),1157 has_default: false,1158 },1159 BuiltinParam {1160 name: "b".into(),1161 has_default: false,1162 },1163 ],1164 TraceBox(Box::new(NativeAdd)),1165 )))),1166 );1167 evaluator.evaluate_snippet_raw(1168 PathBuf::from("native_caller.jsonnet").into(),1169 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1170 )?;1171 Ok(())1172 }11731174 #[test]1175 fn constant_intrinsic() -> crate::error::Result<()> {1176 assert_eval!(1177 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1178 );1179 Ok(())1180 }11811182 #[test]1183 fn standalone_super() -> crate::error::Result<()> {1184 assert_eval!(1185 r#"1186 local obj = {1187 a: 1,1188 b: 2,1189 c: 3,1190 };1191 local test = obj + {1192 fields: std.objectFields(super),1193 d: 5,1194 };1195 test.fields == ['a', 'b', 'c']1196 "#1197 );1198 Ok(())1199 }12001201 #[test]1202 fn comp_self() -> crate::error::Result<()> {1203 assert_eval!(1204 r#"1205 std.objectFields({1206 a:{1207 [name]: name for name in std.objectFields(self)1208 },1209 b: 2,1210 c: 3,1211 }.a) == ['a', 'b', 'c']1212 "#1213 );12141215 Ok(())1216 }12171218 struct TestImportResolver(Vec<u8>);1219 impl crate::import::ImportResolver for TestImportResolver {1220 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1221 Ok(PathBuf::from("/test").into())1222 }12231224 fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {1225 Ok(self.0.clone())1226 }12271228 unsafe fn as_any(&self) -> &dyn std::any::Any {1229 panic!()1230 }12311232 fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {1233 panic!()1234 }1235 }12361237 #[test]1238 fn issue_23() {1239 let state = EvaluationState::default();1240 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1241 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1242 }12431244 #[test]1245 fn issue_40() {1246 let state = EvaluationState::default();1247 state.with_stdlib();12481249 let error = state1250 .evaluate_snippet_raw(1251 PathBuf::from("issue40.jsonnet").into(),1252 r#"1253 local conf = {1254 n: ""1255 };12561257 local result = conf + {1258 assert std.isNumber(self.n): "is number"1259 };12601261 std.manifestJsonEx(result, "")1262 "#1263 .into(),1264 )1265 .unwrap_err();1266 assert_eq!(error.error().to_string(), "assert failed: is number");1267 }12681269 #[test]1270 fn test_ascii_upper_lower() {1271 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1272 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1273 }12741275 #[test]1276 fn test_member() {1277 assert_eval!(r#"!std.member("", "")"#);1278 assert_eval!(r#"std.member("abc", "a")"#);1279 assert_eval!(r#"!std.member("abc", "d")"#);1280 assert_eval!(r#"!std.member([], "")"#);1281 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1282 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1283 }12841285 #[test]1286 fn test_count() {1287 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1288 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1289 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1290 }12911292 mod derive_typed {1293 use crate::{typed::Typed, EvaluationState};1294 use std::path::PathBuf;12951296 #[derive(Typed, PartialEq, Debug)]1297 struct MyTyped {1298 a: u32,1299 #[typed(rename = "b")]1300 c: String,1301 }13021303 #[test]1304 fn test() {1305 let es = EvaluationState::default();1306 let val = eval!("{a: 14, b: 'Hello, world!'}");1307 let typed = es.run_in_state(|| MyTyped::try_from(val).unwrap());13081309 assert_eq!(1310 typed,1311 MyTyped {1312 a: 14,1313 c: "Hello, world!".to_string()1314 }1315 );1316 es.settings_mut().globals.insert(1317 "mytyped".into(),1318 es.run_in_state(|| typed.try_into()).unwrap(),1319 );13201321 let v = es1322 .evaluate_snippet_raw(1323 PathBuf::from("raw.jsonnet").into(),1324 "1325 mytyped == {a: 14, b: 'Hello, world!'}1326 "1327 .into(),1328 )1329 .unwrap()1330 .as_bool()1331 .unwrap();1332 assert!(v)1333 }1334 }1335}crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,4 +1,7 @@
-use std::convert::{TryFrom, TryInto};
+use std::{
+ convert::{TryFrom, TryInto},
+ rc::Rc,
+};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
@@ -306,6 +309,44 @@
}
}
+/// Specialization
+pub struct Bytes(pub Rc<[u8]>);
+
+impl Typed for Bytes {
+ const TYPE: &'static ComplexValType =
+ &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));
+}
+impl TryFrom<Val> for Bytes {
+ type Error = LocError;
+
+ fn try_from(value: Val) -> Result<Self> {
+ match value {
+ Val::Arr(ArrValue::Bytes(bytes)) => Ok(Self(bytes)),
+ _ => {
+ <Self as Typed>::TYPE.check(&value)?;
+ match value {
+ Val::Arr(a) => {
+ let mut out = Vec::with_capacity(a.len());
+ for e in a.iter() {
+ let r = e?;
+ out.push(u8::try_from(r)?);
+ }
+ Ok(Self(out.into()))
+ }
+ _ => unreachable!(),
+ }
+ }
+ }
+ }
+}
+impl TryFrom<Bytes> for Val {
+ type Error = LocError;
+
+ fn try_from(value: Bytes) -> Result<Self> {
+ Ok(Val::Arr(ArrValue::Bytes(value.0)))
+ }
+}
+
pub struct M1;
impl Typed for M1 {
const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -174,6 +174,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)>),
@@ -185,6 +186,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(),
@@ -197,6 +199,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()?))
@@ -218,6 +223,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) => {
@@ -233,6 +241,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() {
@@ -253,6 +268,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()),
@@ -261,6 +277,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(),
@@ -269,6 +286,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
@@ -285,6 +285,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)