difftreelog
refactor unified ContextBuilder
in: master
17 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -699,6 +699,7 @@
"bitmaps",
"rand_core 0.6.4",
"rand_xoshiro",
+ "refpool",
"sized-chunks",
"typenum",
"version_check",
@@ -861,9 +862,9 @@
[[package]]
name = "jrsonnet-gcmodule"
-version = "0.4.3"
+version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8a6a63a6e55ba82764e483d7f8a181f25db95a8f25da8ae6520e95a5fe39c6a6"
+checksum = "21dd97b40cbfb2043094219f95d96519858ba1aee4e8260eb048a1774832a517"
dependencies = [
"im-rc",
"jrsonnet-gcmodule-derive",
@@ -871,9 +872,9 @@
[[package]]
name = "jrsonnet-gcmodule-derive"
-version = "0.4.3"
+version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "095fe3c4c0acf32de80205a8a479ef63c216b9efb0024dec9eb7fe1c5ef1f1a1"
+checksum = "ede3d0445c2a7d7adab0a3cc33bdb33df78ffebebc21a2848c221526cb1795d4"
dependencies = [
"proc-macro2",
"quote",
@@ -1420,6 +1421,12 @@
]
[[package]]
+name = "refpool"
+version = "0.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "369e86b80fa7dc8c561dd9613a5bf25c59d2d3073cd66c47fd9e39802f0ecb58"
+
+[[package]]
name = "regex"
version = "1.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1633,6 +1640,7 @@
checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
dependencies = [
"bitmaps",
+ "refpool",
"typenum",
]
@@ -1738,6 +1746,7 @@
"jrsonnet-evaluator",
"jrsonnet-gcmodule",
"jrsonnet-stdlib",
+ "mimallocator",
"serde",
"serde_json",
]
Cargo.tomldiffbeforeafterboth--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,7 +22,7 @@
jrsonnet-cli = { path = "./crates/jrsonnet-cli", version = "0.5.0-pre98" }
jrsonnet-types = { path = "./crates/jrsonnet-types", version = "0.5.0-pre98" }
jrsonnet-formatter = { path = "./crates/jrsonnet-formatter", version = "0.5.0-pre98" }
-jrsonnet-gcmodule = { version = "0.4.3", features = ["im-rc"] }
+jrsonnet-gcmodule = { version = "0.4.4", features = ["im-rc"] }
# Diagnostics.
# hi-doc is my library, which handles text formatting very well, but isn't polished enough yet
# Previous implementation was based on annotate-snippets, which I don't like for many reasons.
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -200,7 +200,7 @@
val = s.evaluate_snippet_with(
"<exp_apply>".to_owned(),
&apply,
- InitialUnderscore(Thunk::evaluated(val)),
+ &InitialUnderscore(Thunk::evaluated(val)),
)?;
}
crates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -76,7 +76,7 @@
"Hash",
"PartialEq",
] }
-im-rc = "15.1.0"
+im-rc = { version = "15.1.0", features = ["pool"] }
[build-dependencies]
rustversion = "1.0.22"
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -3,11 +3,11 @@
use educe::Educe;
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
-use rustc_hash::FxHashMap;
+use rustc_hash::{FxHashMap, FxHashSet};
use crate::{
- ObjValue, Pending, Result, SupThis, Thunk, Val, error::ErrorKind::*, gc::WithCapacityExt as _,
- map::LayeredHashMap,
+ ObjValue, Pending, Result, SupThis, Thunk, Val, bail, error::ErrorKind::*,
+ gc::WithCapacityExt as _,
};
/// Context keeps information about current lexical code location
///
@@ -20,7 +20,9 @@
struct ContextInternal {
dollar: Option<ObjValue>,
sup_this: Option<SupThis>,
- bindings: LayeredHashMap,
+ bindings: FxHashMap<IStr, Thunk<Val>>,
+
+ branch_point: Option<Context>,
}
impl Context {
pub fn new_future() -> Pending<Self> {
@@ -71,14 +73,18 @@
return Ok(val);
}
+ if let Some(branch_point) = &self.0.branch_point {
+ return branch_point.binding(name);
+ }
+
let mut heap = Vec::new();
- self.0.bindings.clone().iter_keys(|k| {
- let conf = strsim::jaro_winkler(&k as &str, &name as &str);
+ for k in self.0.bindings.keys() {
+ let conf = strsim::jaro_winkler(k as &str, &name as &str);
if conf < 0.8 {
- return;
+ continue;
}
- heap.push((conf, k));
- });
+ heap.push((conf, k.clone()));
+ }
heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
bail!(VariableIsNotDefined(
@@ -98,103 +104,91 @@
}
#[must_use]
- pub fn with_var(self, name: impl Into<IStr>, value: Val) -> Self {
- let mut new_bindings = FxHashMap::with_capacity(1);
- new_bindings.insert(name.into(), Thunk::evaluated(value));
- self.extend_bindings(new_bindings)
- }
-
- #[must_use]
- pub fn extend_bindings_sup_this(
- self,
- new_bindings: FxHashMap<IStr, Thunk<Val>>,
- sup_this: SupThis,
- ) -> Self {
- let ctx = &self;
- let dollar = ctx
- .0
- .dollar
- .clone()
- .or_else(|| Some(sup_this.this().clone()));
- let bindings = if new_bindings.is_empty() {
- ctx.0.bindings.clone()
+ pub fn branch_point(self) -> Self {
+ if self.0.bindings.is_empty() {
+ self
} else {
- ctx.0.bindings.clone().extend(new_bindings)
- };
- Self(Cc::new(ContextInternal {
- dollar,
- sup_this: Some(sup_this),
- bindings,
- }))
- }
- #[must_use]
- pub fn extend_bindings(self, new_bindings: FxHashMap<IStr, Thunk<Val>>) -> Self {
- if new_bindings.is_empty() {
- return self;
+ ContextBuilder::extend(self).build()
}
- let ctx = &self;
- let bindings = if new_bindings.is_empty() {
- ctx.0.bindings.clone()
- } else {
- ctx.0.bindings.clone().extend(new_bindings)
- };
- Self(Cc::new(ContextInternal {
- dollar: ctx.0.dollar.clone(),
- sup_this: ctx.0.sup_this.clone(),
- bindings,
- }))
}
}
-#[derive(Default)]
+#[derive(Clone)]
pub struct ContextBuilder {
+ dollar: Option<ObjValue>,
+ sup_this: Option<SupThis>,
bindings: FxHashMap<IStr, Thunk<Val>>,
- extend: Option<Context>,
+ filled: FxHashSet<IStr>,
+ branch_point: Option<Context>,
}
impl ContextBuilder {
pub fn new() -> Self {
- Self::with_capacity(0)
+ Self {
+ dollar: None,
+ sup_this: None,
+ bindings: FxHashMap::new(),
+ filled: FxHashSet::new(),
+ branch_point: None,
+ }
}
- pub fn with_capacity(capacity: usize) -> Self {
+ pub fn extend_fast(parent: Context) -> Self {
Self {
- bindings: FxHashMap::with_capacity(capacity),
- extend: None,
+ dollar: parent.0.dollar.clone(),
+ sup_this: parent.0.sup_this.clone(),
+ bindings: parent.0.bindings.clone(),
+ filled: FxHashSet::new(),
+ branch_point: parent.0.branch_point.clone(),
}
}
pub fn extend(parent: Context) -> Self {
Self {
+ dollar: parent.0.dollar.clone(),
+ sup_this: parent.0.sup_this.clone(),
bindings: FxHashMap::new(),
- extend: Some(parent),
+ filled: FxHashSet::new(),
+ branch_point: Some(parent.clone()),
}
}
- /// # Panics
- ///
- /// If `name` is already bound. Makes no sense to bind same local multiple times,
- /// unless it is separate context layers.
- pub fn bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) -> &mut Self {
- let old = self.bindings.insert(name.into(), value);
- assert!(old.is_none(), "variable bound twice in single context call");
+ pub fn bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) {
+ let _ = self.bindings.insert(name.into(), value);
+ }
+ /// After commit, binds would shadow the previous declarations
+ #[must_use]
+ pub fn commit(mut self) -> Self {
+ self.filled.clear();
self
}
- pub fn binds(&mut self, bindings: FxHashMap<IStr, Thunk<Val>>) -> &mut Self {
- for (k, v) in bindings {
- self.bind(k, v);
+ pub fn try_bind(&mut self, name: impl Into<IStr>, value: Thunk<Val>) -> Result<()> {
+ let name = name.into();
+ if !self.filled.insert(name.clone()) {
+ bail!(DuplicateLocalVar(name))
}
- self
+ self.bind(name, value);
+ Ok(())
}
pub fn build(self) -> Context {
- if let Some(parent) = self.extend {
- parent.extend_bindings(self.bindings)
- } else {
- Context(Cc::new(ContextInternal {
- bindings: LayeredHashMap::new(self.bindings),
- dollar: None,
- sup_this: None,
- }))
+ Context(Cc::new(ContextInternal {
+ dollar: self.dollar,
+ sup_this: self.sup_this,
+ bindings: self.bindings,
+ branch_point: self.branch_point,
+ }))
+ }
+ pub fn build_sup_this(mut self, st: SupThis) -> Context {
+ if self.dollar.is_none() {
+ self.dollar = Some(st.this().clone());
}
+ self.sup_this = Some(st);
+ self.build()
+ }
+}
+
+impl Default for ContextBuilder {
+ fn default() -> Self {
+ Self::new()
}
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -249,6 +249,10 @@
#[derive(Clone, Trace)]
pub struct Error(Box<(ErrorKind, StackTrace)>);
+
+#[cfg(target_pointer_width = "64")]
+static_assertions::assert_eq_size!(Error, usize);
+
impl Error {
pub fn new(e: ErrorKind) -> Self {
Self(Box::new((e, StackTrace(vec![]))))
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -1,30 +1,23 @@
-use std::{collections::HashMap, hash::BuildHasher};
-
-use jrsonnet_interner::IStr;
use jrsonnet_ir::{BindSpec, Destruct};
#[cfg(feature = "exp-preserve-order")]
use crate::evaluate;
use crate::{
- Context, Pending, Thunk, Val, bail,
- error::{ErrorKind::*, Result},
- evaluate_method, evaluate_named_param,
+ Context, ContextBuilder, Pending, Thunk, Val, error::Result, evaluate_method,
+ evaluate_named_param,
};
#[allow(clippy::too_many_lines)]
#[allow(unused_variables)]
-pub fn destruct<H: BuildHasher>(
+pub fn destruct(
d: &Destruct,
parent: Thunk<Val>,
fctx: Pending<Context>,
- new_bindings: &mut HashMap<IStr, Thunk<Val>, H>,
+ new_bindings: &mut ContextBuilder,
) -> Result<()> {
match d {
Destruct::Full(v) => {
- let old = new_bindings.insert(v.clone(), parent);
- if old.is_some() {
- bail!(DuplicateLocalVar(v.clone()))
- }
+ new_bindings.try_bind(v.clone(), parent)?;
}
#[cfg(feature = "exp-destruct")]
Destruct::Skip => {}
@@ -187,10 +180,10 @@
Ok(())
}
-pub fn evaluate_dest<H: BuildHasher>(
+pub fn evaluate_dest(
d: &BindSpec,
fctx: Pending<Context>,
- new_bindings: &mut HashMap<IStr, Thunk<Val>, H>,
+ new_bindings: &mut ContextBuilder,
) -> Result<()> {
match d {
BindSpec::Field { into, value } => {
@@ -210,13 +203,10 @@
let params = params.clone();
let name = name.clone();
let value = value.clone();
- let old = new_bindings.insert(name.clone(), {
- let name = name.clone();
- Thunk!(move || Ok(evaluate_method(fctx.unwrap(), name, params, value)))
- });
- if old.is_some() {
- bail!(DuplicateLocalVar(name))
- }
+ new_bindings.try_bind(
+ name.clone(),
+ Thunk!(move || Ok(evaluate_method(fctx.unwrap(), name, params, value))),
+ )?;
}
}
Ok(())
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -8,19 +8,17 @@
function::ParamName,
};
use jrsonnet_types::ValType;
-use rustc_hash::FxHashMap;
use self::destructure::destruct;
use crate::{
- Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, ResultExt,
- SupThis, Unbound, Val,
+ Context, ContextBuilder, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
+ ResultExt, SupThis, Unbound, Val,
arr::ArrValue,
bail,
destructure::evaluate_dest,
error::{ErrorKind::*, suggest_object_fields},
evaluate::operator::{evaluate_binary_op_special, evaluate_unary_op},
function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
- gc::WithCapacityExt as _,
in_frame,
typed::{FromUntyped, IntoUntyped as _, Typed},
val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
@@ -130,9 +128,9 @@
guaranteed_reserve = guaranteed_reserve.max(1) * list.len();
for (i, item) in list.iter_lazy().enumerate() {
let fctx = Pending::new();
- let mut new_bindings = FxHashMap::with_capacity(into.binds_len());
- destruct(into, item, fctx.clone(), &mut new_bindings)?;
- let ctx = ctx.clone().extend_bindings(new_bindings).into_future(fctx);
+ let mut ctx = ContextBuilder::extend_fast(ctx.clone());
+ destruct(into, item, fctx.clone(), &mut ctx)?;
+ let ctx = ctx.build().into_future(fctx);
let specs = &specs[1..];
evaluate_comp(
@@ -179,6 +177,7 @@
}
fn evaluate_arr_comp(ctx: Context, expr: &Rc<Expr>, comp_specs: &[CompSpec]) -> Result<ArrValue> {
+ let ctx = ctx.branch_point();
'eager: {
let mut out = Vec::new();
@@ -225,17 +224,13 @@
fn bind(&self, sup_this: SupThis) -> Result<Context> {
let fctx = Context::new_future();
- let mut new_bindings =
- FxHashMap::with_capacity(self.locals.iter().map(BindSpec::binds_len).sum());
+ let ctx = self.fctx.clone();
+ let mut ctx = ContextBuilder::extend(ctx);
for b in self.locals.iter() {
- evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+ evaluate_dest(b, fctx.clone(), &mut ctx)?;
}
- let ctx = self.fctx.clone();
-
- let ctx = ctx
- .extend_bindings_sup_this(new_bindings, sup_this)
- .into_future(fctx);
+ let ctx = ctx.build_sup_this(sup_this).into_future(fctx);
Ok(ctx)
}
@@ -332,10 +327,7 @@
impl Unbound for DirectUnbound {
type Bound = Context;
fn bind(&self, sup_this: SupThis) -> Result<Context> {
- Ok(self
- .0
- .clone()
- .extend_bindings_sup_this(FxHashMap::new(), sup_this))
+ Ok(ContextBuilder::extend(self.0.clone()).build_sup_this(sup_this))
}
}
@@ -408,12 +400,17 @@
builder.with_super(super_obj);
}
let locals = obj.locals.clone();
- evaluate_comp(ctx, &obj.compspecs, 0, &mut |ctx, reserve| {
- let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
- builder.reserve_fields(reserve);
+ evaluate_comp(
+ ctx.branch_point(),
+ &obj.compspecs,
+ 0,
+ &mut |ctx, reserve| {
+ let uctx = evaluate_object_locals(ctx.clone(), locals.clone());
+ builder.reserve_fields(reserve);
- evaluate_field_member(&mut builder, ctx, uctx, &obj.field)
- })?;
+ evaluate_field_member(&mut builder, ctx, uctx, &obj.field)
+ },
+ )?;
builder.build()
}
@@ -684,13 +681,12 @@
Ok(indexable)
})?,
LocalExpr(bindings, returned) => {
- let mut new_bindings: FxHashMap<IStr, Thunk<Val>> =
- FxHashMap::with_capacity(bindings.iter().map(BindSpec::binds_len).sum());
let fctx = Context::new_future();
+ let mut ctx = ContextBuilder::extend(ctx);
for b in bindings {
- evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+ evaluate_dest(b, fctx.clone(), &mut ctx)?;
}
- let ctx = ctx.extend_bindings(new_bindings).into_future(fctx);
+ let ctx = ctx.build().into_future(fctx);
evaluate(ctx, returned)?
}
Arr(items) => {
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -1,12 +1,10 @@
use jrsonnet_ir::ExprParams;
-use rustc_hash::FxHashMap;
use crate::{
- Context, Thunk,
+ Context, ContextBuilder, Thunk,
destructure::destruct,
error::{ErrorKind::*, Result},
evaluate_named_param,
- gc::WithCapacityExt as _,
};
/// Creates Context, which has all argument default values applied
@@ -14,7 +12,7 @@
pub fn parse_default_function_call(body_ctx: Context, params: &ExprParams) -> Result<Context> {
let fctx = Context::new_future();
- let mut bindings = FxHashMap::with_capacity(params.binds_len());
+ let mut ctx = ContextBuilder::extend(body_ctx);
for param in params.exprs.iter() {
if let Some(v) = ¶m.default {
@@ -27,7 +25,7 @@
Thunk!(move || evaluate_named_param(ctx.unwrap(), &value, name))
},
fctx.clone(),
- &mut bindings,
+ &mut ctx,
)?;
} else {
destruct(
@@ -42,10 +40,10 @@
.into()))
},
fctx.clone(),
- &mut bindings,
+ &mut ctx,
)?;
}
}
- Ok(body_ctx.extend_bindings(bindings).into_future(fctx))
+ Ok(ctx.build().into_future(fctx))
}
crates/jrsonnet-evaluator/src/function/prepared.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/prepared.rs
+++ b/crates/jrsonnet-evaluator/src/function/prepared.rs
@@ -2,12 +2,12 @@
use jrsonnet_gcmodule::{Acyclic, Trace};
use jrsonnet_ir::{ExprParams, IStr, function::FunctionSignature};
-use rustc_hash::{FxHashMap, FxHashSet};
+use rustc_hash::FxHashSet;
use super::{CallLocation, FuncVal};
use crate::{
Context, ContextBuilder, Pending, Result, Thunk, Val, bail, destructure::destruct,
- error::ErrorKind::*, evaluate_named_param, gc::WithCapacityExt,
+ error::ErrorKind::*, evaluate_named_param,
};
#[derive(Debug, Trace, Clone)]
@@ -118,7 +118,7 @@
unnamed: &[Thunk<Val>],
named: &[Thunk<Val>],
) -> Result<Context> {
- let mut passed_args = FxHashMap::with_capacity(params.binds_len());
+ let mut ctx = ContextBuilder::extend(body_ctx);
let destruct_ctx = Pending::new();
@@ -127,7 +127,7 @@
¶ms.exprs[param_idx].destruct,
unnamed.clone(),
destruct_ctx.clone(),
- &mut passed_args,
+ &mut ctx,
)?;
}
@@ -136,18 +136,16 @@
¶ms.exprs[param_idx].destruct,
named[arg_idx].clone(),
destruct_ctx.clone(),
- &mut passed_args,
+ &mut ctx,
)?;
}
if prepared.defaults.is_empty() {
- let body_ctx = body_ctx
- .extend_bindings(passed_args)
- .into_future(destruct_ctx);
+ let body_ctx = ctx.build().into_future(destruct_ctx);
Ok(body_ctx)
} else {
let fctx = Context::new_future();
- let mut defaults = FxHashMap::with_capacity(params.binds_len() - passed_args.len());
+ let mut ctx = ctx.commit();
for param_idx in prepared.defaults.iter().copied() {
// let param = params.0.rc_idx(param_idx);
destruct(
@@ -163,13 +161,10 @@
})
},
fctx.clone(),
- &mut defaults,
+ &mut ctx,
)?;
}
- let mut ctx = ContextBuilder::extend(body_ctx);
- ctx.binds(passed_args);
- ctx.binds(defaults);
Ok(ctx.build().into_future(fctx).into_future(destruct_ctx))
}
}
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -31,3 +31,5 @@
}
pub fn assert_trace<T: Trace>(_v: &T) {}
+
+pub type ImHashMap<K, V> = im_rc::HashMap<K, V, FxBuildHasher>;
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -15,7 +15,6 @@
mod import;
mod integrations;
pub mod manifest;
-mod map;
mod obj;
pub mod stack;
pub mod stdlib;
@@ -162,19 +161,7 @@
/// During import, this trait will be called to create initial context for file.
/// It may initialize global variables, stdlib for example.
-pub trait ContextInitializer: Trace {
- /// For which size the builder should be preallocated
- fn reserve_vars(&self) -> usize {
- 0
- }
- /// Initialize default file context.
- /// Has default implementation, which calls `populate`.
- /// Prefer to always implement `populate` instead.
- fn initialize(&self, for_file: Source) -> Context {
- let mut builder = ContextBuilder::with_capacity(self.reserve_vars());
- self.populate(for_file, &mut builder);
- builder.build()
- }
+pub trait ContextInitializer {
/// For composability: extend builder. May panic if this initialization is not supported,
/// and the context may only be created via `initialize`.
fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
@@ -182,6 +169,18 @@
/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
fn as_any(&self) -> &dyn Any;
}
+impl<T> ContextInitializer for &T
+where
+ T: ContextInitializer,
+{
+ fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
+ (*self).populate(for_file, builder);
+ }
+
+ fn as_any(&self) -> &dyn Any {
+ (*self).as_any()
+ }
+}
/// Context initializer which adds nothing.
impl ContextInitializer for () {
@@ -193,16 +192,8 @@
impl<T> ContextInitializer for Option<T>
where
- T: ContextInitializer,
+ T: ContextInitializer + 'static,
{
- fn initialize(&self, for_file: Source) -> Context {
- if let Some(ctx) = self {
- ctx.initialize(for_file)
- } else {
- ().initialize(for_file)
- }
- }
-
fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
if let Some(ctx) = self {
ctx.populate(for_file, builder);
@@ -218,12 +209,6 @@
($($gen:ident)*) => {
#[allow(non_snake_case)]
impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {
- fn reserve_vars(&self) -> usize {
- let mut out = 0;
- let ($($gen,)*) = self;
- $(out += $gen.reserve_vars();)*
- out
- }
fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
let ($($gen,)*) = self;
$($gen.populate(for_file.clone(), builder);)*
@@ -453,19 +438,17 @@
/// Creates context with all passed global variables
pub fn create_default_context(&self, source: Source) -> Context {
- self.context_initializer().initialize(source)
+ self.create_default_context_with(source, &())
}
/// Creates context with all passed global variables, calling custom modifier
pub fn create_default_context_with(
&self,
source: Source,
- context_initializer: impl ContextInitializer,
+ context_initializer: &dyn ContextInitializer,
) -> Context {
let default_initializer = self.context_initializer();
- let mut builder = ContextBuilder::with_capacity(
- default_initializer.reserve_vars() + context_initializer.reserve_vars(),
- );
+ let mut builder = ContextBuilder::new();
default_initializer.populate(source.clone(), &mut builder);
context_initializer.populate(source, &mut builder);
@@ -516,20 +499,14 @@
impl State {
/// Parses and evaluates the given snippet
pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {
- let code = code.into();
- let source = Source::new_virtual(name.into(), code.clone());
- let parsed = parse_jsonnet(&code, source.clone()).map_err(|e| ImportSyntaxError {
- path: source.clone(),
- error: Box::new(e),
- })?;
- evaluate(self.create_default_context(source), &parsed)
+ self.evaluate_snippet_with(name, code, &())
}
/// Parses and evaluates the given snippet with custom context modifier
pub fn evaluate_snippet_with(
&self,
name: impl Into<IStr>,
code: impl Into<IStr>,
- context_initializer: impl ContextInitializer,
+ context_initializer: &dyn ContextInitializer,
) -> Result<Val> {
let code = code.into();
let source = Source::new_virtual(name.into(), code.clone());
@@ -587,7 +564,7 @@
}
pub fn context_initializer(
&mut self,
- context_initializer: impl ContextInitializer,
+ context_initializer: impl ContextInitializer + Trace,
) -> &mut Self {
let _ = self
.context_initializer
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -1,4 +1,4 @@
-use std::{borrow::Cow, fmt::Write, ptr};
+use std::{borrow::Cow, fmt::Write, hint::black_box, ptr};
use crate::{Result, ResultExt, Val, bail, in_description_frame};
@@ -44,6 +44,45 @@
}
}
+pub struct BlackBoxFormat;
+impl ManifestFormat for BlackBoxFormat {
+ #[allow(clippy::only_used_in_recursion)]
+ fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {
+ match val {
+ Val::Bool(v) => {
+ black_box(v);
+ }
+ val @ Val::Null => {
+ black_box(val);
+ }
+ Val::Str(str_value) => {
+ black_box(format!("{str_value}"));
+ }
+ Val::Num(num_value) => {
+ black_box(num_value);
+ }
+ Val::Arr(arr_value) => {
+ for ele in arr_value.iter() {
+ let ele = ele?;
+ self.manifest_buf(ele, buf)?;
+ }
+ }
+ Val::Obj(obj_value) => {
+ for (name, value) in obj_value.iter() {
+ black_box(name);
+ let value = value?;
+ self.manifest_buf(value, buf)?;
+ }
+ }
+ Val::Func(func_val) => {
+ black_box(func_val);
+ bail!("tried to manifest function")
+ }
+ }
+ Ok(())
+ }
+}
+
#[derive(PartialEq, Eq, Clone, Copy)]
enum JsonFormatting {
// Applied in manifestification
@@ -66,7 +105,6 @@
preserve_order: bool,
#[cfg(feature = "exp-bigint")]
preserve_bigints: bool,
- debug_truncate_strings: Option<usize>,
}
impl<'s> JsonFormat<'s> {
@@ -81,7 +119,6 @@
preserve_order,
#[cfg(feature = "exp-bigint")]
preserve_bigints: false,
- debug_truncate_strings: None,
}
}
/// Same format as std.toString, except does not keeps top-level string as-is
@@ -96,7 +133,6 @@
preserve_order: false,
#[cfg(feature = "exp-bigint")]
preserve_bigints: false,
- debug_truncate_strings: None,
}
}
pub fn std_to_json(
@@ -114,7 +150,6 @@
preserve_order,
#[cfg(feature = "exp-bigint")]
preserve_bigints: false,
- debug_truncate_strings: None,
}
}
// Same format as CLI manifestification
@@ -137,7 +172,6 @@
preserve_order,
#[cfg(feature = "exp-bigint")]
preserve_bigints: false,
- debug_truncate_strings: None,
}
}
// Same format as CLI manifestification
@@ -151,7 +185,6 @@
preserve_order: true,
#[cfg(feature = "exp-bigint")]
preserve_bigints: true,
- debug_truncate_strings: Some(256),
}
}
}
@@ -166,7 +199,6 @@
preserve_order: false,
#[cfg(feature = "exp-bigint")]
preserve_bigints: false,
- debug_truncate_strings: None,
}
}
}
@@ -197,18 +229,12 @@
}
Val::Null => buf.push_str("null"),
Val::Str(s) => {
- let flat = s.clone().into_flat();
- if let Some(truncate) = options.debug_truncate_strings {
- if flat.len() > truncate {
- let (start, end) = flat.split_at(truncate / 2);
- let (_, end) = end.split_at(end.len() - truncate / 2);
- escape_string_json_buf(&format!("{start}..{end}"), buf);
- } else {
- escape_string_json_buf(&flat, buf);
- }
- } else {
- escape_string_json_buf(&flat, buf);
- }
+ buf.reserve(2 + s.len());
+ buf.push('"');
+ s.chunks(&mut |c| {
+ escape_string_json_buf_raw(c, buf);
+ });
+ buf.push('"');
}
Val::Num(n) => write!(buf, "{n}").unwrap(),
#[cfg(feature = "exp-bigint")]
@@ -476,15 +502,15 @@
];
pub fn escape_string_json_buf(value: &str, buf: &mut String) {
+ buf.reserve_exact(value.len() + 2);
+ escape_string_json_buf_raw(value, buf);
+}
+
+fn escape_string_json_buf_raw(value: &str, buf: &mut String) {
// Safety: we only write correct utf-8 in this function
let buf: &mut Vec<u8> = unsafe { &mut *ptr::from_mut(buf).cast::<Vec<u8>>() };
let bytes = value.as_bytes();
-
- // Perfect for ascii strings, removes any reallocations
- buf.reserve(value.len() + 2);
- buf.push(b'"');
-
let mut start = 0;
for (i, &byte) in bytes.iter().enumerate() {
@@ -519,10 +545,8 @@
}
if start == bytes.len() {
- buf.push(b'"');
return;
}
buf.extend_from_slice(&bytes[start..]);
- buf.push(b'"');
}
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ /dev/null
@@ -1,67 +0,0 @@
-use jrsonnet_gcmodule::{Cc, Trace};
-use jrsonnet_interner::IStr;
-use rustc_hash::FxHashMap;
-
-use crate::{Thunk, Val, gc::WithCapacityExt as _};
-
-#[derive(Trace, Debug)]
-#[trace(tracking(force))]
-pub struct LayeredHashMapInternals {
- parent: Option<LayeredHashMap>,
- current: FxHashMap<IStr, Thunk<Val>>,
-}
-
-#[derive(Trace, Debug)]
-pub struct LayeredHashMap(Cc<LayeredHashMapInternals>);
-
-impl LayeredHashMap {
- pub fn iter_keys(self, mut handler: impl FnMut(IStr)) {
- for k in self.0.current.keys() {
- handler(k.clone());
- }
- if let Some(parent) = self.0.parent.clone() {
- parent.iter_keys(handler);
- }
- }
-
- pub(crate) fn new(layer: FxHashMap<IStr, Thunk<Val>>) -> Self {
- Self(Cc::new(LayeredHashMapInternals {
- parent: None,
- current: layer,
- }))
- }
-
- pub fn extend(self, new_layer: FxHashMap<IStr, Thunk<Val>>) -> Self {
- Self(Cc::new(LayeredHashMapInternals {
- parent: Some(self),
- current: new_layer,
- }))
- }
-
- pub fn get(&self, key: &IStr) -> Option<&Thunk<Val>> {
- (self.0)
- .current
- .get(key)
- .or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
- }
-
- pub fn contains_key(&self, key: &IStr) -> bool {
- (self.0).current.contains_key(key)
- || self.0.parent.as_ref().is_some_and(|p| p.contains_key(key))
- }
-}
-
-impl Clone for LayeredHashMap {
- fn clone(&self) -> Self {
- Self(self.0.clone())
- }
-}
-
-impl Default for LayeredHashMap {
- fn default() -> Self {
- Self(Cc::new(LayeredHashMapInternals {
- parent: None,
- current: FxHashMap::new(),
- }))
- }
-}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -355,8 +355,19 @@
Self::Tree(Rc::new((a, b, len)))
}
}
+ pub fn chunks(&self, c: &mut impl FnMut(&IStr)) {
+ fn write_buf(s: &StrValue, c: &mut impl FnMut(&IStr)) {
+ match s {
+ StrValue::Flat(f) => c(f),
+ StrValue::Tree(t) => {
+ write_buf(&t.0, c);
+ write_buf(&t.1, c);
+ }
+ }
+ }
+ write_buf(self, c);
+ }
pub fn into_flat(&self) -> IStr {
- #[cold]
fn write_buf(s: &StrValue, out: &mut String) {
match s {
StrValue::Flat(f) => out.push_str(f),
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth1#![allow(clippy::similar_names)]23use std::{4 cell::{Ref, RefCell, RefMut},5 collections::HashMap,6 f64,7 rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,16 error::Result,17 function::{CallLocation, FuncVal, builtin_id},18 tla::TlaArg,19 trace::PathResolver,20 typed::SerializeTypedObj as _,21 val::NumValue,22};23use jrsonnet_gcmodule::{Acyclic, Cc, Trace};24use jrsonnet_ir::Source;25use jrsonnet_macros::{IntoUntyped, Typed};26pub use manifest::*;27pub use math::*;28pub use misc::*;29pub use objects::*;30pub use operator::*;31pub use parse::*;32pub use sets::*;33pub use sort::*;34pub use strings::*;35pub use types::*;3637#[cfg(feature = "exp-regex")]38pub use crate::regex::*;3940mod arrays;41mod compat;42mod encoding;43mod hash;44mod keyf;45mod manifest;46mod math;47mod misc;48mod objects;49mod operator;50mod parse;51#[cfg(feature = "exp-regex")]52mod regex;53mod sets;54mod sort;55mod strings;56mod types;5758#[derive(Typed, IntoUntyped, Default)]59#[allow(non_snake_case)]60struct Builtins {61 #[typed(method)]62 id: builtin_id,63 // Types64 #[typed(method, rename = "type")]65 r#type: builtin_type,66 #[typed(method)]67 isString: builtin_is_string,68 #[typed(method)]69 isNumber: builtin_is_number,70 #[typed(method)]71 isBoolean: builtin_is_boolean,72 #[typed(method)]73 isObject: builtin_is_object,74 #[typed(method)]75 isArray: builtin_is_array,76 #[typed(method)]77 isFunction: builtin_is_function,78 #[typed(method)]79 isNull: builtin_is_null,80 // Arrays81 #[typed(method)]82 makeArray: builtin_make_array,83 #[typed(method)]84 repeat: builtin_repeat,85 #[typed(method)]86 slice: builtin_slice,87 #[typed(method)]88 map: builtin_map,89 #[typed(method)]90 mapWithIndex: builtin_map_with_index,91 #[typed(method)]92 mapWithKey: builtin_map_with_key,93 #[typed(method)]94 flatMap: builtin_flatmap,95 #[typed(method)]96 filter: builtin_filter,97 #[typed(method)]98 foldl: builtin_foldl,99 #[typed(method)]100 foldr: builtin_foldr,101 #[typed(method)]102 range: builtin_range,103 #[typed(method)]104 join: builtin_join,105 #[typed(method)]106 lines: builtin_lines,107 #[typed(method)]108 resolvePath: builtin_resolve_path,109 #[typed(method)]110 deepJoin: builtin_deep_join,111 #[typed(method)]112 reverse: builtin_reverse,113 #[typed(method)]114 any: builtin_any,115 #[typed(method)]116 all: builtin_all,117 #[typed(method)]118 member: builtin_member,119 #[typed(method)]120 find: builtin_find,121 #[typed(method)]122 contains: builtin_contains,123 #[typed(method)]124 count: builtin_count,125 #[typed(method)]126 avg: builtin_avg,127 #[typed(method)]128 removeAt: builtin_remove_at,129 #[typed(method)]130 remove: builtin_remove,131 #[typed(method)]132 flattenArrays: builtin_flatten_arrays,133 #[typed(method)]134 flattenDeepArray: builtin_flatten_deep_array,135 #[typed(method)]136 prune: builtin_prune,137 #[typed(method)]138 filterMap: builtin_filter_map,139 // Math140 #[typed(method)]141 abs: builtin_abs,142 #[typed(method)]143 sign: builtin_sign,144 #[typed(method)]145 max: builtin_max,146 #[typed(method)]147 min: builtin_min,148 #[typed(method)]149 clamp: builtin_clamp,150 #[typed(method)]151 sum: builtin_sum,152 #[typed(method)]153 modulo: builtin_modulo,154 #[typed(method)]155 floor: builtin_floor,156 #[typed(method)]157 ceil: builtin_ceil,158 #[typed(method)]159 log: builtin_log,160 #[typed(method)]161 log2: builtin_log2,162 #[typed(method)]163 log10: builtin_log10,164 #[typed(method)]165 pow: builtin_pow,166 #[typed(method)]167 sqrt: builtin_sqrt,168 #[typed(method)]169 sin: builtin_sin,170 #[typed(method)]171 cos: builtin_cos,172 #[typed(method)]173 tan: builtin_tan,174 #[typed(method)]175 asin: builtin_asin,176 #[typed(method)]177 acos: builtin_acos,178 #[typed(method)]179 atan: builtin_atan,180 #[typed(method)]181 atan2: builtin_atan2,182 #[typed(method)]183 exp: builtin_exp,184 #[typed(method)]185 mantissa: builtin_mantissa,186 #[typed(method)]187 exponent: builtin_exponent,188 #[typed(method)]189 round: builtin_round,190 #[typed(method)]191 isEven: builtin_is_even,192 #[typed(method)]193 isOdd: builtin_is_odd,194 #[typed(method)]195 isInteger: builtin_is_integer,196 #[typed(method)]197 isDecimal: builtin_is_decimal,198 #[typed(method)]199 deg2rad: builtin_deg2rad,200 #[typed(method)]201 rad2deg: builtin_rad2deg,202 #[typed(method)]203 hypot: builtin_hypot,204 // Operator205 #[typed(rename = "mod", method)]206 r#mod: builtin_mod,207 #[typed(method)]208 primitiveEquals: builtin_primitive_equals,209 #[typed(method)]210 equals: builtin_equals,211 #[typed(method)]212 xor: builtin_xor,213 #[typed(method)]214 xnor: builtin_xnor,215 #[typed(method)]216 format: builtin_format,217 // Sort218 #[typed(method)]219 sort: builtin_sort,220 #[typed(method)]221 uniq: builtin_uniq,222 #[typed(method)]223 set: builtin_set,224 #[typed(method)]225 minArray: builtin_min_array,226 #[typed(method)]227 maxArray: builtin_max_array,228 // Hash229 #[typed(method)]230 md5: builtin_md5,231 #[typed(method)]232 sha1: builtin_sha1,233 #[typed(method)]234 sha256: builtin_sha256,235 #[typed(method)]236 sha512: builtin_sha512,237 #[typed(method)]238 sha3: builtin_sha3,239 // Encoding240 #[typed(method)]241 encodeUTF8: builtin_encode_utf8,242 #[typed(method)]243 decodeUTF8: builtin_decode_utf8,244 #[typed(method)]245 base64: builtin_base64,246 #[typed(method)]247 base64Decode: builtin_base64_decode,248 #[typed(method)]249 base64DecodeBytes: builtin_base64_decode_bytes,250 // Objects251 #[typed(method)]252 objectFieldsEx: builtin_object_fields_ex,253 #[typed(method)]254 objectFields: builtin_object_fields,255 #[typed(method)]256 objectFieldsAll: builtin_object_fields_all,257 #[typed(method)]258 objectValues: builtin_object_values,259 #[typed(method)]260 objectValuesAll: builtin_object_values_all,261 #[typed(method)]262 objectKeysValues: builtin_object_keys_values,263 #[typed(method)]264 objectKeysValuesAll: builtin_object_keys_values_all,265 #[typed(method)]266 objectHasEx: builtin_object_has_ex,267 #[typed(method)]268 objectHas: builtin_object_has,269 #[typed(method)]270 objectHasAll: builtin_object_has_all,271 #[typed(method)]272 objectRemoveKey: builtin_object_remove_key,273 // Manifest274 #[typed(method)]275 escapeStringJson: builtin_escape_string_json,276 #[typed(method)]277 escapeStringPython: builtin_escape_string_python,278 #[typed(method)]279 escapeStringXML: builtin_escape_string_xml,280 #[typed(method)]281 manifestJsonEx: builtin_manifest_json_ex,282 #[typed(method)]283 manifestJson: builtin_manifest_json,284 #[typed(method)]285 manifestJsonMinified: builtin_manifest_json_minified,286 #[typed(method)]287 manifestYamlDoc: builtin_manifest_yaml_doc,288 #[typed(method)]289 manifestYamlStream: builtin_manifest_yaml_stream,290 #[typed(method)]291 manifestTomlEx: builtin_manifest_toml_ex,292 #[typed(method)]293 manifestToml: builtin_manifest_toml,294 #[typed(method)]295 toString: builtin_to_string,296 #[typed(method)]297 manifestPython: builtin_manifest_python,298 #[typed(method)]299 manifestPythonVars: builtin_manifest_python_vars,300 #[typed(method)]301 manifestXmlJsonml: builtin_manifest_xml_jsonml,302 #[typed(method)]303 manifestIni: builtin_manifest_ini,304 // Parse305 #[typed(method)]306 parseJson: builtin_parse_json,307 #[typed(method)]308 parseYaml: builtin_parse_yaml,309 // Strings310 #[typed(method)]311 codepoint: builtin_codepoint,312 #[typed(method)]313 substr: builtin_substr,314 #[typed(method)]315 char: builtin_char,316 #[typed(method)]317 strReplace: builtin_str_replace,318 #[typed(method)]319 escapeStringBash: builtin_escape_string_bash,320 #[typed(method)]321 escapeStringDollars: builtin_escape_string_dollars,322 #[typed(method)]323 isEmpty: builtin_is_empty,324 #[typed(method)]325 equalsIgnoreCase: builtin_equals_ignore_case,326 #[typed(method)]327 splitLimit: builtin_splitlimit,328 #[typed(method)]329 splitLimitR: builtin_splitlimitr,330 #[typed(method)]331 split: builtin_split,332 #[typed(method)]333 asciiUpper: builtin_ascii_upper,334 #[typed(method)]335 asciiLower: builtin_ascii_lower,336 #[typed(method)]337 findSubstr: builtin_find_substr,338 #[typed(method)]339 parseInt: builtin_parse_int,340 #[cfg(feature = "exp-bigint")]341 #[typed(method)]342 bigint: builtin_bigint,343 #[typed(method)]344 parseOctal: builtin_parse_octal,345 #[typed(method)]346 parseHex: builtin_parse_hex,347 #[typed(method)]348 stringChars: builtin_string_chars,349 #[typed(method)]350 lstripChars: builtin_lstrip_chars,351 #[typed(method)]352 rstripChars: builtin_rstrip_chars,353 #[typed(method)]354 stripChars: builtin_strip_chars,355 #[typed(method)]356 trim: builtin_trim,357 // Misc358 #[typed(method)]359 length: builtin_length,360 #[typed(method)]361 get: builtin_get,362 #[typed(method)]363 startsWith: builtin_starts_with,364 #[typed(method)]365 endsWith: builtin_ends_with,366 #[typed(method)]367 assertEqual: builtin_assert_equal,368 #[typed(method)]369 mergePatch: builtin_merge_patch,370 // Sets371 #[typed(method)]372 setMember: builtin_set_member,373 #[typed(method)]374 setInter: builtin_set_inter,375 #[typed(method)]376 setDiff: builtin_set_diff,377 #[typed(method)]378 setUnion: builtin_set_union,379 // Regex380 #[cfg(feature = "exp-regex")]381 #[typed(method)]382 regexQuoteMeta: builtin_regex_quote_meta,383 // Compat384 #[typed(method)]385 __compare: builtin___compare,386 #[typed(method)]387 __compare_array: builtin___compare_array,388 #[typed(method)]389 __array_less: builtin___array_less,390 #[typed(method)]391 __array_greater: builtin___array_greater,392 #[typed(method)]393 __array_less_or_equal: builtin___array_less_or_equal,394 #[typed(method)]395 __array_greater_or_equal: builtin___array_greater_or_equal,396}397398#[allow(clippy::too_many_lines)]399pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {400 let mut builder = ObjValueBuilder::new();401402 let builtins = Builtins::default();403 builtins.serialize(&mut builder).expect("no conflicts");404405 builder.method(406 "extVar",407 builtin_ext_var {408 settings: settings.clone(),409 },410 );411 builder.method(412 "native",413 builtin_native {414 settings: settings.clone(),415 },416 );417 builder.method("trace", builtin_trace { settings });418419 builder.field("pi").hide().value(Val::Num(420 NumValue::new(f64::consts::PI).expect("pi is finite"),421 ));422423 #[cfg(feature = "exp-regex")]424 {425 // Regex426 let regex_cache = RegexCache::default();427 builder.method(428 "regexFullMatch",429 builtin_regex_full_match {430 cache: regex_cache.clone(),431 },432 );433 builder.method(434 "regexPartialMatch",435 builtin_regex_partial_match {436 cache: regex_cache.clone(),437 },438 );439 builder.method(440 "regexReplace",441 builtin_regex_replace {442 cache: regex_cache.clone(),443 },444 );445 builder.method(446 "regexGlobalReplace",447 builtin_regex_global_replace { cache: regex_cache },448 );449 };450451 builder.build()452}453454pub trait TracePrinter: Acyclic {455 fn print_trace(&self, loc: CallLocation, value: IStr);456}457458#[derive(Acyclic)]459pub struct StdTracePrinter {460 resolver: PathResolver,461}462impl StdTracePrinter {463 pub fn new(resolver: PathResolver) -> Self {464 Self { resolver }465 }466}467impl TracePrinter for StdTracePrinter {468 fn print_trace(&self, loc: CallLocation, value: IStr) {469 eprint!("TRACE:");470 if let Some(loc) = loc.0 {471 let locs = loc.0.map_source_locations(&[loc.1]);472 eprint!(473 " {}:{}",474 loc.0.source_path().path().map_or_else(475 || loc.0.source_path().to_string(),476 |p| self.resolver.resolve(p)477 ),478 locs[0].line479 );480 }481 eprintln!(" {value}");482 }483}484485#[derive(Clone, Trace)]486pub struct Settings {487 /// Used for `std.extVar`488 pub ext_vars: HashMap<IStr, TlaArg>,489 /// Used for `std.native`490 pub ext_natives: HashMap<IStr, FuncVal>,491 /// Used for `std.trace`492 pub trace_printer: Rc<dyn TracePrinter>,493 /// Used for `std.thisFile`494 pub path_resolver: PathResolver,495}496497#[derive(Trace, Clone)]498pub struct ContextInitializer {499 /// std without applied thisFile overlay500 stdlib_obj: ObjValue,501 settings: Cc<RefCell<Settings>>,502}503impl ContextInitializer {504 pub fn new(resolver: PathResolver) -> Self {505 let settings = Settings {506 ext_vars: HashMap::new(),507 ext_natives: HashMap::new(),508 trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),509 path_resolver: resolver,510 };511 let settings = Cc::new(RefCell::new(settings));512 let stdlib_obj = stdlib_uncached(settings.clone());513 Self {514 stdlib_obj,515 settings,516 }517 }518 pub fn settings(&self) -> Ref<'_, Settings> {519 self.settings.borrow()520 }521 pub fn settings_mut(&self) -> RefMut<'_, Settings> {522 self.settings.borrow_mut()523 }524 pub fn add_ext_var(&self, name: IStr, value: Val) {525 self.settings_mut()526 .ext_vars527 .insert(name, TlaArg::Val(value));528 }529 pub fn add_ext_str(&self, name: IStr, value: IStr) {530 self.settings_mut()531 .ext_vars532 .insert(name, TlaArg::String(value));533 }534 pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {535 // self.data_mut().volatile_files.insert(source_name, code);536 self.settings_mut()537 .ext_vars538 .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));539 Ok(())540 }541 pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {542 self.settings_mut()543 .ext_natives544 .insert(name.into(), cb.into());545 }546}547impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {548 fn reserve_vars(&self) -> usize {549 1550 }551 fn populate(&self, source: Source, builder: &mut ContextBuilder) {552 let mut std = ObjValueBuilder::new();553 std.with_super(self.stdlib_obj.clone());554 std.field("thisFile").hide().value({555 let source_path = source.source_path();556 source_path.path().map_or_else(557 || source_path.to_string(),558 |p| self.settings().path_resolver.resolve(p),559 )560 });561 let stdlib_with_this_file = std.build();562563 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));564 }565 fn as_any(&self) -> &dyn std::any::Any {566 self567 }568}1#![allow(clippy::similar_names)]23use std::{4 cell::{Ref, RefCell, RefMut},5 collections::HashMap,6 f64,7 rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,16 error::Result,17 function::{CallLocation, FuncVal, builtin_id},18 tla::TlaArg,19 trace::PathResolver,20 typed::SerializeTypedObj as _,21 val::NumValue,22};23use jrsonnet_gcmodule::{Acyclic, Cc, Trace};24use jrsonnet_ir::Source;25use jrsonnet_macros::{IntoUntyped, Typed};26pub use manifest::*;27pub use math::*;28pub use misc::*;29pub use objects::*;30pub use operator::*;31pub use parse::*;32pub use sets::*;33pub use sort::*;34pub use strings::*;35pub use types::*;3637#[cfg(feature = "exp-regex")]38pub use crate::regex::*;3940mod arrays;41mod compat;42mod encoding;43mod hash;44mod keyf;45mod manifest;46mod math;47mod misc;48mod objects;49mod operator;50mod parse;51#[cfg(feature = "exp-regex")]52mod regex;53mod sets;54mod sort;55mod strings;56mod types;5758#[derive(Typed, IntoUntyped, Default)]59#[allow(non_snake_case)]60struct Builtins {61 #[typed(method)]62 id: builtin_id,63 // Types64 #[typed(method, rename = "type")]65 r#type: builtin_type,66 #[typed(method)]67 isString: builtin_is_string,68 #[typed(method)]69 isNumber: builtin_is_number,70 #[typed(method)]71 isBoolean: builtin_is_boolean,72 #[typed(method)]73 isObject: builtin_is_object,74 #[typed(method)]75 isArray: builtin_is_array,76 #[typed(method)]77 isFunction: builtin_is_function,78 #[typed(method)]79 isNull: builtin_is_null,80 // Arrays81 #[typed(method)]82 makeArray: builtin_make_array,83 #[typed(method)]84 repeat: builtin_repeat,85 #[typed(method)]86 slice: builtin_slice,87 #[typed(method)]88 map: builtin_map,89 #[typed(method)]90 mapWithIndex: builtin_map_with_index,91 #[typed(method)]92 mapWithKey: builtin_map_with_key,93 #[typed(method)]94 flatMap: builtin_flatmap,95 #[typed(method)]96 filter: builtin_filter,97 #[typed(method)]98 foldl: builtin_foldl,99 #[typed(method)]100 foldr: builtin_foldr,101 #[typed(method)]102 range: builtin_range,103 #[typed(method)]104 join: builtin_join,105 #[typed(method)]106 lines: builtin_lines,107 #[typed(method)]108 resolvePath: builtin_resolve_path,109 #[typed(method)]110 deepJoin: builtin_deep_join,111 #[typed(method)]112 reverse: builtin_reverse,113 #[typed(method)]114 any: builtin_any,115 #[typed(method)]116 all: builtin_all,117 #[typed(method)]118 member: builtin_member,119 #[typed(method)]120 find: builtin_find,121 #[typed(method)]122 contains: builtin_contains,123 #[typed(method)]124 count: builtin_count,125 #[typed(method)]126 avg: builtin_avg,127 #[typed(method)]128 removeAt: builtin_remove_at,129 #[typed(method)]130 remove: builtin_remove,131 #[typed(method)]132 flattenArrays: builtin_flatten_arrays,133 #[typed(method)]134 flattenDeepArray: builtin_flatten_deep_array,135 #[typed(method)]136 prune: builtin_prune,137 #[typed(method)]138 filterMap: builtin_filter_map,139 // Math140 #[typed(method)]141 abs: builtin_abs,142 #[typed(method)]143 sign: builtin_sign,144 #[typed(method)]145 max: builtin_max,146 #[typed(method)]147 min: builtin_min,148 #[typed(method)]149 clamp: builtin_clamp,150 #[typed(method)]151 sum: builtin_sum,152 #[typed(method)]153 modulo: builtin_modulo,154 #[typed(method)]155 floor: builtin_floor,156 #[typed(method)]157 ceil: builtin_ceil,158 #[typed(method)]159 log: builtin_log,160 #[typed(method)]161 log2: builtin_log2,162 #[typed(method)]163 log10: builtin_log10,164 #[typed(method)]165 pow: builtin_pow,166 #[typed(method)]167 sqrt: builtin_sqrt,168 #[typed(method)]169 sin: builtin_sin,170 #[typed(method)]171 cos: builtin_cos,172 #[typed(method)]173 tan: builtin_tan,174 #[typed(method)]175 asin: builtin_asin,176 #[typed(method)]177 acos: builtin_acos,178 #[typed(method)]179 atan: builtin_atan,180 #[typed(method)]181 atan2: builtin_atan2,182 #[typed(method)]183 exp: builtin_exp,184 #[typed(method)]185 mantissa: builtin_mantissa,186 #[typed(method)]187 exponent: builtin_exponent,188 #[typed(method)]189 round: builtin_round,190 #[typed(method)]191 isEven: builtin_is_even,192 #[typed(method)]193 isOdd: builtin_is_odd,194 #[typed(method)]195 isInteger: builtin_is_integer,196 #[typed(method)]197 isDecimal: builtin_is_decimal,198 #[typed(method)]199 deg2rad: builtin_deg2rad,200 #[typed(method)]201 rad2deg: builtin_rad2deg,202 #[typed(method)]203 hypot: builtin_hypot,204 // Operator205 #[typed(rename = "mod", method)]206 r#mod: builtin_mod,207 #[typed(method)]208 primitiveEquals: builtin_primitive_equals,209 #[typed(method)]210 equals: builtin_equals,211 #[typed(method)]212 xor: builtin_xor,213 #[typed(method)]214 xnor: builtin_xnor,215 #[typed(method)]216 format: builtin_format,217 // Sort218 #[typed(method)]219 sort: builtin_sort,220 #[typed(method)]221 uniq: builtin_uniq,222 #[typed(method)]223 set: builtin_set,224 #[typed(method)]225 minArray: builtin_min_array,226 #[typed(method)]227 maxArray: builtin_max_array,228 // Hash229 #[typed(method)]230 md5: builtin_md5,231 #[typed(method)]232 sha1: builtin_sha1,233 #[typed(method)]234 sha256: builtin_sha256,235 #[typed(method)]236 sha512: builtin_sha512,237 #[typed(method)]238 sha3: builtin_sha3,239 // Encoding240 #[typed(method)]241 encodeUTF8: builtin_encode_utf8,242 #[typed(method)]243 decodeUTF8: builtin_decode_utf8,244 #[typed(method)]245 base64: builtin_base64,246 #[typed(method)]247 base64Decode: builtin_base64_decode,248 #[typed(method)]249 base64DecodeBytes: builtin_base64_decode_bytes,250 // Objects251 #[typed(method)]252 objectFieldsEx: builtin_object_fields_ex,253 #[typed(method)]254 objectFields: builtin_object_fields,255 #[typed(method)]256 objectFieldsAll: builtin_object_fields_all,257 #[typed(method)]258 objectValues: builtin_object_values,259 #[typed(method)]260 objectValuesAll: builtin_object_values_all,261 #[typed(method)]262 objectKeysValues: builtin_object_keys_values,263 #[typed(method)]264 objectKeysValuesAll: builtin_object_keys_values_all,265 #[typed(method)]266 objectHasEx: builtin_object_has_ex,267 #[typed(method)]268 objectHas: builtin_object_has,269 #[typed(method)]270 objectHasAll: builtin_object_has_all,271 #[typed(method)]272 objectRemoveKey: builtin_object_remove_key,273 // Manifest274 #[typed(method)]275 escapeStringJson: builtin_escape_string_json,276 #[typed(method)]277 escapeStringPython: builtin_escape_string_python,278 #[typed(method)]279 escapeStringXML: builtin_escape_string_xml,280 #[typed(method)]281 manifestJsonEx: builtin_manifest_json_ex,282 #[typed(method)]283 manifestJson: builtin_manifest_json,284 #[typed(method)]285 manifestJsonMinified: builtin_manifest_json_minified,286 #[typed(method)]287 manifestYamlDoc: builtin_manifest_yaml_doc,288 #[typed(method)]289 manifestYamlStream: builtin_manifest_yaml_stream,290 #[typed(method)]291 manifestTomlEx: builtin_manifest_toml_ex,292 #[typed(method)]293 manifestToml: builtin_manifest_toml,294 #[typed(method)]295 toString: builtin_to_string,296 #[typed(method)]297 manifestPython: builtin_manifest_python,298 #[typed(method)]299 manifestPythonVars: builtin_manifest_python_vars,300 #[typed(method)]301 manifestXmlJsonml: builtin_manifest_xml_jsonml,302 #[typed(method)]303 manifestIni: builtin_manifest_ini,304 // Parse305 #[typed(method)]306 parseJson: builtin_parse_json,307 #[typed(method)]308 parseYaml: builtin_parse_yaml,309 // Strings310 #[typed(method)]311 codepoint: builtin_codepoint,312 #[typed(method)]313 substr: builtin_substr,314 #[typed(method)]315 char: builtin_char,316 #[typed(method)]317 strReplace: builtin_str_replace,318 #[typed(method)]319 escapeStringBash: builtin_escape_string_bash,320 #[typed(method)]321 escapeStringDollars: builtin_escape_string_dollars,322 #[typed(method)]323 isEmpty: builtin_is_empty,324 #[typed(method)]325 equalsIgnoreCase: builtin_equals_ignore_case,326 #[typed(method)]327 splitLimit: builtin_splitlimit,328 #[typed(method)]329 splitLimitR: builtin_splitlimitr,330 #[typed(method)]331 split: builtin_split,332 #[typed(method)]333 asciiUpper: builtin_ascii_upper,334 #[typed(method)]335 asciiLower: builtin_ascii_lower,336 #[typed(method)]337 findSubstr: builtin_find_substr,338 #[typed(method)]339 parseInt: builtin_parse_int,340 #[cfg(feature = "exp-bigint")]341 #[typed(method)]342 bigint: builtin_bigint,343 #[typed(method)]344 parseOctal: builtin_parse_octal,345 #[typed(method)]346 parseHex: builtin_parse_hex,347 #[typed(method)]348 stringChars: builtin_string_chars,349 #[typed(method)]350 lstripChars: builtin_lstrip_chars,351 #[typed(method)]352 rstripChars: builtin_rstrip_chars,353 #[typed(method)]354 stripChars: builtin_strip_chars,355 #[typed(method)]356 trim: builtin_trim,357 // Misc358 #[typed(method)]359 length: builtin_length,360 #[typed(method)]361 get: builtin_get,362 #[typed(method)]363 startsWith: builtin_starts_with,364 #[typed(method)]365 endsWith: builtin_ends_with,366 #[typed(method)]367 assertEqual: builtin_assert_equal,368 #[typed(method)]369 mergePatch: builtin_merge_patch,370 // Sets371 #[typed(method)]372 setMember: builtin_set_member,373 #[typed(method)]374 setInter: builtin_set_inter,375 #[typed(method)]376 setDiff: builtin_set_diff,377 #[typed(method)]378 setUnion: builtin_set_union,379 // Regex380 #[cfg(feature = "exp-regex")]381 #[typed(method)]382 regexQuoteMeta: builtin_regex_quote_meta,383 // Compat384 #[typed(method)]385 __compare: builtin___compare,386 #[typed(method)]387 __compare_array: builtin___compare_array,388 #[typed(method)]389 __array_less: builtin___array_less,390 #[typed(method)]391 __array_greater: builtin___array_greater,392 #[typed(method)]393 __array_less_or_equal: builtin___array_less_or_equal,394 #[typed(method)]395 __array_greater_or_equal: builtin___array_greater_or_equal,396}397398#[allow(clippy::too_many_lines)]399pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {400 let mut builder = ObjValueBuilder::new();401402 let builtins = Builtins::default();403 builtins.serialize(&mut builder).expect("no conflicts");404405 builder.method(406 "extVar",407 builtin_ext_var {408 settings: settings.clone(),409 },410 );411 builder.method(412 "native",413 builtin_native {414 settings: settings.clone(),415 },416 );417 builder.method("trace", builtin_trace { settings });418419 builder.field("pi").hide().value(Val::Num(420 NumValue::new(f64::consts::PI).expect("pi is finite"),421 ));422423 #[cfg(feature = "exp-regex")]424 {425 // Regex426 let regex_cache = RegexCache::default();427 builder.method(428 "regexFullMatch",429 builtin_regex_full_match {430 cache: regex_cache.clone(),431 },432 );433 builder.method(434 "regexPartialMatch",435 builtin_regex_partial_match {436 cache: regex_cache.clone(),437 },438 );439 builder.method(440 "regexReplace",441 builtin_regex_replace {442 cache: regex_cache.clone(),443 },444 );445 builder.method(446 "regexGlobalReplace",447 builtin_regex_global_replace { cache: regex_cache },448 );449 };450451 builder.build()452}453454pub trait TracePrinter: Acyclic {455 fn print_trace(&self, loc: CallLocation, value: IStr);456}457458#[derive(Acyclic)]459pub struct StdTracePrinter {460 resolver: PathResolver,461}462impl StdTracePrinter {463 pub fn new(resolver: PathResolver) -> Self {464 Self { resolver }465 }466}467impl TracePrinter for StdTracePrinter {468 fn print_trace(&self, loc: CallLocation, value: IStr) {469 eprint!("TRACE:");470 if let Some(loc) = loc.0 {471 let locs = loc.0.map_source_locations(&[loc.1]);472 eprint!(473 " {}:{}",474 loc.0.source_path().path().map_or_else(475 || loc.0.source_path().to_string(),476 |p| self.resolver.resolve(p)477 ),478 locs[0].line479 );480 }481 eprintln!(" {value}");482 }483}484485#[derive(Clone, Trace)]486pub struct Settings {487 /// Used for `std.extVar`488 pub ext_vars: HashMap<IStr, TlaArg>,489 /// Used for `std.native`490 pub ext_natives: HashMap<IStr, FuncVal>,491 /// Used for `std.trace`492 pub trace_printer: Rc<dyn TracePrinter>,493 /// Used for `std.thisFile`494 pub path_resolver: PathResolver,495}496497#[derive(Trace, Clone)]498pub struct ContextInitializer {499 /// std without applied thisFile overlay500 stdlib_obj: ObjValue,501 settings: Cc<RefCell<Settings>>,502}503impl ContextInitializer {504 pub fn new(resolver: PathResolver) -> Self {505 let settings = Settings {506 ext_vars: HashMap::new(),507 ext_natives: HashMap::new(),508 trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),509 path_resolver: resolver,510 };511 let settings = Cc::new(RefCell::new(settings));512 let stdlib_obj = stdlib_uncached(settings.clone());513 Self {514 stdlib_obj,515 settings,516 }517 }518 pub fn settings(&self) -> Ref<'_, Settings> {519 self.settings.borrow()520 }521 pub fn settings_mut(&self) -> RefMut<'_, Settings> {522 self.settings.borrow_mut()523 }524 pub fn add_ext_var(&self, name: IStr, value: Val) {525 self.settings_mut()526 .ext_vars527 .insert(name, TlaArg::Val(value));528 }529 pub fn add_ext_str(&self, name: IStr, value: IStr) {530 self.settings_mut()531 .ext_vars532 .insert(name, TlaArg::String(value));533 }534 pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {535 // self.data_mut().volatile_files.insert(source_name, code);536 self.settings_mut()537 .ext_vars538 .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));539 Ok(())540 }541 pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {542 self.settings_mut()543 .ext_natives544 .insert(name.into(), cb.into());545 }546}547impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {548 fn populate(&self, source: Source, builder: &mut ContextBuilder) {549 let mut std = ObjValueBuilder::new();550 std.with_super(self.stdlib_obj.clone());551 std.field("thisFile").hide().value({552 let source_path = source.source_path();553 source_path.path().map_or_else(554 || source_path.to_string(),555 |p| self.settings().path_resolver.resolve(p),556 )557 });558 let stdlib_with_this_file = std.build();559560 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));561 }562 fn as_any(&self) -> &dyn std::any::Any {563 self564 }565}tests/Cargo.tomldiffbeforeafterboth--- a/tests/Cargo.toml
+++ b/tests/Cargo.toml
@@ -18,6 +18,7 @@
jrsonnet-evaluator.workspace = true
jrsonnet-gcmodule.workspace = true
jrsonnet-stdlib.workspace = true
+mimallocator.workspace = true
serde.workspace = true
serde_json.workspace = true