difftreelog
fix build on stable
in: master
16 files changed
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -26,16 +26,22 @@
/// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.
Range(RangeArray),
/// Sliced array view.
- Slice(Box<SliceArray>),
+ Slice(Cc<SliceArray>),
/// Reversed array view.
/// Returned by `std.reverse(other)` call
- Reverse(Box<ReverseArray>),
+ Reverse(Cc<ReverseArray>),
/// Returned by `std.map` call
Mapped(MappedArray),
/// Returned by `std.repeat` call
Repeated(RepeatedArray),
}
+pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}
+impl<I, T> ArrayLikeIter<T> for I where
+ I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator
+{
+}
+
impl ArrValue {
pub fn empty() -> Self {
Self::Range(RangeArray::empty())
@@ -123,7 +129,7 @@
return None;
}
- Some(Self::Slice(Box::new(SliceArray {
+ Some(Self::Slice(Cc::new(SliceArray {
inner: self,
from: from as u32,
to: to as u32,
@@ -160,24 +166,26 @@
pass!(self.get_lazy(index))
}
- /// Evaluate all array elements, returning new array.
- pub fn evaluatedcc(&self) -> Result<Cc<Vec<Val>>> {
- self.evaluated().map(Cc::new)
- }
- pub fn evaluated(&self) -> Result<Vec<Val>> {
- pass!(self.evaluated())
- }
-
- /// Iterate over elements, evaluating them.
+ #[cfg(feature = "nightly")]
pub fn iter(&self) -> UnknownArrayIter<'_> {
pass_iter_call!(self.iter => UnknownArrayIter)
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {
+ (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
+ }
/// Iterate over elements, returning lazy values.
+ #[cfg(feature = "nightly")]
pub fn iter_lazy(&self) -> UnknownArrayIterLazy<'_> {
pass_iter_call!(self.iter_lazy => UnknownArrayIterLazy)
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {
+ (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
+ }
+ #[cfg(feature = "nightly")]
pub fn iter_cheap(&self) -> Option<UnknownArrayIterCheap<'_>> {
macro_rules! question {
($v:expr) => {
@@ -187,10 +195,19 @@
Some(pass_iter_call!(self.iter_cheap in question => UnknownArrayIterCheap))
}
+ #[cfg(not(feature = "nightly"))]
+ pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {
+ if self.is_cheap() {
+ Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))
+ } else {
+ None
+ }
+ }
+
/// Return a reversed view on current array.
#[must_use]
pub fn reversed(self) -> Self {
- Self::Reverse(Box::new(ReverseArray(self)))
+ Self::Reverse(Cc::new(ReverseArray(self)))
}
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -1,26 +1,28 @@
-use std::{
- cell::RefCell,
- iter::{self, Rev},
- mem::replace,
-};
+//! Those implementations are a bit sketchy, as this is mostly performance experiments
+//! of not yet finished nightly rust features
+
+use std::{cell::RefCell, iter, mem::replace};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::LocExpr;
-use super::ArrValue;
+use super::{ArrValue, ArrayLikeIter};
use crate::{
error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, tb, typed::Any,
val::ThunkValue, Context, Error, Result, Thunk, Val,
};
-pub trait ArrayLike {
+pub trait ArrayLike: Sized + Into<ArrValue> {
+ #[cfg(feature = "nightly")]
type Iter<'t>
where
Self: 't;
+ #[cfg(feature = "nightly")]
type IterLazy<'t>
where
Self: 't;
+ #[cfg(feature = "nightly")]
type IterCheap<'t>
where
Self: 't;
@@ -32,11 +34,17 @@
fn get(&self, index: usize) -> Result<Option<Val>>;
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>>;
fn get_cheap(&self, index: usize) -> Option<Val>;
- fn evaluated(&self) -> Result<Vec<Val>>;
+ #[cfg(feature = "nightly")]
#[allow(clippy::iter_not_returning_iterator)]
fn iter(&self) -> Self::Iter<'_>;
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> Self::IterLazy<'_>;
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<Self::IterCheap<'_>>;
+
+ fn reverse(self) -> ArrValue {
+ ArrValue::Reverse(Cc::new(ReverseArray(self.into())))
+ }
}
#[derive(Debug, Clone, Trace)]
@@ -46,14 +54,49 @@
pub(crate) to: u32,
pub(crate) step: u32,
}
+
+impl SliceArray {
+ #[cfg(not(feature = "nightly"))]
+ fn iter(&self) -> impl Iterator<Item = Result<Val>> + '_ {
+ self.inner
+ .iter()
+ .skip(self.from as usize)
+ .take((self.to - self.from) as usize)
+ .step_by(self.step as usize)
+ }
+
+ #[cfg(not(feature = "nightly"))]
+ fn iter_lazy(&self) -> impl Iterator<Item = Thunk<Val>> + '_ {
+ self.inner
+ .iter_lazy()
+ .skip(self.from as usize)
+ .take((self.to - self.from) as usize)
+ .step_by(self.step as usize)
+ }
+
+ #[cfg(not(feature = "nightly"))]
+ fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {
+ Some(
+ self.inner
+ .iter_cheap()?
+ .skip(self.from as usize)
+ .take((self.to - self.from) as usize)
+ .step_by(self.step as usize),
+ )
+ }
+}
+#[cfg(feature = "nightly")]
type SliceArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type SliceArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type SliceArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ArrayLike for SliceArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = SliceArrayIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = SliceArrayLazyIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = SliceArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -75,10 +118,7 @@
self.iter_cheap()?.nth(index)
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- self.iter().collect()
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> SliceArrayIter<'_> {
self.inner
.iter()
@@ -87,6 +127,7 @@
.step_by(self.step as usize)
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> SliceArrayLazyIter<'_> {
self.inner
.iter_lazy()
@@ -95,6 +136,7 @@
.step_by(self.step as usize)
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<SliceArrayCheapIter<'_>> {
Some(
self.inner
@@ -105,17 +147,26 @@
)
}
}
+impl From<SliceArray> for ArrValue {
+ fn from(value: SliceArray) -> Self {
+ Self::Slice(Cc::new(value))
+ }
+}
#[derive(Trace, Debug, Clone)]
pub struct BytesArray(pub IBytes);
+#[cfg(feature = "nightly")]
type BytesArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type BytesArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type BytesArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ArrayLike for BytesArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = BytesArrayIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = BytesArrayLazyIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = BytesArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -134,24 +185,28 @@
self.0.get(index).map(|v| Val::Num(f64::from(*v)))
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- self.iter().collect()
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> BytesArrayIter<'_> {
self.0.iter().map(|v| Ok(Val::Num(f64::from(*v))))
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> BytesArrayLazyIter<'_> {
self.0
.iter()
.map(|v| Thunk::evaluated(Val::Num(f64::from(*v))))
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<BytesArrayCheapIter<'_>> {
Some(self.0.iter().map(|v| Val::Num(f64::from(*v))))
}
}
+impl From<BytesArray> for ArrValue {
+ fn from(value: BytesArray) -> Self {
+ ArrValue::Bytes(value)
+ }
+}
#[derive(Debug, Trace, Clone)]
enum ArrayThunk<T: 'static + Trace> {
@@ -168,9 +223,6 @@
}
#[derive(Debug, Trace, Clone)]
pub struct ExprArray(pub Cc<ExprArrayInner>);
-type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
-type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
-type ExprArrayCheapIter<'t> = iter::Empty<Val>;
impl ExprArray {
pub fn new(ctx: Context, items: impl IntoIterator<Item = LocExpr>) -> Self {
Self(Cc::new(ExprArrayInner {
@@ -179,11 +231,18 @@
}))
}
}
+#[cfg(feature = "nightly")]
+type ExprArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
+type ExprArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
+type ExprArrayCheapIter<'t> = iter::Empty<Val>;
impl ArrayLike for ExprArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = ExprArrayIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = ExprArrayLazyIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = ExprArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -250,18 +309,22 @@
None
}
+ #[cfg(feature = "nightly")]
fn iter(&self) -> ExprArrayIter<'_> {
(0..self.len()).map(|i| self.get(i).transpose().expect("index checked"))
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> ExprArrayLazyIter<'_> {
(0..self.len()).map(|i| self.get_lazy(i).expect("index checked"))
}
- fn iter_cheap(&self) -> Option<ExprArrayCheapIter<'_>> {
+ #[cfg(feature = "nightly")]
+ fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {
None
}
-
- fn evaluated(&self) -> Result<Vec<Val>> {
- self.iter().collect()
+}
+impl From<ExprArray> for ArrValue {
+ fn from(value: ExprArray) -> Self {
+ Self::Expr(value)
}
}
@@ -272,9 +335,14 @@
split: usize,
len: usize,
}
-type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + 't;
-type ExtendedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + 't;
-type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + 't;
+#[cfg(feature = "nightly")]
+
+type ExtendedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
+type ExtendedArrayLazyIter<'t> =
+ impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
+type ExtendedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ExtendedArray {
pub fn new(a: ArrValue, b: ArrValue) -> Self {
let a_len = a.len();
@@ -287,11 +355,49 @@
}
}
}
+
+struct WithExactSize<I>(I, usize);
+impl<I, T> Iterator for WithExactSize<I>
+where
+ I: Iterator<Item = T>,
+{
+ type Item = T;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.0.next()
+ }
+ fn nth(&mut self, n: usize) -> Option<Self::Item> {
+ self.0.nth(n)
+ }
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (self.1, Some(self.1))
+ }
+}
+impl<I> DoubleEndedIterator for WithExactSize<I>
+where
+ I: DoubleEndedIterator,
+{
+ fn next_back(&mut self) -> Option<Self::Item> {
+ self.0.next_back()
+ }
+ fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
+ self.0.nth_back(n)
+ }
+}
+impl<I> ExactSizeIterator for WithExactSize<I>
+where
+ I: Iterator,
+{
+ fn len(&self) -> usize {
+ self.1
+ }
+}
impl ArrayLike for ExtendedArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = ExtendedArrayIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = ExtendedArrayLazyIter<'t>;
-
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = ExtendedArrayCheapIter<'t>;
fn get(&self, index: usize) -> Result<Option<Val>> {
@@ -321,35 +427,43 @@
}
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- let mut out = self.a.evaluated()?;
- out.extend(self.b.evaluated()?.into_iter());
- Ok(out)
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> ExtendedArrayIter<'_> {
- self.a.iter().chain(self.b.iter())
+ WithExactSize(self.a.iter().chain(self.b.iter()), self.len)
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> ExtendedArrayLazyIter<'_> {
- self.a.iter_lazy().chain(self.b.iter_lazy())
+ WithExactSize(self.a.iter_lazy().chain(self.b.iter_lazy()), self.len)
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<ExtendedArrayCheapIter<'_>> {
let a = self.a.iter_cheap()?;
let b = self.b.iter_cheap()?;
- Some(a.chain(b))
+ Some(WithExactSize(a.chain(b), self.len))
}
}
+impl From<ExtendedArray> for ArrValue {
+ fn from(value: ExtendedArray) -> Self {
+ Self::Extended(Cc::new(value))
+ }
+}
#[derive(Trace, Debug, Clone)]
pub struct LazyArray(pub Cc<Vec<Thunk<Val>>>);
+#[cfg(feature = "nightly")]
type LazyArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type LazyArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type LazyArrayCheapIter<'t> = iter::Empty<Val>;
impl ArrayLike for LazyArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = LazyArrayIter<'t>;
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = LazyArrayLazyIter<'t>;
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = LazyArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -367,34 +481,41 @@
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
self.0.get(index).cloned()
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- let mut out = Vec::with_capacity(self.len());
- for i in self.0.iter() {
- out.push(i.evaluate()?);
- }
- Ok(out)
- }
+ #[cfg(feature = "nightly")]
fn iter(&self) -> LazyArrayIter<'_> {
self.0.iter().map(Thunk::evaluate)
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> LazyArrayLazyIter<'_> {
self.0.iter().cloned()
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<LazyArrayCheapIter<'_>> {
None
}
}
+impl From<LazyArray> for ArrValue {
+ fn from(value: LazyArray) -> Self {
+ Self::Lazy(value)
+ }
+}
#[derive(Trace, Debug, Clone)]
pub struct EagerArray(pub Cc<Vec<Val>>);
+#[cfg(feature = "nightly")]
type EagerArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type EagerArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type EagerArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ArrayLike for EagerArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = EagerArrayIter<'t>;
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = EagerArrayLazyIter<'t>;
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = EagerArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -413,105 +534,33 @@
self.0.get(index).cloned()
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- Ok((*self.0).clone())
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> EagerArrayIter<'_> {
self.0.iter().cloned().map(Ok)
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> EagerArrayLazyIter<'_> {
self.0.iter().cloned().map(Thunk::evaluated)
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<EagerArrayCheapIter<'_>> {
Some(self.0.iter().cloned())
}
}
+impl From<EagerArray> for ArrValue {
+ fn from(value: EagerArray) -> Self {
+ Self::Eager(value)
+ }
+}
/// Inclusive range type
#[derive(Debug, Trace, Clone, PartialEq, Eq)]
pub struct RangeArray {
start: i32,
end: i32,
-}
-struct RangeIter {
- start: i32,
- end: i32,
-}
-impl RangeIter {
- fn finished(&self) -> bool {
- self.end < self.start
- }
- fn finish(&mut self) {
- self.start = 0;
- self.end = -1;
- }
}
-impl Iterator for RangeIter {
- type Item = i32;
-
- fn next(&mut self) -> Option<Self::Item> {
- if self.finished() {
- return None;
- }
- let v = self.start;
- if v == self.end {
- self.finish();
- } else {
- self.start = v + 1;
- }
- Some(v)
- }
- fn nth(&mut self, n: usize) -> Option<Self::Item> {
- let v = (self.start as usize) + n;
- if v > self.end as usize {
- self.finish();
- None
- } else {
- self.start = v as i32;
- self.next()
- }
- }
- fn size_hint(&self) -> (usize, Option<usize>) {
- let len = self.len();
- (len, Some(len))
- }
-}
-impl DoubleEndedIterator for RangeIter {
- fn next_back(&mut self) -> Option<Self::Item> {
- if self.finished() {
- return None;
- }
- let v = self.end;
- if v == self.start {
- self.finish();
- } else {
- self.end = v - 1;
- }
- Some(v)
- }
- fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
- let v = (self.end as usize) - n;
- if v < self.start as usize {
- self.finish();
- None
- } else {
- self.end = v as i32;
- self.next_back()
- }
- }
-}
-impl ExactSizeIterator for RangeIter {
- fn len(&self) -> usize {
- if self.finished() {
- 0
- } else {
- (self.end as isize - self.start as isize + 1) as usize
- }
- }
-}
impl RangeArray {
pub fn empty() -> Self {
Self::new_exclusive(0, 0)
@@ -523,29 +572,37 @@
pub fn new_inclusive(start: i32, end: i32) -> Self {
Self { start, end }
}
- fn range(&self) -> RangeIter {
- RangeIter {
- start: self.start,
- end: self.end,
- }
+ fn range(&self) -> impl Iterator<Item = i32> + ExactSizeIterator + DoubleEndedIterator {
+ WithExactSize(
+ self.start..=self.end,
+ (self.end as usize)
+ .wrapping_sub(self.start as usize)
+ .wrapping_add(1),
+ )
}
}
+#[cfg(feature = "nightly")]
type RangeArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type RangeArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type RangeArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ArrayLike for RangeArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = RangeArrayIter<'t>;
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = RangeArrayLazyIter<'t>;
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = RangeArrayCheapIter<'t>;
fn len(&self) -> usize {
self.range().len()
}
fn is_empty(&self) -> bool {
- self.range().finished()
+ self.range().len() == 0
}
fn get(&self, index: usize) -> Result<Option<Val>> {
@@ -560,32 +617,39 @@
self.range().nth(index).map(|i| Val::Num(f64::from(i)))
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- Ok(self.range().map(|i| Val::Num(f64::from(i))).collect())
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> RangeArrayIter<'_> {
self.range().map(|i| Ok(Val::Num(f64::from(i))))
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> RangeArrayLazyIter<'_> {
self.range()
.map(|i| Thunk::evaluated(Val::Num(f64::from(i))))
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<RangeArrayCheapIter<'_>> {
Some(self.range().map(|i| Val::Num(f64::from(i))))
}
}
+impl From<RangeArray> for ArrValue {
+ fn from(value: RangeArray) -> Self {
+ Self::Range(value)
+ }
+}
#[derive(Debug, Trace, Clone)]
pub struct ReverseArray(pub ArrValue);
impl ArrayLike for ReverseArray {
- type Iter<'t> = Rev<UnknownArrayIter<'t>>;
+ #[cfg(feature = "nightly")]
+ type Iter<'t> = iter::Rev<UnknownArrayIter<'t>>;
- type IterLazy<'t> = Rev<UnknownArrayIterLazy<'t>>;
+ #[cfg(feature = "nightly")]
+ type IterLazy<'t> = iter::Rev<UnknownArrayIterLazy<'t>>;
- type IterCheap<'t> = Rev<UnknownArrayIterCheap<'t>>;
+ #[cfg(feature = "nightly")]
+ type IterCheap<'t> = iter::Rev<UnknownArrayIterCheap<'t>>;
fn len(&self) -> usize {
self.0.len()
@@ -603,24 +667,29 @@
self.0.get_cheap(self.0.len() - index - 1)
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- let mut v = self.0.evaluated()?;
- v.reverse();
- Ok(v)
- }
-
- fn iter(&self) -> Rev<UnknownArrayIter<'_>> {
+ #[cfg(feature = "nightly")]
+ fn iter(&self) -> iter::Rev<UnknownArrayIter<'_>> {
self.0.iter().rev()
}
- fn iter_lazy(&self) -> Rev<UnknownArrayIterLazy<'_>> {
+ #[cfg(feature = "nightly")]
+ fn iter_lazy(&self) -> iter::Rev<UnknownArrayIterLazy<'_>> {
self.0.iter_lazy().rev()
}
- fn iter_cheap(&self) -> Option<Rev<UnknownArrayIterCheap<'_>>> {
+ #[cfg(feature = "nightly")]
+ fn iter_cheap(&self) -> Option<iter::Rev<UnknownArrayIterCheap<'_>>> {
Some(self.0.iter_cheap()?.rev())
}
+ fn reverse(self) -> ArrValue {
+ self.0
+ }
}
+impl From<ReverseArray> for ArrValue {
+ fn from(value: ReverseArray) -> Self {
+ Self::Reverse(Cc::new(value))
+ }
+}
#[derive(Trace, Debug)]
pub struct MappedArrayInner {
@@ -640,12 +709,18 @@
}))
}
}
+#[cfg(feature = "nightly")]
type MappedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type MappedArrayLazyIter<'t> = impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type MappedArrayCheapIter<'t> = iter::Empty<Val>;
impl ArrayLike for MappedArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = MappedArrayIter<'t>;
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = MappedArrayLazyIter<'t>;
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = MappedArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -722,23 +797,26 @@
None
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- self.iter().collect()
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> MappedArrayIter<'_> {
(0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> MappedArrayLazyIter<'_> {
(0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<Self::IterCheap<'_>> {
None
}
}
-// impl MappedArray
+impl From<MappedArray> for ArrValue {
+ fn from(value: MappedArray) -> Self {
+ Self::Mapped(value)
+ }
+}
#[derive(Trace, Debug)]
pub struct RepeatedArrayInner {
@@ -762,13 +840,19 @@
}
}
+#[cfg(feature = "nightly")]
type RepeatedArrayIter<'t> = impl DoubleEndedIterator<Item = Result<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type RepeatedArrayLazyIter<'t> =
impl DoubleEndedIterator<Item = Thunk<Val>> + ExactSizeIterator + 't;
+#[cfg(feature = "nightly")]
type RepeatedArrayCheapIter<'t> = impl DoubleEndedIterator<Item = Val> + ExactSizeIterator + 't;
impl ArrayLike for RepeatedArray {
+ #[cfg(feature = "nightly")]
type Iter<'t> = RepeatedArrayIter<'t>;
+ #[cfg(feature = "nightly")]
type IterLazy<'t> = RepeatedArrayLazyIter<'t>;
+ #[cfg(feature = "nightly")]
type IterCheap<'t> = RepeatedArrayCheapIter<'t>;
fn len(&self) -> usize {
@@ -796,15 +880,7 @@
self.0.data.get_cheap(index % self.0.data.len())
}
- fn evaluated(&self) -> Result<Vec<Val>> {
- let mut data = self.0.data.evaluated()?;
- let data_range = 0..data.len();
- for _ in 1..self.0.repeats {
- data.extend_from_within(data_range.clone());
- }
- Ok(data)
- }
-
+ #[cfg(feature = "nightly")]
fn iter(&self) -> RepeatedArrayIter<'_> {
(0..self.0.total_len)
.map(|i| self.get(i))
@@ -812,12 +888,14 @@
.map(Option::unwrap)
}
+ #[cfg(feature = "nightly")]
fn iter_lazy(&self) -> RepeatedArrayLazyIter<'_> {
(0..self.0.total_len)
.map(|i| self.get_lazy(i))
.map(Option::unwrap)
}
+ #[cfg(feature = "nightly")]
fn iter_cheap(&self) -> Option<RepeatedArrayCheapIter<'_>> {
if !self.0.data.is_cheap() {
return None;
@@ -829,7 +907,13 @@
)
}
}
+impl From<RepeatedArray> for ArrValue {
+ fn from(value: RepeatedArray) -> Self {
+ Self::Repeated(value)
+ }
+}
+#[cfg(feature = "nightly")]
macro_rules! impl_iter_enum {
($n:ident => $v:ident) => {
pub enum $n<'t> {
@@ -865,6 +949,7 @@
}
pub(super) use pass;
+#[cfg(feature = "nightly")]
macro_rules! pass_iter_call {
($t:ident.$c:ident $(in $wrap:ident)? => $e:ident) => {
match $t {
@@ -881,8 +966,10 @@
}
};
}
+#[cfg(feature = "nightly")]
pub(super) use pass_iter_call;
+#[cfg(feature = "nightly")]
macro_rules! impl_iter {
($t:ident => $out:ty) => {
impl Iterator for $t<'_> {
@@ -927,9 +1014,15 @@
};
}
+#[cfg(feature = "nightly")]
impl_iter_enum!(UnknownArrayIter => Iter);
-impl_iter_enum!(UnknownArrayIterLazy => IterLazy);
-impl_iter_enum!(UnknownArrayIterCheap => IterCheap);
+#[cfg(feature = "nightly")]
impl_iter!(UnknownArrayIter => Result<Val>);
+#[cfg(feature = "nightly")]
+impl_iter_enum!(UnknownArrayIterLazy => IterLazy);
+#[cfg(feature = "nightly")]
impl_iter!(UnknownArrayIterLazy => Thunk<Val>);
+#[cfg(feature = "nightly")]
+impl_iter_enum!(UnknownArrayIterCheap => IterCheap);
+#[cfg(feature = "nightly")]
impl_iter!(UnknownArrayIterCheap => Val);
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 destructure::evaluate_dest,15 error::ErrorKind::*,16 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17 function::{CallLocation, FuncDesc, FuncVal},18 tb, throw,19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22 Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(Cc::new(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 )))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138 Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139 Thunk::new(tb!(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 })),143 ]))));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => throw!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().clone().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .member(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(tb!(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 }))?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .member(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(tb!(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 }))?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members.iter() {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(tb!(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 }));318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 throw!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417 if let Some(trivial) = evaluate_trivial(&expr) {418 return Ok(trivial);419 }420 let LocExpr(expr, loc) = expr;421 Ok(match &**expr {422 Literal(LiteralType::This) => {423 Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)424 }425 Literal(LiteralType::Super) => Val::Obj(426 ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(427 ctx.this()428 .clone()429 .expect("if super exists - then this should too"),430 ),431 ),432 Literal(LiteralType::Dollar) => {433 Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)434 }435 Literal(LiteralType::True) => Val::Bool(true),436 Literal(LiteralType::False) => Val::Bool(false),437 Literal(LiteralType::Null) => Val::Null,438 Parened(e) => evaluate(ctx, e)?,439 Str(v) => Val::Str(StrValue::Flat(v.clone())),440 Num(v) => Val::new_checked_num(*v)?,441 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,442 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,443 Var(name) => State::push(444 CallLocation::new(loc),445 || format!("variable <{name}> access"),446 || ctx.binding(name.clone())?.evaluate(),447 )?,448 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {449 let name = evaluate(ctx.clone(), index)?;450 let Val::Str(name) = name else {451 throw!(ValueIndexMustBeTypeGot(452 ValType::Obj,453 ValType::Str,454 name.value_type(),455 ))456 };457 ctx.super_obj()458 .clone()459 .expect("no super found")460 .get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?461 .expect("value not found")462 }463 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {464 (Val::Obj(v), Val::Str(key)) => State::push(465 CallLocation::new(loc),466 || format!("field <{key}> access"),467 || match v.get(key.clone().into_flat()) {468 Ok(Some(v)) => Ok(v),469 #[cfg(not(feature = "friendly-errors"))]470 Ok(None) => throw!(NoSuchField(key.clone(), vec![])),471 #[cfg(feature = "friendly-errors")]472 Ok(None) => {473 let mut heap = Vec::new();474 for field in v.fields_ex(475 true,476 #[cfg(feature = "exp-preserve-order")]477 false,478 ) {479 let conf = strsim::jaro_winkler(480 &field as &str,481 &key.clone().into_flat() as &str,482 );483 if conf < 0.8 {484 continue;485 }486 heap.push((conf, field));487 }488 heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));489490 throw!(NoSuchField(491 key.clone().into_flat(),492 heap.into_iter().map(|(_, v)| v).collect()493 ))494 }495 Err(e) => Err(e),496 },497 )?,498 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(499 ValType::Obj,500 ValType::Str,501 n.value_type(),502 )),503504 (Val::Arr(v), Val::Num(n)) => {505 if n.fract() > f64::EPSILON {506 throw!(FractionalIndex)507 }508 v.get(n as usize)?509 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?510 }511 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),512 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(513 ValType::Arr,514 ValType::Num,515 n.value_type(),516 )),517518 (Val::Str(s), Val::Num(n)) => Val::Str({519 let v: IStr = s520 .clone()521 .into_flat()522 .chars()523 .skip(n as usize)524 .take(1)525 .collect::<String>()526 .into();527 if v.is_empty() {528 let size = s.into_flat().chars().count();529 throw!(StringBoundsError(n as usize, size))530 }531 StrValue::Flat(v)532 }),533 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(534 ValType::Str,535 ValType::Num,536 n.value_type(),537 )),538539 (v, _) => throw!(CantIndexInto(v.value_type())),540 },541 LocalExpr(bindings, returned) => {542 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =543 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());544 let fctx = Context::new_future();545 for b in bindings {546 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;547 }548 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);549 evaluate(ctx, &returned.clone())?550 }551 Arr(items) => {552 if items.is_empty() {553 Val::Arr(ArrValue::empty())554 } else if items.len() == 1 {555 #[derive(Trace)]556 struct ArrayElement {557 ctx: Context,558 item: LocExpr,559 }560 impl ThunkValue for ArrayElement {561 type Output = Val;562 fn get(self: Box<Self>) -> Result<Val> {563 evaluate(self.ctx, &self.item)564 }565 }566 Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(567 ArrayElement {568 ctx,569 item: items[0].clone(),570 }571 ))])))572 } else {573 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))574 }575 }576 ArrComp(expr, comp_specs) => {577 let mut out = Vec::new();578 evaluate_comp(ctx, comp_specs, &mut |ctx| {579 out.push(evaluate(ctx, expr)?);580 Ok(())581 })?;582 Val::Arr(ArrValue::eager(Cc::new(out)))583 }584 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),585 ObjExtend(a, b) => evaluate_add_op(586 &evaluate(ctx.clone(), a)?,587 &Val::Obj(evaluate_object(ctx, b)?),588 )?,589 Apply(value, args, tailstrict) => {590 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?591 }592 Function(params, body) => {593 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())594 }595 AssertExpr(assert, returned) => {596 evaluate_assert(ctx.clone(), assert)?;597 evaluate(ctx, returned)?598 }599 ErrorStmt(e) => State::push(600 CallLocation::new(loc),601 || "error statement".to_owned(),602 || throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),603 )?,604 IfElse {605 cond,606 cond_then,607 cond_else,608 } => {609 if State::push(610 CallLocation::new(loc),611 || "if condition".to_owned(),612 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),613 )? {614 evaluate(ctx, cond_then)?615 } else {616 match cond_else {617 Some(v) => evaluate(ctx, v)?,618 None => Val::Null,619 }620 }621 }622 Slice(value, desc) => {623 fn parse_idx<T: Typed>(624 loc: CallLocation<'_>,625 ctx: &Context,626 expr: &Option<LocExpr>,627 desc: &'static str,628 ) -> Result<Option<T>> {629 if let Some(value) = expr {630 Ok(Some(State::push(631 loc,632 || format!("slice {desc}"),633 || T::from_untyped(evaluate(ctx.clone(), value)?),634 )?))635 } else {636 Ok(None)637 }638 }639640 let indexable = evaluate(ctx.clone(), value)?;641 let loc = CallLocation::new(loc);642643 let start = parse_idx(loc, &ctx, &desc.start, "start")?;644 let end = parse_idx(loc, &ctx, &desc.end, "end")?;645 let step = parse_idx(loc, &ctx, &desc.step, "step")?;646647 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?648 }649 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {650 let Expr::Str(path) = &*path.0 else {651 throw!("computed imports are not supported")652 };653 let tmp = loc.clone().0;654 let s = ctx.state();655 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;656 match i {657 Import(_) => State::push(658 CallLocation::new(loc),659 || format!("import {:?}", path.clone()),660 || s.import_resolved(resolved_path),661 )?,662 ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),663 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),664 _ => unreachable!(),665 }666 }667 })668}1use std::{cmp::Ordering, rc::Rc};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,7 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use self::destructure::destruct;12use crate::{13 arr::ArrValue,14 destructure::evaluate_dest,15 error::ErrorKind::*,16 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},17 function::{CallLocation, FuncDesc, FuncVal},18 tb, throw,19 typed::Typed,20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,22 Unbound, Val,23};24pub mod destructure;25pub mod operator;2627pub fn evaluate_trivial(expr: &LocExpr) -> Option<Val> {28 fn is_trivial(expr: &LocExpr) -> bool {29 match &*expr.0 {30 Expr::Str(_)31 | Expr::Num(_)32 | Expr::Literal(LiteralType::False | LiteralType::True | LiteralType::Null) => true,33 Expr::Arr(a) => a.iter().all(is_trivial),34 Expr::Parened(e) => is_trivial(e),35 _ => false,36 }37 }38 Some(match &*expr.0 {39 Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),40 Expr::Num(n) => Val::Num(*n),41 Expr::Literal(LiteralType::False) => Val::Bool(false),42 Expr::Literal(LiteralType::True) => Val::Bool(true),43 Expr::Literal(LiteralType::Null) => Val::Null,44 Expr::Arr(n) => {45 if n.iter().any(|e| !is_trivial(e)) {46 return None;47 }48 Val::Arr(ArrValue::eager(Cc::new(49 n.iter()50 .map(evaluate_trivial)51 .map(|e| e.expect("checked trivial"))52 .collect(),53 )))54 }55 Expr::Parened(e) => evaluate_trivial(e)?,56 _ => return None,57 })58}5960pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {62 name,63 ctx,64 params,65 body,66 })))67}6869pub fn evaluate_field_name(ctx: Context, field_name: &FieldName) -> Result<Option<IStr>> {70 Ok(match field_name {71 FieldName::Fixed(n) => Some(n.clone()),72 FieldName::Dyn(expr) => State::push(73 CallLocation::new(&expr.1),74 || "evaluating field name".to_string(),75 || {76 let value = evaluate(ctx, expr)?;77 if matches!(value, Val::Null) {78 Ok(None)79 } else {80 Ok(Some(IStr::from_untyped(value)?))81 }82 },83 )?,84 })85}8687pub fn evaluate_comp(88 ctx: Context,89 specs: &[CompSpec],90 callback: &mut impl FnMut(Context) -> Result<()>,91) -> Result<()> {92 match specs.get(0) {93 None => callback(ctx)?,94 Some(CompSpec::IfSpec(IfSpecData(cond))) => {95 if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {96 evaluate_comp(ctx, &specs[1..], callback)?;97 }98 }99 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(ctx.clone(), expr)? {100 Val::Arr(list) => {101 for item in list.iter_lazy() {102 let fctx = Pending::new();103 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());104 destruct(var, item, fctx.clone(), &mut new_bindings)?;105 let ctx = ctx106 .clone()107 .extend(new_bindings, None, None, None)108 .into_future(fctx);109110 evaluate_comp(ctx, &specs[1..], callback)?;111 }112 }113 #[cfg(feature = "exp-object-iteration")]114 Val::Obj(obj) => {115 for field in obj.fields(116 // TODO: Should there be ability to preserve iteration order?117 #[cfg(feature = "exp-preserve-order")]118 false,119 ) {120 #[derive(Trace)]121 struct ObjectFieldThunk {122 obj: ObjValue,123 field: IStr,124 }125 impl ThunkValue for ObjectFieldThunk {126 type Output = Val;127128 fn get(self: Box<Self>) -> Result<Self::Output> {129 self.obj.get(self.field).transpose().expect(130 "field exists, as field name was obtained from object.fields()",131 )132 }133 }134135 let fctx = Pending::new();136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![138 Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),139 Thunk::new(tb!(ObjectFieldThunk {140 field: field.clone(),141 obj: obj.clone(),142 })),143 ]))));144 destruct(var, value, fctx.clone(), &mut new_bindings)?;145 let ctx = ctx146 .clone()147 .extend(new_bindings, None, None, None)148 .into_future(fctx);149150 evaluate_comp(ctx, &specs[1..], callback)?;151 }152 }153 _ => throw!(InComprehensionCanOnlyIterateOverArray),154 },155 }156 Ok(())157}158159trait CloneableUnbound<T>: Unbound<Bound = T> + Clone {}160impl<V, T> CloneableUnbound<T> for V where V: Unbound<Bound = T> + Clone {}161162fn evaluate_object_locals(163 fctx: Pending<Context>,164 locals: Rc<Vec<BindSpec>>,165) -> impl CloneableUnbound<Context> {166 #[derive(Trace, Clone)]167 struct UnboundLocals {168 fctx: Pending<Context>,169 locals: Rc<Vec<BindSpec>>,170 }171 impl Unbound for UnboundLocals {172 type Bound = Context;173174 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Context> {175 let fctx = Context::new_future();176 let mut new_bindings =177 GcHashMap::with_capacity(self.locals.iter().map(BindSpec::capacity_hint).sum());178 for b in self.locals.iter() {179 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;180 }181182 let ctx = self.fctx.unwrap();183 let new_dollar = ctx.dollar().clone().or_else(|| this.clone());184185 let ctx = ctx186 .extend(new_bindings, new_dollar, sup, this)187 .into_future(fctx);188189 Ok(ctx)190 }191 }192193 UnboundLocals { fctx, locals }194}195196pub fn evaluate_field_member<B: Unbound<Bound = Context> + Clone>(197 builder: &mut ObjValueBuilder,198 ctx: Context,199 uctx: B,200 field: &FieldMember,201) -> Result<()> {202 let name = evaluate_field_name(ctx, &field.name)?;203 let Some(name) = name else {204 return Ok(());205 };206207 match field {208 FieldMember {209 plus,210 params: None,211 visibility,212 value,213 ..214 } => {215 #[derive(Trace)]216 struct UnboundValue<B: Trace> {217 uctx: B,218 value: LocExpr,219 name: IStr,220 }221 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {222 type Bound = Val;223 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {224 evaluate_named(self.uctx.bind(sup, this)?, &self.value, self.name.clone())225 }226 }227228 builder229 .member(name.clone())230 .with_add(*plus)231 .with_visibility(*visibility)232 .with_location(value.1.clone())233 .bindable(tb!(UnboundValue {234 uctx,235 value: value.clone(),236 name,237 }))?;238 }239 FieldMember {240 params: Some(params),241 visibility,242 value,243 ..244 } => {245 #[derive(Trace)]246 struct UnboundMethod<B: Trace> {247 uctx: B,248 value: LocExpr,249 params: ParamsDesc,250 name: IStr,251 }252 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {253 type Bound = Val;254 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {255 Ok(evaluate_method(256 self.uctx.bind(sup, this)?,257 self.name.clone(),258 self.params.clone(),259 self.value.clone(),260 ))261 }262 }263264 builder265 .member(name.clone())266 .with_visibility(*visibility)267 .with_location(value.1.clone())268 .bindable(tb!(UnboundMethod {269 uctx,270 value: value.clone(),271 params: params.clone(),272 name,273 }))?;274 }275 }276 Ok(())277}278279#[allow(clippy::too_many_lines)]280pub fn evaluate_member_list_object(ctx: Context, members: &[Member]) -> Result<ObjValue> {281 let mut builder = ObjValueBuilder::new();282 let locals = Rc::new(283 members284 .iter()285 .filter_map(|m| match m {286 Member::BindStmt(bind) => Some(bind.clone()),287 _ => None,288 })289 .collect::<Vec<_>>(),290 );291292 let fctx = Context::new_future();293294 // We have single context for all fields, so we can cache binds295 let uctx = CachedUnbound::new(evaluate_object_locals(fctx.clone(), locals));296297 for member in members.iter() {298 match member {299 Member::Field(field) => {300 evaluate_field_member(&mut builder, ctx.clone(), uctx.clone(), field)?;301 }302 Member::AssertStmt(stmt) => {303 #[derive(Trace)]304 struct ObjectAssert<B: Trace> {305 uctx: B,306 assert: AssertStmt,307 }308 impl<B: Unbound<Bound = Context>> ObjectAssertion for ObjectAssert<B> {309 fn run(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<()> {310 let ctx = self.uctx.bind(sup, this)?;311 evaluate_assert(ctx, &self.assert)312 }313 }314 builder.assert(tb!(ObjectAssert {315 uctx: uctx.clone(),316 assert: stmt.clone(),317 }));318 }319 Member::BindStmt(_) => {320 // Already handled321 }322 }323 }324 let this = builder.build();325 fctx.fill(ctx.extend(GcHashMap::new(), None, None, Some(this.clone())));326 Ok(this)327}328329pub fn evaluate_object(ctx: Context, object: &ObjBody) -> Result<ObjValue> {330 Ok(match object {331 ObjBody::MemberList(members) => evaluate_member_list_object(ctx, members)?,332 ObjBody::ObjComp(obj) => {333 let mut builder = ObjValueBuilder::new();334 let locals = Rc::new(335 obj.pre_locals336 .iter()337 .chain(obj.post_locals.iter())338 .cloned()339 .collect::<Vec<_>>(),340 );341 let mut ctxs = vec![];342 evaluate_comp(ctx, &obj.compspecs, &mut |ctx| {343 let fctx = Context::new_future();344 ctxs.push((ctx.clone(), fctx.clone()));345 let uctx = evaluate_object_locals(fctx, locals.clone());346347 evaluate_field_member(&mut builder, ctx, uctx, &obj.field)348 })?;349350 let this = builder.build();351 for (ctx, fctx) in ctxs {352 let _ctx = ctx353 .extend(GcHashMap::new(), None, None, Some(this.clone()))354 .into_future(fctx);355 }356 this357 }358 })359}360361pub fn evaluate_apply(362 ctx: Context,363 value: &LocExpr,364 args: &ArgsDesc,365 loc: CallLocation<'_>,366 tailstrict: bool,367) -> Result<Val> {368 let value = evaluate(ctx.clone(), value)?;369 Ok(match value {370 Val::Func(f) => {371 let body = || f.evaluate(ctx, loc, args, tailstrict);372 if tailstrict {373 body()?374 } else {375 State::push(loc, || format!("function <{}> call", f.name()), body)?376 }377 }378 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),379 })380}381382pub fn evaluate_assert(ctx: Context, assertion: &AssertStmt) -> Result<()> {383 let value = &assertion.0;384 let msg = &assertion.1;385 let assertion_result = State::push(386 CallLocation::new(&value.1),387 || "assertion condition".to_owned(),388 || bool::from_untyped(evaluate(ctx.clone(), value)?),389 )?;390 if !assertion_result {391 State::push(392 CallLocation::new(&value.1),393 || "assertion failure".to_owned(),394 || {395 if let Some(msg) = msg {396 throw!(AssertionFailed(evaluate(ctx, msg)?.to_string()?));397 }398 throw!(AssertionFailed(Val::Null.to_string()?));399 },400 )?;401 }402 Ok(())403}404405pub fn evaluate_named(ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {406 use Expr::*;407 let LocExpr(raw_expr, _loc) = expr;408 Ok(match &**raw_expr {409 Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),410 _ => evaluate(ctx, expr)?,411 })412}413414#[allow(clippy::too_many_lines)]415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {416 use Expr::*;417 if let Some(trivial) = evaluate_trivial(expr) {418 return Ok(trivial);419 }420 let LocExpr(expr, loc) = expr;421 Ok(match &**expr {422 Literal(LiteralType::This) => {423 Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)424 }425 Literal(LiteralType::Super) => Val::Obj(426 ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(427 ctx.this()428 .clone()429 .expect("if super exists - then this should too"),430 ),431 ),432 Literal(LiteralType::Dollar) => {433 Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)434 }435 Literal(LiteralType::True) => Val::Bool(true),436 Literal(LiteralType::False) => Val::Bool(false),437 Literal(LiteralType::Null) => Val::Null,438 Parened(e) => evaluate(ctx, e)?,439 Str(v) => Val::Str(StrValue::Flat(v.clone())),440 Num(v) => Val::new_checked_num(*v)?,441 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,442 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,443 Var(name) => State::push(444 CallLocation::new(loc),445 || format!("variable <{name}> access"),446 || ctx.binding(name.clone())?.evaluate(),447 )?,448 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {449 let name = evaluate(ctx.clone(), index)?;450 let Val::Str(name) = name else {451 throw!(ValueIndexMustBeTypeGot(452 ValType::Obj,453 ValType::Str,454 name.value_type(),455 ))456 };457 ctx.super_obj()458 .clone()459 .expect("no super found")460 .get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?461 .expect("value not found")462 }463 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {464 (Val::Obj(v), Val::Str(key)) => State::push(465 CallLocation::new(loc),466 || format!("field <{key}> access"),467 || match v.get(key.clone().into_flat()) {468 Ok(Some(v)) => Ok(v),469 #[cfg(not(feature = "friendly-errors"))]470 Ok(None) => throw!(NoSuchField(key.clone(), vec![])),471 #[cfg(feature = "friendly-errors")]472 Ok(None) => {473 let mut heap = Vec::new();474 for field in v.fields_ex(475 true,476 #[cfg(feature = "exp-preserve-order")]477 false,478 ) {479 let conf = strsim::jaro_winkler(480 &field as &str,481 &key.clone().into_flat() as &str,482 );483 if conf < 0.8 {484 continue;485 }486 heap.push((conf, field));487 }488 heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));489490 throw!(NoSuchField(491 key.clone().into_flat(),492 heap.into_iter().map(|(_, v)| v).collect()493 ))494 }495 Err(e) => Err(e),496 },497 )?,498 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(499 ValType::Obj,500 ValType::Str,501 n.value_type(),502 )),503504 (Val::Arr(v), Val::Num(n)) => {505 if n.fract() > f64::EPSILON {506 throw!(FractionalIndex)507 }508 v.get(n as usize)?509 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?510 }511 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),512 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(513 ValType::Arr,514 ValType::Num,515 n.value_type(),516 )),517518 (Val::Str(s), Val::Num(n)) => Val::Str({519 let v: IStr = s520 .clone()521 .into_flat()522 .chars()523 .skip(n as usize)524 .take(1)525 .collect::<String>()526 .into();527 if v.is_empty() {528 let size = s.into_flat().chars().count();529 throw!(StringBoundsError(n as usize, size))530 }531 StrValue::Flat(v)532 }),533 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(534 ValType::Str,535 ValType::Num,536 n.value_type(),537 )),538539 (v, _) => throw!(CantIndexInto(v.value_type())),540 },541 LocalExpr(bindings, returned) => {542 let mut new_bindings: GcHashMap<IStr, Thunk<Val>> =543 GcHashMap::with_capacity(bindings.iter().map(BindSpec::capacity_hint).sum());544 let fctx = Context::new_future();545 for b in bindings {546 evaluate_dest(b, fctx.clone(), &mut new_bindings)?;547 }548 let ctx = ctx.extend(new_bindings, None, None, None).into_future(fctx);549 evaluate(ctx, &returned.clone())?550 }551 Arr(items) => {552 if items.is_empty() {553 Val::Arr(ArrValue::empty())554 } else if items.len() == 1 {555 #[derive(Trace)]556 struct ArrayElement {557 ctx: Context,558 item: LocExpr,559 }560 impl ThunkValue for ArrayElement {561 type Output = Val;562 fn get(self: Box<Self>) -> Result<Val> {563 evaluate(self.ctx, &self.item)564 }565 }566 Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(567 ArrayElement {568 ctx,569 item: items[0].clone(),570 }571 ))])))572 } else {573 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))574 }575 }576 ArrComp(expr, comp_specs) => {577 let mut out = Vec::new();578 evaluate_comp(ctx, comp_specs, &mut |ctx| {579 out.push(evaluate(ctx, expr)?);580 Ok(())581 })?;582 Val::Arr(ArrValue::eager(Cc::new(out)))583 }584 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),585 ObjExtend(a, b) => evaluate_add_op(586 &evaluate(ctx.clone(), a)?,587 &Val::Obj(evaluate_object(ctx, b)?),588 )?,589 Apply(value, args, tailstrict) => {590 evaluate_apply(ctx, value, args, CallLocation::new(loc), *tailstrict)?591 }592 Function(params, body) => {593 evaluate_method(ctx, "anonymous".into(), params.clone(), body.clone())594 }595 AssertExpr(assert, returned) => {596 evaluate_assert(ctx.clone(), assert)?;597 evaluate(ctx, returned)?598 }599 ErrorStmt(e) => State::push(600 CallLocation::new(loc),601 || "error statement".to_owned(),602 || throw!(RuntimeError(evaluate(ctx, e)?.to_string()?,)),603 )?,604 IfElse {605 cond,606 cond_then,607 cond_else,608 } => {609 if State::push(610 CallLocation::new(loc),611 || "if condition".to_owned(),612 || bool::from_untyped(evaluate(ctx.clone(), &cond.0)?),613 )? {614 evaluate(ctx, cond_then)?615 } else {616 match cond_else {617 Some(v) => evaluate(ctx, v)?,618 None => Val::Null,619 }620 }621 }622 Slice(value, desc) => {623 fn parse_idx<T: Typed>(624 loc: CallLocation<'_>,625 ctx: &Context,626 expr: &Option<LocExpr>,627 desc: &'static str,628 ) -> Result<Option<T>> {629 if let Some(value) = expr {630 Ok(Some(State::push(631 loc,632 || format!("slice {desc}"),633 || T::from_untyped(evaluate(ctx.clone(), value)?),634 )?))635 } else {636 Ok(None)637 }638 }639640 let indexable = evaluate(ctx.clone(), value)?;641 let loc = CallLocation::new(loc);642643 let start = parse_idx(loc, &ctx, &desc.start, "start")?;644 let end = parse_idx(loc, &ctx, &desc.end, "end")?;645 let step = parse_idx(loc, &ctx, &desc.step, "step")?;646647 IndexableVal::into_untyped(indexable.into_indexable()?.slice(start, end, step)?)?648 }649 i @ (Import(path) | ImportStr(path) | ImportBin(path)) => {650 let Expr::Str(path) = &*path.0 else {651 throw!("computed imports are not supported")652 };653 let tmp = loc.clone().0;654 let s = ctx.state();655 let resolved_path = s.resolve_from(tmp.source_path(), path as &str)?;656 match i {657 Import(_) => State::push(658 CallLocation::new(loc),659 || format!("import {:?}", path.clone()),660 || s.import_resolved(resolved_path),661 )?,662 ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),663 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),664 _ => unreachable!(),665 }666 }667 })668}crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -96,16 +96,37 @@
(Str(a), Str(b)) => a.cmp(b),
(Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),
(Arr(a), Arr(b)) => {
- let ai = a.iter();
- let bi = b.iter();
+ if let (Some(ai), Some(bi)) = (a.iter_cheap(), b.iter_cheap()) {
+ for (a, b) in ai.zip(bi) {
+ let ord = evaluate_compare_op(&a, &b, op)?;
+ if !ord.is_eq() {
+ return Ok(ord);
+ }
+ }
+ } else {
+ {
+ let ai = a.iter();
+ let bi = b.iter();
- for (a, b) in ai.zip(bi) {
- let ord = evaluate_compare_op(&a?, &b?, op)?;
- if !ord.is_eq() {
- return Ok(ord);
+ for (a, b) in ai.zip(bi) {
+ let ord = evaluate_compare_op(&a?, &b?, op)?;
+ if !ord.is_eq() {
+ return Ok(ord);
+ }
+ }
}
- }
+ // {
+ // let ai = a.iter_expl();
+ // let bi = b.iter_expl();
+ // for (a, b) in ai.zip(bi) {
+ // let ord = evaluate_compare_op(&a?, &b?, op)?;
+ // if !ord.is_eq() {
+ // return Ok(ord);
+ // }
+ // }
+ // }
+ }
a.len().cmp(&b.len())
}
(_, _) => throw!(BinaryOperatorDoesNotOperateOnValues(
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,6 +1,5 @@
//! jsonnet interpreter implementation
-#![cfg_attr(feature = "nightly", feature(thread_local))]
-#![feature(type_alias_impl_trait)]
+#![cfg_attr(feature = "nightly", feature(thread_local, type_alias_impl_trait))]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(
clippy::all,
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -13,9 +13,9 @@
|| format!("std.format of {str}"),
|| {
Ok(match vals {
- Val::Arr(vals) => format_arr(&str, &vals.evaluatedcc()?)?,
- Val::Obj(obj) => format_obj(&str, &obj)?,
- o => format_arr(&str, &[o])?,
+ Val::Arr(vals) => format_arr(str, &vals.iter().collect::<Result<Vec<_>>>()?)?,
+ Val::Obj(obj) => format_obj(str, &obj)?,
+ o => format_arr(str, &[o])?,
})
},
)
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -278,19 +278,19 @@
}
/// Specialization, provides faster `TryFrom<VecVal>` for Val
-pub struct VecVal(pub Cc<Vec<Val>>);
+pub struct VecVal(pub Vec<Val>);
impl Typed for VecVal {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Arr(ArrValue::eager(value.0)))
+ Ok(Val::Arr(ArrValue::eager(Cc::new(value.0))))
}
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),
+ Val::Arr(a) => Ok(Self(a.iter().collect::<Result<Vec<_>>>()?)),
_ => unreachable!(),
}
}
crates/jrsonnet-parser/src/source.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/source.rs
+++ b/crates/jrsonnet-parser/src/source.rs
@@ -33,8 +33,8 @@
}
fn dyn_eq(&self, other: &dyn $T) -> bool {
let Some(other) = other.as_any().downcast_ref::<Self>() else {
- return false
- };
+ return false
+ };
let this = <Self as $T>::as_any(self)
.downcast_ref::<Self>()
.expect("restricted by impl");
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -5,7 +5,6 @@
val::{StrValue, Val},
IStr, ObjValue,
};
-use jrsonnet_gcmodule::Cc;
#[builtin]
pub fn builtin_object_fields_ex(
@@ -20,12 +19,12 @@
#[cfg(feature = "exp-preserve-order")]
preserve_order,
);
- Ok(VecVal(Cc::new(
+ Ok(VecVal(
out.into_iter()
.map(StrValue::Flat)
.map(Val::Str)
.collect::<Vec<_>>(),
- )))
+ ))
}
#[builtin]
crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -50,13 +50,12 @@
}
/// * `key_getter` - None, if identity sort required
-pub fn sort(ctx: Context, values: Cc<Vec<Val>>, key_getter: FuncVal) -> Result<Cc<Vec<Val>>> {
+pub fn sort(ctx: Context, mut values: Vec<Val>, key_getter: FuncVal) -> Result<Vec<Val>> {
if values.len() <= 1 {
return Ok(values);
}
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 {
@@ -69,7 +68,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Cc::new(values))
+ Ok(values)
} else {
// Slow path, user provided key getter
let mut vk = Vec::with_capacity(values.len());
@@ -96,7 +95,7 @@
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
+ Ok(vk.into_iter().map(|v| v.0).collect())
}
}
@@ -106,9 +105,9 @@
if arr.len() <= 1 {
return Ok(arr);
}
- Ok(ArrValue::eager(super::sort::sort(
+ Ok(ArrValue::eager(Cc::new(super::sort::sort(
ctx,
- arr.evaluatedcc()?,
+ arr.iter().collect::<Result<Vec<_>>>()?,
keyF.unwrap_or_else(FuncVal::identity),
- )?))
+ )?)))
}
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -6,7 +6,6 @@
val::{ArrValue, StrValue},
Either, IStr, Val,
};
-use jrsonnet_gcmodule::Cc;
#[builtin]
pub const fn builtin_codepoint(str: char) -> Result<u32> {
@@ -31,7 +30,7 @@
#[builtin]
pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> Result<VecVal> {
use Either2::*;
- Ok(VecVal(Cc::new(match maxsplits {
+ Ok(VecVal(match maxsplits {
A(n) => str
.splitn(n + 1, &c as &str)
.map(|s| Val::Str(StrValue::Flat(s.into())))
@@ -40,7 +39,7 @@
.split(&c as &str)
.map(|s| Val::Str(StrValue::Flat(s.into())))
.collect(),
- })))
+ }))
}
#[builtin]
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -15,28 +15,13 @@
"type": "github"
}
},
- "flake-utils_2": {
- "locked": {
- "lastModified": 1659877975,
- "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
- "owner": "numtide",
- "repo": "flake-utils",
- "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
- "type": "github"
- },
- "original": {
- "owner": "numtide",
- "repo": "flake-utils",
- "type": "github"
- }
- },
"nixpkgs": {
"locked": {
- "lastModified": 1668090223,
- "narHash": "sha256-Bynlfyf/LsQJ+CJ//1TGmA7eiCzqk95bz+bxyP39xYY=",
+ "lastModified": 1670089411,
+ "narHash": "sha256-iiW+L7iN8At8s98qb2h1P8Z0BVTZLqY8KHpfZuM7ULQ=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "1f6b98281191b50ba987cabd5bf3068870c26789",
+ "rev": "ffa4eb958a435e9833bda0fdfc834e87232aa879",
"type": "github"
},
"original": {
@@ -45,22 +30,6 @@
"type": "github"
}
},
- "nixpkgs_2": {
- "locked": {
- "lastModified": 1665296151,
- "narHash": "sha256-uOB0oxqxN9K7XGF1hcnY+PQnlQJ+3bP2vCn/+Ru/bbc=",
- "owner": "NixOS",
- "repo": "nixpkgs",
- "rev": "14ccaaedd95a488dd7ae142757884d8e125b3363",
- "type": "github"
- },
- "original": {
- "owner": "NixOS",
- "ref": "nixpkgs-unstable",
- "repo": "nixpkgs",
- "type": "github"
- }
- },
"root": {
"inputs": {
"flake-utils": "flake-utils",
@@ -70,15 +39,19 @@
},
"rust-overlay": {
"inputs": {
- "flake-utils": "flake-utils_2",
- "nixpkgs": "nixpkgs_2"
+ "flake-utils": [
+ "flake-utils"
+ ],
+ "nixpkgs": [
+ "nixpkgs"
+ ]
},
"locked": {
- "lastModified": 1668048396,
- "narHash": "sha256-SUWQlSa/H5XKPeuF9XmWzmwIJrgK42Lak6/1jBAwyd0=",
+ "lastModified": 1670034122,
+ "narHash": "sha256-EqmuOKucPWtMvCZtHraHr3Q3bgVszq1x2PoZtQkUuEk=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "859fefb532bb957f51a9b5e8e3ba2e48394c9353",
+ "rev": "a0d5773275ecd4f141d792d3a0376277c0fc0b65",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -3,7 +3,11 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
- rust-overlay.url = "github:oxalica/rust-overlay";
+ rust-overlay = {
+ url = "github:oxalica/rust-overlay";
+ inputs.nixpkgs.follows = "nixpkgs";
+ inputs.flake-utils.follows = "flake-utils";
+ };
};
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (system:
@@ -12,7 +16,7 @@
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
- rust = ((pkgs.rustChannelOf { date = "2022-11-10"; channel = "nightly"; }).default.override {
+ rust = ((pkgs.rustChannelOf { date = "2022-11-19"; channel = "nightly"; }).default.override {
extensions = [ "rust-src" "miri" ];
});
in
@@ -29,6 +33,13 @@
cargo = rust;
};
};
+ jrsonnet-nightly = pkgs.callPackage ./nix/jrsonnet.nix {
+ rustPlatform = pkgs.makeRustPlatform {
+ rustc = rust;
+ cargo = rust;
+ };
+ withNightlyFeatures = true;
+ };
jrsonnet-release = pkgs.callPackage ./nix/jrsonnet-release.nix {
rustPlatform = pkgs.makeRustPlatform {
rustc = rust;
@@ -37,29 +48,48 @@
};
benchmarks = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ ];
};
benchmarks-quick = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
quick = true;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ ];
};
benchmarks-against-release = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
- againstRelease = true;
+ inherit go-jsonnet sjsonnet jsonnet;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ { drv = jrsonnet-release; name = "before-str-extend"; }
+ ];
};
benchmarks-quick-against-release = pkgs.callPackage ./nix/benchmarks.nix {
- inherit go-jsonnet sjsonnet jsonnet jrsonnet jrsonnet-release;
+ inherit go-jsonnet sjsonnet jsonnet;
quick = true;
- againstRelease = true;
+ jrsonnetVariants = [
+ { drv = jrsonnet; name = "current"; }
+ { drv = jrsonnet-nightly; name = "current-nightly"; }
+ { drv = jrsonnet-release; name = "before-str-extend"; }
+ ];
};
};
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs;[
rust
cargo-edit
+ cargo-asm
lld
hyperfine
valgrind
+ kcachegrind
+ graphviz
];
};
}
nix/benchmarks.nixdiffbeforeafterboth--- a/nix/benchmarks.nix
+++ b/nix/benchmarks.nix
@@ -4,15 +4,16 @@
, cacert
, stdenv
, fetchFromGitHub
-, jrsonnet
-, jrsonnet-release
, go-jsonnet
, sjsonnet
, jsonnet
, hyperfine
, quick ? false
-, againstRelease ? false
+, jrsonnetVariants
}:
+
+with lib;
+
let
jsonnetBench = fetchFromGitHub {
rev = "v0.19.1";
@@ -65,13 +66,12 @@
unpackPhase = "true";
buildInputs = [
- jrsonnet
go-jsonnet
sjsonnet
jsonnet
hyperfine
- ] ++ (if againstRelease then [ jrsonnet-release ] else [ ]);
+ ];
installPhase =
let
@@ -81,47 +81,48 @@
echo >> $out
echo "### ${name}" >> $out
echo >> $out
- ${if skipGo != "" then ''
+ ${optionalString (skipGo != "") ''
echo "> Note: No results for Go, ${skipGo}" >> $out
echo >> $out
- '' else ""}
- ${if skipScala != "" then ''
+ ''}
+ ${optionalString (skipScala != "") ''
echo "> Note: No results for Scala, ${skipScala}" >> $out
echo >> $out
- '' else ""}
- ${if skipCpp != "" then ''
+ ''}
+ ${optionalString (skipCpp != "") ''
echo "> Note: No results for C++, ${skipCpp}" >> $out
echo >> $out
- '' else ""}
- ${if !quick then ''
+ ''}
+ ${optionalString (!quick && !omitSource) ''
echo "<details>" >> $out
echo "<summary>Source</summary>" >> $out
echo >> $out
echo "\`\`\`jsonnet" >> $out
- ${if pathIsGenerator then "echo \"// Generator source\" >> $out" else ""}
- ${if omitSource then "echo \"// Omitted: too large\" >> $out" else "cat ${path} >> $out"}
+ ${optionalString pathIsGenerator "echo \"// Generator source\" >> $out"}
+ cat ${path} >> $out
echo >> $out
echo "\`\`\`" >> $out
echo "</details>" >> $out
echo >> $out
- '' else ""}
+ ''}
path=${path}
- ${if pathIsGenerator then ''
- jrsonnet $path > generated.jsonnet
+ ${optionalString pathIsGenerator ''
+ go-jsonnet $path > generated.jsonnet
path=generated.jsonnet
- '' else ""}
- hyperfine -N -w4 --output=pipe --style=basic --export-markdown result.md \
- "jrsonnet $path ${if vendor != "" then "-J${vendor}" else ""}" -n "Rust" \
- ${if againstRelease then "\"jrsonnet-release $path ${if vendor != "" then "-J${vendor}" else ""}\" -n \"Rust (released)\"" else "" } \
- ${if skipGo == "" then "\"go-jsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"Go\"" else "" } \
- ${if skipScala == "" then "\"sjsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"Scala\"" else "" } \
- ${if skipCpp == "" then "\"jsonnet $path ${if vendor != "" then "-J ${vendor}" else ""}\" -n \"C++\"" else "" }
+ ''}
+ hyperfine -N -w4 -m20 --output=pipe --style=basic --export-markdown result.md \
+ ${concatStringsSep " " (forEach jrsonnetVariants (variant:
+ "\"${variant.drv}/bin/jrsonnet $path ${optionalString (vendor != "") "-J${vendor}"}\" -n \"Rust (${variant.name})\""
+ ))} \
+ ${optionalString (skipGo == "") "\"go-jsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"Go\""} \
+ ${optionalString (skipScala == "") "\"sjsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"Scala\""} \
+ ${optionalString (skipCpp == "") "\"jsonnet $path ${optionalString (vendor != "") "-J ${vendor}"}\" -n \"C++\""}
cat result.md >> $out
'';
in
''
touch $out
- ${if !quick then ''
+ ${optionalString (!quick) ''
cat ${./benchmarks.md} >> $out
echo >> $out
@@ -156,43 +157,43 @@
echo >> $out
echo >> $out
- '' else ""}
+ ''}
echo "## Real world" >> $out
- ${mkBench {name = "Graalvm CI"; path = "${graalvmBench}/ci.jsonnet"; skipCpp = "takes longer than a hour";}}
- ${mkBench {name = "Kube-prometheus manifests"; vendor = "${kubePrometheusBench}/vendor"; path = "${kubePrometheusBench}/example.jsonnet"; skipCpp = skipSlow;}}
+ ${mkBench {name = "Graalvm CI"; path = "${graalvmBench}/ci.jsonnet"; skipCpp = "takes longer than a hour"; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Kube-prometheus manifests"; vendor = "${kubePrometheusBench}/vendor"; path = "${kubePrometheusBench}/example.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from C++ jsonnet (/perf_tests)" >> $out
- ${mkBench {name = "Large string join"; path = "${jsonnetBench}/perf_tests/large_string_join.jsonnet";}}
- ${mkBench {name = "Large string template"; omitSource = true; path = "${jsonnetBench}/perf_tests/large_string_template.jsonnet"; skipGo = "fails with os stack size exhausion"; skipCpp = skipSlow;}}
- ${mkBench {name = "Realistic 1"; path = "${jsonnetBench}/perf_tests/realistic1.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow;}}
- ${mkBench {name = "Realistic 2"; path = "${jsonnetBench}/perf_tests/realistic2.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "Large string join"; path = "${jsonnetBench}/perf_tests/large_string_join.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "Large string template"; omitSource = true; path = "${jsonnetBench}/perf_tests/large_string_template.jsonnet"; skipGo = "fails with os stack size exhausion"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Realistic 1"; path = "${jsonnetBench}/perf_tests/realistic1.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Realistic 2"; path = "${jsonnetBench}/perf_tests/realistic2.jsonnet"; skipGo = skipSlow; skipCpp = skipSlow; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from C++ jsonnet (/benchmarks)" >> $out
- ${mkBench {name = "Tail call"; path = "${jsonnetBench}/benchmarks/bench.01.jsonnet";}}
- ${mkBench {name = "Inheritance recursion"; path = "${jsonnetBench}/benchmarks/bench.02.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "Simple recursive call"; path = "${jsonnetBench}/benchmarks/bench.03.jsonnet";}}
- ${mkBench {name = "Foldl string concat"; path = "${jsonnetBench}/benchmarks/bench.04.jsonnet";}}
+ ${mkBench {name = "Tail call"; path = "${jsonnetBench}/benchmarks/bench.01.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "Inheritance recursion"; path = "${jsonnetBench}/benchmarks/bench.02.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "Simple recursive call"; path = "${jsonnetBench}/benchmarks/bench.03.jsonnet"; skipScala = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "Foldl string concat"; path = "${jsonnetBench}/benchmarks/bench.04.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
${mkBench {name = "Array sorts"; path = "${jsonnetBench}/benchmarks/bench.06.jsonnet"; skipScala = "std.reverse is not implemented"; skipCpp = skipSlow;}}
- ${mkBench {name = "Lazy array"; path = "${jsonnetBench}/benchmarks/bench.07.jsonnet";}}
- ${mkBench {name = "Inheritance function recursion"; path = "${jsonnetBench}/benchmarks/bench.08.jsonnet";}}
- ${mkBench {name = "String strips"; path = "${jsonnetBench}/benchmarks/bench.09.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "Big object"; path = "${jsonnetBench}/benchmarks/gen_big_object.jsonnet"; pathIsGenerator = true;}}
+ ${mkBench {name = "Lazy array"; path = "${jsonnetBench}/benchmarks/bench.07.jsonnet"; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Inheritance function recursion"; path = "${jsonnetBench}/benchmarks/bench.08.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "String strips"; path = "${jsonnetBench}/benchmarks/bench.09.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "Big object"; path = "${jsonnetBench}/benchmarks/gen_big_object.jsonnet"; pathIsGenerator = true; skipScala = skipSlow;}}
echo >> $out
echo "## Benchmarks from Go jsonnet (builtins)" >> $out
- ${mkBench {name = "std.base64"; path = "${goJsonnetBench}/base64.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64Decode"; path = "${goJsonnetBench}/base64Decode.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64DecodeBytes"; path = "${goJsonnetBench}/base64DecodeBytes.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.base64 (byte array)"; path = "${goJsonnetBench}/base64_byte_array.jsonnet"; skipCpp = skipSlow;}}
- ${mkBench {name = "std.foldl"; path = "${goJsonnetBench}/foldl.jsonnet";}}
- ${mkBench {name = "std.manifestJsonEx"; path = "${goJsonnetBench}/manifestJsonEx.jsonnet";}}
- ${mkBench {name = "std.manifestTomlEx"; path = "${goJsonnetBench}/manifestTomlEx.jsonnet"; skipScala = "std.manifestTomlEx is not implemented";}}
- ${mkBench {name = "std.parseInt"; path = "${goJsonnetBench}/parseInt.jsonnet";}}
- ${mkBench {name = "std.reverse"; path = "${goJsonnetBench}/reverse.jsonnet"; skipScala = "std.reverse is not implemented";}}
- ${mkBench {name = "std.substr"; path = "${goJsonnetBench}/substr.jsonnet";}}
+ ${mkBench {name = "std.base64"; path = "${goJsonnetBench}/base64.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64Decode"; path = "${goJsonnetBench}/base64Decode.jsonnet"; skipCpp = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64DecodeBytes"; path = "${goJsonnetBench}/base64DecodeBytes.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.base64 (byte array)"; path = "${goJsonnetBench}/base64_byte_array.jsonnet"; skipCpp = skipSlow; skipGo = skipSlow; skipScala = skipSlow;}}
+ ${mkBench {name = "std.foldl"; path = "${goJsonnetBench}/foldl.jsonnet"; skipScala = skipSlow;}}
+ ${mkBench {name = "std.manifestJsonEx"; path = "${goJsonnetBench}/manifestJsonEx.jsonnet"; skipScala = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "std.manifestTomlEx"; path = "${goJsonnetBench}/manifestTomlEx.jsonnet"; skipScala = "std.manifestTomlEx is not implemented"; skipCpp=skipSlow;}}
+ ${mkBench {name = "std.parseInt"; path = "${goJsonnetBench}/parseInt.jsonnet"; skipScala = skipSlow; skipCpp = skipSlow;}}
+ ${mkBench {name = "std.reverse"; path = "${goJsonnetBench}/reverse.jsonnet"; skipScala = "std.reverse is not implemented"; skipCpp = skipSlow; skipGo = skipSlow;}}
+ ${mkBench {name = "std.substr"; path = "${goJsonnetBench}/substr.jsonnet"; skipScala = skipSlow;}}
${mkBench {name = "Comparsion for array"; path = "${goJsonnetBench}/comparison.jsonnet"; skipScala = "array comparsion is not implemented"; skipCpp = skipSlow;}}
- ${mkBench {name = "Comparsion for primitives"; path = "${goJsonnetBench}/comparison2.jsonnet"; skipCpp = "can't run: uses up to 192GB of RAM";}}
+ ${mkBench {name = "Comparsion for primitives"; path = "${goJsonnetBench}/comparison2.jsonnet"; skipCpp = "can't run: uses up to 192GB of RAM"; skipGo = skipSlow; skipScala = skipSlow;}}
'';
}
nix/jrsonnet-release.nixdiffbeforeafterboth--- a/nix/jrsonnet-release.nix
+++ b/nix/jrsonnet-release.nix
@@ -3,15 +3,15 @@
rustPlatform.buildRustPackage rec {
pname = "jrsonnet";
- version = "d32fe45b8ed28fb39b5359a704922922368af1c0";
+ version = "before-str-extend";
src = fetchFromGitHub {
owner = "CertainLach";
repo = pname;
- rev = version;
+ rev = "d32fe45b8ed28fb39b5359a704922922368af1c0";
hash = "sha256-R9Xt36bYS5upVDzt8hEifwmfocXpJbIKwvxkoJNEGVc=";
};
- cargoHash = "sha256-V+KGWeNlUnelofaGzufNPLGDyxazoFrjZ/n391VYYws=";
+ cargoHash = "sha256-j2sUIzvK66jn8ajmMsXXHstw79jCLog93XCQj1qjAN8=";
cargoTestFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
cargoBuildFlags = [ "--package=jrsonnet --features=mimalloc,legacy-this-file" ];
@@ -19,7 +19,6 @@
buildInputs = [ makeWrapper ];
postInstall = ''
- mv $out/bin/jrsonnet $out/bin/jrsonnet-release
- wrapProgram $out/bin/jrsonnet-release --add-flags "--max-stack=200000 --os-stack=200000"
+ wrapProgram $out/bin/jrsonnet --add-flags "--max-stack=200000 --os-stack=200000"
'';
}
nix/jrsonnet.nixdiffbeforeafterboth--- a/nix/jrsonnet.nix
+++ b/nix/jrsonnet.nix
@@ -1,4 +1,12 @@
-{ lib, fetchFromGitHub, rustPlatform, runCommand, makeWrapper }:
+{ lib
+, fetchFromGitHub
+, rustPlatform
+, runCommand
+, makeWrapper
+, withNightlyFeatures ? false
+}:
+
+with lib;
let
filteredSrc = builtins.path {
@@ -18,10 +26,12 @@
rustPlatform.buildRustPackage rec {
inherit src;
pname = "jrsonnet";
- version = "git";
+ version = "current${optionalString withNightlyFeatures "-nightly"}";
- cargoTestFlags = [ "--features=mimalloc,legacy-this-file,nightly" ];
- cargoBuildFlags = [ "--features=mimalloc,legacy-this-file,nightly" ];
+ cargoTestFlags = [
+ "--features=mimalloc,legacy-this-file${optionalString withNightlyFeatures ",nightly"}"
+ ];
+ cargoBuildFlags = cargoTestFlags;
buildInputs = [ makeWrapper ];