difftreelog
feat std.extVar support
in: master
4 files changed
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -42,19 +42,42 @@
}
}
+#[derive(Clone)]
+struct ExtStr {
+ name: String,
+ value: String,
+}
+impl FromStr for ExtStr {
+ type Err = &'static str;
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let out: Vec<_> = s.split('=').collect();
+ match out.len() {
+ 1 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: std::env::var(out[0]).or(Err("missing env var"))?,
+ }),
+ 2 => Ok(ExtStr {
+ name: out[0].to_owned(),
+ value: out[1].to_owned(),
+ }),
+ _ => Err("bad ext-str syntax"),
+ }
+ }
+}
+
#[derive(Clap)]
#[clap(version = "0.1.0", author = "Lach <iam@lach.pw>")]
struct Opts {
#[clap(long, about = "Disable global std variable")]
no_stdlib: bool,
#[clap(long, about = "Add external string")]
- ext_str: Option<Vec<String>>,
+ ext_str: Vec<ExtStr>,
#[clap(long, about = "Add external string from code")]
- ext_code: Option<Vec<String>>,
+ ext_code: Vec<ExtStr>,
#[clap(long, about = "Add TLA")]
- tla_str: Option<Vec<String>>,
+ tla_str: Vec<ExtStr>,
#[clap(long, about = "Add TLA from code")]
- tla_code: Option<Vec<String>>,
+ tla_code: Vec<ExtStr>,
#[clap(long, short = "f", default_value = "json", possible_values = &["none", "json", "yaml"], about = "Output format, wraps resulting value to corresponding std.manifest call")]
format: Format,
#[clap(long, default_value = "default", possible_values = &["cpp", "go", "default"], about = "Emulated needed stacktrace display")]
@@ -92,6 +115,12 @@
if !opts.no_stdlib {
evaluator.with_stdlib();
}
+ for ExtStr { name, value } in opts.ext_str.iter().cloned() {
+ evaluator.add_ext_var(name, Val::Str(value));
+ }
+ for ExtStr { name, value } in opts.ext_code.iter().cloned() {
+ evaluator.add_ext_var(name, evaluator.parse_evaluate_raw(&value).unwrap());
+ }
let mut input = current_dir().unwrap();
input.push(opts.input.clone());
let code_string = String::from_utf8(std::fs::read(opts.input.clone()).unwrap()).unwrap();
crates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/error.rs
+++ b/crates/jsonnet-evaluator/src/error.rs
@@ -12,6 +12,8 @@
TooManyArgsFunctionHas(usize),
FunctionParameterNotBoundInCall(String),
+ UndefinedExternalVariable(String),
+
RuntimeError(String),
StackOverflow,
FractionalIndex,
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -533,6 +533,20 @@
panic!("bad pow call");
}
}
+ ("std", "extVar") => {
+ assert_eq!(args.len(), 1);
+ if let Val::Str(a) = evaluate(context, &args[0].1)? {
+ with_state(|s| s.0.ext_vars.borrow().get(&a).cloned()).ok_or_else(
+ || {
+ create_error::<()>(crate::Error::UndefinedExternalVariable(a))
+ .err()
+ .unwrap()
+ },
+ )?
+ } else {
+ panic!("bad extVar call");
+ }
+ }
(ns, name) => panic!("Intristic not found: {}.{}", ns, name),
},
Val::Func(f) => {
crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![feature(stmt_expr_attributes)]6mod ctx;7mod dynamic;8mod error;9mod evaluate;10mod function;11mod map;12mod obj;13mod val;1415pub use ctx::*;16pub use dynamic::*;17pub use error::*;18pub use evaluate::*;19pub use function::parse_function_call;20use jsonnet_parser::*;21pub use obj::*;22use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};23pub use val::*;2425rc_fn_helper!(26 Binding,27 binding,28 dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>29);3031type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;32#[derive(Clone)]33pub enum LazyBinding {34 Bindable(Rc<BindableFn>),35 Bound(LazyVal),36}3738impl Debug for LazyBinding {39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 write!(f, "LazyBinding")41 }42}43impl LazyBinding {44 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {45 match self {46 LazyBinding::Bindable(v) => v(this, super_obj),47 LazyBinding::Bound(v) => Ok(v.clone()),48 }49 }50}5152pub struct EvaluationSettings {53 max_stack_frames: usize,54 max_stack_trace_size: usize,55}56impl Default for EvaluationSettings {57 fn default() -> Self {58 EvaluationSettings {59 max_stack_frames: 200,60 max_stack_trace_size: 20,61 }62 }63}6465pub struct FileData(String, LocExpr, Option<Val>);66#[derive(Default)]67pub struct EvaluationStateInternals {68 /// Used for stack-overflows and stacktraces69 stack: RefCell<Vec<StackTraceElement>>,70 /// Contains file source codes and evaluated results for imports and pretty71 /// printing stacktraces72 files: RefCell<HashMap<PathBuf, FileData>>,73 globals: RefCell<HashMap<String, Val>>,7475 settings: EvaluationSettings,76}7778thread_local! {79 /// Contains state for currently executing file80 /// Global state is fine there81 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)82}83#[inline(always)]84pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {85 EVAL_STATE.with(86 #[inline(always)]87 |s| f(s.borrow().as_ref().unwrap()),88 )89}90pub(crate) fn create_error<T>(err: Error) -> Result<T> {91 with_state(|s| s.error(err))92}93#[inline(always)]94pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {95 with_state(|s| s.push(e, comment, f))96}9798/// Maintains stack trace and import resolution99#[derive(Default, Clone)]100pub struct EvaluationState(Rc<EvaluationStateInternals>);101impl EvaluationState {102 pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {103 self.0.files.borrow_mut().insert(104 name.clone(),105 FileData(106 code.clone(),107 parse(108 &code,109 &ParserSettings {110 file_name: name,111 loc_data: true,112 },113 )?,114 None,115 ),116 );117118 Ok(())119 }120 pub fn add_parsed_file(121 &self,122 name: PathBuf,123 code: String,124 parsed: LocExpr,125 ) -> std::result::Result<(), ()> {126 self.0127 .files128 .borrow_mut()129 .insert(name, FileData(code, parsed, None));130131 Ok(())132 }133 pub fn get_source(&self, name: &PathBuf) -> Option<String> {134 let ro_map = self.0.files.borrow();135 ro_map.get(name).map(|value| value.0.clone())136 }137 pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {138 self.begin_state();139 let expr: LocExpr = {140 let ro_map = self.0.files.borrow();141 let value = ro_map142 .get(name)143 .unwrap_or_else(|| panic!("file not added: {:?}", name));144 if value.2.is_some() {145 return Ok(value.2.clone().unwrap());146 }147 value.1.clone()148 };149 let value = evaluate(self.create_default_context()?, &expr)?;150 {151 self.0152 .files153 .borrow_mut()154 .get_mut(name)155 .unwrap()156 .2157 .replace(value.clone());158 }159 self.end_state();160 Ok(value)161 }162163 pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {164 let parsed = parse(165 &code,166 &ParserSettings {167 file_name: PathBuf::from("raw.jsonnet"),168 loc_data: true,169 },170 );171 self.begin_state();172 let value = evaluate(self.create_default_context()?, &parsed.unwrap());173 self.end_state();174 value175 }176177 pub fn add_global(&self, name: String, value: Val) {178 self.0.globals.borrow_mut().insert(name, value);179 }180181 pub fn with_stdlib(&self) -> &Self {182 self.begin_state();183 use jsonnet_stdlib::STDLIB_STR;184 if cfg!(feature = "serialized-stdlib") {185 self.add_parsed_file(186 PathBuf::from("std.jsonnet"),187 STDLIB_STR.to_owned(),188 bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))189 .expect("deserialize stdlib"),190 )191 .unwrap();192 } else {193 self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())194 .unwrap();195 }196 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();197 self.add_global("std".to_owned(), val);198 self.end_state();199 self200 }201202 pub fn create_default_context(&self) -> Result<Context> {203 let globals = self.0.globals.borrow();204 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();205 for (name, value) in globals.iter() {206 new_bindings.insert(207 name.clone(),208 LazyBinding::Bound(resolved_lazy_val!(value.clone())),209 );210 }211 Context::new().extend_unbound(new_bindings, None, None, None)212 }213214 #[inline(always)]215 pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {216 {217 let mut stack = self.0.stack.borrow_mut();218 if stack.len() > self.0.settings.max_stack_frames {219 drop(stack);220 return self.error(Error::StackOverflow);221 } else {222 stack.push(StackTraceElement(e, comment));223 }224 }225 let result = f();226 self.0.stack.borrow_mut().pop();227 result228 }229 pub fn print_stack_trace(&self) {230 for e in self.stack_trace().0 {231 println!("{:?} - {:?}", e.0, e.1)232 }233 }234 pub fn stack_trace(&self) -> StackTrace {235 StackTrace(236 self.0237 .stack238 .borrow()239 .iter()240 .rev()241 .take(self.0.settings.max_stack_trace_size)242 .cloned()243 .collect(),244 )245 }246 pub fn error<T>(&self, err: Error) -> Result<T> {247 Err(LocError(err, self.stack_trace()))248 }249250 fn begin_state(&self) {251 EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));252 }253 fn end_state(&self) {254 EVAL_STATE.with(|v| v.borrow_mut().take());255 }256}257258#[cfg(test)]259pub mod tests {260 use super::Val;261 use crate::EvaluationState;262 use jsonnet_parser::*;263 use std::path::PathBuf;264265 #[test]266 fn eval_state_stacktrace() {267 let state = EvaluationState::default();268 state269 .push(270 loc_expr!(271 Expr::Num(0.0),272 true,273 (PathBuf::from("test1.jsonnet"), 10, 20)274 ),275 "outer".to_owned(),276 || {277 state.push(278 loc_expr!(279 Expr::Num(0.0),280 true,281 (PathBuf::from("test2.jsonnet"), 30, 40)282 ),283 "inner".to_owned(),284 || {285 state.print_stack_trace();286 Ok(())287 },288 )?;289 Ok(())290 },291 )292 .unwrap();293 }294295 #[test]296 fn eval_state_standard() {297 let state = EvaluationState::default();298 state.with_stdlib();299 assert_eq!(300 state301 .parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)302 .unwrap(),303 Val::Bool(true)304 );305 }306307 macro_rules! eval {308 ($str: expr) => {309 EvaluationState::default()310 .with_stdlib()311 .parse_evaluate_raw($str)312 .unwrap()313 };314 }315 macro_rules! eval_json {316 ($str: expr) => {{317 let evaluator = EvaluationState::default();318 evaluator.with_stdlib();319 let val = evaluator.parse_evaluate_raw($str).unwrap();320 evaluator.add_global("__tmp__to_yaml__".to_owned(), val);321 evaluator322 .parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")323 .unwrap()324 .try_cast_str("there should be json string")325 .unwrap()326 .clone()327 .replace("\n", "")328 }};329 }330331 /// Asserts given code returns `true`332 macro_rules! assert_eval {333 ($str: expr) => {334 assert_eq!(eval!($str), Val::Bool(true))335 };336 }337338 /// Asserts given code returns `false`339 macro_rules! assert_eval_neg {340 ($str: expr) => {341 assert_eq!(eval!($str), Val::Bool(false))342 };343 }344 macro_rules! assert_json {345 ($str: expr, $out: expr) => {346 assert_eq!(eval_json!($str), $out.replace("\t", ""))347 };348 }349350 /// Sanity checking, before trusting to another tests351 #[test]352 fn equality_operator() {353 assert_eval!("2 == 2");354 assert_eval_neg!("2 != 2");355 assert_eval!("2 != 3");356 assert_eval_neg!("2 == 3");357 assert_eval!("'Hello' == 'Hello'");358 assert_eval_neg!("'Hello' != 'Hello'");359 assert_eval!("'Hello' != 'World'");360 assert_eval_neg!("'Hello' == 'World'");361 }362363 #[test]364 fn math_evaluation() {365 assert_eval!("2 + 2 * 2 == 6");366 assert_eval!("3 + (2 + 2 * 2) == 9");367 }368369 #[test]370 fn string_concat() {371 assert_eval!("'Hello' + 'World' == 'HelloWorld'");372 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");373 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");374 }375376 #[test]377 fn function_contexts() {378 assert_eval!(379 r#"380 local k = {381 t(name = self.h): [self.h, name],382 h: 3,383 };384 local f = {385 t: k.t(),386 h: 4,387 };388 f.t[0] == f.t[1]389 "#390 );391 }392393 #[test]394 fn local() {395 assert_eval!("local a = 2; local b = 3; a + b == 5");396 assert_eval!("local a = 1, b = a + 1; a + b == 3");397 assert_eval!("local a = 1; local a = 2; a == 2");398 }399400 #[test]401 fn object_lazyness() {402 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);403 }404405 /// FIXME: This test gets stackoverflow in debug build406 #[test]407 fn object_inheritance() {408 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);409 }410411 #[test]412 fn test_object() {413 assert_json!("{a:2}", r#"{"a": 2}"#);414 assert_json!("{a:2+2}", r#"{"a": 4}"#);415 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);416 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);417 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);418 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);419 assert_json!(420 r#"421 {422 name: "Alice",423 welcome: "Hello " + self.name + "!",424 }425 "#,426 r#"{"name": "Alice","welcome": "Hello Alice!"}"#427 );428 assert_json!(429 r#"430 {431 name: "Alice",432 welcome: "Hello " + self.name + "!",433 } + {434 name: "Bob"435 }436 "#,437 r#"{"name": "Bob","welcome": "Hello Bob!"}"#438 );439 }440441 #[test]442 fn functions() {443 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");444 assert_json!(445 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,446 r#""HelloDearWorld""#447 );448 }449450 #[test]451 fn local_methods() {452 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");453 assert_json!(454 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,455 r#""HelloDearWorld""#456 );457 }458459 #[test]460 fn object_locals() {461 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);462 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);463 assert_json!(464 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,465 r#"{"test": {"test": 4}}"#466 );467 }468469 #[test]470 fn direct_self() {471 println!(472 "{:#?}",473 eval!(474 r#"475 {476 local me = self,477 a: 3,478 b(): me.a,479 }480 "#481 )482 );483 }484485 #[test]486 fn indirect_self() {487 // `self` assigned to `me` was lost when being488 // referenced from field489 eval!(490 r#"{491 local me = self,492 a: 3,493 b: me.a,494 }.b"#495 );496 }497498 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly499 #[test]500 fn std_assert_ok() {501 eval!("std.assertEqual(4.5 << 2, 16)");502 }503504 #[test]505 #[should_panic]506 fn std_assert_failure() {507 eval!("std.assertEqual(4.5 << 2, 15)");508 }509510 #[test]511 fn string_is_string() {512 assert_eq!(513 eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),514 Val::Bool(false)515 );516 }517518 #[test]519 fn base64_works() {520 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);521 }522523 #[test]524 fn utf8_chars() {525 assert_json!(526 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,527 r#"{"c": 128526,"l": 1}"#528 )529 }530531 #[test]532 fn json() {533 assert_json!(534 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,535 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#536 );537 }538539 #[test]540 fn test() {541 assert_json!(542 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,543 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"544 );545 }546547 #[test]548 fn sjsonnet() {549 eval!(550 r#"551 local x0 = {k: 1};552 local x1 = {k: x0.k + x0.k};553 local x2 = {k: x1.k + x1.k};554 local x3 = {k: x2.k + x2.k};555 local x4 = {k: x3.k + x3.k};556 local x5 = {k: x4.k + x4.k};557 local x6 = {k: x5.k + x5.k};558 local x7 = {k: x6.k + x6.k};559 local x8 = {k: x7.k + x7.k};560 local x9 = {k: x8.k + x8.k};561 local x10 = {k: x9.k + x9.k};562 local x11 = {k: x10.k + x10.k};563 local x12 = {k: x11.k + x11.k};564 local x13 = {k: x12.k + x12.k};565 local x14 = {k: x13.k + x13.k};566 local x15 = {k: x14.k + x14.k};567 local x16 = {k: x15.k + x15.k};568 local x17 = {k: x16.k + x16.k};569 local x18 = {k: x17.k + x17.k};570 local x19 = {k: x18.k + x18.k};571 local x20 = {k: x19.k + x19.k};572 local x21 = {k: x20.k + x20.k};573 x21.k574 "#575 );576 }577}1#![feature(box_syntax, box_patterns)]2#![feature(type_alias_impl_trait)]3#![feature(debug_non_exhaustive)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]5#![feature(stmt_expr_attributes)]6mod ctx;7mod dynamic;8mod error;9mod evaluate;10mod function;11mod map;12mod obj;13mod val;1415pub use ctx::*;16pub use dynamic::*;17pub use error::*;18pub use evaluate::*;19pub use function::parse_function_call;20use jsonnet_parser::*;21pub use obj::*;22use std::{cell::RefCell, collections::HashMap, fmt::Debug, path::PathBuf, rc::Rc};23pub use val::*;2425rc_fn_helper!(26 Binding,27 binding,28 dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<Val>29);3031type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;32#[derive(Clone)]33pub enum LazyBinding {34 Bindable(Rc<BindableFn>),35 Bound(LazyVal),36}3738impl Debug for LazyBinding {39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {40 write!(f, "LazyBinding")41 }42}43impl LazyBinding {44 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {45 match self {46 LazyBinding::Bindable(v) => v(this, super_obj),47 LazyBinding::Bound(v) => Ok(v.clone()),48 }49 }50}5152pub struct EvaluationSettings {53 max_stack_frames: usize,54 max_stack_trace_size: usize,55}56impl Default for EvaluationSettings {57 fn default() -> Self {58 EvaluationSettings {59 max_stack_frames: 200,60 max_stack_trace_size: 20,61 }62 }63}6465pub struct FileData(String, LocExpr, Option<Val>);66#[derive(Default)]67pub struct EvaluationStateInternals {68 /// Used for stack-overflows and stacktraces69 stack: RefCell<Vec<StackTraceElement>>,70 /// Contains file source codes and evaluated results for imports and pretty71 /// printing stacktraces72 files: RefCell<HashMap<PathBuf, FileData>>,73 globals: RefCell<HashMap<String, Val>>,7475 /// Values to use with std.extVar76 ext_vars: RefCell<HashMap<String, Val>>,7778 settings: EvaluationSettings,79}8081thread_local! {82 /// Contains state for currently executing file83 /// Global state is fine there84 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)85}86#[inline(always)]87pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {88 EVAL_STATE.with(89 #[inline(always)]90 |s| f(s.borrow().as_ref().unwrap()),91 )92}93pub(crate) fn create_error<T>(err: Error) -> Result<T> {94 with_state(|s| s.error(err))95}96#[inline(always)]97pub(crate) fn push<T>(e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {98 with_state(|s| s.push(e, comment, f))99}100101/// Maintains stack trace and import resolution102#[derive(Default, Clone)]103pub struct EvaluationState(Rc<EvaluationStateInternals>);104impl EvaluationState {105 pub fn add_file(&self, name: PathBuf, code: String) -> std::result::Result<(), ParseError> {106 self.0.files.borrow_mut().insert(107 name.clone(),108 FileData(109 code.clone(),110 parse(111 &code,112 &ParserSettings {113 file_name: name,114 loc_data: true,115 },116 )?,117 None,118 ),119 );120121 Ok(())122 }123 pub fn add_parsed_file(124 &self,125 name: PathBuf,126 code: String,127 parsed: LocExpr,128 ) -> std::result::Result<(), ()> {129 self.0130 .files131 .borrow_mut()132 .insert(name, FileData(code, parsed, None));133134 Ok(())135 }136 pub fn get_source(&self, name: &PathBuf) -> Option<String> {137 let ro_map = self.0.files.borrow();138 ro_map.get(name).map(|value| value.0.clone())139 }140 pub fn evaluate_file(&self, name: &PathBuf) -> Result<Val> {141 self.begin_state();142 let expr: LocExpr = {143 let ro_map = self.0.files.borrow();144 let value = ro_map145 .get(name)146 .unwrap_or_else(|| panic!("file not added: {:?}", name));147 if value.2.is_some() {148 return Ok(value.2.clone().unwrap());149 }150 value.1.clone()151 };152 let value = evaluate(self.create_default_context()?, &expr)?;153 {154 self.0155 .files156 .borrow_mut()157 .get_mut(name)158 .unwrap()159 .2160 .replace(value.clone());161 }162 self.end_state();163 Ok(value)164 }165166 pub fn parse_evaluate_raw(&self, code: &str) -> Result<Val> {167 let parsed = parse(168 &code,169 &ParserSettings {170 file_name: PathBuf::from("raw.jsonnet"),171 loc_data: true,172 },173 );174 self.begin_state();175 let value = evaluate(self.create_default_context()?, &parsed.unwrap());176 self.end_state();177 value178 }179180 pub fn add_global(&self, name: String, value: Val) {181 self.0.globals.borrow_mut().insert(name, value);182 }183 pub fn add_ext_var(&self, name: String, value: Val) {184 self.0.ext_vars.borrow_mut().insert(name, value);185 }186187 pub fn with_stdlib(&self) -> &Self {188 self.begin_state();189 use jsonnet_stdlib::STDLIB_STR;190 if cfg!(feature = "serialized-stdlib") {191 self.add_parsed_file(192 PathBuf::from("std.jsonnet"),193 STDLIB_STR.to_owned(),194 bincode::deserialize(include_bytes!(concat!(env!("OUT_DIR"), "/stdlib.bincode")))195 .expect("deserialize stdlib"),196 )197 .unwrap();198 } else {199 self.add_file(PathBuf::from("std.jsonnet"), STDLIB_STR.to_owned())200 .unwrap();201 }202 let val = self.evaluate_file(&PathBuf::from("std.jsonnet")).unwrap();203 self.add_global("std".to_owned(), val);204 self.end_state();205 self206 }207208 pub fn create_default_context(&self) -> Result<Context> {209 let globals = self.0.globals.borrow();210 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();211 for (name, value) in globals.iter() {212 new_bindings.insert(213 name.clone(),214 LazyBinding::Bound(resolved_lazy_val!(value.clone())),215 );216 }217 Context::new().extend_unbound(new_bindings, None, None, None)218 }219220 #[inline(always)]221 pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> Result<T>) -> Result<T> {222 {223 let mut stack = self.0.stack.borrow_mut();224 if stack.len() > self.0.settings.max_stack_frames {225 drop(stack);226 return self.error(Error::StackOverflow);227 } else {228 stack.push(StackTraceElement(e, comment));229 }230 }231 let result = f();232 self.0.stack.borrow_mut().pop();233 result234 }235 pub fn print_stack_trace(&self) {236 for e in self.stack_trace().0 {237 println!("{:?} - {:?}", e.0, e.1)238 }239 }240 pub fn stack_trace(&self) -> StackTrace {241 StackTrace(242 self.0243 .stack244 .borrow()245 .iter()246 .rev()247 .take(self.0.settings.max_stack_trace_size)248 .cloned()249 .collect(),250 )251 }252 pub fn error<T>(&self, err: Error) -> Result<T> {253 Err(LocError(err, self.stack_trace()))254 }255256 fn begin_state(&self) {257 EVAL_STATE.with(|v| v.borrow_mut().replace(self.clone()));258 }259 fn end_state(&self) {260 EVAL_STATE.with(|v| v.borrow_mut().take());261 }262}263264#[cfg(test)]265pub mod tests {266 use super::Val;267 use crate::EvaluationState;268 use jsonnet_parser::*;269 use std::path::PathBuf;270271 #[test]272 fn eval_state_stacktrace() {273 let state = EvaluationState::default();274 state275 .push(276 loc_expr!(277 Expr::Num(0.0),278 true,279 (PathBuf::from("test1.jsonnet"), 10, 20)280 ),281 "outer".to_owned(),282 || {283 state.push(284 loc_expr!(285 Expr::Num(0.0),286 true,287 (PathBuf::from("test2.jsonnet"), 30, 40)288 ),289 "inner".to_owned(),290 || {291 state.print_stack_trace();292 Ok(())293 },294 )?;295 Ok(())296 },297 )298 .unwrap();299 }300301 #[test]302 fn eval_state_standard() {303 let state = EvaluationState::default();304 state.with_stdlib();305 assert_eq!(306 state307 .parse_evaluate_raw(r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#)308 .unwrap(),309 Val::Bool(true)310 );311 }312313 macro_rules! eval {314 ($str: expr) => {315 EvaluationState::default()316 .with_stdlib()317 .parse_evaluate_raw($str)318 .unwrap()319 };320 }321 macro_rules! eval_json {322 ($str: expr) => {{323 let evaluator = EvaluationState::default();324 evaluator.with_stdlib();325 let val = evaluator.parse_evaluate_raw($str).unwrap();326 evaluator.add_global("__tmp__to_yaml__".to_owned(), val);327 evaluator328 .parse_evaluate_raw("std.manifestJsonEx(__tmp__to_yaml__, \"\")")329 .unwrap()330 .try_cast_str("there should be json string")331 .unwrap()332 .clone()333 .replace("\n", "")334 }};335 }336337 /// Asserts given code returns `true`338 macro_rules! assert_eval {339 ($str: expr) => {340 assert_eq!(eval!($str), Val::Bool(true))341 };342 }343344 /// Asserts given code returns `false`345 macro_rules! assert_eval_neg {346 ($str: expr) => {347 assert_eq!(eval!($str), Val::Bool(false))348 };349 }350 macro_rules! assert_json {351 ($str: expr, $out: expr) => {352 assert_eq!(eval_json!($str), $out.replace("\t", ""))353 };354 }355356 /// Sanity checking, before trusting to another tests357 #[test]358 fn equality_operator() {359 assert_eval!("2 == 2");360 assert_eval_neg!("2 != 2");361 assert_eval!("2 != 3");362 assert_eval_neg!("2 == 3");363 assert_eval!("'Hello' == 'Hello'");364 assert_eval_neg!("'Hello' != 'Hello'");365 assert_eval!("'Hello' != 'World'");366 assert_eval_neg!("'Hello' == 'World'");367 }368369 #[test]370 fn math_evaluation() {371 assert_eval!("2 + 2 * 2 == 6");372 assert_eval!("3 + (2 + 2 * 2) == 9");373 }374375 #[test]376 fn string_concat() {377 assert_eval!("'Hello' + 'World' == 'HelloWorld'");378 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");379 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");380 }381382 #[test]383 fn function_contexts() {384 assert_eval!(385 r#"386 local k = {387 t(name = self.h): [self.h, name],388 h: 3,389 };390 local f = {391 t: k.t(),392 h: 4,393 };394 f.t[0] == f.t[1]395 "#396 );397 }398399 #[test]400 fn local() {401 assert_eval!("local a = 2; local b = 3; a + b == 5");402 assert_eval!("local a = 1, b = a + 1; a + b == 3");403 assert_eval!("local a = 1; local a = 2; a == 2");404 }405406 #[test]407 fn object_lazyness() {408 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);409 }410411 /// FIXME: This test gets stackoverflow in debug build412 #[test]413 fn object_inheritance() {414 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);415 }416417 #[test]418 fn test_object() {419 assert_json!("{a:2}", r#"{"a": 2}"#);420 assert_json!("{a:2+2}", r#"{"a": 4}"#);421 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);422 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);423 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);424 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);425 assert_json!(426 r#"427 {428 name: "Alice",429 welcome: "Hello " + self.name + "!",430 }431 "#,432 r#"{"name": "Alice","welcome": "Hello Alice!"}"#433 );434 assert_json!(435 r#"436 {437 name: "Alice",438 welcome: "Hello " + self.name + "!",439 } + {440 name: "Bob"441 }442 "#,443 r#"{"name": "Bob","welcome": "Hello Bob!"}"#444 );445 }446447 #[test]448 fn functions() {449 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");450 assert_json!(451 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,452 r#""HelloDearWorld""#453 );454 }455456 #[test]457 fn local_methods() {458 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");459 assert_json!(460 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,461 r#""HelloDearWorld""#462 );463 }464465 #[test]466 fn object_locals() {467 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);468 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);469 assert_json!(470 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,471 r#"{"test": {"test": 4}}"#472 );473 }474475 #[test]476 fn direct_self() {477 println!(478 "{:#?}",479 eval!(480 r#"481 {482 local me = self,483 a: 3,484 b(): me.a,485 }486 "#487 )488 );489 }490491 #[test]492 fn indirect_self() {493 // `self` assigned to `me` was lost when being494 // referenced from field495 eval!(496 r#"{497 local me = self,498 a: 3,499 b: me.a,500 }.b"#501 );502 }503504 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly505 #[test]506 fn std_assert_ok() {507 eval!("std.assertEqual(4.5 << 2, 16)");508 }509510 #[test]511 #[should_panic]512 fn std_assert_failure() {513 eval!("std.assertEqual(4.5 << 2, 15)");514 }515516 #[test]517 fn string_is_string() {518 assert_eq!(519 eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),520 Val::Bool(false)521 );522 }523524 #[test]525 fn base64_works() {526 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);527 }528529 #[test]530 fn utf8_chars() {531 assert_json!(532 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,533 r#"{"c": 128526,"l": 1}"#534 )535 }536537 #[test]538 fn json() {539 assert_json!(540 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,541 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#542 );543 }544545 #[test]546 fn test() {547 assert_json!(548 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,549 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"550 );551 }552553 #[test]554 fn sjsonnet() {555 eval!(556 r#"557 local x0 = {k: 1};558 local x1 = {k: x0.k + x0.k};559 local x2 = {k: x1.k + x1.k};560 local x3 = {k: x2.k + x2.k};561 local x4 = {k: x3.k + x3.k};562 local x5 = {k: x4.k + x4.k};563 local x6 = {k: x5.k + x5.k};564 local x7 = {k: x6.k + x6.k};565 local x8 = {k: x7.k + x7.k};566 local x9 = {k: x8.k + x8.k};567 local x10 = {k: x9.k + x9.k};568 local x11 = {k: x10.k + x10.k};569 local x12 = {k: x11.k + x11.k};570 local x13 = {k: x12.k + x12.k};571 local x14 = {k: x13.k + x13.k};572 local x15 = {k: x14.k + x14.k};573 local x16 = {k: x15.k + x15.k};574 local x17 = {k: x16.k + x16.k};575 local x18 = {k: x17.k + x17.k};576 local x19 = {k: x18.k + x18.k};577 local x20 = {k: x19.k + x19.k};578 local x21 = {k: x20.k + x20.k};579 x21.k580 "#581 );582 }583}