difftreelog
feat string interning
in: master
20 files changed
bindings/jsonnet/Cargo.tomldiffbeforeafterboth--- a/bindings/jsonnet/Cargo.toml
+++ b/bindings/jsonnet/Cargo.toml
@@ -5,6 +5,7 @@
edition = "2018"
[dependencies]
+jrsonnet-interner = { path = "../../crates/jrsonnet-interner", version = "0.3.3" }
jrsonnet-evaluator = { path = "../../crates/jrsonnet-evaluator", version = "0.3.3" }
jrsonnet-parser = { path = "../../crates/jrsonnet-parser", version = "0.3.3" }
bindings/jsonnet/src/import.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/import.rs
+++ b/bindings/jsonnet/src/import.rs
@@ -4,6 +4,7 @@
error::{Error::*, Result},
throw, EvaluationState, ImportResolver,
};
+use jrsonnet_interner::IStr;
use std::{
any::Any,
cell::RefCell,
@@ -30,7 +31,7 @@
cb: JsonnetImportCallback,
ctx: *mut c_void,
- out: RefCell<HashMap<PathBuf, Rc<str>>>,
+ out: RefCell<HashMap<PathBuf, IStr>>,
}
impl ImportResolver for CallbackImportResolver {
fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
@@ -75,7 +76,7 @@
Ok(Rc::new(found_here_buf))
}
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>> {
+ fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
Ok(self.out.borrow().get(resolved).unwrap().clone())
}
unsafe fn as_any(&self) -> &dyn Any {
@@ -124,7 +125,7 @@
throw!(ImportFileNotFound(from.clone(), path.clone()))
}
}
- fn load_file_contents(&self, id: &PathBuf) -> Result<Rc<str>> {
+ fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = String::new();
file.read_to_string(&mut out)
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -8,6 +8,7 @@
use import::NativeImportResolver;
use jrsonnet_evaluator::{EvaluationState, ManifestFormat, Val};
+use jrsonnet_interner::IStr;
use std::{
alloc::Layout,
ffi::{CStr, CString},
@@ -161,7 +162,7 @@
})
}
-fn multi_to_raw(multi: Vec<(Rc<str>, Rc<str>)>) -> *const c_char {
+fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {
let mut out = Vec::new();
for (i, (k, v)) in multi.iter().enumerate() {
if i != 0 {
@@ -237,7 +238,7 @@
})
}
-fn stream_to_raw(multi: Vec<Rc<str>>) -> *const c_char {
+fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {
let mut out = Vec::new();
for (i, v) in multi.iter().enumerate() {
if i != 0 {
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -12,8 +12,6 @@
serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
# Allow to convert Val into serde_json::Value and backwards
serde-json = ["serde", "serde_json"]
-# Same as above, but with generated code instead of serde. Reduces memory usage, but increases binary size and compilation time
-codegenerated-stdlib = []
# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
# Library works fine without this feature, but requires more memory and time for std function calls
faster = []
@@ -24,6 +22,7 @@
unstable = []
[dependencies]
+jrsonnet-interner = { path = "../jrsonnet-interner" }
jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.3.3" }
jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.3" }
jrsonnet-types = { path = "../jrsonnet-types", version = "0.3.3" }
@@ -58,8 +57,7 @@
optional = true
[build-dependencies]
-jrsonnet-parser = { path = "../jrsonnet-parser", features = ["dump", "serialize", "deserialize"], version = "0.3.3" }
+jrsonnet-parser = { path = "../jrsonnet-parser", features = ["serialize", "deserialize"], version = "0.3.3" }
jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.3" }
-structdump = "0.1.2"
serde = "1.0"
bincode = "1.3.1"
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -10,7 +10,6 @@
path::{Path, PathBuf},
rc::Rc,
};
-use structdump::CodegenResult;
fn main() {
let parsed = parse(
@@ -36,11 +35,11 @@
name: FieldName::Fixed(name),
..
})
- if **name == *"join" || **name == *"manifestJsonEx" ||
- **name == *"escapeStringJson" || **name == *"equals" ||
- **name == *"base64" || **name == *"foldl" || **name == *"foldr" ||
- **name == *"sortImpl" || **name == *"format" || **name == *"range" ||
- **name == *"reverse" || **name == *"slice" || **name == *"mod"
+ if name == "join" || name == "manifestJsonEx" ||
+ name == "escapeStringJson" || name == "equals" ||
+ name == "base64" || name == "foldl" || name == "foldr" ||
+ name == "sortImpl" || name == "format" || name == "range" ||
+ name == "reverse" || name == "slice" || name == "mod"
)
})
.collect(),
@@ -52,15 +51,6 @@
} else {
parsed
};
- {
- let mut codegen = CodegenResult::default();
- let code = codegen.codegen(&parsed);
-
- let out_dir = env::var("OUT_DIR").unwrap();
- let dest_path = Path::new(&out_dir).join("stdlib.rs");
- let mut f = File::create(&dest_path).unwrap();
- f.write_all(&code.as_bytes()).unwrap();
- }
{
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("stdlib.bincode");
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -2,6 +2,7 @@
#![allow(clippy::too_many_arguments)]
use crate::{error::Error::*, throw, LocError, ObjValue, Result, Val};
+use jrsonnet_interner::IStr;
use jrsonnet_types::ValType;
use thiserror::Error;
@@ -20,7 +21,7 @@
#[error("mapping keys required")]
MappingKeysRequired,
#[error("no such format field: {0}")]
- NoSuchFormatField(Rc<str>),
+ NoSuchFormatField(IStr),
}
impl From<FormatError> for LocError {
@@ -29,7 +30,6 @@
}
}
-use std::rc::Rc;
use FormatError::*;
type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;
@@ -680,7 +680,7 @@
}
Element::Code(c) => {
// TODO: Operate on ref
- let f: Rc<str> = c.mkey.into();
+ let f: IStr = c.mkey.into();
let width = match c.width {
Width::Star => {
throw!(CannotUseStarWidthWithObject);
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -6,6 +6,7 @@
with_state, ArrValue, Context, FuncVal, LazyVal, Val,
};
use format::{format_arr, format_obj};
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};
use jrsonnet_types::ty;
use std::{collections::HashMap, path::PathBuf, rc::Rc};
@@ -19,7 +20,7 @@
pub mod manifest;
pub mod sort;
-fn std_format(str: Rc<str>, vals: Val) -> Result<Val> {
+fn std_format(str: IStr, vals: Val) -> Result<Val> {
push(
&Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
|| format!("std.format of {}", str),
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -2,6 +2,7 @@
error::Error::*, future_wrapper, map::LayeredHashMap, rc_fn_helper, resolved_lazy_val,
LazyBinding, LazyVal, ObjValue, Result, Val,
};
+use jrsonnet_interner::IStr;
use rustc_hash::FxHashMap;
use std::hash::BuildHasherDefault;
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
@@ -18,7 +19,7 @@
dollar: Option<ObjValue>,
this: Option<ObjValue>,
super_obj: Option<ObjValue>,
- bindings: LayeredHashMap<Rc<str>, LazyVal>,
+ bindings: LayeredHashMap<IStr, LazyVal>,
}
impl Debug for ContextInternals {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -57,7 +58,7 @@
}))
}
- pub fn binding(&self, name: Rc<str>) -> Result<LazyVal> {
+ pub fn binding(&self, name: IStr) -> Result<LazyVal> {
Ok(self
.0
.bindings
@@ -72,7 +73,7 @@
ctx.unwrap()
}
- pub fn with_var(self, name: Rc<str>, value: Val) -> Self {
+ pub fn with_var(self, name: IStr, value: Val) -> Self {
let mut new_bindings =
FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
new_bindings.insert(name, resolved_lazy_val!(value));
@@ -81,7 +82,7 @@
pub fn extend(
self,
- new_bindings: FxHashMap<Rc<str>, LazyVal>,
+ new_bindings: FxHashMap<IStr, LazyVal>,
new_dollar: Option<ObjValue>,
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
@@ -123,7 +124,7 @@
}
pub fn extend_unbound(
self,
- new_bindings: HashMap<Rc<str>, LazyBinding>,
+ new_bindings: HashMap<IStr, LazyBinding>,
new_dollar: Option<ObjValue>,
new_this: Option<ObjValue>,
new_super_obj: Option<ObjValue>,
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -2,6 +2,7 @@
builtin::{format::FormatError, sort::SortError},
typed::TypeLocError,
};
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};
use jrsonnet_types::ValType;
use std::{path::PathBuf, rc::Rc};
@@ -10,7 +11,7 @@
#[derive(Error, Debug, Clone)]
pub enum Error {
#[error("intrinsic not found: {0}")]
- IntrinsicNotFound(Rc<str>),
+ IntrinsicNotFound(IStr),
#[error("argument reordering in intrisics not supported yet")]
IntrinsicArgumentReorderingIsNotSupportedYet,
@@ -33,36 +34,36 @@
ArrayBoundsError(usize, usize),
#[error("assert failed: {0}")]
- AssertionFailed(Rc<str>),
+ AssertionFailed(IStr),
#[error("variable is not defined: {0}")]
- VariableIsNotDefined(Rc<str>),
+ VariableIsNotDefined(IStr),
#[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]
TypeMismatch(&'static str, Vec<ValType>, ValType),
#[error("no such field: {0}")]
- NoSuchField(Rc<str>),
+ NoSuchField(IStr),
#[error("only functions can be called, got {0}")]
OnlyFunctionsCanBeCalledGot(ValType),
#[error("parameter {0} is not defined")]
UnknownFunctionParameter(String),
#[error("argument {0} is already bound")]
- BindingParameterASecondTime(Rc<str>),
+ BindingParameterASecondTime(IStr),
#[error("too many args, function has {0}")]
TooManyArgsFunctionHas(usize),
#[error("founction argument is not passed: {0}")]
- FunctionParameterNotBoundInCall(Rc<str>),
+ FunctionParameterNotBoundInCall(IStr),
#[error("external variable is not defined: {0}")]
- UndefinedExternalVariable(Rc<str>),
+ UndefinedExternalVariable(IStr),
#[error("native is not defined: {0}")]
- UndefinedExternalFunction(Rc<str>),
+ UndefinedExternalFunction(IStr),
#[error("field name should be string, got {0}")]
FieldMustBeStringGot(ValType),
#[error("attempted to index array with string {0}")]
- AttemptedIndexAnArrayWithString(Rc<str>),
+ AttemptedIndexAnArrayWithString(IStr),
#[error("{0} index type should be {1}, got {2}")]
ValueIndexMustBeTypeGot(ValType, ValType, ValType),
#[error("cant index into {0}")]
@@ -86,12 +87,12 @@
)]
ImportSyntaxError {
path: Rc<PathBuf>,
- source_code: Rc<str>,
+ source_code: IStr,
error: Box<jrsonnet_parser::ParseError>,
},
#[error("runtime error: {0}")]
- RuntimeError(Rc<str>),
+ RuntimeError(IStr),
#[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
StackOverflow,
#[error("tried to index by fractional value")]
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -3,6 +3,7 @@
ContextCreator, FuncDesc, FuncVal, LazyBinding, LazyVal, ObjMember, ObjValue, Result, Val,
};
use closure::closure;
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, ExprLocation, FieldMember,
ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, UnaryOpType,
@@ -12,7 +13,7 @@
use rustc_hash::FxHashMap;
use std::{collections::HashMap, rc::Rc};
-pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (Rc<str>, LazyBinding) {
+pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {
let b = b.clone();
if let Some(params) = &b.params {
let params = params.clone();
@@ -45,7 +46,7 @@
}
}
-pub fn evaluate_method(ctx: Context, name: Rc<str>, params: ParamsDesc, body: LocExpr) -> Val {
+pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
Val::Func(Rc::new(FuncVal::Normal(FuncDesc {
name,
ctx,
@@ -57,7 +58,7 @@
pub fn evaluate_field_name(
context: Context,
field_name: &jrsonnet_parser::FieldName,
-) -> Result<Option<Rc<str>>> {
+) -> Result<Option<IStr>> {
Ok(match field_name {
jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
jrsonnet_parser::FieldName::Dyn(expr) => {
@@ -182,7 +183,7 @@
})
}
-future_wrapper!(HashMap<Rc<str>, LazyBinding>, FutureNewBindings);
+future_wrapper!(HashMap<IStr, LazyBinding>, FutureNewBindings);
future_wrapper!(ObjValue, FutureObjValue);
pub fn evaluate_comp<T>(
@@ -230,7 +231,7 @@
})
);
{
- let mut bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();
+ let mut bindings: HashMap<IStr, LazyBinding> = HashMap::new();
for (n, b) in members
.iter()
.filter_map(|m| match m {
@@ -334,7 +335,7 @@
)?)
})
);
- let mut bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();
+ let mut bindings: HashMap<IStr, LazyBinding> = HashMap::new();
for (n, b) in obj
.pre_locals
.iter()
@@ -401,7 +402,7 @@
})
}
-pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: Rc<str>) -> Result<Val> {
+pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {
use Expr::*;
let LocExpr(expr, _loc) = lexpr;
Ok(match &**expr {
@@ -498,7 +499,7 @@
}
}
LocalExpr(bindings, returned) => {
- let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();
+ let mut new_bindings: HashMap<IStr, LazyBinding> = HashMap::new();
let future_context = Context::new_future();
let context_creator = context_creator!(
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,8 +1,9 @@
use crate::{error::Error::*, evaluate, lazy_val, resolved_lazy_val, throw, Context, Result, Val};
use closure::closure;
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, ParamsDesc};
use rustc_hash::FxHashMap;
-use std::{collections::HashMap, hash::BuildHasherDefault, rc::Rc};
+use std::{collections::HashMap, hash::BuildHasherDefault};
const NO_DEFAULT_CONTEXT: &str =
"no default context set for call with defined default parameter value";
@@ -66,7 +67,7 @@
ctx: Context,
body_ctx: Option<Context>,
params: &ParamsDesc,
- args: &HashMap<Rc<str>, Val>,
+ args: &HashMap<IStr, Val>,
tailstrict: bool,
) -> Result<Context> {
let mut out = FxHashMap::with_capacity_and_hasher(params.len(), BuildHasherDefault::default());
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -3,6 +3,7 @@
throw,
};
use fs::File;
+use jrsonnet_interner::IStr;
use std::fs;
use std::io::Read;
use std::{any::Any, cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc};
@@ -15,7 +16,7 @@
fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>>;
/// Reads file from filesystem, should be used only with path received from `resolve_file`
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>>;
+ fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr>;
/// # Safety
///
@@ -32,7 +33,7 @@
throw!(ImportNotSupported(from.clone(), path.clone()))
}
- fn load_file_contents(&self, _resolved: &PathBuf) -> Result<Rc<str>> {
+ fn load_file_contents(&self, _resolved: &PathBuf) -> Result<IStr> {
// Can be only caused by library direct consumer, not by supplied jsonnet
panic!("dummy resolver can't load any file")
}
@@ -72,7 +73,7 @@
throw!(ImportFileNotFound(from.clone(), path.clone()))
}
}
- fn load_file_contents(&self, id: &PathBuf) -> Result<Rc<str>> {
+ fn load_file_contents(&self, id: &PathBuf) -> Result<IStr> {
let mut file = File::open(id).map_err(|_e| ResolvedFileNotFound(id.clone()))?;
let mut out = String::new();
file.read_to_string(&mut out)
@@ -89,7 +90,7 @@
/// Caches results of the underlying resolver
pub struct CachingImportResolver {
resolution_cache: RefCell<HashMap<ResolutionData, Result<Rc<PathBuf>>>>,
- loading_cache: RefCell<HashMap<PathBuf, Result<Rc<str>>>>,
+ loading_cache: RefCell<HashMap<PathBuf, Result<IStr>>>,
inner: Box<dyn ImportResolver>,
}
impl ImportResolver for CachingImportResolver {
@@ -101,7 +102,7 @@
.clone()
}
- fn load_file_contents(&self, resolved: &PathBuf) -> Result<Rc<str>> {
+ fn load_file_contents(&self, resolved: &PathBuf) -> Result<IStr> {
self.loading_cache
.borrow_mut()
.entry(resolved.clone())
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]2#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]3#![warn(clippy::all, clippy::nursery)]45mod builtin;6mod ctx;7mod dynamic;8pub mod error;9mod evaluate;10mod function;11mod import;12mod integrations;13mod map;14pub mod native;15mod obj;16pub mod trace;17mod typed;18mod val;1920pub use ctx::*;21pub use dynamic::*;22use error::{Error::*, LocError, Result, StackTraceElement};23pub use evaluate::*;24pub use function::parse_function_call;25pub use import::*;26use jrsonnet_parser::*;27use native::NativeCallback;28pub use obj::*;29use std::{30 cell::{Ref, RefCell, RefMut},31 collections::HashMap,32 fmt::Debug,33 path::PathBuf,34 rc::Rc,35};36use trace::{offset_to_location, CodeLocation, CompactFormat, TraceFormat};37pub use val::*;3839type BindableFn = dyn Fn(Option<ObjValue>, Option<ObjValue>) -> Result<LazyVal>;40#[derive(Clone)]41pub enum LazyBinding {42 Bindable(Rc<BindableFn>),43 Bound(LazyVal),44}4546impl Debug for LazyBinding {47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {48 write!(f, "LazyBinding")49 }50}51impl LazyBinding {52 pub fn evaluate(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {53 match self {54 Self::Bindable(v) => v(this, super_obj),55 Self::Bound(v) => Ok(v.clone()),56 }57 }58}5960pub struct EvaluationSettings {61 /// Limits recursion by limiting the number of stack frames62 pub max_stack: usize,63 /// Limits amount of stack trace items preserved64 pub max_trace: usize,65 /// Used for s`td.extVar`66 pub ext_vars: HashMap<Rc<str>, Val>,67 /// Used for ext.native68 pub ext_natives: HashMap<Rc<str>, Rc<NativeCallback>>,69 /// TLA vars70 pub tla_vars: HashMap<Rc<str>, Val>,71 /// Global variables are inserted in default context72 pub globals: HashMap<Rc<str>, Val>,73 /// Used to resolve file locations/contents74 pub import_resolver: Box<dyn ImportResolver>,75 /// Used in manifestification functions76 pub manifest_format: ManifestFormat,77 /// Used for bindings78 pub trace_format: Box<dyn TraceFormat>,79}80impl Default for EvaluationSettings {81 fn default() -> Self {82 Self {83 max_stack: 200,84 max_trace: 20,85 globals: Default::default(),86 ext_vars: Default::default(),87 ext_natives: Default::default(),88 tla_vars: Default::default(),89 import_resolver: Box::new(DummyImportResolver),90 manifest_format: ManifestFormat::Json(4),91 trace_format: Box::new(CompactFormat {92 padding: 4,93 resolver: trace::PathResolver::Absolute,94 }),95 }96 }97}9899#[derive(Default)]100struct EvaluationData {101 /// Used for stack overflow detection, stacktrace is populated on unwind102 stack_depth: usize,103 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces104 files: HashMap<Rc<PathBuf>, FileData>,105 str_files: HashMap<Rc<PathBuf>, Rc<str>>,106}107108pub struct FileData {109 source_code: Rc<str>,110 parsed: LocExpr,111 evaluated: Option<Val>,112}113#[derive(Default)]114pub struct EvaluationStateInternals {115 /// Internal state116 data: RefCell<EvaluationData>,117 /// Settings, safe to change at runtime118 settings: RefCell<EvaluationSettings>,119}120121thread_local! {122 /// Contains the state for a currently executed file.123 /// Global state is fine here.124 pub(crate) static EVAL_STATE: RefCell<Option<EvaluationState>> = RefCell::new(None)125}126pub(crate) fn with_state<T>(f: impl FnOnce(&EvaluationState) -> T) -> T {127 EVAL_STATE.with(|s| f(s.borrow().as_ref().unwrap()))128}129pub(crate) fn push<T>(130 e: &Option<ExprLocation>,131 frame_desc: impl FnOnce() -> String,132 f: impl FnOnce() -> Result<T>,133) -> Result<T> {134 if let Some(v) = e {135 with_state(|s| s.push(v, frame_desc, f))136 } else {137 f()138 }139}140141/// Maintains stack trace and import resolution142#[derive(Default, Clone)]143pub struct EvaluationState(Rc<EvaluationStateInternals>);144145impl EvaluationState {146 /// Parses and adds file as loaded147 pub fn add_file(&self, path: Rc<PathBuf>, source_code: Rc<str>) -> Result<()> {148 self.add_parsed_file(149 path.clone(),150 source_code.clone(),151 parse(152 &source_code,153 &ParserSettings {154 file_name: path.clone(),155 loc_data: true,156 },157 )158 .map_err(|error| ImportSyntaxError {159 error: Box::new(error),160 path,161 source_code,162 })?,163 )?;164165 Ok(())166 }167168 /// Adds file by source code and parsed expr169 pub fn add_parsed_file(170 &self,171 name: Rc<PathBuf>,172 source_code: Rc<str>,173 parsed: LocExpr,174 ) -> Result<()> {175 self.data_mut().files.insert(176 name,177 FileData {178 source_code,179 parsed,180 evaluated: None,181 },182 );183184 Ok(())185 }186 pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {187 let ro_map = &self.data().files;188 ro_map.get(name).map(|value| value.source_code.clone())189 }190 pub fn map_source_locations(&self, file: &PathBuf, locs: &[usize]) -> Vec<CodeLocation> {191 offset_to_location(&self.get_source(file).unwrap(), locs)192 }193194 pub(crate) fn import_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Val> {195 let file_path = self.resolve_file(from, path)?;196 {197 let data = self.data();198 let files = &data.files;199 if files.contains_key(&file_path) {200 drop(data);201 return self.evaluate_loaded_file_raw(&file_path);202 }203 }204 let contents = self.load_file_contents(&file_path)?;205 self.add_file(file_path.clone(), contents)?;206 self.evaluate_loaded_file_raw(&file_path)207 }208 pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {209 let path = self.resolve_file(from, path)?;210 if !self.data().str_files.contains_key(&path) {211 let file_str = self.load_file_contents(&path)?;212 self.data_mut().str_files.insert(path.clone(), file_str);213 }214 Ok(self.data().str_files.get(&path).cloned().unwrap())215 }216217 fn evaluate_loaded_file_raw(&self, name: &PathBuf) -> Result<Val> {218 let expr: LocExpr = {219 let ro_map = &self.data().files;220 let value = ro_map221 .get(name)222 .unwrap_or_else(|| panic!("file not added: {:?}", name));223 if let Some(ref evaluated) = value.evaluated {224 return Ok(evaluated.clone());225 }226 value.parsed.clone()227 };228 let value = evaluate(self.create_default_context()?, &expr)?;229 {230 self.data_mut()231 .files232 .get_mut(name)233 .unwrap()234 .evaluated235 .replace(value.clone());236 }237 Ok(value)238 }239240 /// Adds standard library global variable (std) to this evaluator241 pub fn with_stdlib(&self) -> &Self {242 use jrsonnet_stdlib::STDLIB_STR;243 let std_path = Rc::new(PathBuf::from("std.jsonnet"));244 self.run_in_state(|| {245 self.add_parsed_file(246 std_path.clone(),247 STDLIB_STR.to_owned().into(),248 builtin::get_parsed_stdlib(),249 )250 .unwrap();251 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();252 self.settings_mut().globals.insert("std".into(), val);253 });254 self255 }256257 /// Creates context with all passed global variables258 pub fn create_default_context(&self) -> Result<Context> {259 let globals = &self.settings().globals;260 let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();261 for (name, value) in globals.iter() {262 new_bindings.insert(263 name.clone(),264 LazyBinding::Bound(resolved_lazy_val!(value.clone())),265 );266 }267 Context::new().extend_unbound(new_bindings, None, None, None)268 }269270 /// Executes code creating a new stack frame271 pub fn push<T>(272 &self,273 e: &ExprLocation,274 frame_desc: impl FnOnce() -> String,275 f: impl FnOnce() -> Result<T>,276 ) -> Result<T> {277 {278 let mut data = self.data_mut();279 let stack_depth = &mut data.stack_depth;280 if *stack_depth > self.max_stack() {281 // Error creation uses data, so i drop guard here282 drop(data);283 throw!(StackOverflow);284 } else {285 *stack_depth += 1;286 }287 }288 let result = f();289 self.data_mut().stack_depth -= 1;290 if let Err(mut err) = result {291 err.trace_mut().0.push(StackTraceElement {292 location: e.clone(),293 desc: frame_desc(),294 });295 return Err(err);296 }297 result298 }299300 /// Runs passed function in state (required if function needs to modify stack trace)301 pub fn run_in_state<T>(&self, f: impl FnOnce() -> T) -> T {302 EVAL_STATE.with(|v| {303 let has_state = v.borrow().is_some();304 if !has_state {305 v.borrow_mut().replace(self.clone());306 }307 let result = f();308 if !has_state {309 v.borrow_mut().take();310 }311 result312 })313 }314315 pub fn stringify_err(&self, e: &LocError) -> String {316 let mut out = String::new();317 self.settings()318 .trace_format319 .write_trace(&mut out, self, e)320 .unwrap();321 out322 }323324 pub fn manifest(&self, val: Val) -> Result<Rc<str>> {325 self.run_in_state(|| val.manifest(&self.manifest_format()))326 }327 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(Rc<str>, Rc<str>)>> {328 self.run_in_state(|| val.manifest_multi(&self.manifest_format()))329 }330 pub fn manifest_stream(&self, val: Val) -> Result<Vec<Rc<str>>> {331 self.run_in_state(|| val.manifest_stream(&self.manifest_format()))332 }333334 /// If passed value is function then call with set TLA335 pub fn with_tla(&self, val: Val) -> Result<Val> {336 self.run_in_state(|| {337 Ok(match val {338 Val::Func(func) => func.evaluate_map(339 self.create_default_context()?,340 &self.settings().tla_vars,341 true,342 )?,343 v => v,344 })345 })346 }347}348349/// Internals350impl EvaluationState {351 fn data(&self) -> Ref<EvaluationData> {352 self.0.data.borrow()353 }354 fn data_mut(&self) -> RefMut<EvaluationData> {355 self.0.data.borrow_mut()356 }357 pub fn settings(&self) -> Ref<EvaluationSettings> {358 self.0.settings.borrow()359 }360 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {361 self.0.settings.borrow_mut()362 }363}364365/// Raw methods evaluate passed values but don't perform TLA execution366impl EvaluationState {367 pub fn evaluate_file_raw(&self, name: &PathBuf) -> Result<Val> {368 self.run_in_state(|| self.import_file(&std::env::current_dir().expect("cwd"), name))369 }370 pub fn evaluate_file_raw_nocwd(&self, name: &PathBuf) -> Result<Val> {371 self.run_in_state(|| self.import_file(&PathBuf::from("."), name))372 }373 /// Parses and evaluates the given snippet374 pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: Rc<str>) -> Result<Val> {375 let parsed = parse(376 &code,377 &ParserSettings {378 file_name: source.clone(),379 loc_data: true,380 },381 )382 .unwrap();383 self.add_parsed_file(source, code, parsed.clone())?;384 self.evaluate_expr_raw(parsed)385 }386 /// Evaluates the parsed expression387 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {388 self.run_in_state(|| evaluate(self.create_default_context()?, &code))389 }390}391392/// Settings utilities393impl EvaluationState {394 pub fn add_ext_var(&self, name: Rc<str>, value: Val) {395 self.settings_mut().ext_vars.insert(name, value);396 }397 pub fn add_ext_str(&self, name: Rc<str>, value: Rc<str>) {398 self.add_ext_var(name, Val::Str(value));399 }400 pub fn add_ext_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {401 let value =402 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;403 self.add_ext_var(name, value);404 Ok(())405 }406407 pub fn add_tla(&self, name: Rc<str>, value: Val) {408 self.settings_mut().tla_vars.insert(name, value);409 }410 pub fn add_tla_str(&self, name: Rc<str>, value: Rc<str>) {411 self.add_tla(name, Val::Str(value));412 }413 pub fn add_tla_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {414 let value =415 self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;416 self.add_tla(name, value);417 Ok(())418 }419420 pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {421 Ok(self.settings().import_resolver.resolve_file(from, path)?)422 }423 pub fn load_file_contents(&self, path: &PathBuf) -> Result<Rc<str>> {424 Ok(self.settings().import_resolver.load_file_contents(path)?)425 }426427 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {428 Ref::map(self.settings(), |s| &*s.import_resolver)429 }430 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {431 self.settings_mut().import_resolver = resolver;432 }433434 pub fn add_native(&self, name: Rc<str>, cb: Rc<NativeCallback>) {435 self.settings_mut().ext_natives.insert(name, cb);436 }437438 pub fn manifest_format(&self) -> ManifestFormat {439 self.settings().manifest_format.clone()440 }441 pub fn set_manifest_format(&self, format: ManifestFormat) {442 self.settings_mut().manifest_format = format;443 }444445 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {446 Ref::map(self.settings(), |s| &*s.trace_format)447 }448 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {449 self.settings_mut().trace_format = format;450 }451452 pub fn max_trace(&self) -> usize {453 self.settings().max_trace454 }455 pub fn set_max_trace(&self, trace: usize) {456 self.settings_mut().max_trace = trace;457 }458459 pub fn max_stack(&self) -> usize {460 self.settings().max_stack461 }462 pub fn set_max_stack(&self, trace: usize) {463 self.settings_mut().max_stack = trace;464 }465}466467#[cfg(test)]468pub mod tests {469 use super::Val;470 use crate::{error::Error::*, primitive_equals, EvaluationState};471 use jrsonnet_parser::*;472 use std::{path::PathBuf, rc::Rc};473474 #[test]475 #[should_panic]476 fn eval_state_stacktrace() {477 let state = EvaluationState::default();478 state.run_in_state(|| {479 state480 .push(481 &ExprLocation(Rc::new(PathBuf::from("test1.jsonnet")), 10, 20),482 || "outer".to_owned(),483 || {484 state.push(485 &ExprLocation(Rc::new(PathBuf::from("test2.jsonnet")), 30, 40),486 || "inner".to_owned(),487 || Err(RuntimeError("".into()).into()),488 )?;489 Ok(())490 },491 )492 .unwrap();493 });494 }495496 #[test]497 fn eval_state_standard() {498 let state = EvaluationState::default();499 state.with_stdlib();500 assert!(primitive_equals(501 &state502 .evaluate_snippet_raw(503 Rc::new(PathBuf::from("raw.jsonnet")),504 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()505 )506 .unwrap(),507 &Val::Bool(true),508 )509 .unwrap());510 }511512 macro_rules! eval {513 ($str: expr) => {514 EvaluationState::default()515 .with_stdlib()516 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())517 .unwrap()518 };519 }520 macro_rules! eval_json {521 ($str: expr) => {{522 let evaluator = EvaluationState::default();523 evaluator.with_stdlib();524 evaluator.run_in_state(|| {525 evaluator526 .evaluate_snippet_raw(Rc::new(PathBuf::from("raw.jsonnet")), $str.into())527 .unwrap()528 .to_json(0)529 .unwrap()530 .replace("\n", "")531 })532 }};533 }534535 /// Asserts given code returns `true`536 macro_rules! assert_eval {537 ($str: expr) => {538 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())539 };540 }541542 /// Asserts given code returns `false`543 macro_rules! assert_eval_neg {544 ($str: expr) => {545 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())546 };547 }548 macro_rules! assert_json {549 ($str: expr, $out: expr) => {550 assert_eq!(eval_json!($str), $out.replace("\t", ""))551 };552 }553554 /// Sanity checking, before trusting to another tests555 #[test]556 fn equality_operator() {557 assert_eval!("2 == 2");558 assert_eval_neg!("2 != 2");559 assert_eval!("2 != 3");560 assert_eval_neg!("2 == 3");561 assert_eval!("'Hello' == 'Hello'");562 assert_eval_neg!("'Hello' != 'Hello'");563 assert_eval!("'Hello' != 'World'");564 assert_eval_neg!("'Hello' == 'World'");565 }566567 #[test]568 fn math_evaluation() {569 assert_eval!("2 + 2 * 2 == 6");570 assert_eval!("3 + (2 + 2 * 2) == 9");571 }572573 #[test]574 fn string_concat() {575 assert_eval!("'Hello' + 'World' == 'HelloWorld'");576 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");577 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");578 }579580 #[test]581 fn faster_join() {582 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");583 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");584 }585586 #[test]587 fn function_contexts() {588 assert_eval!(589 r#"590 local k = {591 t(name = self.h): [self.h, name],592 h: 3,593 };594 local f = {595 t: k.t(),596 h: 4,597 };598 f.t[0] == f.t[1]599 "#600 );601 }602603 #[test]604 fn local() {605 assert_eval!("local a = 2; local b = 3; a + b == 5");606 assert_eval!("local a = 1, b = a + 1; a + b == 3");607 assert_eval!("local a = 1; local a = 2; a == 2");608 }609610 #[test]611 fn object_lazyness() {612 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);613 }614615 #[test]616 fn object_inheritance() {617 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);618 }619620 #[test]621 fn object_assertion_success() {622 eval!("{assert \"a\" in self} + {a:2}");623 }624625 #[test]626 fn object_assertion_error() {627 eval!("{assert \"a\" in self}");628 }629630 #[test]631 fn lazy_args() {632 eval!("local test(a) = 2; test(error '3')");633 }634635 #[test]636 #[should_panic]637 fn tailstrict_args() {638 eval!("local test(a) = 2; test(error '3') tailstrict");639 }640641 #[test]642 #[should_panic]643 fn no_binding_error() {644 eval!("a");645 }646647 #[test]648 fn test_object() {649 assert_json!("{a:2}", r#"{"a": 2}"#);650 assert_json!("{a:2+2}", r#"{"a": 4}"#);651 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);652 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);653 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);654 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);655 assert_json!(656 r#"657 {658 name: "Alice",659 welcome: "Hello " + self.name + "!",660 }661 "#,662 r#"{"name": "Alice","welcome": "Hello Alice!"}"#663 );664 assert_json!(665 r#"666 {667 name: "Alice",668 welcome: "Hello " + self.name + "!",669 } + {670 name: "Bob"671 }672 "#,673 r#"{"name": "Bob","welcome": "Hello Bob!"}"#674 );675 }676677 #[test]678 fn functions() {679 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");680 assert_json!(681 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,682 r#""HelloDearWorld""#683 );684 }685686 #[test]687 fn local_methods() {688 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");689 assert_json!(690 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,691 r#""HelloDearWorld""#692 );693 }694695 #[test]696 fn object_locals() {697 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);698 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);699 assert_json!(700 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,701 r#"{"test": {"test": 4}}"#702 );703 }704705 #[test]706 fn object_comp() {707 assert_json!(708 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}"#,709 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"710 )711 }712713 #[test]714 fn direct_self() {715 println!(716 "{:#?}",717 eval!(718 r#"719 {720 local me = self,721 a: 3,722 b(): me.a,723 }724 "#725 )726 );727 }728729 #[test]730 fn indirect_self() {731 // `self` assigned to `me` was lost when being732 // referenced from field733 eval!(734 r#"{735 local me = self,736 a: 3,737 b: me.a,738 }.b"#739 );740 }741742 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly743 #[test]744 fn std_assert_ok() {745 eval!("std.assertEqual(4.5 << 2, 16)");746 }747748 #[test]749 #[should_panic]750 fn std_assert_failure() {751 eval!("std.assertEqual(4.5 << 2, 15)");752 }753754 #[test]755 fn string_is_string() {756 assert!(primitive_equals(757 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),758 &Val::Bool(false),759 )760 .unwrap());761 }762763 #[test]764 fn base64_works() {765 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);766 }767768 #[test]769 fn utf8_chars() {770 assert_json!(771 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,772 r#"{"c": 128526,"l": 1}"#773 )774 }775776 #[test]777 fn json() {778 assert_json!(779 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,780 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#781 );782 }783784 #[test]785 fn test() {786 assert_json!(787 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,788 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"789 );790 }791792 #[test]793 fn sjsonnet() {794 eval!(795 r#"796 local x0 = {k: 1};797 local x1 = {k: x0.k + x0.k};798 local x2 = {k: x1.k + x1.k};799 local x3 = {k: x2.k + x2.k};800 local x4 = {k: x3.k + x3.k};801 local x5 = {k: x4.k + x4.k};802 local x6 = {k: x5.k + x5.k};803 local x7 = {k: x6.k + x6.k};804 local x8 = {k: x7.k + x7.k};805 local x9 = {k: x8.k + x8.k};806 local x10 = {k: x9.k + x9.k};807 local x11 = {k: x10.k + x10.k};808 local x12 = {k: x11.k + x11.k};809 local x13 = {k: x12.k + x12.k};810 local x14 = {k: x13.k + x13.k};811 local x15 = {k: x14.k + x14.k};812 local x16 = {k: x15.k + x15.k};813 local x17 = {k: x16.k + x16.k};814 local x18 = {k: x17.k + x17.k};815 local x19 = {k: x18.k + x18.k};816 local x20 = {k: x19.k + x19.k};817 local x21 = {k: x20.k + x20.k};818 x21.k819 "#820 );821 }822823 // This test is commented out by default, because of huge compilation slowdown824 // #[bench]825 // fn bench_codegen(b: &mut Bencher) {826 // b.iter(|| {827 // #[allow(clippy::all)]828 // let stdlib = {829 // use jrsonnet_parser::*;830 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))831 // };832 // stdlib833 // })834 // }835836 /*837 #[bench]838 fn bench_serialize(b: &mut Bencher) {839 b.iter(|| {840 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(841 env!("OUT_DIR"),842 "/stdlib.bincode"843 )))844 .expect("deserialize stdlib")845 })846 }847848 #[bench]849 fn bench_parse(b: &mut Bencher) {850 b.iter(|| {851 jrsonnet_parser::parse(852 jrsonnet_stdlib::STDLIB_STR,853 &jrsonnet_parser::ParserSettings {854 loc_data: true,855 file_name: Rc::new(PathBuf::from("std.jsonnet")),856 },857 )858 })859 }860 */861862 #[test]863 fn equality() {864 println!(865 "{:?}",866 jrsonnet_parser::parse(867 "{ x: 1, y: 2 } == { x: 1, y: 2 }",868 &ParserSettings::default()869 )870 );871 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")872 }873874 #[test]875 fn native_ext() -> crate::error::Result<()> {876 use super::native::NativeCallback;877 let evaluator = EvaluationState::default();878879 evaluator.with_stdlib();880 evaluator.settings_mut().ext_natives.insert(881 "native_add".into(),882 Rc::new(NativeCallback::new(883 ParamsDesc(Rc::new(vec![884 Param("a".into(), None),885 Param("b".into(), None),886 ])),887 |args| match (&args[0], &args[1]) {888 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),889 (_, _) => todo!(),890 },891 )),892 );893 evaluator.evaluate_snippet_raw(894 Rc::new(PathBuf::from("test.jsonnet")),895 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),896 )?;897 Ok(())898 }899900 #[test]901 fn constant_intrinsic() -> crate::error::Result<()> {902 assert_eval!(903 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"904 );905 Ok(())906 }907908 struct TestImportResolver(Rc<str>);909 impl crate::import::ImportResolver for TestImportResolver {910 fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {911 Ok(Rc::new(PathBuf::from("/test")))912 }913914 fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<Rc<str>> {915 Ok(self.0.clone())916 }917918 unsafe fn as_any(&self) -> &dyn std::any::Any {919 panic!()920 }921 }922923 #[test]924 fn issue_23() {925 let state = EvaluationState::default();926 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));927 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));928 }929}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -1,5 +1,6 @@
use crate::{evaluate_add_op, LazyBinding, Result, Val};
use indexmap::IndexMap;
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{ExprLocation, Visibility};
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
@@ -12,11 +13,11 @@
}
// Field => This
-type CacheKey = (Rc<str>, usize);
+type CacheKey = (IStr, usize);
#[derive(Debug)]
pub struct ObjValueInternals {
super_obj: Option<ObjValue>,
- this_entries: Rc<HashMap<Rc<str>, ObjMember>>,
+ this_entries: Rc<HashMap<IStr, ObjMember>>,
value_cache: RefCell<HashMap<CacheKey, Option<Val>>>,
}
#[derive(Clone)]
@@ -33,7 +34,7 @@
}
let mut debug = f.debug_struct("ObjValue");
for (name, member) in self.0.this_entries.iter() {
- debug.field(name, member);
+ debug.field(&name, member);
}
#[cfg(feature = "unstable")]
{
@@ -47,7 +48,7 @@
}
impl ObjValue {
- pub fn new(super_obj: Option<Self>, this_entries: Rc<HashMap<Rc<str>, ObjMember>>) -> Self {
+ pub fn new(super_obj: Option<Self>, this_entries: Rc<HashMap<IStr, ObjMember>>) -> Self {
Self(Rc::new(ObjValueInternals {
super_obj,
this_entries,
@@ -63,7 +64,7 @@
Some(v) => Self::new(Some(v.with_super(super_obj)), self.0.this_entries.clone()),
}
}
- pub fn enum_fields(&self, handler: &impl Fn(&Rc<str>, &Visibility)) {
+ pub fn enum_fields(&self, handler: &impl Fn(&IStr, &Visibility)) {
if let Some(s) = &self.0.super_obj {
s.enum_fields(handler);
}
@@ -71,7 +72,7 @@
handler(name, &member.visibility);
}
}
- pub fn fields_visibility(&self) -> IndexMap<Rc<str>, bool> {
+ pub fn fields_visibility(&self) -> IndexMap<IStr, bool> {
let out = Rc::new(RefCell::new(IndexMap::new()));
self.enum_fields(&|name, visibility| {
let mut out = out.borrow_mut();
@@ -91,7 +92,7 @@
});
Rc::try_unwrap(out).unwrap().into_inner()
}
- pub fn visible_fields(&self) -> Vec<Rc<str>> {
+ pub fn visible_fields(&self) -> Vec<IStr> {
let mut visible_fields: Vec<_> = self
.fields_visibility()
.into_iter()
@@ -101,10 +102,10 @@
visible_fields.sort();
visible_fields
}
- pub fn get(&self, key: Rc<str>) -> Result<Option<Val>> {
+ pub fn get(&self, key: IStr) -> Result<Option<Val>> {
Ok(self.get_raw(key, None)?)
}
- pub(crate) fn get_raw(&self, key: Rc<str>, real_this: Option<&Self>) -> Result<Option<Val>> {
+ pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
let real_this = real_this.unwrap_or(self);
let cache_key = (key.clone(), Rc::as_ptr(&real_this.0) as usize);
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -9,6 +9,7 @@
native::NativeCallback,
throw, with_state, Context, ObjValue, Result,
};
+use jrsonnet_interner::IStr;
use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
@@ -61,7 +62,7 @@
#[derive(Debug, PartialEq)]
pub struct FuncDesc {
- pub name: Rc<str>,
+ pub name: IStr,
pub ctx: Context,
pub params: ParamsDesc,
pub body: LocExpr,
@@ -72,9 +73,9 @@
/// Plain function implemented in jsonnet
Normal(FuncDesc),
/// Standard library function
- Intrinsic(Rc<str>),
+ Intrinsic(IStr),
/// Library functions implemented in native
- NativeExt(Rc<str>, Rc<NativeCallback>),
+ NativeExt(IStr, Rc<NativeCallback>),
}
impl PartialEq for FuncVal {
@@ -91,7 +92,7 @@
pub fn is_ident(&self) -> bool {
matches!(&self, Self::Intrinsic(n) if n as &str == "id")
}
- pub fn name(&self) -> Rc<str> {
+ pub fn name(&self) -> IStr {
match self {
Self::Normal(normal) => normal.name.clone(),
Self::Intrinsic(name) => format!("std.{}", name).into(),
@@ -131,7 +132,7 @@
pub fn evaluate_map(
&self,
call_ctx: Context,
- args: &HashMap<Rc<str>, Val>,
+ args: &HashMap<IStr, Val>,
tailstrict: bool,
) -> Result<Val> {
match self {
@@ -270,7 +271,7 @@
pub enum Val {
Bool(bool),
Null,
- Str(Rc<str>),
+ Str(IStr),
Num(f64),
Arr(ArrValue),
Obj(ObjValue),
@@ -314,7 +315,7 @@
self.assert_type(context, ValType::Bool)?;
Ok(matches_unwrap!(self, Self::Bool(v), v))
}
- pub fn try_cast_str(self, context: &'static str) -> Result<Rc<str>> {
+ pub fn try_cast_str(self, context: &'static str) -> Result<IStr> {
self.assert_type(context, ValType::Str)?;
Ok(matches_unwrap!(self, Self::Str(v), v))
}
@@ -334,7 +335,7 @@
}
}
- pub fn to_string(&self) -> Result<Rc<str>> {
+ pub fn to_string(&self) -> Result<IStr> {
Ok(match self {
Self::Bool(true) => "true".into(),
Self::Bool(false) => "false".into(),
@@ -352,7 +353,7 @@
}
/// Expects value to be object, outputs (key, manifested value) pairs
- pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(Rc<str>, Rc<str>)>> {
+ pub fn manifest_multi(&self, ty: &ManifestFormat) -> Result<Vec<(IStr, IStr)>> {
let obj = match self {
Self::Obj(obj) => obj,
_ => throw!(MultiManifestOutputIsNotAObject),
@@ -370,7 +371,7 @@
}
/// Expects value to be array, outputs manifested values
- pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<Rc<str>>> {
+ pub fn manifest_stream(&self, ty: &ManifestFormat) -> Result<Vec<IStr>> {
let arr = match self {
Self::Arr(a) => a,
_ => throw!(StreamManifestOutputIsNotAArray),
@@ -382,7 +383,7 @@
Ok(out)
}
- pub fn manifest(&self, ty: &ManifestFormat) -> Result<Rc<str>> {
+ pub fn manifest(&self, ty: &ManifestFormat) -> Result<IStr> {
Ok(match ty {
ManifestFormat::YamlStream(format) => {
let arr = match self {
@@ -419,7 +420,7 @@
}
/// For manifestification
- pub fn to_json(&self, padding: usize) -> Result<Rc<str>> {
+ pub fn to_json(&self, padding: usize) -> Result<IStr> {
manifest_json_ex(
self,
&ManifestJsonOptions {
@@ -471,7 +472,7 @@
.try_cast_str("to json")?)
})
}
- pub fn to_yaml(&self, padding: usize) -> Result<Rc<str>> {
+ pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
with_state(|s| {
let ctx = s
.create_default_context()?
crates/jrsonnet-interner/.gitignorediffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-interner/.gitignore
@@ -0,0 +1,2 @@
+/target
+Cargo.lock
crates/jrsonnet-interner/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-interner/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "jrsonnet-interner"
+version = "0.3.3"
+authors = ["Yaroslav Bolyukin <iam@lach.pw>"]
+edition = "2018"
+
+[dependencies]
+serde = { version = "1.0" }
+rustc-hash = "1.1.0"
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -0,0 +1,104 @@
+use rustc_hash::FxHashMap;
+use serde::{Deserialize, Serialize};
+use std::{
+ cell::RefCell,
+ fmt::{self, Display},
+ hash::{BuildHasherDefault, Hash, Hasher},
+ ops::Deref,
+ rc::Rc,
+};
+
+#[derive(Clone, PartialOrd, Ord, Eq)]
+pub struct IStr(Rc<str>);
+
+impl Deref for IStr {
+ type Target = str;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl PartialEq for IStr {
+ fn eq(&self, other: &Self) -> bool {
+ // It is ok, since all IStr should be inlined into same pool
+ Rc::ptr_eq(&self.0, &other.0)
+ }
+}
+
+impl PartialEq<str> for IStr {
+ fn eq(&self, other: &str) -> bool {
+ &self.0 as &str == other
+ }
+}
+
+impl Hash for IStr {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ state.write_usize(Rc::as_ptr(&self.0) as *const () as usize)
+ }
+}
+
+impl Drop for IStr {
+ fn drop(&mut self) {
+ // First reference - current object, second - POOL
+ if Rc::strong_count(&self.0) <= 2 {
+ STR_POOL.with(|pool| pool.borrow_mut().remove(&self.0));
+ }
+ }
+}
+
+impl fmt::Debug for IStr {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{:?}", &self.0)
+ }
+}
+
+impl Display for IStr {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str(&self.0)
+ }
+}
+
+thread_local! {
+ static STR_POOL: RefCell<FxHashMap<Rc<str>, ()>> = RefCell::new(FxHashMap::with_capacity_and_hasher(200, BuildHasherDefault::default()));
+}
+
+impl From<&str> for IStr {
+ fn from(str: &str) -> Self {
+ IStr(STR_POOL.with(|pool| {
+ let mut pool = pool.borrow_mut();
+ if let Some((k, _)) = pool.get_key_value(str) {
+ return k.clone();
+ } else {
+ let rc: Rc<str> = str.into();
+ pool.insert(rc.clone(), ());
+ rc
+ }
+ }))
+ }
+}
+
+impl From<String> for IStr {
+ fn from(str: String) -> Self {
+ (&str as &str).into()
+ }
+}
+
+impl Serialize for IStr {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ (&self.0 as &str).serialize(serializer)
+ }
+}
+
+impl<'de> Deserialize<'de> for IStr {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let s = <&str>::deserialize(deserializer)?;
+ Ok(s.into())
+ }
+}
crates/jrsonnet-parser/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-parser/Cargo.toml
+++ b/crates/jrsonnet-parser/Cargo.toml
@@ -10,16 +10,14 @@
default = []
serialize = ["serde"]
deserialize = ["serde"]
-# Adds ability to dump AST as source code for easy embedding
-dump = ["structdump", "structdump-derive"]
[dependencies]
+jrsonnet-interner = { path = "../jrsonnet-interner" }
+
peg = "0.6.3"
unescape = "0.1.0"
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
-structdump = { version = "0.1.2", optional = true }
-structdump-derive = { version = "0.1.2", optional = true }
[dev-dependencies]
jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.3.3" }
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -1,3 +1,4 @@
+use jrsonnet_interner::IStr;
#[cfg(feature = "deserialize")]
use serde::Deserialize;
#[cfg(feature = "serialize")]
@@ -8,21 +9,17 @@
path::PathBuf,
rc::Rc,
};
-#[cfg(feature = "dump")]
-use structdump_derive::Codegen;
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
pub enum FieldName {
/// {fixed: 2}
- Fixed(Rc<str>),
+ Fixed(IStr),
/// {["dyn"+"amic"]: 3}
Dyn(LocExpr),
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -35,13 +32,11 @@
Unhide,
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
pub struct AssertStmt(pub LocExpr, pub Option<LocExpr>);
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -53,7 +48,6 @@
pub value: LocExpr,
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -63,7 +57,6 @@
AssertStmt(AssertStmt),
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -89,7 +82,6 @@
}
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -147,14 +139,12 @@
}
/// name, default value
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
-pub struct Param(pub Rc<str>, pub Option<LocExpr>);
+pub struct Param(pub IStr, pub Option<LocExpr>);
/// Defined function parameters
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, PartialEq)]
@@ -166,13 +156,11 @@
}
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
pub struct Arg(pub Option<String>, pub LocExpr);
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -184,29 +172,25 @@
}
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct BindSpec {
- pub name: Rc<str>,
+ pub name: IStr,
pub params: Option<ParamsDesc>,
pub value: LocExpr,
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
pub struct IfSpecData(pub LocExpr);
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
-pub struct ForSpecData(pub Rc<str>, pub LocExpr);
+pub struct ForSpecData(pub IStr, pub LocExpr);
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -215,7 +199,6 @@
ForSpec(ForSpecData),
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -227,7 +210,6 @@
pub compspecs: Vec<CompSpec>,
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -236,7 +218,6 @@
ObjComp(ObjComp),
}
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq, Clone, Copy)]
@@ -257,7 +238,6 @@
}
/// Syntax base
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq)]
@@ -265,11 +245,11 @@
Literal(LiteralType),
/// String value: "hello"
- Str(Rc<str>),
+ Str(IStr),
/// Number: 1, 2.0, 2e+20
Num(f64),
/// Variable name: test
- Var(Rc<str>),
+ Var(IStr),
/// Array of expressions: [1, 2, "Hello"]
Arr(Vec<LocExpr>),
@@ -316,7 +296,7 @@
/// function(x) x
Function(ParamsDesc, LocExpr),
/// std.primitiveEquals
- Intrinsic(Rc<str>),
+ Intrinsic(IStr),
/// if true == false then 1 else 2
IfElse {
cond: IfSpecData,
@@ -326,7 +306,6 @@
}
/// file, begin offset, end offset
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Clone, PartialEq)]
@@ -338,7 +317,6 @@
}
/// Holds AST expression and its location in source file
-#[cfg_attr(feature = "dump", derive(Codegen))]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Clone, PartialEq)]