difftreelog
refactor better rebinding handling
in: master
15 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -4,34 +4,15 @@
use jrsonnet_interner::IStr;
use crate::{
- cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, LazyBinding, ObjValue, Pending,
- Result, State, Thunk, Val,
+ cc_ptr_eq, error::Error::*, gc::GcHashMap, map::LayeredHashMap, ObjValue, Pending, Result,
+ Thunk, Val,
};
-#[derive(Clone, Trace)]
-pub struct ContextCreator(pub Context, pub Pending<GcHashMap<IStr, LazyBinding>>);
-impl ContextCreator {
- pub fn create(
- &self,
- s: State,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<Context> {
- self.0.clone().extend_unbound(
- s,
- self.1.clone().unwrap(),
- self.0.dollar().clone().or_else(|| this.clone()),
- this,
- super_obj,
- )
- }
-}
-
#[derive(Trace)]
struct ContextInternals {
dollar: Option<ObjValue>,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
bindings: LayeredHashMap,
}
impl Debug for ContextInternals {
@@ -56,14 +37,14 @@
}
pub fn super_obj(&self) -> &Option<ObjValue> {
- &self.0.super_obj
+ &self.0.sup
}
pub fn new() -> Self {
Self(Cc::new(ContextInternals {
dollar: None,
this: None,
- super_obj: None,
+ sup: None,
bindings: LayeredHashMap::default(),
}))
}
@@ -92,11 +73,6 @@
let mut new_bindings = GcHashMap::with_capacity(1);
new_bindings.insert(name, Thunk::evaluated(value));
self.extend(new_bindings, None, None, None)
- }
-
- #[must_use]
- pub fn with_this_super(self, new_this: ObjValue, new_super_obj: Option<ObjValue>) -> Self {
- self.extend(GcHashMap::new(), None, Some(new_this), new_super_obj)
}
#[must_use]
@@ -104,13 +80,13 @@
self,
new_bindings: GcHashMap<IStr, Thunk<Val>>,
new_dollar: Option<ObjValue>,
+ new_sup: Option<ObjValue>,
new_this: Option<ObjValue>,
- new_super_obj: Option<ObjValue>,
) -> Self {
let ctx = &self.0;
let dollar = new_dollar.or_else(|| ctx.dollar.clone());
let this = new_this.or_else(|| ctx.this.clone());
- let super_obj = new_super_obj.or_else(|| ctx.super_obj.clone());
+ let sup = new_sup.or_else(|| ctx.sup.clone());
let bindings = if new_bindings.is_empty() {
ctx.bindings.clone()
} else {
@@ -118,32 +94,10 @@
};
Self(Cc::new(ContextInternals {
dollar,
+ sup,
this,
- super_obj,
bindings,
}))
- }
- #[must_use]
- pub fn extend_bound(self, new_bindings: GcHashMap<IStr, Thunk<Val>>) -> Self {
- let new_this = self.0.this.clone();
- let new_super_obj = self.0.super_obj.clone();
- self.extend(new_bindings, None, new_this, new_super_obj)
- }
- pub fn extend_unbound(
- self,
- s: State,
- new_bindings: GcHashMap<IStr, LazyBinding>,
- new_dollar: Option<ObjValue>,
- new_this: Option<ObjValue>,
- new_super_obj: Option<ObjValue>,
- ) -> Result<Self> {
- let this = new_this.or_else(|| self.0.this.clone());
- let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());
- let mut new = GcHashMap::with_capacity(new_bindings.len());
- for (k, v) in new_bindings.0 {
- new.insert(k, v.evaluate(s.clone(), this.clone(), super_obj.clone())?);
- }
- Ok(self.extend(new, new_dollar, this, super_obj))
}
}
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -11,12 +11,13 @@
Context, Pending, State, Thunk, Val,
};
+#[allow(clippy::too_many_lines)]
fn destruct(
d: &Destruct,
parent: Thunk<Val>,
new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,
) -> Result<()> {
- Ok(match d {
+ match d {
Destruct::Full(v) => {
let old = new_bindings.insert(v.clone(), parent);
if old.is_some() {
@@ -157,8 +158,6 @@
}
#[cfg(feature = "exp-destruct")]
Destruct::Object { fields, rest } => {
- use jrsonnet_parser::DestructRest;
-
use crate::{obj::ObjValue, throw_runtime};
#[derive(Trace)]
@@ -223,7 +222,8 @@
}
}
}
- })
+ }
+ Ok(())
}
pub fn evaluate_dest(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -1,7 +1,9 @@
+use std::rc::Rc;
+
use gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
- ArgsDesc, AssertStmt, BindSpec, CompSpec, Destruct, Expr, FieldMember, ForSpecData, IfSpecData,
+ ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,
LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
@@ -14,147 +16,13 @@
stdlib::{std_slice, BUILTINS},
tb, throw,
typed::Typed,
- val::{ArrValue, Thunk, ThunkValue},
- Bindable, Context, ContextCreator, GcHashMap, LazyBinding, ObjValue, ObjValueBuilder,
- ObjectAssertion, Pending, Result, State, Val,
+ val::{ArrValue, CachedBindable, Thunk, ThunkValue},
+ Bindable, Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
+ State, Val,
};
pub mod destructure;
pub mod operator;
-
-#[allow(clippy::too_many_lines)]
-pub fn evaluate_binding(b: BindSpec, cctx: ContextCreator) -> Result<(IStr, LazyBinding)> {
- match b {
- BindSpec::Field {
- into: Destruct::Full(name),
- value,
- } => {
- #[derive(Trace)]
- struct BindableNamedThunk {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
-
- cctx: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl ThunkValue for BindableNamedThunk {
- type Output = Val;
- fn get(self: Box<Self>, s: State) -> Result<Val> {
- evaluate_named(
- s.clone(),
- self.cctx.create(s, self.this, self.super_obj)?,
- &self.value,
- self.name,
- )
- }
- }
-
- #[derive(Trace)]
- struct BindableNamed {
- cctx: ContextCreator,
- name: IStr,
- value: LocExpr,
- }
- impl Bindable for BindableNamed {
- fn bind(
- &self,
- _: State,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<Thunk<Val>> {
- Ok(Thunk::new(tb!(BindableNamedThunk {
- this,
- super_obj,
- cctx: self.cctx.clone(),
- name: self.name.clone(),
- value: self.value.clone(),
- })))
- }
- }
-
- Ok((
- name.clone(),
- LazyBinding::Bindable(Cc::new(tb!(BindableNamed {
- cctx,
- name: name.clone(),
- value: value.clone(),
- }))),
- ))
- }
- #[cfg(feature = "exp-destruct")]
- BindSpec::Field { into: _, .. } => {
- use crate::throw_runtime;
- throw_runtime!("destructuring is not yet supported here")
- }
- BindSpec::Function {
- name,
- params,
- value,
- } => {
- #[derive(Trace)]
- struct BindableMethodThunk {
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
-
- cctx: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl ThunkValue for BindableMethodThunk {
- type Output = Val;
- fn get(self: Box<Self>, s: State) -> Result<Val> {
- Ok(evaluate_method(
- self.cctx.create(s, self.this, self.super_obj)?,
- self.name,
- self.params,
- self.value,
- ))
- }
- }
-
- #[derive(Trace)]
- struct BindableMethod {
- cctx: ContextCreator,
- name: IStr,
- params: ParamsDesc,
- value: LocExpr,
- }
- impl Bindable for BindableMethod {
- fn bind(
- &self,
- _: State,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<Thunk<Val>> {
- Ok(Thunk::<Val>::new(tb!(BindableMethodThunk {
- this,
- super_obj,
-
- cctx: self.cctx.clone(),
- name: self.name.clone(),
- params: self.params.clone(),
- value: self.value.clone(),
- })))
- }
- }
-
- let params = params.clone();
-
- Ok((
- name.clone(),
- LazyBinding::Bindable(Cc::new(tb!(BindableMethod {
- cctx,
- name: name.clone(),
- params,
- value,
- }))),
- ))
- }
- }
-}
-
pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {
Val::Func(FuncVal::Normal(Cc::new(FuncDesc {
name,
@@ -218,28 +86,65 @@
Ok(())
}
+trait CloneableBindable<T>: Bindable<Bound = T> + Clone {}
+
+fn evaluate_object_locals(
+ fctx: Pending<Context>,
+ locals: Rc<Vec<BindSpec>>,
+) -> impl CloneableBindable<Context> {
+ #[derive(Trace, Clone)]
+ struct WithObjectLocals {
+ fctx: Pending<Context>,
+ locals: Rc<Vec<BindSpec>>,
+ }
+ impl CloneableBindable<Context> for WithObjectLocals {}
+ impl Bindable for WithObjectLocals {
+ type Bound = Context;
+
+ fn bind(
+ &self,
+ _s: State,
+ sup: Option<ObjValue>,
+ this: Option<ObjValue>,
+ ) -> Result<Context> {
+ let fctx = Context::new_future();
+ let mut new_bindings = GcHashMap::new();
+ for b in self.locals.iter() {
+ evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
+ }
+
+ let ctx = self.fctx.unwrap();
+ let new_dollar = ctx.dollar().clone().or_else(|| this.clone());
+
+ let ctx = ctx
+ .extend(new_bindings, new_dollar, sup, this)
+ .into_future(fctx);
+
+ Ok(ctx)
+ }
+ }
+
+ WithObjectLocals { fctx, locals }
+}
+
#[allow(clippy::too_many_lines)]
pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {
- let new_bindings = Pending::new();
- let future_this = Pending::new();
- let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
- {
- let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());
- for r in members
+ let mut builder = ObjValueBuilder::new();
+ let locals = Rc::new(
+ members
.iter()
.filter_map(|m| match m {
- Member::BindStmt(b) => Some(b.clone()),
+ Member::BindStmt(bind) => Some(bind.clone()),
_ => None,
})
- .map(|b| evaluate_binding(b.clone(), cctx.clone()))
- {
- let (n, b) = r?;
- bindings.insert(n, b);
- }
- new_bindings.fill(bindings);
- }
+ .collect::<Vec<_>>(),
+ );
+
+ let fctx = Context::new_future();
+
+ // We have single context for all fields, so we can cache binds
+ let uctx = CachedBindable::new(evaluate_object_locals(fctx.clone(), locals));
- let mut builder = ObjValueBuilder::new();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -250,21 +155,22 @@
value,
}) => {
#[derive(Trace)]
- struct ObjMemberBinding {
- cctx: ContextCreator,
+ struct ObjMemberBinding<B> {
+ uctx: B,
value: LocExpr,
name: IStr,
}
- impl Bindable for ObjMemberBinding {
+ impl<B: Bindable<Bound = Context>> Bindable for ObjMemberBinding<B> {
+ type Bound = Thunk<Val>;
fn bind(
&self,
s: State,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
) -> Result<Thunk<Val>> {
Ok(Thunk::evaluated(evaluate_named(
s.clone(),
- self.cctx.create(s, this, super_obj)?,
+ self.uctx.bind(s, sup, this)?,
&self.value,
self.name.clone(),
)?))
@@ -286,9 +192,9 @@
.bindable(
s.clone(),
tb!(ObjMemberBinding {
- cctx: cctx.clone(),
+ uctx: uctx.clone(),
value: value.clone(),
- name,
+ name: name.clone()
}),
)?;
}
@@ -299,21 +205,22 @@
..
}) => {
#[derive(Trace)]
- struct ObjMemberBinding {
- cctx: ContextCreator,
+ struct ObjMemberBinding<B> {
+ uctx: B,
value: LocExpr,
params: ParamsDesc,
name: IStr,
}
- impl Bindable for ObjMemberBinding {
+ impl<B: Bindable<Bound = Context>> Bindable for ObjMemberBinding<B> {
+ type Bound = Thunk<Val>;
fn bind(
&self,
s: State,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
) -> Result<Thunk<Val>> {
Ok(Thunk::evaluated(evaluate_method(
- self.cctx.create(s, this, super_obj)?,
+ self.uctx.bind(s, sup, this)?,
self.name.clone(),
self.params.clone(),
self.value.clone(),
@@ -334,40 +241,42 @@
.bindable(
s.clone(),
tb!(ObjMemberBinding {
- cctx: cctx.clone(),
+ uctx: uctx.clone(),
value: value.clone(),
params: params.clone(),
- name,
+ name: name.clone()
}),
)?;
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
#[derive(Trace)]
- struct ObjectAssert {
- cctx: ContextCreator,
+ struct ObjectAssert<B> {
+ uctx: B,
assert: AssertStmt,
}
- impl ObjectAssertion for ObjectAssert {
+ impl<B: Bindable<Bound = Context>> ObjectAssertion for ObjectAssert<B> {
fn run(
&self,
s: State,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
) -> Result<()> {
- let ctx = self.cctx.create(s.clone(), this, super_obj)?;
+ let ctx = self.uctx.bind(s.clone(), sup, this)?;
evaluate_assert(s, ctx, &self.assert)
}
}
builder.assert(tb!(ObjectAssert {
- cctx: cctx.clone(),
+ uctx: uctx.clone(),
assert: stmt.clone(),
}));
}
}
}
let this = builder.build();
- future_this.fill(this.clone());
+ let _ctx = ctx
+ .extend(GcHashMap::new(), None, None, Some(this.clone()))
+ .into_future(fctx);
Ok(this)
}
@@ -375,44 +284,45 @@
Ok(match object {
ObjBody::MemberList(members) => evaluate_member_list_object(s, ctx, members)?,
ObjBody::ObjComp(obj) => {
- let future_this = Pending::new();
let mut builder = ObjValueBuilder::new();
- evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {
- let new_bindings = Pending::new();
- let cctx = ContextCreator(ctx.clone(), new_bindings.clone());
- let mut bindings: GcHashMap<IStr, LazyBinding> =
- GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());
- for r in obj
- .pre_locals
+ let locals = Rc::new(
+ obj.pre_locals
.iter()
.chain(obj.post_locals.iter())
- .map(|b| evaluate_binding(b.clone(), cctx.clone()))
- {
- let (n, b) = r?;
- bindings.insert(n, b);
- }
- new_bindings.fill(bindings.clone());
- let ctx = ctx.extend_unbound(s.clone(), bindings, None, None, None)?;
+ .cloned()
+ .collect::<Vec<_>>(),
+ );
+ let mut ctxs = vec![];
+ evaluate_comp(s.clone(), ctx, &obj.compspecs, &mut |ctx| {
let key = evaluate(s.clone(), ctx.clone(), &obj.key)?;
+ let fctx = Context::new_future();
+ ctxs.push((ctx, fctx.clone()));
+ let uctx = evaluate_object_locals(fctx, locals.clone());
match key {
Val::Null => {}
Val::Str(n) => {
#[derive(Trace)]
- struct ObjCompBinding {
- ctx: Context,
+ struct ObjCompBinding<B> {
+ uctx: B,
value: LocExpr,
}
- impl Bindable for ObjCompBinding {
+ impl<B: Bindable<Bound = Context>> Bindable for ObjCompBinding<B> {
+ type Bound = Thunk<Val>;
fn bind(
&self,
s: State,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- _super_obj: Option<ObjValue>,
) -> Result<Thunk<Val>> {
Ok(Thunk::evaluated(evaluate(
- s,
- self.ctx.clone().extend(GcHashMap::new(), None, this, None),
+ s.clone(),
+ self.uctx.bind(s, sup, this.clone())?.extend(
+ GcHashMap::new(),
+ None,
+ None,
+ this,
+ ),
&self.value,
)?))
}
@@ -424,7 +334,7 @@
.bindable(
s.clone(),
tb!(ObjCompBinding {
- ctx,
+ uctx,
value: obj.value.clone(),
}),
)?;
@@ -436,7 +346,11 @@
})?;
let this = builder.build();
- future_this.fill(this.clone());
+ for (ctx, fctx) in ctxs {
+ let _ctx = ctx
+ .extend(GcHashMap::new(), None, None, Some(this.clone()))
+ .into_future(fctx);
+ }
this
}
})
@@ -596,7 +510,7 @@
for b in bindings {
evaluate_dest(b, fctx.clone(), &mut new_bindings)?;
}
- let ctx = ctx.extend_bound(new_bindings).into_future(fctx);
+ let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);
evaluate(s, ctx, &returned.clone())?
}
Arr(items) => {
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -139,11 +139,12 @@
) -> Result<Val> {
match self {
Self::Id => {
+ #[allow(clippy::unnecessary_wraps)]
#[builtin]
- fn builtin_id(v: Any) -> Result<Any> {
+ const fn builtin_id(v: Any) -> Result<Any> {
Ok(v)
}
- static ID: &'static builtin_id = &builtin_id {};
+ static ID: &builtin_id = &builtin_id {};
ID.call(s, call_ctx, loc, args)
}
@@ -159,10 +160,10 @@
self.evaluate(s, Context::default(), CallLocation::native(), args, true)
}
- pub fn is_identity(&self) -> bool {
+ pub const fn is_identity(&self) -> bool {
matches!(self, Self::Id)
}
- pub fn identity() -> Self {
+ pub const fn identity() -> Self {
Self::Id
}
}
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -111,7 +111,7 @@
Ok(body_ctx
.extend(passed_args, None, None, None)
- .extend_bound(defaults)
+ .extend(defaults, None, None, None)
.into_future(fctx))
} else {
let body_ctx = body_ctx.extend(passed_args, None, None, None);
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -10,7 +10,7 @@
use rustc_hash::{FxHashMap, FxHashSet};
/// Replacement for box, which assumes that the underlying type is [`Trace`]
-/// Used in places, where Cc<dyn Trait> should be used instead, but it can't, because CoerceUnsiced is not stable
+/// Used in places, where `Cc<dyn Trait>` should be used instead, but it can't, because `CoerceUnsiced` is not stable
#[derive(Debug, Clone)]
pub struct TraceBox<T: ?Sized>(pub Box<T>);
#[macro_export]
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -61,17 +61,13 @@
pub use val::{ManifestFormat, Thunk, Val};
pub trait Bindable: Trace + 'static {
- fn bind(
- &self,
- s: State,
- this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
- ) -> Result<Thunk<Val>>;
+ type Bound;
+ fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Self::Bound>;
}
#[derive(Clone, Trace)]
pub enum LazyBinding {
- Bindable(Cc<TraceBox<dyn Bindable>>),
+ Bindable(Cc<TraceBox<dyn Bindable<Bound = Thunk<Val>>>>),
Bound(Thunk<Val>),
}
@@ -84,11 +80,11 @@
pub fn evaluate(
&self,
s: State,
+ sup: Option<ObjValue>,
this: Option<ObjValue>,
- super_obj: Option<ObjValue>,
) -> Result<Thunk<Val>> {
match self {
- Self::Bindable(v) => v.bind(s, this, super_obj),
+ Self::Bindable(v) => v.bind(s, sup, this),
Self::Bound(v) => Ok(v.clone()),
}
}
@@ -345,7 +341,7 @@
for (name, value) in globals.iter() {
new_bindings.insert(name.clone(), Thunk::evaluated(value.clone()));
}
- Context::new().extend_bound(new_bindings)
+ Context::new().extend(new_bindings, None, None, None)
}
/// Executes code creating a new stack frame
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -106,7 +106,7 @@
}
pub trait ObjectAssertion: Trace {
- fn run(&self, s: State, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<()>;
+ fn run(&self, s: State, super_obj: Option<ObjValue>, this: Option<ObjValue>) -> Result<()>;
}
// Field => This
@@ -124,10 +124,11 @@
#[derive(Trace)]
#[force_tracking]
pub struct ObjValueInternals {
- super_obj: Option<ObjValue>,
+ sup: Option<ObjValue>,
+ this: Option<ObjValue>,
+
assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
assertions_ran: RefCell<GcHashSet<ObjValue>>,
- this_obj: Option<ObjValue>,
this_entries: Cc<GcHashMap<IStr, ObjMember>>,
value_cache: RefCell<GcHashMap<CacheKey, CacheValue>>,
}
@@ -153,7 +154,7 @@
pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);
impl Debug for ObjValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- if let Some(super_obj) = self.0.super_obj.as_ref() {
+ if let Some(super_obj) = self.0.sup.as_ref() {
if f.alternate() {
write!(f, "{:#?}", super_obj)?;
} else {
@@ -171,15 +172,15 @@
impl ObjValue {
pub fn new(
- super_obj: Option<Self>,
+ sup: Option<Self>,
this_entries: Cc<GcHashMap<IStr, ObjMember>>,
assertions: Cc<Vec<TraceBox<dyn ObjectAssertion>>>,
) -> Self {
Self(Cc::new(ObjValueInternals {
- super_obj,
+ sup,
+ this: None,
assertions,
assertions_ran: RefCell::new(GcHashSet::new()),
- this_obj: None,
this_entries,
value_cache: RefCell::new(GcHashMap::new()),
}))
@@ -188,15 +189,15 @@
Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))
}
#[must_use]
- pub fn extend_from(&self, super_obj: Self) -> Self {
- match &self.0.super_obj {
+ pub fn extend_from(&self, sup: Self) -> Self {
+ match &self.0.sup {
None => Self::new(
- Some(super_obj),
+ Some(sup),
self.0.this_entries.clone(),
self.0.assertions.clone(),
),
Some(v) => Self::new(
- Some(v.extend_from(super_obj)),
+ Some(v.extend_from(sup)),
self.0.this_entries.clone(),
self.0.assertions.clone(),
),
@@ -212,12 +213,12 @@
}
#[must_use]
- pub fn with_this(&self, this_obj: Self) -> Self {
+ pub fn with_this(&self, this: Self) -> Self {
Self(Cc::new(ObjValueInternals {
- super_obj: self.0.super_obj.clone(),
+ sup: self.0.sup.clone(),
assertions: self.0.assertions.clone(),
assertions_ran: RefCell::new(GcHashSet::new()),
- this_obj: Some(this_obj),
+ this: Some(this),
this_entries: self.0.this_entries.clone(),
value_cache: RefCell::new(GcHashMap::new()),
}))
@@ -234,7 +235,7 @@
if !self.0.this_entries.is_empty() {
return false;
}
- self.0.super_obj.as_ref().map_or(true, Self::is_empty)
+ self.0.sup.as_ref().map_or(true, Self::is_empty)
}
/// Run callback for every field found in object
@@ -243,7 +244,7 @@
depth: SuperDepth,
handler: &mut impl FnMut(SuperDepth, &IStr, &ObjMember) -> bool,
) -> bool {
- if let Some(s) = &self.0.super_obj {
+ if let Some(s) = &self.0.sup {
if s.enum_fields(depth.deeper(), handler) {
return true;
}
@@ -332,13 +333,13 @@
Some(match &m.visibility {
Visibility::Normal => self
.0
- .super_obj
+ .sup
.as_ref()
.and_then(|super_obj| super_obj.field_visibility(name))
.unwrap_or(Visibility::Normal),
v => *v,
})
- } else if let Some(super_obj) = &self.0.super_obj {
+ } else if let Some(super_obj) = &self.0.sup {
super_obj.field_visibility(name)
} else {
None
@@ -348,7 +349,7 @@
fn has_field_include_hidden(&self, name: IStr) -> bool {
if self.0.this_entries.contains_key(&name) {
true
- } else if let Some(super_obj) = &self.0.super_obj {
+ } else if let Some(super_obj) = &self.0.sup {
super_obj.has_field_include_hidden(name)
} else {
false
@@ -369,13 +370,12 @@
pub fn get(&self, s: State, key: IStr) -> Result<Option<Val>> {
self.run_assertions(s.clone())?;
- self.get_raw(s, key, self.0.this_obj.as_ref())
+ self.get_raw(s, key, self.0.this.clone().unwrap_or_else(|| self.clone()))
}
// pub fn extend_with(self, key: )
- fn get_raw(&self, s: State, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
- let real_this = real_this.unwrap_or(self);
+ fn get_raw(&self, s: State, key: IStr, real_this: Self) -> Result<Option<Val>> {
let cache_key = (key.clone(), WeakObjValue(real_this.0.downgrade()));
if let Some(v) = self.0.value_cache.borrow().get(&cache_key) {
@@ -397,17 +397,17 @@
.insert(cache_key.clone(), CacheValue::Errored(e.clone()));
e
};
- let value = match (self.0.this_entries.get(&key), &self.0.super_obj) {
+ let value = match (self.0.this_entries.get(&key), &self.0.sup) {
(Some(k), None) => Ok(Some(
self.evaluate_this(s, k, real_this).map_err(fill_error)?,
)),
(Some(k), Some(super_obj)) => {
let our = self
- .evaluate_this(s.clone(), k, real_this)
+ .evaluate_this(s.clone(), k, real_this.clone())
.map_err(fill_error)?;
if k.add {
super_obj
- .get_raw(s.clone(), key, Some(real_this))
+ .get_raw(s.clone(), key, real_this)
.map_err(fill_error)?
.map_or(Ok(Some(our.clone())), |v| {
Ok(Some(evaluate_add_op(s.clone(), &v, &our)?))
@@ -416,7 +416,7 @@
Ok(Some(our))
}
}
- (None, Some(super_obj)) => super_obj.get_raw(s, key, Some(real_this)),
+ (None, Some(super_obj)) => super_obj.get_raw(s, key, real_this),
(None, None) => Ok(None),
}
.map_err(fill_error)?;
@@ -429,9 +429,9 @@
);
Ok(value)
}
- fn evaluate_this(&self, s: State, v: &ObjMember, real_this: &Self) -> Result<Val> {
+ fn evaluate_this(&self, s: State, v: &ObjMember, real_this: Self) -> Result<Val> {
v.invoke
- .evaluate(s.clone(), Some(real_this.clone()), self.0.super_obj.clone())?
+ .evaluate(s.clone(), self.0.sup.clone(), Some(real_this))?
.evaluate(s)
}
@@ -439,13 +439,13 @@
if self.0.assertions_ran.borrow_mut().insert(real_this.clone()) {
for assertion in self.0.assertions.iter() {
if let Err(e) =
- assertion.run(s.clone(), Some(real_this.clone()), self.0.super_obj.clone())
+ assertion.run(s.clone(), self.0.sup.clone(), Some(real_this.clone()))
{
self.0.assertions_ran.borrow_mut().remove(real_this);
return Err(e);
}
}
- if let Some(super_obj) = &self.0.super_obj {
+ if let Some(super_obj) = &self.0.sup {
super_obj.run_assertions_raw(s, real_this)?;
}
}
@@ -458,6 +458,9 @@
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
cc_ptr_eq(&a.0, &b.0)
}
+ pub fn downgrade(self) -> WeakObjValue {
+ WeakObjValue(self.0.downgrade())
+ }
}
impl PartialEq for ObjValue {
@@ -475,7 +478,7 @@
#[allow(clippy::module_name_repetitions)]
pub struct ObjValueBuilder {
- super_obj: Option<ObjValue>,
+ sup: Option<ObjValue>,
map: GcHashMap<IStr, ObjMember>,
assertions: Vec<TraceBox<dyn ObjectAssertion>>,
next_field_index: FieldIndex,
@@ -486,7 +489,7 @@
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
- super_obj: None,
+ sup: None,
map: GcHashMap::with_capacity(capacity),
assertions: Vec::new(),
next_field_index: FieldIndex::default(),
@@ -497,7 +500,7 @@
self
}
pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {
- self.super_obj = Some(super_obj);
+ self.sup = Some(super_obj);
self
}
@@ -512,7 +515,7 @@
}
pub fn build(self) -> ObjValue {
- ObjValue::new(self.super_obj, Cc::new(self.map), Cc::new(self.assertions))
+ ObjValue::new(self.sup, Cc::new(self.map), Cc::new(self.assertions))
}
}
impl Default for ObjValueBuilder {
@@ -583,7 +586,11 @@
pub fn value(self, s: State, value: Val) -> Result<()> {
self.binding(s, LazyBinding::Bound(Thunk::evaluated(value)))
}
- pub fn bindable(self, s: State, bindable: TraceBox<dyn Bindable>) -> Result<()> {
+ pub fn bindable(
+ self,
+ s: State,
+ bindable: TraceBox<dyn Bindable<Bound = Thunk<Val>>>,
+ ) -> Result<()> {
self.binding(s, LazyBinding::Bindable(Cc::new(bindable)))
}
pub fn binding(self, s: State, binding: LazyBinding) -> Result<()> {
@@ -606,7 +613,7 @@
pub fn value(self, value: Val) {
self.binding(LazyBinding::Bound(Thunk::evaluated(value)));
}
- pub fn bindable(self, bindable: TraceBox<dyn Bindable>) {
+ pub fn bindable(self, bindable: TraceBox<dyn Bindable<Bound = Thunk<Val>>>) {
self.binding(LazyBinding::Bindable(Cc::new(bindable)));
}
pub fn binding(self, binding: LazyBinding) {
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth1// All builtins should return results2#![allow(clippy::unnecessary_wraps)]34use std::collections::HashMap;56use format::{format_arr, format_obj};7use gcmodule::Cc;8use jrsonnet_interner::IStr;9use serde::Deserialize;10use serde_yaml::DeserializingQuirks;1112use crate::{13 error::{Error::*, Result},14 function::{builtin::StaticBuiltin, CallLocation, FuncVal},15 operator::evaluate_mod_op,16 stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},17 throw,18 typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, Typed, VecVal, M1},19 val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},20 Either, ObjValue, State, Val,21};2223pub mod expr;24pub use expr::*;2526use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2728pub mod format;29pub mod manifest;30pub mod sort;3132pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {33 s.push(34 CallLocation::native(),35 || format!("std.format of {}", str),36 || {37 Ok(match vals {38 Val::Arr(vals) => format_arr(s.clone(), &str, &vals.evaluated(s.clone())?)?,39 Val::Obj(obj) => format_obj(s.clone(), &str, &obj)?,40 o => format_arr(s.clone(), &str, &[o])?,41 })42 },43 )44}4546pub fn std_slice(47 indexable: IndexableVal,48 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,49 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,50 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,51) -> Result<Val> {52 match &indexable {53 IndexableVal::Str(s) => {54 let index = index.as_deref().copied().unwrap_or(0);55 let end = end.as_deref().copied().unwrap_or(usize::MAX);56 let step = step.as_deref().copied().unwrap_or(1);5758 if index >= end {59 return Ok(Val::Str("".into()));60 }6162 Ok(Val::Str(63 (s.chars()64 .skip(index)65 .take(end - index)66 .step_by(step)67 .collect::<String>())68 .into(),69 ))70 }71 IndexableVal::Arr(arr) => {72 let index = index.as_deref().copied().unwrap_or(0);73 let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());74 let step = step.as_deref().copied().unwrap_or(1);7576 if index >= end {77 return Ok(Val::Arr(ArrValue::new_eager()));78 }7980 Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {81 inner: arr.clone(),82 from: index as u32,83 to: end as u32,84 step: step as u32,85 }))))86 }87 }88}8990type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;9192thread_local! {93 pub static BUILTINS: BuiltinsType = {94 [95 ("length".into(), builtin_length::INST),96 ("type".into(), builtin_type::INST),97 ("makeArray".into(), builtin_make_array::INST),98 ("codepoint".into(), builtin_codepoint::INST),99 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),100 ("objectHasEx".into(), builtin_object_has_ex::INST),101 ("slice".into(), builtin_slice::INST),102 ("substr".into(), builtin_substr::INST),103 ("primitiveEquals".into(), builtin_primitive_equals::INST),104 ("equals".into(), builtin_equals::INST),105 ("modulo".into(), builtin_modulo::INST),106 ("mod".into(), builtin_mod::INST),107 ("floor".into(), builtin_floor::INST),108 ("ceil".into(), builtin_ceil::INST),109 ("log".into(), builtin_log::INST),110 ("pow".into(), builtin_pow::INST),111 ("sqrt".into(), builtin_sqrt::INST),112 ("sin".into(), builtin_sin::INST),113 ("cos".into(), builtin_cos::INST),114 ("tan".into(), builtin_tan::INST),115 ("asin".into(), builtin_asin::INST),116 ("acos".into(), builtin_acos::INST),117 ("atan".into(), builtin_atan::INST),118 ("exp".into(), builtin_exp::INST),119 ("mantissa".into(), builtin_mantissa::INST),120 ("exponent".into(), builtin_exponent::INST),121 ("extVar".into(), builtin_ext_var::INST),122 ("native".into(), builtin_native::INST),123 ("filter".into(), builtin_filter::INST),124 ("map".into(), builtin_map::INST),125 ("flatMap".into(), builtin_flatmap::INST),126 ("foldl".into(), builtin_foldl::INST),127 ("foldr".into(), builtin_foldr::INST),128 ("sort".into(), builtin_sort::INST),129 ("format".into(), builtin_format::INST),130 ("range".into(), builtin_range::INST),131 ("char".into(), builtin_char::INST),132 ("encodeUTF8".into(), builtin_encode_utf8::INST),133 ("decodeUTF8".into(), builtin_decode_utf8::INST),134 ("md5".into(), builtin_md5::INST),135 ("base64".into(), builtin_base64::INST),136 ("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),137 ("base64Decode".into(), builtin_base64_decode::INST),138 ("trace".into(), builtin_trace::INST),139 ("join".into(), builtin_join::INST),140 ("escapeStringJson".into(), builtin_escape_string_json::INST),141 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),142 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),143 ("reverse".into(), builtin_reverse::INST),144 ("strReplace".into(), builtin_str_replace::INST),145 ("splitLimit".into(), builtin_splitlimit::INST),146 ("parseJson".into(), builtin_parse_json::INST),147 ("parseYaml".into(), builtin_parse_yaml::INST),148 ("asciiUpper".into(), builtin_ascii_upper::INST),149 ("asciiLower".into(), builtin_ascii_lower::INST),150 ("member".into(), builtin_member::INST),151 ("count".into(), builtin_count::INST),152 ("any".into(), builtin_any::INST),153 ("all".into(), builtin_all::INST),154 ].iter().cloned().collect()155 };156}157158#[jrsonnet_macros::builtin]159fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {160 use Either4::*;161 Ok(match x {162 A(x) => x.chars().count(),163 B(x) => x.len(),164 C(x) => x.len(),165 D(f) => f.params_len(),166 })167}168169#[jrsonnet_macros::builtin]170fn builtin_type(x: Any) -> Result<IStr> {171 Ok(x.0.value_type().name().into())172}173174#[jrsonnet_macros::builtin]175fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {176 let mut out = Vec::with_capacity(sz);177 for i in 0..sz {178 out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);179 }180 Ok(VecVal(Cc::new(out)))181}182183#[jrsonnet_macros::builtin]184const fn builtin_codepoint(str: char) -> Result<u32> {185 Ok(str as u32)186}187188#[jrsonnet_macros::builtin]189fn builtin_object_fields_ex(190 obj: ObjValue,191 inc_hidden: bool,192 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,193) -> Result<VecVal> {194 #[cfg(feature = "exp-preserve-order")]195 let preserve_order = preserve_order.unwrap_or(false);196 let out = obj.fields_ex(197 inc_hidden,198 #[cfg(feature = "exp-preserve-order")]199 preserve_order,200 );201 Ok(VecVal(Cc::new(202 out.into_iter().map(Val::Str).collect::<Vec<_>>(),203 )))204}205206#[jrsonnet_macros::builtin]207fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {208 Ok(obj.has_field_ex(f, inc_hidden))209}210211#[jrsonnet_macros::builtin]212fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {213 use serde_json::Value;214 let value: Value = serde_json::from_str(&s)215 .map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;216 Ok(Any(Value::into_untyped(value, st)?))217}218219#[jrsonnet_macros::builtin]220fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {221 use serde_json::Value;222 let value = serde_yaml::Deserializer::from_str_with_quirks(223 &s,224 DeserializingQuirks { old_octals: true },225 );226 let mut out = vec![];227 for item in value {228 let value = Value::deserialize(item)229 .map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;230 let val = Value::into_untyped(value, st.clone())?;231 out.push(val);232 }233 Ok(Any(if out.is_empty() {234 Val::Null235 } else if out.len() == 1 {236 out.into_iter().next().unwrap()237 } else {238 Val::Arr(out.into())239 }))240}241242#[jrsonnet_macros::builtin]243fn builtin_slice(244 indexable: IndexableVal,245 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,246 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,247 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,248) -> Result<Any> {249 std_slice(indexable, index, end, step).map(Any)250}251252#[jrsonnet_macros::builtin]253fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {254 Ok(str.chars().skip(from as usize).take(len as usize).collect())255}256257#[jrsonnet_macros::builtin]258fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {259 primitive_equals(&a.0, &b.0)260}261262#[jrsonnet_macros::builtin]263fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {264 equals(s, &a.0, &b.0)265}266267#[jrsonnet_macros::builtin]268fn builtin_modulo(a: f64, b: f64) -> Result<f64> {269 Ok(a % b)270}271272#[jrsonnet_macros::builtin]273fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {274 use Either2::*;275 Ok(Any(evaluate_mod_op(276 s,277 &match a {278 A(v) => Val::Num(v),279 B(s) => Val::Str(s),280 },281 &b.0,282 )?))283}284285#[jrsonnet_macros::builtin]286fn builtin_floor(x: f64) -> Result<f64> {287 Ok(x.floor())288}289290#[jrsonnet_macros::builtin]291fn builtin_ceil(x: f64) -> Result<f64> {292 Ok(x.ceil())293}294295#[jrsonnet_macros::builtin]296fn builtin_log(n: f64) -> Result<f64> {297 Ok(n.ln())298}299300#[jrsonnet_macros::builtin]301fn builtin_pow(x: f64, n: f64) -> Result<f64> {302 Ok(x.powf(n))303}304305#[jrsonnet_macros::builtin]306fn builtin_sqrt(x: PositiveF64) -> Result<f64> {307 Ok(x.0.sqrt())308}309310#[jrsonnet_macros::builtin]311fn builtin_sin(x: f64) -> Result<f64> {312 Ok(x.sin())313}314315#[jrsonnet_macros::builtin]316fn builtin_cos(x: f64) -> Result<f64> {317 Ok(x.cos())318}319320#[jrsonnet_macros::builtin]321fn builtin_tan(x: f64) -> Result<f64> {322 Ok(x.tan())323}324325#[jrsonnet_macros::builtin]326fn builtin_asin(x: f64) -> Result<f64> {327 Ok(x.asin())328}329330#[jrsonnet_macros::builtin]331fn builtin_acos(x: f64) -> Result<f64> {332 Ok(x.acos())333}334335#[jrsonnet_macros::builtin]336fn builtin_atan(x: f64) -> Result<f64> {337 Ok(x.atan())338}339340#[jrsonnet_macros::builtin]341fn builtin_exp(x: f64) -> Result<f64> {342 Ok(x.exp())343}344345fn frexp(s: f64) -> (f64, i16) {346 if 0.0 == s {347 (s, 0)348 } else {349 let lg = s.abs().log2();350 let x = (lg - lg.floor() - 1.0).exp2();351 let exp = lg.floor() + 1.0;352 (s.signum() * x, exp as i16)353 }354}355356#[jrsonnet_macros::builtin]357fn builtin_mantissa(x: f64) -> Result<f64> {358 Ok(frexp(x).0)359}360361#[jrsonnet_macros::builtin]362fn builtin_exponent(x: f64) -> Result<i16> {363 Ok(frexp(x).1)364}365366#[jrsonnet_macros::builtin]367fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {368 Ok(Any(s369 .settings()370 .ext_vars371 .get(&x)372 .cloned()373 .ok_or(UndefinedExternalVariable(x))?))374}375376#[jrsonnet_macros::builtin]377fn builtin_native(s: State, name: IStr) -> Result<Any> {378 Ok(Any(s379 .settings()380 .ext_natives381 .get(&name)382 .cloned()383 .map_or(Val::Null, |v| {384 Val::Func(FuncVal::Builtin(v.clone()))385 })))386}387388#[jrsonnet_macros::builtin]389fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {390 arr.filter(s.clone(), |val| {391 bool::from_untyped(392 func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,393 s.clone(),394 )395 })396}397398#[jrsonnet_macros::builtin]399fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {400 arr.map(s.clone(), |val| {401 func.evaluate_simple(s.clone(), &(Any(val),))402 })403}404405#[jrsonnet_macros::builtin]406fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {407 match arr {408 IndexableVal::Str(str) => {409 let mut out = String::new();410 for c in str.chars() {411 match func.evaluate_simple(s.clone(), &(c.to_string(),))? {412 Val::Str(o) => out.push_str(&o),413 Val::Null => continue,414 _ => throw!(RuntimeError(415 "in std.join all items should be strings".into()416 )),417 };418 }419 Ok(IndexableVal::Str(out.into()))420 }421 IndexableVal::Arr(a) => {422 let mut out = Vec::new();423 for el in a.iter(s.clone()) {424 let el = el?;425 match func.evaluate_simple(s.clone(), &(Any(el),))? {426 Val::Arr(o) => {427 for oe in o.iter(s.clone()) {428 out.push(oe?);429 }430 }431 Val::Null => continue,432 _ => throw!(RuntimeError(433 "in std.join all items should be arrays".into()434 )),435 };436 }437 Ok(IndexableVal::Arr(out.into()))438 }439 }440}441442#[jrsonnet_macros::builtin]443fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {444 let mut acc = init.0;445 for i in arr.iter(s.clone()) {446 acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;447 }448 Ok(Any(acc))449}450451#[jrsonnet_macros::builtin]452fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {453 let mut acc = init.0;454 for i in arr.iter(s.clone()).rev() {455 acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;456 }457 Ok(Any(acc))458}459460#[jrsonnet_macros::builtin]461#[allow(non_snake_case)]462fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {463 if arr.len() <= 1 {464 return Ok(arr);465 }466 Ok(ArrValue::Eager(sort::sort(467 s.clone(),468 arr.evaluated(s)?,469 keyF.unwrap_or(FuncVal::identity()),470 )?))471}472473#[jrsonnet_macros::builtin]474fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {475 std_format(s, str, vals.0)476}477478#[jrsonnet_macros::builtin]479fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {480 if to < from {481 return Ok(ArrValue::new_eager());482 }483 Ok(ArrValue::new_range(from, to))484}485486#[jrsonnet_macros::builtin]487fn builtin_char(n: u32) -> Result<char> {488 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)489}490491#[jrsonnet_macros::builtin]492fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {493 Ok(Bytes(str.bytes().collect::<Vec<u8>>().into()))494}495496#[jrsonnet_macros::builtin]497fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {498 Ok(std::str::from_utf8(&arr.0)499 .map_err(|_| RuntimeError("bad utf8".into()))?500 .into())501}502503#[jrsonnet_macros::builtin]504fn builtin_md5(str: IStr) -> Result<String> {505 Ok(format!("{:x}", md5::compute(&str.as_bytes())))506}507508#[jrsonnet_macros::builtin]509fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {510 eprint!("TRACE:");511 if let Some(loc) = loc.0 {512 let locs = s.map_source_locations(&loc.0, &[loc.1]);513 eprint!(514 " {}:{}",515 loc.0.file_name().unwrap().to_str().unwrap(),516 locs[0].line517 );518 }519 eprintln!(" {}", str);520 Ok(rest) as Result<Any>521}522523#[jrsonnet_macros::builtin]524fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {525 use Either2::*;526 Ok(match input {527 A(a) => base64::encode(a.0),528 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),529 })530}531532#[jrsonnet_macros::builtin]533fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {534 Ok(Bytes(535 base64::decode(&input.as_bytes())536 .map_err(|_| RuntimeError("bad base64".into()))?537 .into(),538 ))539}540541#[jrsonnet_macros::builtin]542fn builtin_base64_decode(input: IStr) -> Result<String> {543 let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;544 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)545}546547#[jrsonnet_macros::builtin]548fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {549 Ok(match sep {550 IndexableVal::Arr(joiner_items) => {551 let mut out = Vec::new();552553 let mut first = true;554 for item in arr.iter(s.clone()) {555 let item = item?.clone();556 if let Val::Arr(items) = item {557 if !first {558 out.reserve(joiner_items.len());559 // TODO: extend560 for item in joiner_items.iter(s.clone()) {561 out.push(item?);562 }563 }564 first = false;565 out.reserve(items.len());566 for item in items.iter(s.clone()) {567 out.push(item?);568 }569 } else if matches!(item, Val::Null) {570 continue;571 } else {572 throw!(RuntimeError(573 "in std.join all items should be arrays".into()574 ));575 }576 }577578 IndexableVal::Arr(out.into())579 }580 IndexableVal::Str(sep) => {581 let mut out = String::new();582583 let mut first = true;584 for item in arr.iter(s) {585 let item = item?.clone();586 if let Val::Str(item) = item {587 if !first {588 out += &sep;589 }590 first = false;591 out += &item;592 } else if matches!(item, Val::Null) {593 continue;594 } else {595 throw!(RuntimeError(596 "in std.join all items should be strings".into()597 ));598 }599 }600601 IndexableVal::Str(out.into())602 }603 })604}605606#[jrsonnet_macros::builtin]607fn builtin_escape_string_json(str_: IStr) -> Result<String> {608 Ok(escape_string_json(&str_))609}610611#[jrsonnet_macros::builtin]612fn builtin_manifest_json_ex(613 s: State,614 value: Any,615 indent: IStr,616 newline: Option<IStr>,617 key_val_sep: Option<IStr>,618 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,619) -> Result<String> {620 let newline = newline.as_deref().unwrap_or("\n");621 let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");622 manifest_json_ex(623 s,624 &value.0,625 &ManifestJsonOptions {626 padding: &indent,627 mtype: ManifestType::Std,628 newline,629 key_val_sep,630 #[cfg(feature = "exp-preserve-order")]631 preserve_order: preserve_order.unwrap_or(false),632 },633 )634}635636#[jrsonnet_macros::builtin]637fn builtin_manifest_yaml_doc(638 s: State,639 value: Any,640 indent_array_in_object: Option<bool>,641 quote_keys: Option<bool>,642 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,643) -> Result<String> {644 manifest_yaml_ex(645 s,646 &value.0,647 &ManifestYamlOptions {648 padding: " ",649 arr_element_padding: if indent_array_in_object.unwrap_or(false) {650 " "651 } else {652 ""653 },654 quote_keys: quote_keys.unwrap_or(true),655 #[cfg(feature = "exp-preserve-order")]656 preserve_order: preserve_order.unwrap_or(false),657 },658 )659}660661#[jrsonnet_macros::builtin]662fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {663 Ok(value.reversed())664}665666#[jrsonnet_macros::builtin]667fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {668 Ok(str.replace(&from as &str, &to as &str))669}670671#[jrsonnet_macros::builtin]672fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {673 use Either2::*;674 Ok(VecVal(Cc::new(match maxsplits {675 A(n) => str676 .splitn(n + 1, &c as &str)677 .map(|s| Val::Str(s.into()))678 .collect(),679 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),680 })))681}682683#[jrsonnet_macros::builtin]684fn builtin_ascii_upper(str: IStr) -> Result<String> {685 Ok(str.to_ascii_uppercase())686}687688#[jrsonnet_macros::builtin]689fn builtin_ascii_lower(str: IStr) -> Result<String> {690 Ok(str.to_ascii_lowercase())691}692693#[jrsonnet_macros::builtin]694fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {695 match arr {696 IndexableVal::Str(str) => {697 let x: IStr = IStr::from_untyped(x.0, s)?;698 Ok(!x.is_empty() && str.contains(&*x))699 }700 IndexableVal::Arr(a) => {701 for item in a.iter(s.clone()) {702 let item = item?;703 if equals(s.clone(), &item, &x.0)? {704 return Ok(true);705 }706 }707 Ok(false)708 }709 }710}711712#[jrsonnet_macros::builtin]713fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {714 let mut count = 0;715 for item in &arr {716 if equals(s.clone(), &item.0, &v.0)? {717 count += 1;718 }719 }720 Ok(count)721}722723#[jrsonnet_macros::builtin]724fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {725 for v in arr.iter(s.clone()) {726 let v = bool::from_untyped(v?, s.clone())?;727 if v {728 return Ok(true);729 }730 }731 Ok(false)732}733734#[jrsonnet_macros::builtin]735fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {736 for v in arr.iter(s.clone()) {737 let v = bool::from_untyped(v?, s.clone())?;738 if !v {739 return Ok(false);740 }741 }742 Ok(true)743}1// All builtins should return results2#![allow(clippy::unnecessary_wraps)]34use std::collections::HashMap;56use format::{format_arr, format_obj};7use gcmodule::Cc;8use jrsonnet_interner::IStr;9use serde::Deserialize;10use serde_yaml::DeserializingQuirks;1112use crate::{13 error::{Error::*, Result},14 function::{builtin::StaticBuiltin, CallLocation, FuncVal},15 operator::evaluate_mod_op,16 stdlib::manifest::{manifest_yaml_ex, ManifestYamlOptions},17 throw,18 typed::{Any, BoundedUsize, Bytes, Either2, Either4, PositiveF64, Typed, VecVal, M1},19 val::{equals, primitive_equals, ArrValue, IndexableVal, Slice},20 Either, ObjValue, State, Val,21};2223pub mod expr;24pub use expr::*;2526use self::manifest::{escape_string_json, manifest_json_ex, ManifestJsonOptions, ManifestType};2728pub mod format;29pub mod manifest;30pub mod sort;3132pub fn std_format(s: State, str: IStr, vals: Val) -> Result<String> {33 s.push(34 CallLocation::native(),35 || format!("std.format of {}", str),36 || {37 Ok(match vals {38 Val::Arr(vals) => format_arr(s.clone(), &str, &vals.evaluated(s.clone())?)?,39 Val::Obj(obj) => format_obj(s.clone(), &str, &obj)?,40 o => format_arr(s.clone(), &str, &[o])?,41 })42 },43 )44}4546pub fn std_slice(47 indexable: IndexableVal,48 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,49 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,50 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,51) -> Result<Val> {52 match &indexable {53 IndexableVal::Str(s) => {54 let index = index.as_deref().copied().unwrap_or(0);55 let end = end.as_deref().copied().unwrap_or(usize::MAX);56 let step = step.as_deref().copied().unwrap_or(1);5758 if index >= end {59 return Ok(Val::Str("".into()));60 }6162 Ok(Val::Str(63 (s.chars()64 .skip(index)65 .take(end - index)66 .step_by(step)67 .collect::<String>())68 .into(),69 ))70 }71 IndexableVal::Arr(arr) => {72 let index = index.as_deref().copied().unwrap_or(0);73 let end = end.as_deref().copied().unwrap_or(usize::MAX).min(arr.len());74 let step = step.as_deref().copied().unwrap_or(1);7576 if index >= end {77 return Ok(Val::Arr(ArrValue::new_eager()));78 }7980 Ok(Val::Arr(ArrValue::Slice(Box::new(Slice {81 inner: arr.clone(),82 from: index as u32,83 to: end as u32,84 step: step as u32,85 }))))86 }87 }88}8990type BuiltinsType = HashMap<IStr, &'static dyn StaticBuiltin>;9192thread_local! {93 pub static BUILTINS: BuiltinsType = {94 [95 ("length".into(), builtin_length::INST),96 ("type".into(), builtin_type::INST),97 ("makeArray".into(), builtin_make_array::INST),98 ("codepoint".into(), builtin_codepoint::INST),99 ("objectFieldsEx".into(), builtin_object_fields_ex::INST),100 ("objectHasEx".into(), builtin_object_has_ex::INST),101 ("slice".into(), builtin_slice::INST),102 ("substr".into(), builtin_substr::INST),103 ("primitiveEquals".into(), builtin_primitive_equals::INST),104 ("equals".into(), builtin_equals::INST),105 ("modulo".into(), builtin_modulo::INST),106 ("mod".into(), builtin_mod::INST),107 ("floor".into(), builtin_floor::INST),108 ("ceil".into(), builtin_ceil::INST),109 ("log".into(), builtin_log::INST),110 ("pow".into(), builtin_pow::INST),111 ("sqrt".into(), builtin_sqrt::INST),112 ("sin".into(), builtin_sin::INST),113 ("cos".into(), builtin_cos::INST),114 ("tan".into(), builtin_tan::INST),115 ("asin".into(), builtin_asin::INST),116 ("acos".into(), builtin_acos::INST),117 ("atan".into(), builtin_atan::INST),118 ("exp".into(), builtin_exp::INST),119 ("mantissa".into(), builtin_mantissa::INST),120 ("exponent".into(), builtin_exponent::INST),121 ("extVar".into(), builtin_ext_var::INST),122 ("native".into(), builtin_native::INST),123 ("filter".into(), builtin_filter::INST),124 ("map".into(), builtin_map::INST),125 ("flatMap".into(), builtin_flatmap::INST),126 ("foldl".into(), builtin_foldl::INST),127 ("foldr".into(), builtin_foldr::INST),128 ("sort".into(), builtin_sort::INST),129 ("format".into(), builtin_format::INST),130 ("range".into(), builtin_range::INST),131 ("char".into(), builtin_char::INST),132 ("encodeUTF8".into(), builtin_encode_utf8::INST),133 ("decodeUTF8".into(), builtin_decode_utf8::INST),134 ("md5".into(), builtin_md5::INST),135 ("base64".into(), builtin_base64::INST),136 ("base64DecodeBytes".into(), builtin_base64_decode_bytes::INST),137 ("base64Decode".into(), builtin_base64_decode::INST),138 ("trace".into(), builtin_trace::INST),139 ("join".into(), builtin_join::INST),140 ("escapeStringJson".into(), builtin_escape_string_json::INST),141 ("manifestJsonEx".into(), builtin_manifest_json_ex::INST),142 ("manifestYamlDoc".into(), builtin_manifest_yaml_doc::INST),143 ("reverse".into(), builtin_reverse::INST),144 ("strReplace".into(), builtin_str_replace::INST),145 ("splitLimit".into(), builtin_splitlimit::INST),146 ("parseJson".into(), builtin_parse_json::INST),147 ("parseYaml".into(), builtin_parse_yaml::INST),148 ("asciiUpper".into(), builtin_ascii_upper::INST),149 ("asciiLower".into(), builtin_ascii_lower::INST),150 ("member".into(), builtin_member::INST),151 ("count".into(), builtin_count::INST),152 ("any".into(), builtin_any::INST),153 ("all".into(), builtin_all::INST),154 ].iter().cloned().collect()155 };156}157158#[jrsonnet_macros::builtin]159fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> Result<usize> {160 use Either4::*;161 Ok(match x {162 A(x) => x.chars().count(),163 B(x) => x.len(),164 C(x) => x.len(),165 D(f) => f.params_len(),166 })167}168169#[jrsonnet_macros::builtin]170fn builtin_type(x: Any) -> Result<IStr> {171 Ok(x.0.value_type().name().into())172}173174#[jrsonnet_macros::builtin]175fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {176 let mut out = Vec::with_capacity(sz);177 for i in 0..sz {178 out.push(func.evaluate_simple(s.clone(), &(i as f64,))?);179 }180 Ok(VecVal(Cc::new(out)))181}182183#[jrsonnet_macros::builtin]184const fn builtin_codepoint(str: char) -> Result<u32> {185 Ok(str as u32)186}187188#[jrsonnet_macros::builtin]189fn builtin_object_fields_ex(190 obj: ObjValue,191 inc_hidden: bool,192 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,193) -> Result<VecVal> {194 #[cfg(feature = "exp-preserve-order")]195 let preserve_order = preserve_order.unwrap_or(false);196 let out = obj.fields_ex(197 inc_hidden,198 #[cfg(feature = "exp-preserve-order")]199 preserve_order,200 );201 Ok(VecVal(Cc::new(202 out.into_iter().map(Val::Str).collect::<Vec<_>>(),203 )))204}205206#[jrsonnet_macros::builtin]207fn builtin_object_has_ex(obj: ObjValue, f: IStr, inc_hidden: bool) -> Result<bool> {208 Ok(obj.has_field_ex(f, inc_hidden))209}210211#[jrsonnet_macros::builtin]212fn builtin_parse_json(st: State, s: IStr) -> Result<Any> {213 use serde_json::Value;214 let value: Value = serde_json::from_str(&s)215 .map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;216 Ok(Any(Value::into_untyped(value, st)?))217}218219#[jrsonnet_macros::builtin]220fn builtin_parse_yaml(st: State, s: IStr) -> Result<Any> {221 use serde_json::Value;222 let value = serde_yaml::Deserializer::from_str_with_quirks(223 &s,224 DeserializingQuirks { old_octals: true },225 );226 let mut out = vec![];227 for item in value {228 let value = Value::deserialize(item)229 .map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;230 let val = Value::into_untyped(value, st.clone())?;231 out.push(val);232 }233 Ok(Any(if out.is_empty() {234 Val::Null235 } else if out.len() == 1 {236 out.into_iter().next().unwrap()237 } else {238 Val::Arr(out.into())239 }))240}241242#[jrsonnet_macros::builtin]243fn builtin_slice(244 indexable: IndexableVal,245 index: Option<BoundedUsize<0, { i32::MAX as usize }>>,246 end: Option<BoundedUsize<0, { i32::MAX as usize }>>,247 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,248) -> Result<Any> {249 std_slice(indexable, index, end, step).map(Any)250}251252#[jrsonnet_macros::builtin]253fn builtin_substr(str: IStr, from: usize, len: usize) -> Result<String> {254 Ok(str.chars().skip(from as usize).take(len as usize).collect())255}256257#[jrsonnet_macros::builtin]258fn builtin_primitive_equals(a: Any, b: Any) -> Result<bool> {259 primitive_equals(&a.0, &b.0)260}261262#[jrsonnet_macros::builtin]263fn builtin_equals(s: State, a: Any, b: Any) -> Result<bool> {264 equals(s, &a.0, &b.0)265}266267#[jrsonnet_macros::builtin]268fn builtin_modulo(a: f64, b: f64) -> Result<f64> {269 Ok(a % b)270}271272#[jrsonnet_macros::builtin]273fn builtin_mod(s: State, a: Either![f64, IStr], b: Any) -> Result<Any> {274 use Either2::*;275 Ok(Any(evaluate_mod_op(276 s,277 &match a {278 A(v) => Val::Num(v),279 B(s) => Val::Str(s),280 },281 &b.0,282 )?))283}284285#[jrsonnet_macros::builtin]286fn builtin_floor(x: f64) -> Result<f64> {287 Ok(x.floor())288}289290#[jrsonnet_macros::builtin]291fn builtin_ceil(x: f64) -> Result<f64> {292 Ok(x.ceil())293}294295#[jrsonnet_macros::builtin]296fn builtin_log(n: f64) -> Result<f64> {297 Ok(n.ln())298}299300#[jrsonnet_macros::builtin]301fn builtin_pow(x: f64, n: f64) -> Result<f64> {302 Ok(x.powf(n))303}304305#[jrsonnet_macros::builtin]306fn builtin_sqrt(x: PositiveF64) -> Result<f64> {307 Ok(x.0.sqrt())308}309310#[jrsonnet_macros::builtin]311fn builtin_sin(x: f64) -> Result<f64> {312 Ok(x.sin())313}314315#[jrsonnet_macros::builtin]316fn builtin_cos(x: f64) -> Result<f64> {317 Ok(x.cos())318}319320#[jrsonnet_macros::builtin]321fn builtin_tan(x: f64) -> Result<f64> {322 Ok(x.tan())323}324325#[jrsonnet_macros::builtin]326fn builtin_asin(x: f64) -> Result<f64> {327 Ok(x.asin())328}329330#[jrsonnet_macros::builtin]331fn builtin_acos(x: f64) -> Result<f64> {332 Ok(x.acos())333}334335#[jrsonnet_macros::builtin]336fn builtin_atan(x: f64) -> Result<f64> {337 Ok(x.atan())338}339340#[jrsonnet_macros::builtin]341fn builtin_exp(x: f64) -> Result<f64> {342 Ok(x.exp())343}344345fn frexp(s: f64) -> (f64, i16) {346 if 0.0 == s {347 (s, 0)348 } else {349 let lg = s.abs().log2();350 let x = (lg - lg.floor() - 1.0).exp2();351 let exp = lg.floor() + 1.0;352 (s.signum() * x, exp as i16)353 }354}355356#[jrsonnet_macros::builtin]357fn builtin_mantissa(x: f64) -> Result<f64> {358 Ok(frexp(x).0)359}360361#[jrsonnet_macros::builtin]362fn builtin_exponent(x: f64) -> Result<i16> {363 Ok(frexp(x).1)364}365366#[jrsonnet_macros::builtin]367fn builtin_ext_var(s: State, x: IStr) -> Result<Any> {368 Ok(Any(s369 .settings()370 .ext_vars371 .get(&x)372 .cloned()373 .ok_or(UndefinedExternalVariable(x))?))374}375376#[jrsonnet_macros::builtin]377fn builtin_native(s: State, name: IStr) -> Result<Any> {378 Ok(Any(s379 .settings()380 .ext_natives381 .get(&name)382 .cloned()383 .map_or(Val::Null, |v| {384 Val::Func(FuncVal::Builtin(v.clone()))385 })))386}387388#[jrsonnet_macros::builtin]389fn builtin_filter(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {390 arr.filter(s.clone(), |val| {391 bool::from_untyped(392 func.evaluate_simple(s.clone(), &(Any(val.clone()),))?,393 s.clone(),394 )395 })396}397398#[jrsonnet_macros::builtin]399fn builtin_map(s: State, func: FuncVal, arr: ArrValue) -> Result<ArrValue> {400 arr.map(s.clone(), |val| {401 func.evaluate_simple(s.clone(), &(Any(val),))402 })403}404405#[jrsonnet_macros::builtin]406fn builtin_flatmap(s: State, func: FuncVal, arr: IndexableVal) -> Result<IndexableVal> {407 match arr {408 IndexableVal::Str(str) => {409 let mut out = String::new();410 for c in str.chars() {411 match func.evaluate_simple(s.clone(), &(c.to_string(),))? {412 Val::Str(o) => out.push_str(&o),413 Val::Null => continue,414 _ => throw!(RuntimeError(415 "in std.join all items should be strings".into()416 )),417 };418 }419 Ok(IndexableVal::Str(out.into()))420 }421 IndexableVal::Arr(a) => {422 let mut out = Vec::new();423 for el in a.iter(s.clone()) {424 let el = el?;425 match func.evaluate_simple(s.clone(), &(Any(el),))? {426 Val::Arr(o) => {427 for oe in o.iter(s.clone()) {428 out.push(oe?);429 }430 }431 Val::Null => continue,432 _ => throw!(RuntimeError(433 "in std.join all items should be arrays".into()434 )),435 };436 }437 Ok(IndexableVal::Arr(out.into()))438 }439 }440}441442#[jrsonnet_macros::builtin]443fn builtin_foldl(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {444 let mut acc = init.0;445 for i in arr.iter(s.clone()) {446 acc = func.evaluate_simple(s.clone(), &(Any(acc), Any(i?)))?;447 }448 Ok(Any(acc))449}450451#[jrsonnet_macros::builtin]452fn builtin_foldr(s: State, func: FuncVal, arr: ArrValue, init: Any) -> Result<Any> {453 let mut acc = init.0;454 for i in arr.iter(s.clone()).rev() {455 acc = func.evaluate_simple(s.clone(), &(Any(i?), Any(acc)))?;456 }457 Ok(Any(acc))458}459460#[jrsonnet_macros::builtin]461#[allow(non_snake_case)]462fn builtin_sort(s: State, arr: ArrValue, keyF: Option<FuncVal>) -> Result<ArrValue> {463 if arr.len() <= 1 {464 return Ok(arr);465 }466 Ok(ArrValue::Eager(sort::sort(467 s.clone(),468 arr.evaluated(s)?,469 keyF.unwrap_or_else(FuncVal::identity),470 )?))471}472473#[jrsonnet_macros::builtin]474fn builtin_format(s: State, str: IStr, vals: Any) -> Result<String> {475 std_format(s, str, vals.0)476}477478#[jrsonnet_macros::builtin]479fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {480 if to < from {481 return Ok(ArrValue::new_eager());482 }483 Ok(ArrValue::new_range(from, to))484}485486#[jrsonnet_macros::builtin]487fn builtin_char(n: u32) -> Result<char> {488 Ok(std::char::from_u32(n as u32).ok_or(InvalidUnicodeCodepointGot(n as u32))?)489}490491#[jrsonnet_macros::builtin]492fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {493 Ok(Bytes(str.bytes().collect::<Vec<u8>>().into()))494}495496#[jrsonnet_macros::builtin]497fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {498 Ok(std::str::from_utf8(&arr.0)499 .map_err(|_| RuntimeError("bad utf8".into()))?500 .into())501}502503#[jrsonnet_macros::builtin]504fn builtin_md5(str: IStr) -> Result<String> {505 Ok(format!("{:x}", md5::compute(&str.as_bytes())))506}507508#[jrsonnet_macros::builtin]509fn builtin_trace(s: State, loc: CallLocation, str: IStr, rest: Any) -> Result<Any> {510 eprint!("TRACE:");511 if let Some(loc) = loc.0 {512 let locs = s.map_source_locations(&loc.0, &[loc.1]);513 eprint!(514 " {}:{}",515 loc.0.file_name().unwrap().to_str().unwrap(),516 locs[0].line517 );518 }519 eprintln!(" {}", str);520 Ok(rest) as Result<Any>521}522523#[jrsonnet_macros::builtin]524fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {525 use Either2::*;526 Ok(match input {527 A(a) => base64::encode(a.0),528 B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),529 })530}531532#[jrsonnet_macros::builtin]533fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {534 Ok(Bytes(535 base64::decode(&input.as_bytes())536 .map_err(|_| RuntimeError("bad base64".into()))?537 .into(),538 ))539}540541#[jrsonnet_macros::builtin]542fn builtin_base64_decode(input: IStr) -> Result<String> {543 let bytes = base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?;544 Ok(String::from_utf8(bytes).map_err(|_| RuntimeError("bad utf8".into()))?)545}546547#[jrsonnet_macros::builtin]548fn builtin_join(s: State, sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {549 Ok(match sep {550 IndexableVal::Arr(joiner_items) => {551 let mut out = Vec::new();552553 let mut first = true;554 for item in arr.iter(s.clone()) {555 let item = item?.clone();556 if let Val::Arr(items) = item {557 if !first {558 out.reserve(joiner_items.len());559 // TODO: extend560 for item in joiner_items.iter(s.clone()) {561 out.push(item?);562 }563 }564 first = false;565 out.reserve(items.len());566 for item in items.iter(s.clone()) {567 out.push(item?);568 }569 } else if matches!(item, Val::Null) {570 continue;571 } else {572 throw!(RuntimeError(573 "in std.join all items should be arrays".into()574 ));575 }576 }577578 IndexableVal::Arr(out.into())579 }580 IndexableVal::Str(sep) => {581 let mut out = String::new();582583 let mut first = true;584 for item in arr.iter(s) {585 let item = item?.clone();586 if let Val::Str(item) = item {587 if !first {588 out += &sep;589 }590 first = false;591 out += &item;592 } else if matches!(item, Val::Null) {593 continue;594 } else {595 throw!(RuntimeError(596 "in std.join all items should be strings".into()597 ));598 }599 }600601 IndexableVal::Str(out.into())602 }603 })604}605606#[jrsonnet_macros::builtin]607fn builtin_escape_string_json(str_: IStr) -> Result<String> {608 Ok(escape_string_json(&str_))609}610611#[jrsonnet_macros::builtin]612fn builtin_manifest_json_ex(613 s: State,614 value: Any,615 indent: IStr,616 newline: Option<IStr>,617 key_val_sep: Option<IStr>,618 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,619) -> Result<String> {620 let newline = newline.as_deref().unwrap_or("\n");621 let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");622 manifest_json_ex(623 s,624 &value.0,625 &ManifestJsonOptions {626 padding: &indent,627 mtype: ManifestType::Std,628 newline,629 key_val_sep,630 #[cfg(feature = "exp-preserve-order")]631 preserve_order: preserve_order.unwrap_or(false),632 },633 )634}635636#[jrsonnet_macros::builtin]637fn builtin_manifest_yaml_doc(638 s: State,639 value: Any,640 indent_array_in_object: Option<bool>,641 quote_keys: Option<bool>,642 #[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,643) -> Result<String> {644 manifest_yaml_ex(645 s,646 &value.0,647 &ManifestYamlOptions {648 padding: " ",649 arr_element_padding: if indent_array_in_object.unwrap_or(false) {650 " "651 } else {652 ""653 },654 quote_keys: quote_keys.unwrap_or(true),655 #[cfg(feature = "exp-preserve-order")]656 preserve_order: preserve_order.unwrap_or(false),657 },658 )659}660661#[jrsonnet_macros::builtin]662fn builtin_reverse(value: ArrValue) -> Result<ArrValue> {663 Ok(value.reversed())664}665666#[jrsonnet_macros::builtin]667fn builtin_str_replace(str: String, from: IStr, to: IStr) -> Result<String> {668 Ok(str.replace(&from as &str, &to as &str))669}670671#[jrsonnet_macros::builtin]672fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {673 use Either2::*;674 Ok(VecVal(Cc::new(match maxsplits {675 A(n) => str676 .splitn(n + 1, &c as &str)677 .map(|s| Val::Str(s.into()))678 .collect(),679 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),680 })))681}682683#[jrsonnet_macros::builtin]684fn builtin_ascii_upper(str: IStr) -> Result<String> {685 Ok(str.to_ascii_uppercase())686}687688#[jrsonnet_macros::builtin]689fn builtin_ascii_lower(str: IStr) -> Result<String> {690 Ok(str.to_ascii_lowercase())691}692693#[jrsonnet_macros::builtin]694fn builtin_member(s: State, arr: IndexableVal, x: Any) -> Result<bool> {695 match arr {696 IndexableVal::Str(str) => {697 let x: IStr = IStr::from_untyped(x.0, s)?;698 Ok(!x.is_empty() && str.contains(&*x))699 }700 IndexableVal::Arr(a) => {701 for item in a.iter(s.clone()) {702 let item = item?;703 if equals(s.clone(), &item, &x.0)? {704 return Ok(true);705 }706 }707 Ok(false)708 }709 }710}711712#[jrsonnet_macros::builtin]713fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {714 let mut count = 0;715 for item in &arr {716 if equals(s.clone(), &item.0, &v.0)? {717 count += 1;718 }719 }720 Ok(count)721}722723#[jrsonnet_macros::builtin]724fn builtin_any(s: State, arr: ArrValue) -> Result<bool> {725 for v in arr.iter(s.clone()) {726 let v = bool::from_untyped(v?, s.clone())?;727 if v {728 return Ok(true);729 }730 }731 Ok(false)732}733734#[jrsonnet_macros::builtin]735fn builtin_all(s: State, arr: ArrValue) -> Result<bool> {736 for v in arr.iter(s.clone()) {737 let v = bool::from_untyped(v?, s.clone())?;738 if !v {739 return Ok(false);740 }741 }742 Ok(true)743}crates/jrsonnet-evaluator/src/stdlib/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/sort.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/sort.rs
@@ -68,7 +68,23 @@
if values.len() <= 1 {
return Ok(values);
}
- if !key_getter.is_identity() {
+ if key_getter.is_identity() {
+ // Fast path, identity key getter
+ let mut values = (*values).clone();
+ let sort_type = get_sort_type(&mut values, |k| k)?;
+ match sort_type {
+ SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
+ Val::Num(n) => NonNaNf64(*n),
+ _ => unreachable!(),
+ }),
+ SortKeyType::String => values.sort_unstable_by_key(|v| match v {
+ Val::Str(s) => s.clone(),
+ _ => unreachable!(),
+ }),
+ SortKeyType::Unknown => unreachable!(),
+ };
+ Ok(Cc::new(values))
+ } else {
// Slow path, user provided key getter
let mut vk = Vec::with_capacity(values.len());
for value in values.iter() {
@@ -90,21 +106,5 @@
SortKeyType::Unknown => unreachable!(),
};
Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
- } else {
- // Fast path, identity key getter
- let mut values = (*values).clone();
- let sort_type = get_sort_type(&mut values, |k| k)?;
- match sort_type {
- SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
- Val::Num(n) => NonNaNf64(*n),
- _ => unreachable!(),
- }),
- SortKeyType::String => values.sort_unstable_by_key(|v| match v {
- Val::Str(s) => s.clone(),
- _ => unreachable!(),
- }),
- SortKeyType::Unknown => unreachable!(),
- };
- Ok(Cc::new(values))
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -8,11 +8,11 @@
cc_ptr_eq,
error::{Error::*, LocError},
function::FuncVal,
- gc::TraceBox,
+ gc::{GcHashMap, TraceBox},
stdlib::manifest::{
manifest_json_ex, manifest_yaml_ex, ManifestJsonOptions, ManifestType, ManifestYamlOptions,
},
- throw, ObjValue, Result, State,
+ throw, Bindable, ObjValue, Result, State, WeakObjValue,
};
pub trait ThunkValue: Trace {
@@ -71,6 +71,47 @@
}
}
+type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);
+
+#[derive(Trace, Clone)]
+pub struct CachedBindable<I, T>
+where
+ I: Bindable<Bound = T>,
+{
+ cache: Cc<RefCell<GcHashMap<CacheKey, T>>>,
+ value: I,
+}
+impl<I: Bindable<Bound = T>, T: Trace> CachedBindable<I, T> {
+ pub fn new(value: I) -> Self {
+ Self {
+ cache: Cc::new(RefCell::new(GcHashMap::new())),
+ value,
+ }
+ }
+}
+impl<I: Bindable<Bound = T>, T: Clone + Trace> Bindable for CachedBindable<I, T> {
+ type Bound = T;
+ fn bind(&self, s: State, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<T> {
+ let cache_key = (
+ sup.as_ref().map(|s| s.clone().downgrade()),
+ this.as_ref().map(|t| t.clone().downgrade()),
+ );
+ {
+ if let Some(t) = self.cache.borrow().get(&cache_key) {
+ return Ok(t.clone());
+ }
+ }
+ let bound = self.value.bind(s, sup, this)?;
+
+ {
+ let mut cache = self.cache.borrow_mut();
+ cache.insert(cache_key, bound.clone());
+ }
+
+ Ok(bound)
+ }
+}
+
impl<T: Debug> Debug for Thunk<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Lazy")
crates/jrsonnet-evaluator/tests/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/builtin.rs
+++ b/crates/jrsonnet-evaluator/tests/builtin.rs
@@ -6,7 +6,6 @@
use jrsonnet_evaluator::{
error::Result,
function::{builtin, builtin::Builtin, CallLocation, FuncVal},
- gc::TraceBox,
tb,
typed::Typed,
State, Val,
crates/jrsonnet-evaluator/tests/common.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/common.rs
+++ b/crates/jrsonnet-evaluator/tests/common.rs
@@ -30,8 +30,18 @@
if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {
::jrsonnet_evaluator::throw_runtime!(
"assertion failed: a != b\na={:#?}\nb={:#?}",
- $a.to_json($s.clone(), 2)?,
- $b.to_json($s.clone(), 2)?,
+ $a.to_json(
+ $s.clone(),
+ 2,
+ #[cfg(feature = "exp-preserve-order")]
+ false
+ )?,
+ $b.to_json(
+ $s.clone(),
+ 2,
+ #[cfg(feature = "exp-preserve-order")]
+ false
+ )?,
)
}
}};
crates/jrsonnet-evaluator/tests/golden.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/golden.rs
+++ b/crates/jrsonnet-evaluator/tests/golden.rs
@@ -24,7 +24,12 @@
Ok(v) => v,
Err(e) => return s.stringify_err(&e),
};
- match v.to_json(s.clone(), 3) {
+ match v.to_json(
+ s.clone(),
+ 3,
+ #[cfg(feature = "exp-preserve-order")]
+ false,
+ ) {
Ok(v) => v.to_string(),
Err(e) => s.stringify_err(&e),
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -165,7 +165,7 @@
/ string_block() } / expected!("<string>")
pub rule field_name(s: &ParserSettings) -> expr::FieldName
- = name:id() {expr::FieldName::Fixed(name.into())}
+ = name:id() {expr::FieldName::Fixed(name)}
/ name:string() {expr::FieldName::Fixed(name.into())}
/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}
pub rule visibility() -> expr::Visibility