git.delta.rocks / jrsonnet / refs/commits / 80f37a416bf7

difftreelog

style enforce import style

Yaroslav Bolyukin2022-04-20parent: #4c00868.patch.diff
in: master

38 files changed

modified.rustfmt.tomldiffbeforeafterboth
--- a/.rustfmt.toml
+++ b/.rustfmt.toml
@@ -1 +1,3 @@
 hard_tabs = true
+imports_granularity = "crate"
+group_imports = "stdexternalcrate"
modifiedbindings/jsonnet/src/import.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -1,9 +1,5 @@
 //! Import resolution manipulation utilities
 
-use jrsonnet_evaluator::{
-	error::{Error::*, Result},
-	throw, EvaluationState, ImportResolver,
-};
 use std::{
 	any::Any,
 	cell::RefCell,
@@ -17,6 +13,11 @@
 	rc::Rc,
 };
 
+use jrsonnet_evaluator::{
+	error::{Error::*, Result},
+	throw, EvaluationState, ImportResolver,
+};
+
 pub type JsonnetImportCallback = unsafe extern "C" fn(
 	ctx: *mut c_void,
 	base: *const c_char,
modifiedbindings/jsonnet/src/interop.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/interop.rs
+++ b/bindings/jsonnet/src/interop.rs
@@ -1,12 +1,14 @@
 //! Jrsonnet specific additional binding helpers
 
-use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
-use jrsonnet_evaluator::{EvaluationState, Val};
 use std::{
 	ffi::c_void,
 	os::raw::{c_char, c_int},
 };
 
+use jrsonnet_evaluator::{EvaluationState, Val};
+
+use crate::{import::jsonnet_import_callback, native::jsonnet_native_callback};
+
 extern "C" {
 	pub fn _jrsonnet_static_import_callback(
 		ctx: *mut c_void,
modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -8,8 +8,6 @@
 pub mod val_modify;
 pub mod vars_tlas;
 
-use import::NativeImportResolver;
-use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
 use std::{
 	alloc::Layout,
 	ffi::{CStr, CString},
@@ -17,6 +15,9 @@
 	path::PathBuf,
 };
 
+use import::NativeImportResolver;
+use jrsonnet_evaluator::{EvaluationState, IStr, ManifestFormat, Val};
+
 /// WASM stub
 #[cfg(target_arch = "wasm32")]
 #[no_mangle]
modifiedbindings/jsonnet/src/native.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -1,3 +1,11 @@
+use std::{
+	convert::TryFrom,
+	ffi::{c_void, CStr},
+	os::raw::{c_char, c_int},
+	path::Path,
+	rc::Rc,
+};
+
 use gcmodule::Cc;
 use jrsonnet_evaluator::{
 	error::{Error, LocError},
@@ -5,13 +13,6 @@
 	gc::TraceBox,
 	native::{NativeCallback, NativeCallbackHandler},
 	EvaluationState, IStr, Val,
-};
-use std::{
-	convert::TryFrom,
-	ffi::{c_void, CStr},
-	os::raw::{c_char, c_int},
-	path::Path,
-	rc::Rc,
 };
 
 type JsonnetNativeCallback = unsafe extern "C" fn(
modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -1,12 +1,12 @@
 //! Extract values from VM
 
-use jrsonnet_evaluator::{EvaluationState, Val};
-
 use std::{
 	ffi::CString,
 	os::raw::{c_char, c_double, c_int},
 };
 
+use jrsonnet_evaluator::{EvaluationState, Val};
+
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_string(_vm: &EvaluationState, v: &Val) -> *mut c_char {
 	match v {
modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -1,12 +1,13 @@
 //! Create values in VM
 
-use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, ObjValue, Val};
 use std::{
 	ffi::CStr,
 	os::raw::{c_char, c_double, c_int},
 };
 
+use gcmodule::Cc;
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, ObjValue, Val};
+
 /// # Safety
 ///
 /// This function is safe, if received v is a pointer to normal C string
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -2,10 +2,11 @@
 //! Only tested with variables, which haven't altered by code before appearing here
 //! In jrsonnet every value is immutable, and this code is probally broken
 
+use std::{ffi::CStr, os::raw::c_char};
+
 use gcmodule::Cc;
-use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
+use jrsonnet_evaluator::{val::ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
 use jrsonnet_parser::Visibility;
-use std::{ffi::CStr, os::raw::c_char};
 
 /// # Safety
 ///
modifiedbindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -1,8 +1,9 @@
 //! Manipulate external variables and top level arguments
 
-use jrsonnet_evaluator::EvaluationState;
 use std::{ffi::CStr, os::raw::c_char};
 
+use jrsonnet_evaluator::EvaluationState;
+
 /// # Safety
 #[no_mangle]
 pub unsafe extern "C" fn jsonnet_ext_var(
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -1,13 +1,13 @@
+use std::{
+	fs::{create_dir_all, File},
+	io::{Read, Write},
+	path::PathBuf,
+};
+
 use clap::{AppSettings, IntoApp, Parser};
 use clap_complete::Shell;
 use jrsonnet_cli::{ConfigureState, GcOpts, GeneralOpts, InputOpts, ManifestOpts, OutputOpts};
 use jrsonnet_evaluator::{error::LocError, EvaluationState};
-use std::{
-	fs::{create_dir_all, File},
-	io::Read,
-	io::Write,
-	path::PathBuf,
-};
 
 #[cfg(feature = "mimalloc")]
 #[global_allocator]
modifiedcrates/jrsonnet-cli/src/ext.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/ext.rs
+++ b/crates/jrsonnet-cli/src/ext.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{fs::read_to_string, str::FromStr};
+
 use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
-use std::{fs::read_to_string, str::FromStr};
 
+use crate::ConfigureState;
+
 #[derive(Clone)]
 pub struct ExtStr {
 	pub name: String,
modifiedcrates/jrsonnet-cli/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/lib.rs
+++ b/crates/jrsonnet-cli/src/lib.rs
@@ -3,14 +3,14 @@
 mod tla;
 mod trace;
 
+use std::{env, path::PathBuf};
+
+use clap::Parser;
 pub use ext::*;
+use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
 pub use manifest::*;
 pub use tla::*;
 pub use trace::*;
-
-use clap::Parser;
-use jrsonnet_evaluator::{error::Result, EvaluationState, FileImportResolver};
-use std::{env, path::PathBuf};
 
 pub trait ConfigureState {
 	fn configure(&self, state: &EvaluationState) -> Result<()>;
modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -1,8 +1,10 @@
-use crate::ConfigureState;
+use std::{path::PathBuf, str::FromStr};
+
 use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};
-use std::{path::PathBuf, str::FromStr};
 
+use crate::ConfigureState;
+
 pub enum ManifestFormatName {
 	/// Expect string as output, and write them directly
 	String,
modifiedcrates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,7 +1,8 @@
-use crate::{ConfigureState, ExtFile, ExtStr};
 use clap::Parser;
 use jrsonnet_evaluator::{error::Result, EvaluationState};
 
+use crate::{ConfigureState, ExtFile, ExtStr};
+
 #[derive(Parser)]
 #[clap(next_help_heading = "TOP LEVEL ARGUMENTS")]
 pub struct TLAOpts {
modifiedcrates/jrsonnet-cli/src/trace.rsdiffbeforeafterboth
--- a/crates/jrsonnet-cli/src/trace.rs
+++ b/crates/jrsonnet-cli/src/trace.rs
@@ -1,12 +1,14 @@
-use crate::ConfigureState;
+use std::str::FromStr;
+
 use clap::Parser;
 use jrsonnet_evaluator::{
 	error::Result,
 	trace::{CompactFormat, ExplainingFormat, PathResolver},
 	EvaluationState,
 };
-use std::str::FromStr;
 
+use crate::ConfigureState;
+
 #[derive(PartialEq)]
 pub enum TraceFormatName {
 	Compact,
modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,6 +1,3 @@
-use bincode::serialize;
-use jrsonnet_parser::{parse, ParserSettings};
-use jrsonnet_stdlib::STDLIB_STR;
 use std::{
 	env,
 	fs::File,
@@ -8,6 +5,10 @@
 	path::{Path, PathBuf},
 };
 
+use bincode::serialize;
+use jrsonnet_parser::{parse, ParserSettings};
+use jrsonnet_stdlib::STDLIB_STR;
+
 fn main() {
 	let parsed = parse(
 		STDLIB_STR,
modifiedcrates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -1,13 +1,15 @@
 //! faster std.format impl
 #![allow(clippy::too_many_arguments)]
 
-use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use std::convert::TryFrom;
+
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
 use jrsonnet_types::ValType;
-use std::convert::TryFrom;
 use thiserror::Error;
 
+use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+
 #[derive(Debug, Clone, Error, Trace)]
 pub enum FormatError {
 	#[error("truncated format code")]
modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -1,7 +1,7 @@
-use crate::error::Error::*;
-use crate::error::Result;
-use crate::push_description_frame;
-use crate::{throw, Val};
+use crate::{
+	error::{Error::*, Result},
+	push_description_frame, throw, Val,
+};
 
 #[derive(PartialEq, Clone, Copy)]
 pub enum ManifestType {
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,23 +1,25 @@
-use crate::function::{CallLocation, StaticBuiltin};
-use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
-use crate::{
-	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
-	equals,
-	error::{Error::*, Result},
-	operator::evaluate_mod_op,
-	primitive_equals, push_frame, throw,
-	typed::{Either2, Either4},
-	with_state, ArrValue, FuncVal, IndexableVal, Val,
+use std::{
+	collections::HashMap,
+	convert::{TryFrom, TryInto},
 };
-use crate::{Either, ObjValue};
+
 use format::{format_arr, format_obj};
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
 use serde::Deserialize;
 use serde_yaml::DeserializingQuirks;
-use std::collections::HashMap;
-use std::convert::{TryFrom, TryInto};
 
+use crate::{
+	builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
+	error::{Error::*, Result},
+	function::{CallLocation, StaticBuiltin},
+	operator::evaluate_mod_op,
+	push_frame, throw,
+	typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, VecVal, M1},
+	val::{equals, primitive_equals, ArrValue, FuncVal, IndexableVal, Slice},
+	with_state, Either, ObjValue, Val,
+};
+
 pub mod stdlib;
 pub use stdlib::*;
 
@@ -58,12 +60,12 @@
 			}
 
 			Ok(Val::Str(
-			(s.chars()
-				.skip(index)
-				.take(end - index)
-				.step_by(step)
-				.collect::<String>())
-			.into(),
+				(s.chars()
+					.skip(index)
+					.take(end - index)
+					.step_by(step)
+					.collect::<String>())
+				.into(),
 			))
 		}
 		IndexableVal::Arr(arr) => {
modifiedcrates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -1,10 +1,12 @@
+use gcmodule::{Cc, Trace};
+
 use crate::{
 	error::{Error, LocError, Result},
 	throw,
 	typed::Any,
-	FuncVal, Val,
+	val::FuncVal,
+	Val,
 };
-use gcmodule::{Cc, Trace};
 
 #[derive(Debug, Clone, thiserror::Error, Trace)]
 pub enum SortError {
modifiedcrates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -1,6 +1,7 @@
-use jrsonnet_parser::{LocExpr, ParserSettings};
 use std::path::PathBuf;
 
+use jrsonnet_parser::{LocExpr, ParserSettings};
+
 thread_local! {
 	/// To avoid parsing again when issued from the same thread
 	#[allow(unreachable_code)]
modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -1,12 +1,12 @@
-use crate::cc_ptr_eq;
-use crate::gc::GcHashMap;
+use std::fmt::Debug;
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+
 use crate::{
-	error::Error::*, map::LayeredHashMap, FutureWrapper, LazyBinding, LazyVal, ObjValue, Result,
-	Val,
+	cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, FutureWrapper, LazyBinding,
+	LazyVal, ObjValue, Result, Val,
 };
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use std::fmt::Debug;
 
 #[derive(Clone, Trace)]
 pub struct ContextCreator(pub Context, pub FutureWrapper<GcHashMap<IStr, LazyBinding>>);
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -1,16 +1,18 @@
-use crate::{
-	builtin::{format::FormatError, sort::SortError},
-	typed::TypeLocError,
+use std::{
+	path::{Path, PathBuf},
+	rc::Rc,
 };
+
 use gcmodule::Trace;
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
 use jrsonnet_types::ValType;
-use std::{
-	path::{Path, PathBuf},
-	rc::Rc,
+use thiserror::Error;
+
+use crate::{
+	builtin::{format::FormatError, sort::SortError},
+	typed::TypeLocError,
 };
-use thiserror::Error;
 
 #[derive(Error, Debug, Clone, Trace)]
 pub enum Error {
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,15 +1,5 @@
 use std::convert::TryFrom;
 
-use crate::{
-	builtin::{std_slice, BUILTINS},
-	error::Error::*,
-	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
-	function::CallLocation,
-	gc::TraceBox,
-	push_frame, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
-	FutureWrapper, GcHashMap, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder,
-	ObjectAssertion, Result, Val,
-};
 use gcmodule::{Cc, Trace};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{
@@ -17,6 +7,19 @@
 	LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
 };
 use jrsonnet_types::ValType;
+
+use crate::{
+	builtin::{std_slice, BUILTINS},
+	error::Error::*,
+	evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
+	function::CallLocation,
+	gc::TraceBox,
+	push_frame, throw,
+	typed::BoundedUsize,
+	val::{ArrValue, FuncDesc, FuncVal, LazyValValue},
+	with_state, Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal,
+	ObjValue, ObjValueBuilder, ObjectAssertion, Result, Val,
+};
 pub mod operator;
 
 pub fn evaluate_binding_in_future(
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -1,10 +1,11 @@
 use std::convert::TryInto;
 
-use crate::builtin::std_format;
-use crate::{equals, evaluate, Context, Val};
-use crate::{error::Error::*, throw, Result};
 use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
 
+use crate::{
+	builtin::std_format, error::Error::*, evaluate, throw, val::equals, Context, Result, Val,
+};
+
 pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
 	use UnaryOpType::*;
 	use Val::*;
modifiedcrates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,16 +1,19 @@
+use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
+
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+pub use jrsonnet_macros::builtin;
+use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
+
 use crate::{
 	error::{Error::*, LocError},
 	evaluate, evaluate_named,
 	gc::TraceBox,
 	throw,
 	typed::Typed,
-	Context, FutureWrapper, GcHashMap, LazyVal, LazyValValue, Result, Val,
+	val::LazyValValue,
+	Context, FutureWrapper, GcHashMap, LazyVal, Result, Val,
 };
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{ArgsDesc, ExprLocation, LocExpr, ParamsDesc};
-use std::{borrow::Cow, collections::HashMap, convert::TryFrom};
 
 #[derive(Clone, Copy)]
 pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
modifiedcrates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -1,17 +1,20 @@
-use crate::{
-	error::{Error::*, Result},
-	throw,
-};
-use fs::File;
-use jrsonnet_interner::IStr;
-use std::fs;
 use std::{
 	any::Any,
+	convert::TryFrom,
+	fs,
+	io::Read,
 	path::{Path, PathBuf},
 	rc::Rc,
 };
-use std::{convert::TryFrom, io::Read};
 
+use fs::File;
+use jrsonnet_interner::IStr;
+
+use crate::{
+	error::{Error::*, Result},
+	throw,
+};
+
 /// Implements file resolution logic for `import` and `importStr`
 pub trait ImportResolver {
 	/// Resolves real file path, e.g. `(/home/user/manifests, b.libjsonnet)` can correspond
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,9 +1,11 @@
+use std::convert::{TryFrom, TryInto};
+
+use serde_json::{Map, Number, Value};
+
 use crate::{
 	error::{Error::*, LocError, Result},
 	throw, ObjValueBuilder, Val,
 };
-use serde_json::{Map, Number, Value};
-use std::convert::{TryFrom, TryInto};
 
 impl TryFrom<&Val> for Value {
 	type Error = LocError;
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
before · crates/jrsonnet-evaluator/src/lib.rs
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}
after · crates/jrsonnet-evaluator/src/lib.rs
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;16pub mod gc;17mod import;18mod integrations;19mod map;20pub mod native;21mod obj;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27	cell::{Ref, RefCell, RefMut},28	collections::HashMap,29	fmt::Debug,30	path::{Path, PathBuf},31	rc::Rc,32};3334pub use ctx::*;35pub use dynamic::*;36use error::{Error::*, LocError, Result, StackTraceElement};37pub use evaluate::*;38use function::{Builtin, CallLocation, TlaArg};39use gc::{GcHashMap, TraceBox};40use gcmodule::{Cc, Trace, Weak};41pub use import::*;42pub use jrsonnet_interner::IStr;43pub use jrsonnet_parser as parser;44use jrsonnet_parser::*;45pub use obj::*;46use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};47pub use val::{LazyVal, ManifestFormat, Val};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 std::{697		path::{Path, PathBuf},698		rc::Rc,699	};700701	use gcmodule::{Cc, Trace};702	use jrsonnet_parser::*;703704	use super::Val;705	use crate::{706		error::Error::*,707		function::{BuiltinParam, CallLocation},708		gc::TraceBox,709		native::NativeCallbackHandler,710		val::primitive_equals,711		EvaluationState,712	};713714	#[test]715	#[should_panic]716	fn eval_state_stacktrace() {717		let state = EvaluationState::default();718		state.run_in_state(|| {719			state720				.push(721					CallLocation::new(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),722					|| "outer".to_owned(),723					|| {724						state.push(725							CallLocation::new(&ExprLocation(726								PathBuf::from("test2.jsonnet").into(),727								30,728								40,729							)),730							|| "inner".to_owned(),731							|| Err(RuntimeError("".into()).into()),732						)?;733						Ok(Val::Null)734					},735				)736				.unwrap();737		});738	}739740	#[test]741	fn eval_state_standard() {742		let state = EvaluationState::default();743		state.with_stdlib();744		assert!(primitive_equals(745			&state746				.evaluate_snippet_raw(747					PathBuf::from("raw.jsonnet").into(),748					r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()749				)750				.unwrap(),751			&Val::Bool(true),752		)753		.unwrap());754	}755756	macro_rules! eval {757		($str: expr) => {{758			let evaluator = EvaluationState::default();759			evaluator.with_stdlib();760			evaluator.run_in_state(|| {761				evaluator762					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())763					.unwrap()764			})765		}};766	}767	macro_rules! eval_json {768		($str: expr) => {{769			let evaluator = EvaluationState::default();770			evaluator.with_stdlib();771			evaluator.run_in_state(|| {772				evaluator773					.evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())774					.unwrap()775					.to_json(0)776					.unwrap()777					.replace("\n", "")778			})779		}};780	}781782	/// Asserts given code returns `true`783	macro_rules! assert_eval {784		($str: expr) => {785			assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())786		};787	}788789	/// Asserts given code returns `false`790	macro_rules! assert_eval_neg {791		($str: expr) => {792			assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())793		};794	}795	macro_rules! assert_json {796		($str: expr, $out: expr) => {797			assert_eq!(eval_json!($str), $out.replace("\t", ""))798		};799	}800801	/// Sanity checking, before trusting to another tests802	#[test]803	fn equality_operator() {804		assert_eval!("2 == 2");805		assert_eval_neg!("2 != 2");806		assert_eval!("2 != 3");807		assert_eval_neg!("2 == 3");808		assert_eval!("'Hello' == 'Hello'");809		assert_eval_neg!("'Hello' != 'Hello'");810		assert_eval!("'Hello' != 'World'");811		assert_eval_neg!("'Hello' == 'World'");812	}813814	#[test]815	fn math_evaluation() {816		assert_eval!("2 + 2 * 2 == 6");817		assert_eval!("3 + (2 + 2 * 2) == 9");818	}819820	#[test]821	fn string_concat() {822		assert_eval!("'Hello' + 'World' == 'HelloWorld'");823		assert_eval!("'Hello' * 3 == 'HelloHelloHello'");824		assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");825	}826827	#[test]828	fn faster_join() {829		assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");830		assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");831	}832833	#[test]834	fn function_contexts() {835		assert_eval!(836			r#"837				local k = {838					t(name = self.h): [self.h, name],839					h: 3,840				};841				local f = {842					t: k.t(),843					h: 4,844				};845				f.t[0] == f.t[1]846			"#847		);848	}849850	#[test]851	fn local() {852		assert_eval!("local a = 2; local b = 3; a + b == 5");853		assert_eval!("local a = 1, b = a + 1; a + b == 3");854		assert_eval!("local a = 1; local a = 2; a == 2");855	}856857	#[test]858	fn object_lazyness() {859		assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);860	}861862	#[test]863	fn object_inheritance() {864		assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);865	}866867	#[test]868	fn object_assertion_success() {869		eval!("{assert \"a\" in self} + {a:2}");870	}871872	#[test]873	fn object_assertion_error() {874		eval!("{assert \"a\" in self}");875	}876877	#[test]878	fn lazy_args() {879		eval!("local test(a) = 2; test(error '3')");880	}881882	#[test]883	#[should_panic]884	fn tailstrict_args() {885		eval!("local test(a) = 2; test(error '3') tailstrict");886	}887888	#[test]889	#[should_panic]890	fn no_binding_error() {891		eval!("a");892	}893894	#[test]895	fn test_object() {896		assert_json!("{a:2}", r#"{"a": 2}"#);897		assert_json!("{a:2+2}", r#"{"a": 4}"#);898		assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);899		assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);900		assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);901		assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);902		assert_json!(903			r#"904				{905					name: "Alice",906					welcome: "Hello " + self.name + "!",907				}908			"#,909			r#"{"name": "Alice","welcome": "Hello Alice!"}"#910		);911		assert_json!(912			r#"913				{914					name: "Alice",915					welcome: "Hello " + self.name + "!",916				} + {917					name: "Bob"918				}919			"#,920			r#"{"name": "Bob","welcome": "Hello Bob!"}"#921		);922	}923924	#[test]925	fn functions() {926		assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");927		assert_json!(928			r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,929			r#""HelloDearWorld""#930		);931	}932933	#[test]934	fn local_methods() {935		assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");936		assert_json!(937			r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,938			r#""HelloDearWorld""#939		);940	}941942	#[test]943	fn object_locals() {944		assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);945		assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);946		assert_json!(947			r#"{local a = function (b) {[b]:4}, test: a("test")}"#,948			r#"{"test": {"test": 4}}"#949		);950	}951952	#[test]953	fn object_comp() {954		assert_json!(955			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}"#,956			"{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"957		)958	}959960	#[test]961	fn direct_self() {962		println!(963			"{:#?}",964			eval!(965				r#"966					{967						local me = self,968						a: 3,969						b(): me.a,970					}971				"#972			)973		);974	}975976	#[test]977	fn indirect_self() {978		// `self` assigned to `me` was lost when being979		// referenced from field980		eval!(981			r#"{982				local me = self,983				a: 3,984				b: me.a,985			}.b"#986		);987	}988989	// We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly990	#[test]991	fn std_assert_ok() {992		eval!("std.assertEqual(4.5 << 2, 16)");993	}994995	#[test]996	#[should_panic]997	fn std_assert_failure() {998		eval!("std.assertEqual(4.5 << 2, 15)");999	}10001001	#[test]1002	fn string_is_string() {1003		assert!(primitive_equals(1004			&eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),1005			&Val::Bool(false),1006		)1007		.unwrap());1008	}10091010	#[test]1011	fn base64_works() {1012		assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);1013	}10141015	#[test]1016	fn utf8_chars() {1017		assert_json!(1018			r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,1019			r#"{"c": 128526,"l": 1}"#1020		)1021	}10221023	#[test]1024	fn json() {1025		assert_json!(1026			r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,1027			r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#1028		);1029	}10301031	#[test]1032	fn json_minified() {1033		assert_json!(1034			r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,1035			r#""{\"a\":3,\"b\":4,\"c\":6}""#1036		);1037	}10381039	#[test]1040	fn parse_json() {1041		assert_json!(1042			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,1043			r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#1044		);1045	}10461047	#[test]1048	fn test() {1049		assert_json!(1050			r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,1051			"[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"1052		);1053	}10541055	#[test]1056	fn sjsonnet() {1057		eval!(1058			r#"1059			local x0 = {k: 1};1060			local x1 = {k: x0.k + x0.k};1061			local x2 = {k: x1.k + x1.k};1062			local x3 = {k: x2.k + x2.k};1063			local x4 = {k: x3.k + x3.k};1064			local x5 = {k: x4.k + x4.k};1065			local x6 = {k: x5.k + x5.k};1066			local x7 = {k: x6.k + x6.k};1067			local x8 = {k: x7.k + x7.k};1068			local x9 = {k: x8.k + x8.k};1069			local x10 = {k: x9.k + x9.k};1070			local x11 = {k: x10.k + x10.k};1071			local x12 = {k: x11.k + x11.k};1072			local x13 = {k: x12.k + x12.k};1073			local x14 = {k: x13.k + x13.k};1074			local x15 = {k: x14.k + x14.k};1075			local x16 = {k: x15.k + x15.k};1076			local x17 = {k: x16.k + x16.k};1077			local x18 = {k: x17.k + x17.k};1078			local x19 = {k: x18.k + x18.k};1079			local x20 = {k: x19.k + x19.k};1080			local x21 = {k: x20.k + x20.k};1081			x21.k1082		"#1083		);1084	}10851086	// This test is commented out by default, because of huge compilation slowdown1087	// #[bench]1088	// fn bench_codegen(b: &mut Bencher) {1089	// 	b.iter(|| {1090	// 		#[allow(clippy::all)]1091	// 		let stdlib = {1092	// 			use jrsonnet_parser::*;1093	// 			include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1094	// 		};1095	// 		stdlib1096	// 	})1097	// }10981099	/*1100	#[bench]1101	fn bench_serialize(b: &mut Bencher) {1102		b.iter(|| {1103			bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1104				env!("OUT_DIR"),1105				"/stdlib.bincode"1106			)))1107			.expect("deserialize stdlib")1108		})1109	}11101111	#[bench]1112	fn bench_parse(b: &mut Bencher) {1113		b.iter(|| {1114			jrsonnet_parser::parse(1115				jrsonnet_stdlib::STDLIB_STR,1116				&jrsonnet_parser::ParserSettings {1117					loc_data: true,1118					file_name: Rc::new(PathBuf::from("std.jsonnet")),1119				},1120			)1121		})1122	}1123	*/11241125	#[test]1126	fn equality() {1127		println!(1128			"{:?}",1129			jrsonnet_parser::parse(1130				"{ x: 1, y: 2 } == { x: 1, y: 2 }",1131				&ParserSettings {1132					file_name: PathBuf::from("equality").into(),1133				}1134			)1135		);1136		assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1137	}11381139	#[test]1140	fn native_ext() -> crate::error::Result<()> {1141		use super::native::NativeCallback;1142		let evaluator = EvaluationState::default();11431144		evaluator.with_stdlib();11451146		#[derive(Trace)]1147		struct NativeAdd;1148		impl NativeCallbackHandler for NativeAdd {1149			fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {1150				assert_eq!(1151					&from.unwrap() as &Path,1152					&PathBuf::from("native_caller.jsonnet")1153				);1154				match (&args[0], &args[1]) {1155					(Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1156					(_, _) => unreachable!(),1157				}1158			}1159		}1160		evaluator.settings_mut().ext_natives.insert(1161			"native_add".into(),1162			#[allow(deprecated)]1163			Cc::new(TraceBox(Box::new(NativeCallback::new(1164				vec![1165					BuiltinParam {1166						name: "a".into(),1167						has_default: false,1168					},1169					BuiltinParam {1170						name: "b".into(),1171						has_default: false,1172					},1173				],1174				TraceBox(Box::new(NativeAdd)),1175			)))),1176		);1177		evaluator.evaluate_snippet_raw(1178			PathBuf::from("native_caller.jsonnet").into(),1179			"std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1180		)?;1181		Ok(())1182	}11831184	#[test]1185	fn constant_intrinsic() -> crate::error::Result<()> {1186		assert_eval!(1187			"local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1188		);1189		Ok(())1190	}11911192	#[test]1193	fn standalone_super() -> crate::error::Result<()> {1194		assert_eval!(1195			r#"1196			local obj = {1197				a: 1,1198				b: 2,1199				c: 3,1200			};1201			local test = obj + {1202				fields: std.objectFields(super),1203				d: 5,1204			};1205			test.fields == ['a', 'b', 'c']1206		"#1207		);1208		Ok(())1209	}12101211	#[test]1212	fn comp_self() -> crate::error::Result<()> {1213		assert_eval!(1214			r#"1215			std.objectFields({1216				a:{1217					[name]: name for name in std.objectFields(self)1218				},1219				b: 2,1220				c: 3,1221			}.a) == ['a', 'b', 'c']1222			"#1223		);12241225		Ok(())1226	}12271228	struct TestImportResolver(Vec<u8>);1229	impl crate::import::ImportResolver for TestImportResolver {1230		fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1231			Ok(PathBuf::from("/test").into())1232		}12331234		fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {1235			Ok(self.0.clone())1236		}12371238		unsafe fn as_any(&self) -> &dyn std::any::Any {1239			panic!()1240		}12411242		fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {1243			panic!()1244		}1245	}12461247	#[test]1248	fn issue_23() {1249		let state = EvaluationState::default();1250		state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1251		let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1252	}12531254	#[test]1255	fn issue_40() {1256		let state = EvaluationState::default();1257		state.with_stdlib();12581259		let error = state1260			.evaluate_snippet_raw(1261				PathBuf::from("issue40.jsonnet").into(),1262				r#"1263				local conf = {1264					n: ""1265				};12661267				local result = conf + {1268					assert std.isNumber(self.n): "is number"1269				};12701271				std.manifestJsonEx(result, "")1272			"#1273				.into(),1274			)1275			.unwrap_err();1276		assert_eq!(error.error().to_string(), "assert failed: is number");1277	}12781279	#[test]1280	fn test_ascii_upper_lower() {1281		assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1282		assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1283	}12841285	#[test]1286	fn test_member() {1287		assert_eval!(r#"!std.member("", "")"#);1288		assert_eval!(r#"std.member("abc", "a")"#);1289		assert_eval!(r#"!std.member("abc", "d")"#);1290		assert_eval!(r#"!std.member([], "")"#);1291		assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1292		assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1293	}12941295	#[test]1296	fn test_count() {1297		assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1298		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1299		assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1300	}13011302	mod derive_typed {1303		use std::path::PathBuf;13041305		use crate::{typed::Typed, EvaluationState};13061307		#[derive(PartialEq, Debug, Typed)]1308		struct MyTyped {1309			a: u32,1310			#[typed(rename = "b")]1311			c: String,1312		}13131314		#[test]1315		fn test() {1316			let es = EvaluationState::default();1317			let val = eval!("{a: 14, b: 'Hello, world!'}");1318			let typed = es.run_in_state(|| MyTyped::try_from(val).unwrap());13191320			assert_eq!(1321				typed,1322				MyTyped {1323					a: 14,1324					c: "Hello, world!".to_string()1325				}1326			);1327			es.settings_mut().globals.insert(1328				"mytyped".into(),1329				es.run_in_state(|| typed.try_into()).unwrap(),1330			);13311332			let v = es1333				.evaluate_snippet_raw(1334					PathBuf::from("raw.jsonnet").into(),1335					"1336				mytyped == {a: 14, b: 'Hello, world!'}1337			"1338					.into(),1339				)1340				.unwrap()1341				.as_bool()1342				.unwrap();1343			assert!(v)1344		}1345	}1346}
modifiedcrates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,13 +1,16 @@
 #![allow(clippy::type_complexity)]
 
-use crate::function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation};
-use crate::gc::TraceBox;
-use crate::Context;
-use crate::{error::Result, Val};
+use std::{path::Path, rc::Rc};
+
 use gcmodule::Trace;
-use std::path::Path;
-use std::rc::Rc;
 
+use crate::{
+	error::Result,
+	function::{parse_builtin_call, ArgsLike, Builtin, BuiltinParam, CallLocation},
+	gc::TraceBox,
+	Context, Val,
+};
+
 #[derive(Trace)]
 pub struct NativeCallback {
 	pub(crate) params: Vec<BuiltinParam>,
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,20 +1,23 @@
-use crate::error::LocError;
-use crate::function::CallLocation;
-use crate::gc::{GcHashMap, GcHashSet, TraceBox};
-use crate::operator::evaluate_add_op;
-use crate::push_frame;
-use crate::{
-	cc_ptr_eq, error::Error::*, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal,
-	Result, Val,
+use std::{
+	cell::RefCell,
+	fmt::Debug,
+	hash::{Hash, Hasher},
 };
+
 use gcmodule::{Cc, Trace, Weak};
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ExprLocation, Visibility};
 use rustc_hash::FxHashMap;
-use std::cell::RefCell;
-use std::fmt::Debug;
-use std::hash::{Hash, Hasher};
 
+use crate::{
+	cc_ptr_eq,
+	error::{Error::*, LocError},
+	function::CallLocation,
+	gc::{GcHashMap, GcHashSet, TraceBox},
+	operator::evaluate_add_op,
+	push_frame, throw, weak_ptr_eq, weak_raw, Bindable, LazyBinding, LazyVal, Result, Val,
+};
+
 #[derive(Debug, Trace)]
 pub struct ObjMember {
 	pub add: bool,
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -1,8 +1,10 @@
 mod location;
 
+use std::path::{Path, PathBuf};
+
+pub use location::*;
+
 use crate::{error::Error, EvaluationState, LocError};
-pub use location::*;
-use std::path::{Path, PathBuf};
 
 /// The way paths should be displayed
 pub enum PathResolver {
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -2,14 +2,14 @@
 
 mod conversions;
 pub use conversions::*;
+use gcmodule::Trace;
+pub use jrsonnet_types::{ComplexValType, ValType};
+use thiserror::Error;
 
 use crate::{
 	error::{Error, LocError, Result},
 	push_description_frame, Val,
 };
-use gcmodule::Trace;
-pub use jrsonnet_types::{ComplexValType, ValType};
-use thiserror::Error;
 
 #[derive(Debug, Error, Clone, Trace)]
 pub enum TypeError {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,3 +1,10 @@
+use std::{cell::RefCell, fmt::Debug, rc::Rc};
+
+use gcmodule::{Cc, Trace};
+use jrsonnet_interner::IStr;
+use jrsonnet_parser::{LocExpr, ParamsDesc};
+use jrsonnet_types::ValType;
+
 use crate::{
 	builtin::manifest::{
 		manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
@@ -12,11 +19,6 @@
 	gc::TraceBox,
 	throw, Context, ObjValue, Result,
 };
-use gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use jrsonnet_parser::{LocExpr, ParamsDesc};
-use jrsonnet_types::ValType;
-use std::{cell::RefCell, fmt::Debug, rc::Rc};
 
 pub trait LazyValValue: Trace {
 	fn get(self: Box<Self>) -> Result<Val>;
modifiedcrates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -1,6 +1,3 @@
-use gcmodule::Trace;
-use rustc_hash::FxHashMap;
-use serde::{Deserialize, Serialize};
 use std::{
 	borrow::Cow,
 	cell::RefCell,
@@ -12,6 +9,10 @@
 	str::Utf8Error,
 };
 
+use gcmodule::Trace;
+use rustc_hash::FxHashMap;
+use serde::{Deserialize, Serialize};
+
 #[derive(Clone, PartialOrd, Ord, Eq)]
 pub struct IStr(Rc<str>);
 impl Trace for IStr {
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,7 +1,3 @@
-use gcmodule::Trace;
-use jrsonnet_interner::IStr;
-#[cfg(feature = "serde")]
-use serde::{Deserialize, Serialize};
 use std::{
 	fmt::{Debug, Display},
 	ops::Deref,
@@ -9,6 +5,11 @@
 	rc::Rc,
 };
 
+use gcmodule::Trace;
+use jrsonnet_interner::IStr;
+#[cfg(feature = "serde")]
+use serde::{Deserialize, Serialize};
+
 #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
 #[derive(Debug, PartialEq, Trace)]
 pub enum FieldName {
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,10 +1,11 @@
 #![allow(clippy::redundant_closure_call)]
 
-use peg::parser;
 use std::{
 	path::{Path, PathBuf},
 	rc::Rc,
 };
+
+use peg::parser;
 mod expr;
 pub use expr::*;
 pub use jrsonnet_interner::IStr;
@@ -317,11 +318,13 @@
 
 #[cfg(test)]
 pub mod tests {
-	use super::{expr::*, parse};
-	use crate::ParserSettings;
 	use std::path::PathBuf;
+
 	use BinaryOpType::*;
 
+	use super::{expr::*, parse};
+	use crate::ParserSettings;
+
 	macro_rules! parse {
 		($s:expr) => {
 			parse(
modifiedcrates/jrsonnet-types/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -1,8 +1,9 @@
 #![allow(clippy::redundant_closure_call)]
 
-use gcmodule::Trace;
 use std::fmt::Display;
 
+use gcmodule::Trace;
+
 #[macro_export]
 macro_rules! ty {
 	((Array<number>)) => {{