difftreelog
Merge remote-tracking branch 'origin/master' into gcmodule
in: master
7 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -19,6 +19,8 @@
pub struct ManifestJsonOptions<'s> {
pub padding: &'s str,
pub mtype: ManifestType,
+ pub newline: &'s str,
+ pub key_val_sep: &'s str,
}
pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
@@ -49,7 +51,7 @@
buf.push('[');
if !items.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -60,7 +62,7 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
@@ -69,7 +71,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
@@ -86,7 +88,7 @@
let fields = obj.fields();
if !fields.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -97,12 +99,12 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
- buf.push_str(": ");
+ buf.push_str(options.key_val_sep);
push_description_frame(
|| format!("field <{}> manifestification", field.clone()),
|| {
@@ -115,7 +117,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,6 +1,5 @@
use crate::function::StaticBuiltin;
-use crate::typed::{Any, Null, PositiveF64, VecVal, M1};
-use crate::{self as jrsonnet_evaluator, Either, ObjValue};
+use crate::typed::{Any, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
equals,
@@ -10,6 +9,7 @@
typed::{Either2, Either4},
with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
};
+use crate::{Either, ObjValue};
use format::{format_arr, format_obj};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
@@ -145,7 +145,7 @@
fn builtin_length(x: Either![IStr, VecVal, ObjValue, Cc<FuncVal>]) -> Result<usize> {
use Either4::*;
Ok(match x {
- A(x) => x.len(),
+ A(x) => x.chars().count(),
B(x) => x.0.len(),
C(x) => x
.fields_visibility()
@@ -566,12 +566,21 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(value: Any, indent: IStr) -> Result<String> {
+fn builtin_manifest_json_ex(
+ value: Any,
+ indent: IStr,
+ newline: Option<IStr>,
+ key_val_sep: Option<IStr>,
+) -> Result<String> {
+ let newline = newline.as_deref().unwrap_or("\n");
+ let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
manifest_json_ex(
&value.0,
&ManifestJsonOptions {
padding: &indent,
mtype: ManifestType::Std,
+ newline,
+ key_val_sep,
},
)
}
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)]78mod builtin;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13mod function;14mod import;15mod integrations;16mod map;17pub mod native;18mod obj;19pub mod trace;20pub mod typed;21mod val;2223pub use ctx::*;24pub use dynamic::*;25use error::{Error::*, LocError, Result, StackTraceElement};26pub use evaluate::*;27pub use function::parse_function_call;28use function::TlaArg;29use gc::{GcHashMap, TraceBox};30use gcmodule::{Cc, Trace};31pub use import::*;32pub use jrsonnet_interner::IStr;33use jrsonnet_parser::*;34use native::NativeCallback;35pub use obj::*;36use std::{37 cell::{Ref, RefCell, RefMut},38 collections::HashMap,39 fmt::Debug,40 path::{Path, PathBuf},41 rc::Rc,42};43use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};44pub use val::*;45pub mod gc;4647pub trait Bindable: Trace + 'static {48 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal>;49}5051#[derive(Clone, Trace)]52pub enum LazyBinding {53 Bindable(Cc<TraceBox<dyn Bindable>>),54 Bound(LazyVal),55}5657impl Debug for LazyBinding {58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {59 write!(f, "LazyBinding")60 }61}62impl LazyBinding {63 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {64 match self {65 Self::Bindable(v) => v.bind(this, super_obj),66 Self::Bound(v) => Ok(v.clone()),67 }68 }69}7071pub struct EvaluationSettings {72 /// Limits recursion by limiting the number of stack frames73 pub max_stack: usize,74 /// Limits amount of stack trace items preserved75 pub max_trace: usize,76 /// Used for s`td.extVar`77 pub ext_vars: HashMap<IStr, Val>,78 /// Used for ext.native79 pub ext_natives: HashMap<IStr, Cc<NativeCallback>>,80 /// TLA vars81 pub tla_vars: HashMap<IStr, TlaArg>,82 /// Global variables are inserted in default context83 pub globals: HashMap<IStr, Val>,84 /// Used to resolve file locations/contents85 pub import_resolver: Box<dyn ImportResolver>,86 /// Used in manifestification functions87 pub manifest_format: ManifestFormat,88 /// Used for bindings89 pub trace_format: Box<dyn TraceFormat>,90}91impl Default for EvaluationSettings {92 fn default() -> Self {93 Self {94 max_stack: 200,95 max_trace: 20,96 globals: Default::default(),97 ext_vars: Default::default(),98 ext_natives: Default::default(),99 tla_vars: Default::default(),100 import_resolver: Box::new(DummyImportResolver),101 manifest_format: ManifestFormat::Json(4),102 trace_format: Box::new(CompactFormat {103 padding: 4,104 resolver: trace::PathResolver::Absolute,105 }),106 }107 }108}109110#[derive(Default)]111struct EvaluationData {112 /// Used for stack overflow detection, stacktrace is populated on unwind113 stack_depth: usize,114 /// Updated every time stack entry is popt115 stack_generation: usize,116117 breakpoints: Breakpoints,118 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces119 files: HashMap<Rc<Path>, FileData>,120 str_files: HashMap<Rc<Path>, IStr>,121}122123pub struct FileData {124 source_code: IStr,125 parsed: LocExpr,126 evaluated: Option<Val>,127}128129#[allow(clippy::type_complexity)]130pub struct Breakpoint {131 loc: ExprLocation,132 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,133}134#[derive(Default)]135struct Breakpoints(Vec<Rc<Breakpoint>>);136impl Breakpoints {137 fn insert(138 &self,139 stack_depth: usize,140 stack_generation: usize,141 loc: &ExprLocation,142 result: Result<Val>,143 ) -> Result<Val> {144 if self.0.is_empty() {145 return result;146 }147 for item in self.0.iter() {148 if item.loc.belongs_to(loc) {149 let mut collected = item.collected.borrow_mut();150 let (depth, vals) = collected.entry(stack_generation).or_default();151 if stack_depth > *depth {152 vals.clear();153 }154 vals.push(result.clone());155 }156 }157 result158 }159}160161#[derive(Default)]162pub struct EvaluationStateInternals {163 /// Internal state164 data: RefCell<EvaluationData>,165 /// Settings, safe to change at runtime166 settings: RefCell<EvaluationSettings>,167}168169thread_local! {170 /// Contains the state for a currently executed file.171 /// Global state is fine here.172 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)173}174pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {175 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))176}177pub(crate) fn push_frame<T>(178 e: Option<&ExprLocation>,179 frame_desc: impl FnOnce() -> String,180 f: impl FnOnce() -> Result<T>,181) -> Result<T> {182 with_state(|s| s.push(e, frame_desc, f))183}184185#[allow(dead_code)]186pub(crate) fn push_val_frame(187 e: &ExprLocation,188 frame_desc: impl FnOnce() -> String,189 f: impl FnOnce() -> Result<Val>,190) -> Result<Val> {191 with_state(|s| s.push_val(e, frame_desc, f))192}193#[allow(dead_code)]194pub(crate) fn push_description_frame<T>(195 frame_desc: impl FnOnce() -> String,196 f: impl FnOnce() -> Result<T>,197) -> Result<T> {198 with_state(|s| s.push_description(frame_desc, f))199}200201/// Maintains stack trace and import resolution202#[derive(Default, Clone)]203pub struct EvaluationState(Rc<EvaluationStateInternals>);204205impl EvaluationState {206 /// Parses and adds file as loaded207 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {208 let parsed = parse(209 &source_code,210 &ParserSettings {211 file_name: path.clone(),212 },213 )214 .map_err(|error| ImportSyntaxError {215 error: Box::new(error),216 path: path.to_owned(),217 source_code: source_code.clone(),218 })?;219 self.add_parsed_file(path, source_code, parsed.clone())?;220221 Ok(parsed)222 }223224 pub fn reset_evaluation_state(&self, name: &Path) {225 self.data_mut()226 .files227 .get_mut(name)228 .unwrap()229 .evaluated230 .take();231 }232233 /// Adds file by source code and parsed expr234 pub fn add_parsed_file(235 &self,236 name: Rc<Path>,237 source_code: IStr,238 parsed: LocExpr,239 ) -> Result<()> {240 self.data_mut().files.insert(241 name,242 FileData {243 source_code,244 parsed,245 evaluated: None,246 },247 );248249 Ok(())250 }251 pub fn get_source(&self, name: &Path) -> Option<IStr> {252 let ro_map = &self.data().files;253 ro_map.get(name).map(|value| value.source_code.clone())254 }255 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {256 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)257 }258 pub fn map_from_source_location(259 &self,260 file: &Path,261 line: usize,262 column: usize,263 ) -> Option<usize> {264 location_to_offset(&self.get_source(file).unwrap(), line, column)265 }266 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {267 let file_path = self.resolve_file(from, path)?;268 {269 let data = self.data();270 let files = &data.files;271 if files.contains_key(&file_path as &Path) {272 drop(data);273 return self.evaluate_loaded_file_raw(&file_path);274 }275 }276 let contents = self.load_file_contents(&file_path)?;277 self.add_file(file_path.clone(), contents)?;278 self.evaluate_loaded_file_raw(&file_path)279 }280 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {281 let path = self.resolve_file(from, path)?;282 if !self.data().str_files.contains_key(&path) {283 let file_str = self.load_file_contents(&path)?;284 self.data_mut().str_files.insert(path.clone(), file_str);285 }286 Ok(self.data().str_files.get(&path).cloned().unwrap())287 }288289 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {290 let expr: LocExpr = {291 let ro_map = &self.data().files;292 let value = ro_map293 .get(name)294 .unwrap_or_else(|| panic!("file not added: {:?}", name));295 if let Some(ref evaluated) = value.evaluated {296 return Ok(evaluated.clone());297 }298 value.parsed.clone()299 };300 let value = evaluate(self.create_default_context(), &expr)?;301 {302 self.data_mut()303 .files304 .get_mut(name)305 .unwrap()306 .evaluated307 .replace(value.clone());308 }309 Ok(value)310 }311312 /// Adds standard library global variable (std) to this evaluator313 pub fn with_stdlib(&self) -> &Self {314 use jrsonnet_stdlib::STDLIB_STR;315 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();316 self.run_in_state(|| {317 self.add_parsed_file(318 std_path.clone(),319 STDLIB_STR.to_owned().into(),320 builtin::get_parsed_stdlib(),321 )322 .unwrap();323 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();324 self.settings_mut().globals.insert("std".into(), val);325 });326 self327 }328329 /// Creates context with all passed global variables330 pub fn create_default_context(&self) -> Context {331 let globals = &self.settings().globals;332 let mut new_bindings = GcHashMap::with_capacity(globals.len());333 for (name, value) in globals.iter() {334 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));335 }336 Context::new().extend_bound(new_bindings)337 }338339 /// Executes code creating a new stack frame340 pub fn push<T>(341 &self,342 e: Option<&ExprLocation>,343 frame_desc: impl FnOnce() -> String,344 f: impl FnOnce() -> Result<T>,345 ) -> Result<T> {346 {347 let mut data = self.data_mut();348 let stack_depth = &mut data.stack_depth;349 if *stack_depth > self.max_stack() {350 // Error creation uses data, so i drop guard here351 drop(data);352 throw!(StackOverflow);353 } else {354 *stack_depth += 1;355 }356 }357 let result = f();358 {359 let mut data = self.data_mut();360 data.stack_depth -= 1;361 data.stack_generation += 1;362 }363 if let Err(mut err) = result {364 err.trace_mut().0.push(StackTraceElement {365 location: e.cloned(),366 desc: frame_desc(),367 });368 return Err(err);369 }370 result371 }372373 /// Executes code creating a new stack frame374 pub fn push_val(375 &self,376 e: &ExprLocation,377 frame_desc: impl FnOnce() -> String,378 f: impl FnOnce() -> Result<Val>,379 ) -> Result<Val> {380 {381 let mut data = self.data_mut();382 let stack_depth = &mut data.stack_depth;383 if *stack_depth > self.max_stack() {384 // Error creation uses data, so i drop guard here385 drop(data);386 throw!(StackOverflow);387 } else {388 *stack_depth += 1;389 }390 }391 let mut result = f();392 {393 let mut data = self.data_mut();394 data.stack_depth -= 1;395 data.stack_generation += 1;396 result = data397 .breakpoints398 .insert(data.stack_depth, data.stack_generation, e, result);399 }400 if let Err(mut err) = result {401 err.trace_mut().0.push(StackTraceElement {402 location: Some(e.clone()),403 desc: frame_desc(),404 });405 return Err(err);406 }407 result408 }409 /// Executes code creating a new stack frame410 pub fn push_description<T>(411 &self,412 frame_desc: impl FnOnce() -> String,413 f: impl FnOnce() -> Result<T>,414 ) -> Result<T> {415 {416 let mut data = self.data_mut();417 let stack_depth = &mut data.stack_depth;418 if *stack_depth > self.max_stack() {419 // Error creation uses data, so i drop guard here420 drop(data);421 throw!(StackOverflow);422 } else {423 *stack_depth += 1;424 }425 }426 let result = f();427 {428 let mut data = self.data_mut();429 data.stack_depth -= 1;430 data.stack_generation += 1;431 }432 if let Err(mut err) = result {433 err.trace_mut().0.push(StackTraceElement {434 location: None,435 desc: frame_desc(),436 });437 return Err(err);438 }439 result440 }441442 /// Runs passed function in state (required if function needs to modify stack trace)443 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {444 EVAL_STATE.with(|v| {445 let has_state = v.borrow().is_some();446 if !has_state {447 v.borrow_mut().replace(self.clone());448 }449 let result = f();450 if !has_state {451 v.borrow_mut().take();452 }453 result454 })455 }456 pub fn run_in_state_with_breakpoint(457 &self,458 bp: Rc<Breakpoint>,459 f: impl FnOnce() -> Result<()>,460 ) -> Result<()> {461 {462 let mut data = self.data_mut();463 data.breakpoints.0.push(bp);464 }465466 let result = self.run_in_state(f);467468 {469 let mut data = self.data_mut();470 data.breakpoints.0.pop();471 }472473 result474 }475476 pub fn stringify_err(&self, e: &LocError) -> String {477 let mut out = String::new();478 self.settings()479 .trace_format480 .write_trace(&mut out, self, e)481 .unwrap();482 out483 }484485 pub fn manifest(&self, val: Val) -> Result<IStr> {486 self.run_in_state(|| {487 push_description_frame(488 || "manifestification".to_string(),489 || val.manifest(&self.manifest_format()),490 )491 })492 }493 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {494 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))495 }496 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {497 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))498 }499500 /// If passed value is function then call with set TLA501 pub fn with_tla(&self, val: Val) -> Result<Val> {502 self.run_in_state(|| {503 Ok(match val {504 Val::Func(func) => push_description_frame(505 || "during TLA call".to_owned(),506 || {507 func.evaluate(508 self.create_default_context(),509 None,510 &self.settings().tla_vars,511 true,512 )513 },514 )?,515 v => v,516 })517 })518 }519}520521/// Internals522impl EvaluationState {523 fn data(&self) -> Ref<EvaluationData> {524 self.0.data.borrow()525 }526 fn data_mut(&self) -> RefMut<EvaluationData> {527 self.0.data.borrow_mut()528 }529 pub fn settings(&self) -> Ref<EvaluationSettings> {530 self.0.settings.borrow()531 }532 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {533 self.0.settings.borrow_mut()534 }535}536537/// Raw methods evaluate passed values but don't perform TLA execution538impl EvaluationState {539 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {540 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))541 }542 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {543 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))544 }545 /// Parses and evaluates the given snippet546 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {547 let parsed = parse(548 &code,549 &ParserSettings {550 file_name: source.clone(),551 },552 )553 .map_err(|e| ImportSyntaxError {554 path: source.clone(),555 source_code: code.clone(),556 error: Box::new(e),557 })?;558 self.add_parsed_file(source, code, parsed.clone())?;559 self.evaluate_expr_raw(parsed)560 }561 /// Evaluates the parsed expression562 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {563 self.run_in_state(|| evaluate(self.create_default_context(), &code))564 }565}566567/// Settings utilities568impl EvaluationState {569 pub fn add_ext_var(&self, name: IStr, value: Val) {570 self.settings_mut().ext_vars.insert(name, value);571 }572 pub fn add_ext_str(&self, name: IStr, value: IStr) {573 self.add_ext_var(name, Val::Str(value));574 }575 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {576 let value =577 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;578 self.add_ext_var(name, value);579 Ok(())580 }581582 pub fn add_tla(&self, name: IStr, value: Val) {583 self.settings_mut()584 .tla_vars585 .insert(name, TlaArg::Val(value));586 }587 pub fn add_tla_str(&self, name: IStr, value: IStr) {588 self.settings_mut()589 .tla_vars590 .insert(name, TlaArg::String(value));591 }592 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {593 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;594 self.settings_mut()595 .tla_vars596 .insert(name, TlaArg::Code(parsed));597 Ok(())598 }599600 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {601 self.settings().import_resolver.resolve_file(from, path)602 }603 pub fn load_file_contents(&self, path: &Path) -> Result<IStr> {604 self.settings().import_resolver.load_file_contents(path)605 }606607 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {608 Ref::map(self.settings(), |s| &*s.import_resolver)609 }610 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {611 self.settings_mut().import_resolver = resolver;612 }613614 pub fn add_native(&self, name: IStr, cb: Cc<NativeCallback>) {615 self.settings_mut().ext_natives.insert(name, cb);616 }617618 pub fn manifest_format(&self) -> ManifestFormat {619 self.settings().manifest_format.clone()620 }621 pub fn set_manifest_format(&self, format: ManifestFormat) {622 self.settings_mut().manifest_format = format;623 }624625 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {626 Ref::map(self.settings(), |s| &*s.trace_format)627 }628 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {629 self.settings_mut().trace_format = format;630 }631632 pub fn max_trace(&self) -> usize {633 self.settings().max_trace634 }635 pub fn set_max_trace(&self, trace: usize) {636 self.settings_mut().max_trace = trace;637 }638639 pub fn max_stack(&self) -> usize {640 self.settings().max_stack641 }642 pub fn set_max_stack(&self, trace: usize) {643 self.settings_mut().max_stack = trace;644 }645}646647pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {648 let a = a as &T;649 let b = b as &T;650 std::ptr::eq(a, b)651}652653#[cfg(test)]654pub mod tests {655 use super::Val;656 use crate::{657 error::Error::*, gc::TraceBox, native::NativeCallbackHandler, primitive_equals,658 EvaluationState,659 };660 use gcmodule::{Cc, Trace};661 use jrsonnet_interner::IStr;662 use jrsonnet_parser::*;663 use std::{664 path::{Path, PathBuf},665 rc::Rc,666 };667668 #[test]669 #[should_panic]670 fn eval_state_stacktrace() {671 let state = EvaluationState::default();672 state.run_in_state(|| {673 state674 .push(675 Some(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),676 || "outer".to_owned(),677 || {678 state.push(679 Some(&ExprLocation(PathBuf::from("test2.jsonnet").into(), 30, 40)),680 || "inner".to_owned(),681 || Err(RuntimeError("".into()).into()),682 )?;683 Ok(Val::Null)684 },685 )686 .unwrap();687 });688 }689690 #[test]691 fn eval_state_standard() {692 let state = EvaluationState::default();693 state.with_stdlib();694 assert!(primitive_equals(695 &state696 .evaluate_snippet_raw(697 PathBuf::from("raw.jsonnet").into(),698 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()699 )700 .unwrap(),701 &Val::Bool(true),702 )703 .unwrap());704 }705706 macro_rules! eval {707 ($str: expr) => {708 EvaluationState::default()709 .with_stdlib()710 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())711 .unwrap()712 };713 }714 macro_rules! eval_json {715 ($str: expr) => {{716 let evaluator = EvaluationState::default();717 evaluator.with_stdlib();718 evaluator.run_in_state(|| {719 evaluator720 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())721 .unwrap()722 .to_json(0)723 .unwrap()724 .replace("\n", "")725 })726 }};727 }728729 /// Asserts given code returns `true`730 macro_rules! assert_eval {731 ($str: expr) => {732 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())733 };734 }735736 /// Asserts given code returns `false`737 macro_rules! assert_eval_neg {738 ($str: expr) => {739 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())740 };741 }742 macro_rules! assert_json {743 ($str: expr, $out: expr) => {744 assert_eq!(eval_json!($str), $out.replace("\t", ""))745 };746 }747748 /// Sanity checking, before trusting to another tests749 #[test]750 fn equality_operator() {751 assert_eval!("2 == 2");752 assert_eval_neg!("2 != 2");753 assert_eval!("2 != 3");754 assert_eval_neg!("2 == 3");755 assert_eval!("'Hello' == 'Hello'");756 assert_eval_neg!("'Hello' != 'Hello'");757 assert_eval!("'Hello' != 'World'");758 assert_eval_neg!("'Hello' == 'World'");759 }760761 #[test]762 fn math_evaluation() {763 assert_eval!("2 + 2 * 2 == 6");764 assert_eval!("3 + (2 + 2 * 2) == 9");765 }766767 #[test]768 fn string_concat() {769 assert_eval!("'Hello' + 'World' == 'HelloWorld'");770 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");771 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");772 }773774 #[test]775 fn faster_join() {776 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");777 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");778 }779780 #[test]781 fn function_contexts() {782 assert_eval!(783 r#"784 local k = {785 t(name = self.h): [self.h, name],786 h: 3,787 };788 local f = {789 t: k.t(),790 h: 4,791 };792 f.t[0] == f.t[1]793 "#794 );795 }796797 #[test]798 fn local() {799 assert_eval!("local a = 2; local b = 3; a + b == 5");800 assert_eval!("local a = 1, b = a + 1; a + b == 3");801 assert_eval!("local a = 1; local a = 2; a == 2");802 }803804 #[test]805 fn object_lazyness() {806 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);807 }808809 #[test]810 fn object_inheritance() {811 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);812 }813814 #[test]815 fn object_assertion_success() {816 eval!("{assert \"a\" in self} + {a:2}");817 }818819 #[test]820 fn object_assertion_error() {821 eval!("{assert \"a\" in self}");822 }823824 #[test]825 fn lazy_args() {826 eval!("local test(a) = 2; test(error '3')");827 }828829 #[test]830 #[should_panic]831 fn tailstrict_args() {832 eval!("local test(a) = 2; test(error '3') tailstrict");833 }834835 #[test]836 #[should_panic]837 fn no_binding_error() {838 eval!("a");839 }840841 #[test]842 fn test_object() {843 assert_json!("{a:2}", r#"{"a": 2}"#);844 assert_json!("{a:2+2}", r#"{"a": 4}"#);845 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);846 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);847 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);848 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);849 assert_json!(850 r#"851 {852 name: "Alice",853 welcome: "Hello " + self.name + "!",854 }855 "#,856 r#"{"name": "Alice","welcome": "Hello Alice!"}"#857 );858 assert_json!(859 r#"860 {861 name: "Alice",862 welcome: "Hello " + self.name + "!",863 } + {864 name: "Bob"865 }866 "#,867 r#"{"name": "Bob","welcome": "Hello Bob!"}"#868 );869 }870871 #[test]872 fn functions() {873 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");874 assert_json!(875 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,876 r#""HelloDearWorld""#877 );878 }879880 #[test]881 fn local_methods() {882 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");883 assert_json!(884 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,885 r#""HelloDearWorld""#886 );887 }888889 #[test]890 fn object_locals() {891 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);892 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);893 assert_json!(894 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,895 r#"{"test": {"test": 4}}"#896 );897 }898899 #[test]900 fn object_comp() {901 assert_json!(902 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}"#,903 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"904 )905 }906907 #[test]908 fn direct_self() {909 println!(910 "{:#?}",911 eval!(912 r#"913 {914 local me = self,915 a: 3,916 b(): me.a,917 }918 "#919 )920 );921 }922923 #[test]924 fn indirect_self() {925 // `self` assigned to `me` was lost when being926 // referenced from field927 eval!(928 r#"{929 local me = self,930 a: 3,931 b: me.a,932 }.b"#933 );934 }935936 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly937 #[test]938 fn std_assert_ok() {939 eval!("std.assertEqual(4.5 << 2, 16)");940 }941942 #[test]943 #[should_panic]944 fn std_assert_failure() {945 eval!("std.assertEqual(4.5 << 2, 15)");946 }947948 #[test]949 fn string_is_string() {950 assert!(primitive_equals(951 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),952 &Val::Bool(false),953 )954 .unwrap());955 }956957 #[test]958 fn base64_works() {959 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);960 }961962 #[test]963 fn utf8_chars() {964 assert_json!(965 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,966 r#"{"c": 128526,"l": 1}"#967 )968 }969970 #[test]971 fn json() {972 assert_json!(973 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,974 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#975 );976 }977978 #[test]979 fn parse_json() {980 assert_json!(981 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,982 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#983 );984 }985986 #[test]987 fn test() {988 assert_json!(989 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,990 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"991 );992 }993994 #[test]995 fn sjsonnet() {996 eval!(997 r#"998 local x0 = {k: 1};999 local x1 = {k: x0.k + x0.k};1000 local x2 = {k: x1.k + x1.k};1001 local x3 = {k: x2.k + x2.k};1002 local x4 = {k: x3.k + x3.k};1003 local x5 = {k: x4.k + x4.k};1004 local x6 = {k: x5.k + x5.k};1005 local x7 = {k: x6.k + x6.k};1006 local x8 = {k: x7.k + x7.k};1007 local x9 = {k: x8.k + x8.k};1008 local x10 = {k: x9.k + x9.k};1009 local x11 = {k: x10.k + x10.k};1010 local x12 = {k: x11.k + x11.k};1011 local x13 = {k: x12.k + x12.k};1012 local x14 = {k: x13.k + x13.k};1013 local x15 = {k: x14.k + x14.k};1014 local x16 = {k: x15.k + x15.k};1015 local x17 = {k: x16.k + x16.k};1016 local x18 = {k: x17.k + x17.k};1017 local x19 = {k: x18.k + x18.k};1018 local x20 = {k: x19.k + x19.k};1019 local x21 = {k: x20.k + x20.k};1020 x21.k1021 "#1022 );1023 }10241025 // This test is commented out by default, because of huge compilation slowdown1026 // #[bench]1027 // fn bench_codegen(b: &mut Bencher) {1028 // b.iter(|| {1029 // #[allow(clippy::all)]1030 // let stdlib = {1031 // use jrsonnet_parser::*;1032 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1033 // };1034 // stdlib1035 // })1036 // }10371038 /*1039 #[bench]1040 fn bench_serialize(b: &mut Bencher) {1041 b.iter(|| {1042 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1043 env!("OUT_DIR"),1044 "/stdlib.bincode"1045 )))1046 .expect("deserialize stdlib")1047 })1048 }10491050 #[bench]1051 fn bench_parse(b: &mut Bencher) {1052 b.iter(|| {1053 jrsonnet_parser::parse(1054 jrsonnet_stdlib::STDLIB_STR,1055 &jrsonnet_parser::ParserSettings {1056 loc_data: true,1057 file_name: Rc::new(PathBuf::from("std.jsonnet")),1058 },1059 )1060 })1061 }1062 */10631064 #[test]1065 fn equality() {1066 println!(1067 "{:?}",1068 jrsonnet_parser::parse(1069 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1070 &ParserSettings {1071 file_name: PathBuf::from("equality").into(),1072 }1073 )1074 );1075 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1076 }10771078 #[test]1079 fn native_ext() -> crate::error::Result<()> {1080 use super::native::NativeCallback;1081 let evaluator = EvaluationState::default();10821083 evaluator.with_stdlib();10841085 #[derive(Trace)]1086 struct NativeAdd;1087 impl NativeCallbackHandler for NativeAdd {1088 fn call(&self, from: Rc<Path>, args: &[Val]) -> crate::error::Result<Val> {1089 assert_eq!(&from as &Path, &PathBuf::from("native_caller.jsonnet"));1090 match (&args[0], &args[1]) {1091 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1092 (_, _) => unreachable!(),1093 }1094 }1095 }1096 evaluator.settings_mut().ext_natives.insert(1097 "native_add".into(),1098 Cc::new(NativeCallback::new(1099 ParamsDesc(Rc::new(vec![1100 Param("a".into(), None),1101 Param("b".into(), None),1102 ])),1103 TraceBox(Box::new(NativeAdd)),1104 )),1105 );1106 evaluator.evaluate_snippet_raw(1107 PathBuf::from("native_caller.jsonnet").into(),1108 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1109 )?;1110 Ok(())1111 }11121113 #[test]1114 fn constant_intrinsic() -> crate::error::Result<()> {1115 assert_eval!(1116 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1117 );1118 Ok(())1119 }11201121 #[test]1122 fn standalone_super() -> crate::error::Result<()> {1123 assert_eval!(1124 r#"1125 local obj = {1126 a: 1,1127 b: 2,1128 c: 3,1129 };1130 local test = obj + {1131 fields: std.objectFields(super),1132 d: 5,1133 };1134 test.fields == ['a', 'b', 'c']1135 "#1136 );1137 Ok(())1138 }11391140 #[test]1141 fn comp_self() -> crate::error::Result<()> {1142 assert_eval!(1143 r#"1144 std.objectFields({1145 a:{1146 [name]: name for name in std.objectFields(self)1147 },1148 b: 2,1149 c: 3,1150 }.a) == ['a', 'b', 'c']1151 "#1152 );11531154 Ok(())1155 }11561157 struct TestImportResolver(IStr);1158 impl crate::import::ImportResolver for TestImportResolver {1159 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1160 Ok(PathBuf::from("/test").into())1161 }11621163 fn load_file_contents(&self, _: &Path) -> crate::error::Result<IStr> {1164 Ok(self.0.clone())1165 }11661167 unsafe fn as_any(&self) -> &dyn std::any::Any {1168 panic!()1169 }1170 }11711172 #[test]1173 fn issue_23() {1174 let state = EvaluationState::default();1175 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1176 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1177 }11781179 #[test]1180 fn issue_40() {1181 let state = EvaluationState::default();1182 state.with_stdlib();11831184 let error = state1185 .evaluate_snippet_raw(1186 PathBuf::from("issue40.jsonnet").into(),1187 r#"1188 local conf = {1189 n: ""1190 };11911192 local result = conf + {1193 assert std.isNumber(self.n): "is number"1194 };11951196 std.manifestJsonEx(result, "")1197 "#1198 .into(),1199 )1200 .unwrap_err();1201 assert_eq!(error.error().to_string(), "assert failed: is number");1202 }12031204 #[test]1205 fn test_ascii_upper_lower() {1206 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1207 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1208 }12091210 #[test]1211 fn test_member() {1212 assert_eval!(r#"!std.member("", "")"#);1213 assert_eval!(r#"std.member("abc", "a")"#);1214 assert_eval!(r#"!std.member("abc", "d")"#);1215 assert_eval!(r#"!std.member([], "")"#);1216 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1217 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1218 }12191220 #[test]1221 fn test_count() {1222 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1223 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1224 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1225 }1226}crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -19,7 +19,7 @@
($($ty:ty)*) => {$(
impl Typed for $ty {
const TYPE: &'static ComplexValType =
- &ComplexValType::BoundedNumber(Some(<$ty>::MIN as f64), Some(<$ty>::MAX as f64));
+ &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));
}
impl TryFrom<Val> for $ty {
type Error = LocError;
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -400,6 +400,8 @@
&ManifestJsonOptions {
padding: "",
mtype: ManifestType::ToString,
+ newline: "\n",
+ key_val_sep: ": ",
},
)?
.into(),
@@ -484,6 +486,8 @@
} else {
ManifestType::Manifest
},
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
@@ -496,6 +500,8 @@
&ManifestJsonOptions {
padding: &" ".repeat(padding),
mtype: ManifestType::Std,
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -105,7 +105,7 @@
if let Some(opt_ty) = extract_type_from_option(&t.ty) {
quote! {{
if let Some(value) = parsed.get(#ident) {
- Some(jrsonnet_evaluator::push_description_frame(
+ Some(::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#opt_ty>::try_from(value.evaluate()?),
)?)
@@ -117,7 +117,7 @@
quote! {{
let value = parsed.get(#ident).unwrap();
- jrsonnet_evaluator::push_description_frame(
+ ::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#ty>::try_from(value.evaluate()?),
)?
@@ -136,7 +136,7 @@
#[derive(Clone, Copy, gcmodule::Trace)]
#vis struct #name {}
const _: () = {
- use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
+ use ::jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
const PARAMS: &'static [BuiltinParam] = &[
#(#params),*
];
@@ -156,7 +156,7 @@
PARAMS
}
fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
- let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+ let parsed = ::jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
let result: #result = #name(#(#args),*);
let result = result?;
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -373,6 +373,8 @@
manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,
+ manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
+
manifestJsonEx:: $intrinsic(manifestJsonEx),
manifestYamlDoc:: $intrinsic(manifestYamlDoc),
@@ -530,6 +532,9 @@
else
patch,
+ get(o, f, default = null, inc_hidden = true)::
+ if std.objectHasEx(o, f, inc_hidden) then o[f] else default,
+
objectFields(o)::
std.objectFieldsEx(o, false),