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.rsdiffbeforeafterboth1use crate::{2 equals,3 error::{Error::*, Result},4 evaluate, parse_args, primitive_equals, push, throw,5 typed::CheckType,6 with_state, ArrValue, Context, FuncVal, LazyVal, Val,7};8use format::{format_arr, format_obj};9use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};10use jrsonnet_types::ty;11use std::{collections::HashMap, path::PathBuf, rc::Rc};1213pub mod stdlib;14pub use stdlib::*;1516use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1718pub mod format;19pub mod manifest;20pub mod sort;2122fn std_format(str: Rc<str>, vals: Val) -> Result<Val> {23 push(24 &Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),25 || format!("std.format of {}", str),26 || {27 Ok(match vals {28 Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),29 Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),30 o => Val::Str(format_arr(&str, &[o])?.into()),31 })32 },33 )34}3536type Builtin = fn(context: Context, loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val>;3738type BuiltinsType = HashMap<Box<str>, Builtin>;3940thread_local! {41 static BUILTINS: BuiltinsType = {42 [43 ("length".into(), builtin_length as Builtin),44 ("type".into(), builtin_type),45 ("makeArray".into(), builtin_make_array),46 ("codepoint".into(), builtin_codepoint),47 ("objectFieldsEx".into(), builtin_object_fields_ex),48 ("objectHasEx".into(), builtin_object_has_ex),49 ("slice".into(), builtin_slice),50 ("primitiveEquals".into(), builtin_primitive_equals),51 ("equals".into(), builtin_equals),52 ("modulo".into(), builtin_modulo),53 ("mod".into(), builtin_mod),54 ("floor".into(), builtin_floor),55 ("log".into(), builtin_log),56 ("pow".into(), builtin_pow),57 ("extVar".into(), builtin_ext_var),58 ("native".into(), builtin_native),59 ("filter".into(), builtin_filter),60 ("foldl".into(), builtin_foldl),61 ("foldr".into(), builtin_foldr),62 ("sortImpl".into(), builtin_sort_impl),63 ("format".into(), builtin_format),64 ("range".into(), builtin_range),65 ("char".into(), builtin_char),66 ("encodeUTF8".into(), builtin_encode_utf8),67 ("md5".into(), builtin_md5),68 ("base64".into(), builtin_base64),69 ("trace".into(), builtin_trace),70 ("join".into(), builtin_join),71 ("escapeStringJson".into(), builtin_escape_string_json),72 ("manifestJsonEx".into(), builtin_manifest_json_ex),73 ("reverse".into(), builtin_reverse),74 ("id".into(), builtin_id),75 ].iter().cloned().collect()76 };77}7879fn builtin_length(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {80 parse_args!(context, "length", args, 1, [81 0, x: ty!((str | obj | [any]));82 ], {83 Ok(match x {84 Val::Str(n) => Val::Num(n.chars().count() as f64),85 Val::Arr(a) => Val::Num(a.len() as f64),86 Val::Obj(o) => Val::Num(87 o.fields_visibility()88 .into_iter()89 .filter(|(_k, v)| *v)90 .count() as f64,91 ),92 _ => unreachable!(),93 })94 })95}9697fn builtin_type(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {98 parse_args!(context, "type", args, 1, [99 0, x: ty!(any);100 ], {101 Ok(Val::Str(x.value_type().name().into()))102 })103}104105fn builtin_make_array(106 context: Context,107 _loc: &Option<ExprLocation>,108 args: &ArgsDesc,109) -> Result<Val> {110 parse_args!(context, "makeArray", args, 2, [111 0, sz: ty!(number((Some(0.0))..(None))) => Val::Num;112 1, func: ty!(fn.any) => Val::Func;113 ], {114 let mut out = Vec::with_capacity(sz as usize);115 for i in 0..sz as usize {116 out.push(LazyVal::new_resolved(func.evaluate_values(117 Context::new(),118 &[Val::Num(i as f64)]119 )?))120 }121 Ok(Val::Arr(out.into()))122 })123}124125fn builtin_codepoint(126 context: Context,127 _loc: &Option<ExprLocation>,128 args: &ArgsDesc,129) -> Result<Val> {130 parse_args!(context, "codepoint", args, 1, [131 0, str: ty!(char) => Val::Str;132 ], {133 Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))134 })135}136137fn builtin_object_fields_ex(138 context: Context,139 _loc: &Option<ExprLocation>,140 args: &ArgsDesc,141) -> Result<Val> {142 parse_args!(context, "objectFieldsEx", args, 2, [143 0, obj: ty!(obj) => Val::Obj;144 1, inc_hidden: ty!(bool) => Val::Bool;145 ], {146 let mut out = obj.fields_visibility()147 .into_iter()148 .filter(|(_k, v)| *v || inc_hidden)149 .map(|(k, _v)|k)150 .collect::<Vec<_>>();151 out.sort();152 Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))153 })154}155156fn builtin_object_has_ex(157 context: Context,158 _loc: &Option<ExprLocation>,159 args: &ArgsDesc,160) -> Result<Val> {161 parse_args!(context, "objectHasEx", args, 3, [162 0, obj: ty!(obj) => Val::Obj;163 1, f: ty!(str) => Val::Str;164 2, inc_hidden: ty!(bool) => Val::Bool;165 ], {166 Ok(Val::Bool(167 obj.fields_visibility()168 .into_iter()169 .filter(|(_k, v)| *v || inc_hidden)170 .any(|(k, _v)| *k == *f),171 ))172 })173}174175// faster176fn builtin_slice(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {177 parse_args!(context, "slice", args, 4, [178 0, indexable: ty!((str | [any]));179 1, index: ty!((num | null));180 2, end: ty!((num | null));181 3, step: ty!((num | null));182 ], {183 let index = match index {184 Val::Num(v) => v as usize,185 Val::Null => 0,186 _ => unreachable!(),187 };188 let end = match end {189 Val::Num(v) => v as usize,190 Val::Null => match &indexable {191 Val::Str(s) => s.chars().count(),192 Val::Arr(v) => v.len(),193 _ => unreachable!()194 },195 _ => unreachable!()196 };197 let step = match step {198 Val::Num(v) => v as usize,199 Val::Null => 1,200 _ => unreachable!()201 };202 match &indexable {203 Val::Str(s) => {204 Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))205 }206 Val::Arr(arr) => {207 Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))208 }209 _ => unreachable!()210 }211 })212}213214// faster215fn builtin_primitive_equals(216 context: Context,217 _loc: &Option<ExprLocation>,218 args: &ArgsDesc,219) -> Result<Val> {220 parse_args!(context, "primitiveEquals", args, 2, [221 0, a: ty!(any);222 1, b: ty!(any);223 ], {224 Ok(Val::Bool(primitive_equals(&a, &b)?))225 })226}227228// faster229fn builtin_equals(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {230 parse_args!(context, "equals", args, 2, [231 0, a: ty!(any);232 1, b: ty!(any);233 ], {234 Ok(Val::Bool(equals(&a, &b)?))235 })236}237238fn builtin_modulo(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {239 parse_args!(context, "modulo", args, 2, [240 0, a: ty!(num) => Val::Num;241 1, b: ty!(num) => Val::Num;242 ], {243 Ok(Val::Num(a % b))244 })245}246247fn builtin_mod(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {248 parse_args!(context, "mod", args, 2, [249 0, a: ty!((num | str));250 1, b: ty!(any);251 ], {252 match (a, b) {253 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),254 (Val::Str(str), vals) => std_format(str, vals),255 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))256 }257 })258}259260fn builtin_floor(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {261 parse_args!(context, "floor", args, 1, [262 0, x: ty!(num) => Val::Num;263 ], {264 Ok(Val::Num(x.floor()))265 })266}267268fn builtin_log(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {269 parse_args!(context, "log", args, 1, [270 0, n: ty!(num) => Val::Num;271 ], {272 Ok(Val::Num(n.ln()))273 })274}275276fn builtin_pow(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {277 parse_args!(context, "pow", args, 2, [278 0, x: ty!(num) => Val::Num;279 1, n: ty!(num) => Val::Num;280 ], {281 Ok(Val::Num(x.powf(n)))282 })283}284285fn builtin_ext_var(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {286 parse_args!(context, "extVar", args, 1, [287 0, x: ty!(str) => Val::Str;288 ], {289 Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)290 })291}292293fn builtin_native(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {294 parse_args!(context, "native", args, 1, [295 0, x: ty!(str) => Val::Str;296 ], {297 Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Rc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)298 })299}300301fn builtin_filter(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {302 parse_args!(context, "filter", args, 2, [303 0, func: ty!(fn.any) => Val::Func;304 1, arr: ty!([any]) => Val::Arr;305 ], {306 let mut out = Vec::new();307 for item in arr.iter() {308 let item = item?;309 if func310 .evaluate_values(context.clone(), &[item.clone()])?311 .try_cast_bool("filter predicate")? {312 out.push(item);313 }314 }315 Ok(Val::Arr(out.into()))316 })317}318319fn builtin_foldl(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {320 parse_args!(context, "foldl", args, 3, [321 0, func: ty!(fn.any) => Val::Func;322 1, arr: ty!([any]) => Val::Arr;323 2, init: ty!(any);324 ], {325 let mut acc = init;326 for i in arr.iter() {327 acc = func.evaluate_values(context.clone(), &[acc, i?])?;328 }329 Ok(acc)330 })331}332333fn builtin_foldr(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {334 parse_args!(context, "foldr", args, 3, [335 0, func: ty!(fn.any) => Val::Func;336 1, arr: ty!([any]) => Val::Arr;337 2, init: ty!(any);338 ], {339 let mut acc = init;340 for i in arr.iter().rev() {341 acc = func.evaluate_values(context.clone(), &[acc, i?])?;342 }343 Ok(acc)344 })345}346347#[allow(non_snake_case)]348fn builtin_sort_impl(349 context: Context,350 _loc: &Option<ExprLocation>,351 args: &ArgsDesc,352) -> Result<Val> {353 parse_args!(context, "sort", args, 2, [354 0, arr: ty!([any]) => Val::Arr;355 1, keyF: ty!(fn.any) => Val::Func;356 ], {357 if arr.len() <= 1 {358 return Ok(Val::Arr(arr))359 }360 Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))361 })362}363364// faster365fn builtin_format(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {366 parse_args!(context, "format", args, 2, [367 0, str: ty!(str) => Val::Str;368 1, vals: ty!(any)369 ], {370 std_format(str, vals)371 })372}373374fn builtin_range(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {375 parse_args!(context, "range", args, 2, [376 0, from: ty!(num) => Val::Num;377 1, to: ty!(num) => Val::Num;378 ], {379 let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));380 for i in from as usize..=to as usize {381 out.push(Val::Num(i as f64));382 }383 Ok(Val::Arr(out.into()))384 })385}386387fn builtin_char(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {388 parse_args!(context, "char", args, 1, [389 0, n: ty!(num) => Val::Num;390 ], {391 let mut out = String::new();392 out.push(std::char::from_u32(n as u32).ok_or_else(||393 InvalidUnicodeCodepointGot(n as u32)394 )?);395 Ok(Val::Str(out.into()))396 })397}398399fn builtin_encode_utf8(400 context: Context,401 _loc: &Option<ExprLocation>,402 args: &ArgsDesc,403) -> Result<Val> {404 parse_args!(context, "encodeUTF8", args, 1, [405 0, str: ty!(str) => Val::Str;406 ], {407 Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))408 })409}410411fn builtin_md5(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {412 parse_args!(context, "md5", args, 1, [413 0, str: ty!(str) => Val::Str;414 ], {415 Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))416 })417}418419fn builtin_trace(context: Context, loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {420 parse_args!(context, "trace", args, 2, [421 0, str: ty!(str) => Val::Str;422 1, rest: ty!(any);423 ], {424 eprint!("TRACE:");425 if let Some(loc) = loc {426 with_state(|s|{427 let locs = s.map_source_locations(&loc.0, &[loc.1]);428 eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);429 });430 }431 eprintln!(" {}", str);432 Ok(rest)433 })434}435436fn builtin_base64(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {437 parse_args!(context, "base64", args, 1, [438 0, input: ty!((str | [num]));439 ], {440 Ok(Val::Str(match input {441 Val::Str(s) => {442 base64::encode(s.bytes().collect::<Vec<_>>()).into()443 },444 Val::Arr(a) => {445 base64::encode(a.iter().map(|v| {446 Ok(v?.clone().unwrap_num()? as u8)447 }).collect::<Result<Vec<_>>>()?).into()448 },449 _ => unreachable!()450 }))451 })452}453454fn builtin_join(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {455 parse_args!(context, "join", args, 2, [456 0, sep: ty!((str | [any]));457 1, arr: ty!([any]) => Val::Arr;458 ], {459 Ok(match sep {460 Val::Arr(joiner_items) => {461 let mut out = Vec::new();462463 let mut first = true;464 for item in arr.iter() {465 let item = item?.clone();466 if let Val::Arr(items) = item {467 if !first {468 out.reserve(joiner_items.len());469 // TODO: extend470 for item in joiner_items.iter() {471 out.push(item?);472 }473 }474 first = false;475 out.reserve(items.len());476 // TODO: extend477 for item in items.iter() {478 out.push(item?);479 }480 } else {481 throw!(RuntimeError("in std.join all items should be arrays".into()));482 }483 }484485 Val::Arr(out.into())486 },487 Val::Str(sep) => {488 let mut out = String::new();489490 let mut first = true;491 for item in arr.iter() {492 let item = item?.clone();493 if let Val::Str(item) = item {494 if !first {495 out += &sep;496 }497 first = false;498 out += &item;499 } else {500 throw!(RuntimeError("in std.join all items should be strings".into()));501 }502 }503504 Val::Str(out.into())505 },506 _ => unreachable!()507 })508 })509}510511// faster512fn builtin_escape_string_json(513 context: Context,514 _loc: &Option<ExprLocation>,515 args: &ArgsDesc,516) -> Result<Val> {517 parse_args!(context, "escapeStringJson", args, 1, [518 0, str_: ty!(str) => Val::Str;519 ], {520 Ok(Val::Str(escape_string_json(&str_).into()))521 })522}523524// faster525fn builtin_manifest_json_ex(526 context: Context,527 _loc: &Option<ExprLocation>,528 args: &ArgsDesc,529) -> Result<Val> {530 parse_args!(context, "manifestJsonEx", args, 2, [531 0, value: ty!(any);532 1, indent: ty!(str) => Val::Str;533 ], {534 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {535 padding: &indent,536 mtype: ManifestType::Std,537 })?.into()))538 })539}540541// faster542fn builtin_reverse(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {543 parse_args!(context, "reverse", args, 1, [544 0, value: ty!([any]) => Val::Arr;545 ], {546 Ok(Val::Arr(value.reversed()))547 })548}549550fn builtin_id(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {551 parse_args!(context, "id", args, 1, [552 0, v: ty!(any);553 ], {554 Ok(v)555 })556}557558#[allow(clippy::cognitive_complexity)]559pub fn call_builtin(560 context: Context,561 loc: &Option<ExprLocation>,562 name: &str,563 args: &ArgsDesc,564) -> Result<Val> {565 if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).map(|f| *f)) {566 return Ok(f(context, loc, args)?);567 }568 throw!(IntrinsicNotFound(name.into()))569}1use crate::{2 equals,3 error::{Error::*, Result},4 evaluate, parse_args, primitive_equals, push, throw,5 typed::CheckType,6 with_state, ArrValue, Context, FuncVal, LazyVal, Val,7};8use format::{format_arr, format_obj};9use jrsonnet_interner::IStr;10use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};11use jrsonnet_types::ty;12use std::{collections::HashMap, path::PathBuf, rc::Rc};1314pub mod stdlib;15pub use stdlib::*;1617use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};1819pub mod format;20pub mod manifest;21pub mod sort;2223fn std_format(str: IStr, vals: Val) -> Result<Val> {24 push(25 &Some(ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),26 || format!("std.format of {}", str),27 || {28 Ok(match vals {29 Val::Arr(vals) => Val::Str(format_arr(&str, &vals.evaluated()?)?.into()),30 Val::Obj(obj) => Val::Str(format_obj(&str, &obj)?.into()),31 o => Val::Str(format_arr(&str, &[o])?.into()),32 })33 },34 )35}3637type Builtin = fn(context: Context, loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val>;3839type BuiltinsType = HashMap<Box<str>, Builtin>;4041thread_local! {42 static BUILTINS: BuiltinsType = {43 [44 ("length".into(), builtin_length as Builtin),45 ("type".into(), builtin_type),46 ("makeArray".into(), builtin_make_array),47 ("codepoint".into(), builtin_codepoint),48 ("objectFieldsEx".into(), builtin_object_fields_ex),49 ("objectHasEx".into(), builtin_object_has_ex),50 ("slice".into(), builtin_slice),51 ("primitiveEquals".into(), builtin_primitive_equals),52 ("equals".into(), builtin_equals),53 ("modulo".into(), builtin_modulo),54 ("mod".into(), builtin_mod),55 ("floor".into(), builtin_floor),56 ("log".into(), builtin_log),57 ("pow".into(), builtin_pow),58 ("extVar".into(), builtin_ext_var),59 ("native".into(), builtin_native),60 ("filter".into(), builtin_filter),61 ("foldl".into(), builtin_foldl),62 ("foldr".into(), builtin_foldr),63 ("sortImpl".into(), builtin_sort_impl),64 ("format".into(), builtin_format),65 ("range".into(), builtin_range),66 ("char".into(), builtin_char),67 ("encodeUTF8".into(), builtin_encode_utf8),68 ("md5".into(), builtin_md5),69 ("base64".into(), builtin_base64),70 ("trace".into(), builtin_trace),71 ("join".into(), builtin_join),72 ("escapeStringJson".into(), builtin_escape_string_json),73 ("manifestJsonEx".into(), builtin_manifest_json_ex),74 ("reverse".into(), builtin_reverse),75 ("id".into(), builtin_id),76 ].iter().cloned().collect()77 };78}7980fn builtin_length(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {81 parse_args!(context, "length", args, 1, [82 0, x: ty!((str | obj | [any]));83 ], {84 Ok(match x {85 Val::Str(n) => Val::Num(n.chars().count() as f64),86 Val::Arr(a) => Val::Num(a.len() as f64),87 Val::Obj(o) => Val::Num(88 o.fields_visibility()89 .into_iter()90 .filter(|(_k, v)| *v)91 .count() as f64,92 ),93 _ => unreachable!(),94 })95 })96}9798fn builtin_type(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {99 parse_args!(context, "type", args, 1, [100 0, x: ty!(any);101 ], {102 Ok(Val::Str(x.value_type().name().into()))103 })104}105106fn builtin_make_array(107 context: Context,108 _loc: &Option<ExprLocation>,109 args: &ArgsDesc,110) -> Result<Val> {111 parse_args!(context, "makeArray", args, 2, [112 0, sz: ty!(number((Some(0.0))..(None))) => Val::Num;113 1, func: ty!(fn.any) => Val::Func;114 ], {115 let mut out = Vec::with_capacity(sz as usize);116 for i in 0..sz as usize {117 out.push(LazyVal::new_resolved(func.evaluate_values(118 Context::new(),119 &[Val::Num(i as f64)]120 )?))121 }122 Ok(Val::Arr(out.into()))123 })124}125126fn builtin_codepoint(127 context: Context,128 _loc: &Option<ExprLocation>,129 args: &ArgsDesc,130) -> Result<Val> {131 parse_args!(context, "codepoint", args, 1, [132 0, str: ty!(char) => Val::Str;133 ], {134 Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))135 })136}137138fn builtin_object_fields_ex(139 context: Context,140 _loc: &Option<ExprLocation>,141 args: &ArgsDesc,142) -> Result<Val> {143 parse_args!(context, "objectFieldsEx", args, 2, [144 0, obj: ty!(obj) => Val::Obj;145 1, inc_hidden: ty!(bool) => Val::Bool;146 ], {147 let mut out = obj.fields_visibility()148 .into_iter()149 .filter(|(_k, v)| *v || inc_hidden)150 .map(|(k, _v)|k)151 .collect::<Vec<_>>();152 out.sort();153 Ok(Val::Arr(out.into_iter().map(Val::Str).collect::<Vec<_>>().into()))154 })155}156157fn builtin_object_has_ex(158 context: Context,159 _loc: &Option<ExprLocation>,160 args: &ArgsDesc,161) -> Result<Val> {162 parse_args!(context, "objectHasEx", args, 3, [163 0, obj: ty!(obj) => Val::Obj;164 1, f: ty!(str) => Val::Str;165 2, inc_hidden: ty!(bool) => Val::Bool;166 ], {167 Ok(Val::Bool(168 obj.fields_visibility()169 .into_iter()170 .filter(|(_k, v)| *v || inc_hidden)171 .any(|(k, _v)| *k == *f),172 ))173 })174}175176// faster177fn builtin_slice(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {178 parse_args!(context, "slice", args, 4, [179 0, indexable: ty!((str | [any]));180 1, index: ty!((num | null));181 2, end: ty!((num | null));182 3, step: ty!((num | null));183 ], {184 let index = match index {185 Val::Num(v) => v as usize,186 Val::Null => 0,187 _ => unreachable!(),188 };189 let end = match end {190 Val::Num(v) => v as usize,191 Val::Null => match &indexable {192 Val::Str(s) => s.chars().count(),193 Val::Arr(v) => v.len(),194 _ => unreachable!()195 },196 _ => unreachable!()197 };198 let step = match step {199 Val::Num(v) => v as usize,200 Val::Null => 1,201 _ => unreachable!()202 };203 match &indexable {204 Val::Str(s) => {205 Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))206 }207 Val::Arr(arr) => {208 Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))209 }210 _ => unreachable!()211 }212 })213}214215// faster216fn builtin_primitive_equals(217 context: Context,218 _loc: &Option<ExprLocation>,219 args: &ArgsDesc,220) -> Result<Val> {221 parse_args!(context, "primitiveEquals", args, 2, [222 0, a: ty!(any);223 1, b: ty!(any);224 ], {225 Ok(Val::Bool(primitive_equals(&a, &b)?))226 })227}228229// faster230fn builtin_equals(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {231 parse_args!(context, "equals", args, 2, [232 0, a: ty!(any);233 1, b: ty!(any);234 ], {235 Ok(Val::Bool(equals(&a, &b)?))236 })237}238239fn builtin_modulo(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {240 parse_args!(context, "modulo", args, 2, [241 0, a: ty!(num) => Val::Num;242 1, b: ty!(num) => Val::Num;243 ], {244 Ok(Val::Num(a % b))245 })246}247248fn builtin_mod(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {249 parse_args!(context, "mod", args, 2, [250 0, a: ty!((num | str));251 1, b: ty!(any);252 ], {253 match (a, b) {254 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),255 (Val::Str(str), vals) => std_format(str, vals),256 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))257 }258 })259}260261fn builtin_floor(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {262 parse_args!(context, "floor", args, 1, [263 0, x: ty!(num) => Val::Num;264 ], {265 Ok(Val::Num(x.floor()))266 })267}268269fn builtin_log(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {270 parse_args!(context, "log", args, 1, [271 0, n: ty!(num) => Val::Num;272 ], {273 Ok(Val::Num(n.ln()))274 })275}276277fn builtin_pow(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {278 parse_args!(context, "pow", args, 2, [279 0, x: ty!(num) => Val::Num;280 1, n: ty!(num) => Val::Num;281 ], {282 Ok(Val::Num(x.powf(n)))283 })284}285286fn builtin_ext_var(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {287 parse_args!(context, "extVar", args, 1, [288 0, x: ty!(str) => Val::Str;289 ], {290 Ok(with_state(|s| s.settings().ext_vars.get(&x).cloned()).ok_or(UndefinedExternalVariable(x))?)291 })292}293294fn builtin_native(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {295 parse_args!(context, "native", args, 1, [296 0, x: ty!(str) => Val::Str;297 ], {298 Ok(with_state(|s| s.settings().ext_natives.get(&x).cloned()).map(|v| Val::Func(Rc::new(FuncVal::NativeExt(x.clone(), v)))).ok_or(UndefinedExternalFunction(x))?)299 })300}301302fn builtin_filter(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {303 parse_args!(context, "filter", args, 2, [304 0, func: ty!(fn.any) => Val::Func;305 1, arr: ty!([any]) => Val::Arr;306 ], {307 let mut out = Vec::new();308 for item in arr.iter() {309 let item = item?;310 if func311 .evaluate_values(context.clone(), &[item.clone()])?312 .try_cast_bool("filter predicate")? {313 out.push(item);314 }315 }316 Ok(Val::Arr(out.into()))317 })318}319320fn builtin_foldl(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {321 parse_args!(context, "foldl", args, 3, [322 0, func: ty!(fn.any) => Val::Func;323 1, arr: ty!([any]) => Val::Arr;324 2, init: ty!(any);325 ], {326 let mut acc = init;327 for i in arr.iter() {328 acc = func.evaluate_values(context.clone(), &[acc, i?])?;329 }330 Ok(acc)331 })332}333334fn builtin_foldr(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {335 parse_args!(context, "foldr", args, 3, [336 0, func: ty!(fn.any) => Val::Func;337 1, arr: ty!([any]) => Val::Arr;338 2, init: ty!(any);339 ], {340 let mut acc = init;341 for i in arr.iter().rev() {342 acc = func.evaluate_values(context.clone(), &[acc, i?])?;343 }344 Ok(acc)345 })346}347348#[allow(non_snake_case)]349fn builtin_sort_impl(350 context: Context,351 _loc: &Option<ExprLocation>,352 args: &ArgsDesc,353) -> Result<Val> {354 parse_args!(context, "sort", args, 2, [355 0, arr: ty!([any]) => Val::Arr;356 1, keyF: ty!(fn.any) => Val::Func;357 ], {358 if arr.len() <= 1 {359 return Ok(Val::Arr(arr))360 }361 Ok(Val::Arr(ArrValue::Eager(sort::sort(context, arr.evaluated()?, &keyF)?)))362 })363}364365// faster366fn builtin_format(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {367 parse_args!(context, "format", args, 2, [368 0, str: ty!(str) => Val::Str;369 1, vals: ty!(any)370 ], {371 std_format(str, vals)372 })373}374375fn builtin_range(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {376 parse_args!(context, "range", args, 2, [377 0, from: ty!(num) => Val::Num;378 1, to: ty!(num) => Val::Num;379 ], {380 let mut out = Vec::with_capacity((1+to as usize-from as usize).max(0));381 for i in from as usize..=to as usize {382 out.push(Val::Num(i as f64));383 }384 Ok(Val::Arr(out.into()))385 })386}387388fn builtin_char(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {389 parse_args!(context, "char", args, 1, [390 0, n: ty!(num) => Val::Num;391 ], {392 let mut out = String::new();393 out.push(std::char::from_u32(n as u32).ok_or_else(||394 InvalidUnicodeCodepointGot(n as u32)395 )?);396 Ok(Val::Str(out.into()))397 })398}399400fn builtin_encode_utf8(401 context: Context,402 _loc: &Option<ExprLocation>,403 args: &ArgsDesc,404) -> Result<Val> {405 parse_args!(context, "encodeUTF8", args, 1, [406 0, str: ty!(str) => Val::Str;407 ], {408 Ok(Val::Arr((str.bytes().map(|b| Val::Num(b as f64)).collect::<Vec<Val>>()).into()))409 })410}411412fn builtin_md5(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {413 parse_args!(context, "md5", args, 1, [414 0, str: ty!(str) => Val::Str;415 ], {416 Ok(Val::Str(format!("{:x}", md5::compute(&str.as_bytes())).into()))417 })418}419420fn builtin_trace(context: Context, loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {421 parse_args!(context, "trace", args, 2, [422 0, str: ty!(str) => Val::Str;423 1, rest: ty!(any);424 ], {425 eprint!("TRACE:");426 if let Some(loc) = loc {427 with_state(|s|{428 let locs = s.map_source_locations(&loc.0, &[loc.1]);429 eprint!(" {}:{}", loc.0.file_name().unwrap().to_str().unwrap(), locs[0].line);430 });431 }432 eprintln!(" {}", str);433 Ok(rest)434 })435}436437fn builtin_base64(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {438 parse_args!(context, "base64", args, 1, [439 0, input: ty!((str | [num]));440 ], {441 Ok(Val::Str(match input {442 Val::Str(s) => {443 base64::encode(s.bytes().collect::<Vec<_>>()).into()444 },445 Val::Arr(a) => {446 base64::encode(a.iter().map(|v| {447 Ok(v?.clone().unwrap_num()? as u8)448 }).collect::<Result<Vec<_>>>()?).into()449 },450 _ => unreachable!()451 }))452 })453}454455fn builtin_join(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {456 parse_args!(context, "join", args, 2, [457 0, sep: ty!((str | [any]));458 1, arr: ty!([any]) => Val::Arr;459 ], {460 Ok(match sep {461 Val::Arr(joiner_items) => {462 let mut out = Vec::new();463464 let mut first = true;465 for item in arr.iter() {466 let item = item?.clone();467 if let Val::Arr(items) = item {468 if !first {469 out.reserve(joiner_items.len());470 // TODO: extend471 for item in joiner_items.iter() {472 out.push(item?);473 }474 }475 first = false;476 out.reserve(items.len());477 // TODO: extend478 for item in items.iter() {479 out.push(item?);480 }481 } else {482 throw!(RuntimeError("in std.join all items should be arrays".into()));483 }484 }485486 Val::Arr(out.into())487 },488 Val::Str(sep) => {489 let mut out = String::new();490491 let mut first = true;492 for item in arr.iter() {493 let item = item?.clone();494 if let Val::Str(item) = item {495 if !first {496 out += &sep;497 }498 first = false;499 out += &item;500 } else {501 throw!(RuntimeError("in std.join all items should be strings".into()));502 }503 }504505 Val::Str(out.into())506 },507 _ => unreachable!()508 })509 })510}511512// faster513fn builtin_escape_string_json(514 context: Context,515 _loc: &Option<ExprLocation>,516 args: &ArgsDesc,517) -> Result<Val> {518 parse_args!(context, "escapeStringJson", args, 1, [519 0, str_: ty!(str) => Val::Str;520 ], {521 Ok(Val::Str(escape_string_json(&str_).into()))522 })523}524525// faster526fn builtin_manifest_json_ex(527 context: Context,528 _loc: &Option<ExprLocation>,529 args: &ArgsDesc,530) -> Result<Val> {531 parse_args!(context, "manifestJsonEx", args, 2, [532 0, value: ty!(any);533 1, indent: ty!(str) => Val::Str;534 ], {535 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {536 padding: &indent,537 mtype: ManifestType::Std,538 })?.into()))539 })540}541542// faster543fn builtin_reverse(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {544 parse_args!(context, "reverse", args, 1, [545 0, value: ty!([any]) => Val::Arr;546 ], {547 Ok(Val::Arr(value.reversed()))548 })549}550551fn builtin_id(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {552 parse_args!(context, "id", args, 1, [553 0, v: ty!(any);554 ], {555 Ok(v)556 })557}558559#[allow(clippy::cognitive_complexity)]560pub fn call_builtin(561 context: Context,562 loc: &Option<ExprLocation>,563 name: &str,564 args: &ArgsDesc,565) -> Result<Val> {566 if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).map(|f| *f)) {567 return Ok(f(context, loc, args)?);568 }569 throw!(IntrinsicNotFound(name.into()))570}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -23,6 +23,7 @@
pub use evaluate::*;
pub use function::parse_function_call;
pub use import::*;
+use jrsonnet_interner::IStr;
use jrsonnet_parser::*;
use native::NativeCallback;
pub use obj::*;
@@ -63,13 +64,13 @@
/// Limits amount of stack trace items preserved
pub max_trace: usize,
/// Used for s`td.extVar`
- pub ext_vars: HashMap<Rc<str>, Val>,
+ pub ext_vars: HashMap<IStr, Val>,
/// Used for ext.native
- pub ext_natives: HashMap<Rc<str>, Rc<NativeCallback>>,
+ pub ext_natives: HashMap<IStr, Rc<NativeCallback>>,
/// TLA vars
- pub tla_vars: HashMap<Rc<str>, Val>,
+ pub tla_vars: HashMap<IStr, Val>,
/// Global variables are inserted in default context
- pub globals: HashMap<Rc<str>, Val>,
+ pub globals: HashMap<IStr, Val>,
/// Used to resolve file locations/contents
pub import_resolver: Box<dyn ImportResolver>,
/// Used in manifestification functions
@@ -102,11 +103,11 @@
stack_depth: usize,
/// Contains file source codes and evaluation results for imports and pretty-printed stacktraces
files: HashMap<Rc<PathBuf>, FileData>,
- str_files: HashMap<Rc<PathBuf>, Rc<str>>,
+ str_files: HashMap<Rc<PathBuf>, IStr>,
}
pub struct FileData {
- source_code: Rc<str>,
+ source_code: IStr,
parsed: LocExpr,
evaluated: Option<Val>,
}
@@ -144,7 +145,7 @@
impl EvaluationState {
/// Parses and adds file as loaded
- pub fn add_file(&self, path: Rc<PathBuf>, source_code: Rc<str>) -> Result<()> {
+ pub fn add_file(&self, path: Rc<PathBuf>, source_code: IStr) -> Result<()> {
self.add_parsed_file(
path.clone(),
source_code.clone(),
@@ -169,7 +170,7 @@
pub fn add_parsed_file(
&self,
name: Rc<PathBuf>,
- source_code: Rc<str>,
+ source_code: IStr,
parsed: LocExpr,
) -> Result<()> {
self.data_mut().files.insert(
@@ -183,7 +184,7 @@
Ok(())
}
- pub fn get_source(&self, name: &PathBuf) -> Option<Rc<str>> {
+ pub fn get_source(&self, name: &PathBuf) -> Option<IStr> {
let ro_map = &self.data().files;
ro_map.get(name).map(|value| value.source_code.clone())
}
@@ -205,7 +206,7 @@
self.add_file(file_path.clone(), contents)?;
self.evaluate_loaded_file_raw(&file_path)
}
- pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<str>> {
+ pub(crate) fn import_file_str(&self, from: &PathBuf, path: &PathBuf) -> Result<IStr> {
let path = self.resolve_file(from, path)?;
if !self.data().str_files.contains_key(&path) {
let file_str = self.load_file_contents(&path)?;
@@ -257,7 +258,7 @@
/// Creates context with all passed global variables
pub fn create_default_context(&self) -> Result<Context> {
let globals = &self.settings().globals;
- let mut new_bindings: HashMap<Rc<str>, LazyBinding> = HashMap::new();
+ let mut new_bindings: HashMap<IStr, LazyBinding> = HashMap::new();
for (name, value) in globals.iter() {
new_bindings.insert(
name.clone(),
@@ -321,13 +322,13 @@
out
}
- pub fn manifest(&self, val: Val) -> Result<Rc<str>> {
+ pub fn manifest(&self, val: Val) -> Result<IStr> {
self.run_in_state(|| val.manifest(&self.manifest_format()))
}
- pub fn manifest_multi(&self, val: Val) -> Result<Vec<(Rc<str>, Rc<str>)>> {
+ pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {
self.run_in_state(|| val.manifest_multi(&self.manifest_format()))
}
- pub fn manifest_stream(&self, val: Val) -> Result<Vec<Rc<str>>> {
+ pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {
self.run_in_state(|| val.manifest_stream(&self.manifest_format()))
}
@@ -371,7 +372,7 @@
self.run_in_state(|| self.import_file(&PathBuf::from("."), name))
}
/// Parses and evaluates the given snippet
- pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: Rc<str>) -> Result<Val> {
+ pub fn evaluate_snippet_raw(&self, source: Rc<PathBuf>, code: IStr) -> Result<Val> {
let parsed = parse(
&code,
&ParserSettings {
@@ -391,26 +392,26 @@
/// Settings utilities
impl EvaluationState {
- pub fn add_ext_var(&self, name: Rc<str>, value: Val) {
+ pub fn add_ext_var(&self, name: IStr, value: Val) {
self.settings_mut().ext_vars.insert(name, value);
}
- pub fn add_ext_str(&self, name: Rc<str>, value: Rc<str>) {
+ pub fn add_ext_str(&self, name: IStr, value: IStr) {
self.add_ext_var(name, Val::Str(value));
}
- pub fn add_ext_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {
+ pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {
let value =
self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("ext_code {}", name))), code)?;
self.add_ext_var(name, value);
Ok(())
}
- pub fn add_tla(&self, name: Rc<str>, value: Val) {
+ pub fn add_tla(&self, name: IStr, value: Val) {
self.settings_mut().tla_vars.insert(name, value);
}
- pub fn add_tla_str(&self, name: Rc<str>, value: Rc<str>) {
+ pub fn add_tla_str(&self, name: IStr, value: IStr) {
self.add_tla(name, Val::Str(value));
}
- pub fn add_tla_code(&self, name: Rc<str>, code: Rc<str>) -> Result<()> {
+ pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {
let value =
self.evaluate_snippet_raw(Rc::new(PathBuf::from(format!("tla_code {}", name))), code)?;
self.add_tla(name, value);
@@ -420,7 +421,7 @@
pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
Ok(self.settings().import_resolver.resolve_file(from, path)?)
}
- pub fn load_file_contents(&self, path: &PathBuf) -> Result<Rc<str>> {
+ pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {
Ok(self.settings().import_resolver.load_file_contents(path)?)
}
@@ -431,7 +432,7 @@
self.settings_mut().import_resolver = resolver;
}
- pub fn add_native(&self, name: Rc<str>, cb: Rc<NativeCallback>) {
+ pub fn add_native(&self, name: IStr, cb: Rc<NativeCallback>) {
self.settings_mut().ext_natives.insert(name, cb);
}
@@ -468,6 +469,7 @@
pub mod tests {
use super::Val;
use crate::{error::Error::*, primitive_equals, EvaluationState};
+ use jrsonnet_interner::IStr;
use jrsonnet_parser::*;
use std::{path::PathBuf, rc::Rc};
@@ -905,13 +907,13 @@
Ok(())
}
- struct TestImportResolver(Rc<str>);
+ struct TestImportResolver(IStr);
impl crate::import::ImportResolver for TestImportResolver {
fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {
Ok(Rc::new(PathBuf::from("/test")))
}
- fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<Rc<str>> {
+ fn load_file_contents(&self, _: &PathBuf) -> crate::error::Result<IStr> {
Ok(self.0.clone())
}
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)]