difftreelog
build stable rustc support
in: master
7 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -1,5 +1,3 @@
-#![feature(custom_inner_attributes)]
-
pub mod import;
pub mod interop;
pub mod val_extract;
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -20,6 +20,9 @@
# Rustc-like trace visualization
explaining-traces = ["annotate-snippets"]
+# Unlocks extra features, but works only on unstable
+unstable = []
+
[dependencies]
jrsonnet-parser = { path = "../jrsonnet-parser", version = "1.0.0" }
jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "1.0.0" }
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -2,12 +2,7 @@
error::Error::*, future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val,
LazyBinding, LazyVal, ObjValue, Result, Val,
};
-use std::{
- cell::RefCell,
- collections::HashMap,
- fmt::Debug,
- rc::{Rc, Weak},
-};
+use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
rc_fn_helper!(
ContextCreator,
@@ -138,6 +133,7 @@
}
Ok(self.extend(new, new_dollar, this, super_obj))
}
+ #[cfg(feature = "unstable")]
pub fn into_weak(self) -> WeakContext {
WeakContext(Rc::downgrade(&self.0))
}
@@ -155,13 +151,16 @@
}
}
+#[cfg(feature = "unstable")]
#[derive(Debug, Clone)]
-pub struct WeakContext(Weak<ContextInternals>);
+pub struct WeakContext(std::rc::Weak<ContextInternals>);
+#[cfg(feature = "unstable")]
impl WeakContext {
pub fn upgrade(&self) -> Context {
Context(self.0.upgrade().expect("context is removed"))
}
}
+#[cfg(feature = "unstable")]
impl PartialEq for WeakContext {
fn eq(&self, other: &Self) -> bool {
self.0.ptr_eq(&other.0)
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -390,10 +390,16 @@
/// Extracts code block and disables inlining for them
/// Fixes WASM to java bytecode compilation failing because of very large method
+#[cfg(feature = "unstable")]
+macro_rules! noinline {
+ ($e:expr) => {
+ (#![inline(never)] move || $e)()
+ };
+}
+#[cfg(not(feature = "unstable"))]
macro_rules! noinline {
($e:expr) => {
- (#[inline(never)]
- move || $e)()
+ (move || $e)()
};
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]34mod builtin;5mod ctx;6mod dynamic;7pub mod error;8mod evaluate;9mod function;10mod import;11mod map;12mod obj;13pub mod trace;14mod val;1516pub use ctx::*;17pub use dynamic::*;18use error::{Error::*, LocError, Result, StackTraceElement};19pub use evaluate::*;20pub use function::parse_function_call;21pub use import::*;22use jrsonnet_parser::*;23pub use obj::*;24use std::{25 cell::{Ref, RefCell, RefMut},26 collections::HashMap,27 fmt::Debug,28 path::PathBuf,29 rc::Rc,30};31use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};32pub use val::*;3334type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;35#[derive(Clone)]36pub enum LazyBinding {37 Bindable(Rc<BindableFn>),38 Bound(LazyVal),39}4041impl Debug for LazyBinding {42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {43 write!(f, "LazyBinding")44 }45}46impl LazyBinding {47 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {48 match self {49 LazyBinding::Bindable(v) => v(this, super_obj),50 LazyBinding::Bound(v) => Ok(v.clone()),51 }52 }53}5455#[derive(Clone)]56pub enum ManifestFormat {57 Yaml(usize),58 Json(usize),59 None,60}6162pub struct EvaluationSettings {63 /// Limits recursion by limiting stack frames64 pub max_stack: usize,65 /// Limit amount of stack trace items preserved66 pub max_trace: usize,67 /// Used for std.extVar68 pub ext_vars: HashMap<Rc<str>, Val>,69 /// TLA vars70 pub tla_vars: HashMap<Rc<str>, Val>,71 /// Global variables are inserted in default context72 pub globals: HashMap<Rc<str>, Val>,73 /// Used to resolve file locations/contents74 pub import_resolver: Box<dyn ImportResolver>,75 /// Used in manifestification functions76 pub manifest_format: ManifestFormat,77 /// Used for bindings78 pub trace_format: Box<dyn TraceFormat>,79}80impl Default for EvaluationSettings {81 fn default() -> Self {82 EvaluationSettings {83 max_stack: 200,84 max_trace: 20,85 globals: Default::default(),86 ext_vars: Default::default(),87 tla_vars: Default::default(),88 import_resolver: Box::new(DummyImportResolver),89 manifest_format: ManifestFormat::Json(4),90 trace_format: Box::new(CompactFormat {91 padding: 4,92 resolver: trace::PathResolver::Absolute,93 }),94 }95 }96}9798#[derive(Default)]99struct EvaluationData {100 /// Used for stack overflow detection, stacktrace is now populated on unwind101 stack_depth: usize,102 /// Contains file source codes and evaluated results for imports and pretty103 /// printing stacktraces104 files: HashMap<Rc<PathBuf>, FileData>,105 str_files: HashMap<Rc<PathBuf>, Rc<str>>,106}107108pub struct FileData {109 source_code: Rc<str>,110 parsed: LocExpr,111 evaluated: Option<Val>,112}113#[derive(Default)]114pub struct EvaluationStateInternals {115 /// Internal state116 data: RefCell<EvaluationData>,117 /// Settings, safe to change at runtime118 settings: RefCell<EvaluationSettings>,119}120121thread_local! {122 /// Contains state for currently executing file123 /// Global state is fine there124 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)125}126pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {127 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))128}129pub(crate) fn push<T>(130 e: &Option<ExprLocation>,131 frame_desc: impl FnOnce() -> String,132 f: impl FnOnce() -> Result<T>,133) -> Result<T> {134 if let Some(v) = e {135 with_state(|s| s.push(&v, frame_desc, f))136 } else {137 f()138 }139}140141/// Maintains stack trace and import resolution142#[derive(Default, Clone)]143pub struct EvaluationState(Rc<EvaluationStateInternals>);144145impl EvaluationState {146 /// Parses and adds file to loaded147 pub fn add_file(&self, path: Rc<PathBuf>, source_code: Rc<str>) -> Result<()> {148 self.add_parsed_file(149 path.clone(),150 source_code.clone(),151 parse(152 &source_code,153 &ParserSettings {154 file_name: path.clone(),155 loc_data: true,156 },157 )158 .map_err(|error| ImportSyntaxError {159 error,160 path,161 source_code,162 })?,163 )?;164165 Ok(())166 }167168 /// Adds file by source code and parsed expr169 pub fn add_parsed_file(170 &self,171 name: Rc<PathBuf>,172 source_code: Rc<str>,173 parsed: LocExpr,174 ) -> Result<()> {175 self.data_mut().files.insert(176 name,177 FileData {178 source_code,179 parsed,180 evaluated: None,181 },182 );183184 Ok(())185 }186 pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {187 let ro_map = &self.data().files;188 ro_map.get(name).map(|value| value.source_code.clone())189 }190 pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {191 offset_to_location(&self.get_source(file).unwrap(), locs)192 }193194 pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {195 let file_path = self.resolve_file(from, path)?;196 {197 let files = &self.data().files;198 if files.contains_key(&file_path) {199 return self.evaluate_loaded_file_raw(&file_path);200 }201 }202 let contents = self.load_file_contents(&file_path)?;203 self.add_file(file_path.clone(), contents)?;204 self.evaluate_loaded_file_raw(&file_path)205 }206 pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {207 let path = self.resolve_file(from, path)?;208 if !self.data().str_files.contains_key(&path) {209 let file_str = self.load_file_contents(&path)?;210 self.data_mut().str_files.insert(path.clone(), file_str);211 }212 Ok(self.data().str_files.get(&path).cloned().unwrap())213 }214215 fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {216 let expr: LocExpr = {217 let ro_map = &self.data().files;218 let value = ro_map219 .get(name)220 .unwrap_or_else(|| panic!("file not added: {:?}", name));221 if let Some(ref evaluated) = value.evaluated {222 return Ok(evaluated.clone());223 }224 value.parsed.clone()225 };226 let value = evaluate(self.create_default_context()?, &expr)?;227 {228 self.data_mut()229 .files230 .get_mut(name)231 .unwrap()232 .evaluated233 .replace(value.clone());234 }235 Ok(value)236 }237238 /// Adds standard library global variable (std) to this evaluator239 pub fn with_stdlib(&self) -> &Self {240 use jrsonnet_stdlib::STDLIB_STR;241 let std_path = Rc::new(PathBuf::from("std.jsonnet"));242 self.run_in_state(|| {243 self.add_parsed_file(244 std_path.clone(),245 STDLIB_STR.to_owned().into(),246 builtin::get_parsed_stdlib(),247 )248 .unwrap();249 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();250 self.settings_mut().globals.insert("std".into(), val);251 });252 self253 }254255 /// Creates context with all passed global variables256 pub fn create_default_context(&self) -> Result<Context> {257 let globals = &self.settings().globals;258 let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();259 for (name, value) in globals.iter() {260 new_bindings.insert(261 name.clone(),262 LazyBinding::Bound(resolved_lazy_val!(value.clone())),263 );264 }265 Context::new().extend_unbound(new_bindings, None, None, None)266 }267268 /// Executes code, creating new stack frame269 pub fn push<T>(270 &self,271 e: &ExprLocation,272 frame_desc: impl FnOnce() -> String,273 f: impl FnOnce() -> Result<T>,274 ) -> Result<T> {275 {276 let mut data = self.data_mut();277 let stack_depth = &mut data.stack_depth;278 if *stack_depth > self.max_stack() {279 // Error creation uses data, so i drop guard here280 drop(data);281 throw!(StackOverflow);282 } else {283 *stack_depth += 1;284 }285 }286 let result = f();287 self.data_mut().stack_depth -= 1;288 if let Err(mut err) = result {289 (err.1).0.push(StackTraceElement {290 location: e.clone(),291 desc: frame_desc(),292 });293 return Err(err);294 }295 result296 }297298 /// Runs passed function in state (required, if function needs to modify stack trace)299 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {300 EVAL_STATE.with(|v| {301 let has_state = v.borrow().is_some();302 if !has_state {303 v.borrow_mut().replace(self.clone());304 }305 let result = f();306 if !has_state {307 v.borrow_mut().take();308 }309 result310 })311 }312313 pub fn stringify_err(&self, e: &LocError) -> String {314 let mut out = String::new();315 self.settings()316 .trace_format317 .write_trace(&mut out, self, e)318 .unwrap();319 out320 }321322 pub fn manifest(&self, val: Val) -> Result<Rc<str>> {323 self.run_in_state(|| {324 Ok(match self.manifest_format() {325 ManifestFormat::Yaml(padding) => val.into_yaml(padding)?,326 ManifestFormat::Json(padding) => val.into_json(padding)?,327 ManifestFormat::None => match val {328 Val::Str(s) => s,329 _ => throw!(StringManifestOutputIsNotAString),330 },331 })332 })333 }334335 /// If passed value is function - call with set TLA336 pub fn with_tla(&self, val: Val) -> Result<Val> {337 Ok(match val {338 Val::Func(func) => func.evaluate_map(339 self.create_default_context()?,340 &self.settings().tla_vars,341 true,342 )?,343 v => v,344 })345 }346}347348/// Internals349impl EvaluationState {350 fn data(&self) -> Ref<EvaluationData> {351 self.0.data.borrow()352 }353 fn data_mut(&self) -> RefMut<EvaluationData> {354 self.0.data.borrow_mut()355 }356 pub fn settings(&self) -> Ref<EvaluationSettings> {357 self.0.settings.borrow()358 }359 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {360 self.0.settings.borrow_mut()361 }362}363364/// Raw methods evaluates passed values, but not performs TLA execution365impl EvaluationState {366 pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {367 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), &name))368 }369 pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {370 self.run_in_state(|| self.import_file(&PathBuf::from("."), &name))371 }372 /// Parses and evaluates snippet373 pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: Rc<str>) -> Result<Val> {374 let parsed = parse(375 &code,376 &ParserSettings {377 file_name: source.clone(),378 loc_data: true,379 },380 )381 .unwrap();382 self.add_parsed_file(source, code, parsed.clone())?;383 self.evaluate_expr_raw(parsed)384 }385 /// Evaluates parsed expression386 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {387 self.run_in_state(|| evaluate(self.create_default_context()?, &code))388 }389}390391/// Settings utilities392impl EvaluationState {393 pub fn add_ext_var(&self, name: Rc<str>, value: Val) {394 self.settings_mut().ext_vars.insert(name, value);395 }396 pub fn add_ext_str(&self, name: Rc<str>, value: Rc<str>) {397 self.add_ext_var(name, Val::Str(value));398 }399 pub fn add_ext_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {400 let value =401 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;402 self.add_ext_var(name, value);403 Ok(())404 }405406 pub fn add_tla(&self, name: Rc<str>, value: Val) {407 self.settings_mut().tla_vars.insert(name, value);408 }409 pub fn add_tla_str(&self, name: Rc<str>, value: Rc<str>) {410 self.add_tla(name, Val::Str(value));411 }412 pub fn add_tla_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {413 let value =414 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;415 self.add_ext_var(name, value);416 Ok(())417 }418419 pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {420 Ok(self.settings().import_resolver.resolve_file(from, path)?)421 }422 pub fn load_file_contents(&self, path: &PathBuf) -> Result<Rc<str>> {423 Ok(self.settings().import_resolver.load_file_contents(path)?)424 }425426 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {427 Ref::map(self.settings(), |s| &*s.import_resolver)428 }429 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {430 self.settings_mut().import_resolver = resolver;431 }432433 pub fn manifest_format(&self) -> ManifestFormat {434 self.settings().manifest_format.clone()435 }436 pub fn set_manifest_format(&self, format: ManifestFormat) {437 self.settings_mut().manifest_format = format;438 }439440 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {441 Ref::map(self.settings(), |s| &*s.trace_format)442 }443 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {444 self.settings_mut().trace_format = format;445 }446447 pub fn max_trace(&self) -> usize {448 self.settings().max_trace449 }450 pub fn set_max_trace(&self, trace: usize) {451 self.settings_mut().max_trace = trace;452 }453454 pub fn max_stack(&self) -> usize {455 self.settings().max_stack456 }457 pub fn set_max_stack(&self, trace: usize) {458 self.settings_mut().max_stack = trace;459 }460}461462#[cfg(test)]463pub mod tests {464 use super::Val;465 use crate::{error::Error::*, primitive_equals, EvaluationState};466 use jrsonnet_parser::*;467 use std::{path::PathBuf, rc::Rc};468469 #[test]470 #[should_panic]471 fn eval_state_stacktrace() {472 let state = EvaluationState::default();473 state.run_in_state(|| {474 state475 .push(476 &ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20),477 || "outer".to_owned(),478 || {479 state.push(480 &ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),481 || "inner".to_owned(),482 || Err(RuntimeError("".into()).into()),483 )?;484 Ok(())485 },486 )487 .unwrap();488 });489 }490491 #[test]492 fn eval_state_standard() {493 let state = EvaluationState::default();494 state.with_stdlib();495 assert!(primitive_equals(496 &state497 .evaluate_snippet_raw(498 Rc::new(PathBuf::from("raw.jsonnet")),499 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()500 )501 .unwrap(),502 &Val::Bool(true),503 )504 .unwrap());505 }506507 macro_rules! eval {508 ($str: expr) => {509 EvaluationState::default()510 .with_stdlib()511 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())512 .unwrap()513 };514 }515 macro_rules! eval_json {516 ($str: expr) => {{517 let evaluator = EvaluationState::default();518 evaluator.with_stdlib();519 evaluator.run_in_state(|| {520 evaluator521 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())522 .unwrap()523 .into_json(0)524 .unwrap()525 .replace("\n", "")526 })527 }};528 }529530 /// Asserts given code returns `true`531 macro_rules! assert_eval {532 ($str: expr) => {533 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())534 };535 }536537 /// Asserts given code returns `false`538 macro_rules! assert_eval_neg {539 ($str: expr) => {540 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())541 };542 }543 macro_rules! assert_json {544 ($str: expr, $out: expr) => {545 assert_eq!(eval_json!($str), $out.replace("\t", ""))546 };547 }548549 /// Sanity checking, before trusting to another tests550 #[test]551 fn equality_operator() {552 assert_eval!("2 == 2");553 assert_eval_neg!("2 != 2");554 assert_eval!("2 != 3");555 assert_eval_neg!("2 == 3");556 assert_eval!("'Hello' == 'Hello'");557 assert_eval_neg!("'Hello' != 'Hello'");558 assert_eval!("'Hello' != 'World'");559 assert_eval_neg!("'Hello' == 'World'");560 }561562 #[test]563 fn math_evaluation() {564 assert_eval!("2 + 2 * 2 == 6");565 assert_eval!("3 + (2 + 2 * 2) == 9");566 }567568 #[test]569 fn string_concat() {570 assert_eval!("'Hello' + 'World' == 'HelloWorld'");571 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");572 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");573 }574575 #[test]576 fn faster_join() {577 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");578 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");579 }580581 #[test]582 fn function_contexts() {583 assert_eval!(584 r#"585 local k = {586 t(name = self.h): [self.h, name],587 h: 3,588 };589 local f = {590 t: k.t(),591 h: 4,592 };593 f.t[0] == f.t[1]594 "#595 );596 }597598 #[test]599 fn local() {600 assert_eval!("local a = 2; local b = 3; a + b == 5");601 assert_eval!("local a = 1, b = a + 1; a + b == 3");602 assert_eval!("local a = 1; local a = 2; a == 2");603 }604605 #[test]606 fn object_lazyness() {607 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);608 }609610 #[test]611 fn object_inheritance() {612 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);613 }614615 #[test]616 fn object_assertion_success() {617 eval!("{assert \"a\" in self} + {a:2}");618 }619620 #[test]621 fn object_assertion_error() {622 eval!("{assert \"a\" in self}");623 }624625 #[test]626 fn lazy_args() {627 eval!("local test(a) = 2; test(error '3')");628 }629630 #[test]631 #[should_panic]632 fn tailstrict_args() {633 eval!("local test(a) = 2; test(error '3') tailstrict");634 }635636 #[test]637 #[should_panic]638 fn no_binding_error() {639 eval!("a");640 }641642 #[test]643 fn test_object() {644 assert_json!("{a:2}", r#"{"a": 2}"#);645 assert_json!("{a:2+2}", r#"{"a": 4}"#);646 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);647 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);648 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);649 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);650 assert_json!(651 r#"652 {653 name: "Alice",654 welcome: "Hello " + self.name + "!",655 }656 "#,657 r#"{"name": "Alice","welcome": "Hello Alice!"}"#658 );659 assert_json!(660 r#"661 {662 name: "Alice",663 welcome: "Hello " + self.name + "!",664 } + {665 name: "Bob"666 }667 "#,668 r#"{"name": "Bob","welcome": "Hello Bob!"}"#669 );670 }671672 #[test]673 fn functions() {674 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");675 assert_json!(676 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,677 r#""HelloDearWorld""#678 );679 }680681 #[test]682 fn local_methods() {683 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");684 assert_json!(685 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,686 r#""HelloDearWorld""#687 );688 }689690 #[test]691 fn object_locals() {692 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);693 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);694 assert_json!(695 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,696 r#"{"test": {"test": 4}}"#697 );698 }699700 #[test]701 fn object_comp() {702 assert_json!(703 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}"#,704 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"705 )706 }707708 #[test]709 fn direct_self() {710 println!(711 "{:#?}",712 eval!(713 r#"714 {715 local me = self,716 a: 3,717 b(): me.a,718 }719 "#720 )721 );722 }723724 #[test]725 fn indirect_self() {726 // `self` assigned to `me` was lost when being727 // referenced from field728 eval!(729 r#"{730 local me = self,731 a: 3,732 b: me.a,733 }.b"#734 );735 }736737 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly738 #[test]739 fn std_assert_ok() {740 eval!("std.assertEqual(4.5 << 2, 16)");741 }742743 #[test]744 #[should_panic]745 fn std_assert_failure() {746 eval!("std.assertEqual(4.5 << 2, 15)");747 }748749 #[test]750 fn string_is_string() {751 assert!(primitive_equals(752 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),753 &Val::Bool(false),754 )755 .unwrap());756 }757758 #[test]759 fn base64_works() {760 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);761 }762763 #[test]764 fn utf8_chars() {765 assert_json!(766 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,767 r#"{"c": 128526,"l": 1}"#768 )769 }770771 #[test]772 fn json() {773 assert_json!(774 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,775 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#776 );777 }778779 #[test]780 fn test() {781 assert_json!(782 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,783 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"784 );785 }786787 #[test]788 fn sjsonnet() {789 eval!(790 r#"791 local x0 = {k: 1};792 local x1 = {k: x0.k + x0.k};793 local x2 = {k: x1.k + x1.k};794 local x3 = {k: x2.k + x2.k};795 local x4 = {k: x3.k + x3.k};796 local x5 = {k: x4.k + x4.k};797 local x6 = {k: x5.k + x5.k};798 local x7 = {k: x6.k + x6.k};799 local x8 = {k: x7.k + x7.k};800 local x9 = {k: x8.k + x8.k};801 local x10 = {k: x9.k + x9.k};802 local x11 = {k: x10.k + x10.k};803 local x12 = {k: x11.k + x11.k};804 local x13 = {k: x12.k + x12.k};805 local x14 = {k: x13.k + x13.k};806 local x15 = {k: x14.k + x14.k};807 local x16 = {k: x15.k + x15.k};808 local x17 = {k: x16.k + x16.k};809 local x18 = {k: x17.k + x17.k};810 local x19 = {k: x18.k + x18.k};811 local x20 = {k: x19.k + x19.k};812 local x21 = {k: x20.k + x20.k};813 x21.k814 "#815 );816 }817818 // This test is commented out by default, because of huge compilation slowdown819 // #[bench]820 // fn bench_codegen(b: &mut Bencher) {821 // b.iter(|| {822 // #[allow(clippy::all)]823 // let stdlib = {824 // use jrsonnet_parser::*;825 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))826 // };827 // stdlib828 // })829 // }830831 /*832 #[bench]833 fn bench_serialize(b: &mut Bencher) {834 b.iter(|| {835 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(836 env!("OUT_DIR"),837 "/stdlib.bincode"838 )))839 .expect("deserialize stdlib")840 })841 }842843 #[bench]844 fn bench_parse(b: &mut Bencher) {845 b.iter(|| {846 jrsonnet_parser::parse(847 jrsonnet_stdlib::STDLIB_STR,848 &jrsonnet_parser::ParserSettings {849 loc_data: true,850 file_name: Rc::new(PathBuf::from("std.jsonnet")),851 },852 )853 })854 }855 */856857 #[test]858 fn equality() {859 println!(860 "{:?}",861 jrsonnet_parser::parse(862 "{ x: 1, y: 2 } == { x: 1, y: 2 }",863 &ParserSettings::default()864 )865 );866 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")867 }868}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -35,7 +35,14 @@
for (name, member) in self.0.this_entries.iter() {
debug.field(name, member);
}
- debug.finish_non_exhaustive()
+ #[cfg(feature = "unstable")]
+ {
+ debug.finish_non_exhaustive()
+ }
+ #[cfg(not(feature = "unstable"))]
+ {
+ debug.finish()
+ }
}
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,8 +1,3 @@
-#![feature(box_syntax)]
-#![feature(test)]
-
-extern crate test;
-
use peg::parser;
use std::{path::PathBuf, rc::Rc};
mod expr;
@@ -581,11 +576,11 @@
parse!(jrsonnet_stdlib::STDLIB_STR);
}
- use test::Bencher;
-
// From source code
+ /*
#[bench]
fn bench_parse_peg(b: &mut Bencher) {
b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))
}
+ */
}