difftreelog
refactor more array specializations
in: master
16 files changed
bindings/jsonnet/src/val_make.rsdiffbeforeafterboth46/// Assign elements with [`jsonnet_json_array_append`].46/// Assign elements with [`jsonnet_json_array_append`].47#[no_mangle]47#[no_mangle]48pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {48pub extern "C" fn jsonnet_json_make_array(_vm: &VM) -> *mut Val {49 Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))49 Box::into_raw(Box::new(Val::Arr(ArrValue::eager(Cc::new(Vec::new())))))50}50}515152/// Make a `JsonnetJsonValue` representing an object.52/// Make a `JsonnetJsonValue` representing an object.bindings/jsonnet/src/val_modify.rsdiffbeforeafterboth25 }25 }262627 new.push(Thunk::evaluated(val.clone()));27 new.push(Thunk::evaluated(val.clone()));28 *arr = Val::Arr(ArrValue::Lazy(Cc::new(new)));28 *arr = Val::Arr(ArrValue::lazy(Cc::new(new)));29 }29 }30 _ => panic!("should receive array"),30 _ => panic!("should receive array"),31 }31 }crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth50 /// Preserve order in object manifestification50 /// Preserve order in object manifestification51 #[cfg(feature = "exp-preserve-order")]51 #[cfg(feature = "exp-preserve-order")]52 #[clap(long)]52 #[clap(long)]53 preserve_order: bool,53 pub preserve_order: bool,54}54}55impl ConfigureState for ManifestOpts {55impl ConfigureState for ManifestOpts {56 type Guards = Box<dyn ManifestFormat>;56 type Guards = Box<dyn ManifestFormat>;crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterbothno changes
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterbothno changes
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth32 Destruct::Array { start, rest, end } => {32 Destruct::Array { start, rest, end } => {33 use jrsonnet_parser::DestructRest;33 use jrsonnet_parser::DestructRest;343435 use crate::val::ArrValue;35 use crate::arr::ArrValue;363637 #[derive(Trace)]37 #[derive(Trace)]38 struct DataThunk {38 struct DataThunk {112 let to = full.len() - self.end;112 let to = full.len() - self.end;113 Ok(Val::Arr(full.slice(Some(self.start), Some(to), None)))113 Ok(Val::Arr(114 full.slice(Some(self.start), Some(to), None)115 .expect("arguments checked"),116 ))114 }117 }115 }118 }crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth101011use self::destructure::destruct;11use self::destructure::destruct;12use crate::{12use crate::{13 arr::ArrValue,13 destructure::evaluate_dest,14 destructure::evaluate_dest,14 error::ErrorKind::*,15 error::ErrorKind::*,15 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},16 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},16 function::{CallLocation, FuncDesc, FuncVal},17 function::{CallLocation, FuncDesc, FuncVal},17 tb, throw,18 tb, throw,18 typed::Typed,19 typed::Typed,19 val::{ArrValue, CachedUnbound, IndexableVal, Thunk, ThunkValue},20 val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},20 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,21 Unbound, Val,22 Unbound, Val,22};23};23pub mod destructure;24pub mod destructure;24pub mod operator;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(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}255926pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {60pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {27 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {61 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {100134101 let fctx = Pending::new();135 let fctx = Pending::new();102 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());103 let value = Thunk::evaluated(Val::Arr(ArrValue::Lazy(Cc::new(vec![137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![104 Thunk::evaluated(Val::Str(field.clone())),138 Thunk::evaluated(Val::Str(field.clone())),105 Thunk::new(tb!(ObjectFieldThunk {139 Thunk::new(tb!(ObjectFieldThunk {106 field: field.clone(),140 field: field.clone(),380#[allow(clippy::too_many_lines)]414#[allow(clippy::too_many_lines)]381pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {415pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {382 use Expr::*;416 use Expr::*;417 if let Some(trivial) = evaluate_trivial(&expr) {418 return Ok(trivial);419 }383 let LocExpr(expr, loc) = expr;420 let LocExpr(expr, loc) = expr;384 Ok(match &**expr {421 Ok(match &**expr {385 Literal(LiteralType::This) => {422 Literal(LiteralType::This) => {507 evaluate(ctx, &returned.clone())?544 evaluate(ctx, &returned.clone())?508 }545 }509 Arr(items) => {546 Arr(items) => {510 let mut out = Vec::with_capacity(items.len());547 if items.is_empty() {548 Val::Arr(ArrValue::empty())511 for item in items {549 } else if items.len() == 1 {512 // TODO: Implement ArrValue::Lazy with same context for every element?513 #[derive(Trace)]550 #[derive(Trace)]514 struct ArrayElement {551 struct ArrayElement {515 ctx: Context,552 ctx: Context,521 evaluate(self.ctx, &self.item)558 evaluate(self.ctx, &self.item)522 }559 }523 }560 }524 out.push(Thunk::new(tb!(ArrayElement {561 Val::Arr(ArrValue::lazy(Cc::new(vec![Thunk::new(tb!(562 ArrayElement {525 ctx: ctx.clone(),563 ctx,526 item: item.clone(),564 item: items[0].clone(),527 })));565 }566 ))])))528 }567 } else {529 Val::Arr(out.into())568 Val::Arr(ArrValue::expr(ctx, items.iter().cloned()))569 }530 }570 }531 ArrComp(expr, comp_specs) => {571 ArrComp(expr, comp_specs) => {532 let mut out = Vec::new();572 let mut out = Vec::new();533 evaluate_comp(ctx, comp_specs, &mut |ctx| {573 evaluate_comp(ctx, comp_specs, &mut |ctx| {534 out.push(evaluate(ctx, expr)?);574 out.push(evaluate(ctx, expr)?);535 Ok(())575 Ok(())536 })?;576 })?;537 Val::Arr(ArrValue::Eager(Cc::new(out)))577 Val::Arr(ArrValue::eager(Cc::new(out)))538 }578 }539 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),579 Obj(body) => Val::Obj(evaluate_object(ctx, body)?),540 ObjExtend(a, b) => evaluate_add_op(580 ObjExtend(a, b) => evaluate_add_op(615 || s.import_resolved(resolved_path),655 || s.import_resolved(resolved_path),616 )?,656 )?,617 ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),657 ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),618 ImportBin(_) => Val::Arr(ArrValue::Bytes(s.import_resolved_bin(resolved_path)?)),658 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),619 _ => unreachable!(),659 _ => unreachable!(),620 }660 }621 }661 }crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth3use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};3use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};445use crate::{5use crate::{6 error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed, val::equals, Context,6 arr::ArrValue, error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed,7 Result, Val,7 val::equals, Context, Result, Val,8};8};9918 })18 })19}19}2021/// Arbitrary threshold202221pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {23pub fn evaluate_add_op(a: &Val, b: &Val) -> Result<Val> {22 use Val::*;24 use Val::*;34 (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),36 (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),353736 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),38 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),37 (Arr(a), Arr(b)) => {39 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),38 let mut out = Vec::with_capacity(a.len() + b.len());4039 out.extend(a.iter_lazy());40 out.extend(b.iter_lazy());41 Arr(out.into())42 }43 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,41 (Num(v1), Num(v2)) => Val::new_checked_num(v1 + v2)?,44 _ => throw!(BinaryOperatorDoesNotOperateOnValues(42 _ => throw!(BinaryOperatorDoesNotOperateOnValues(45 BinaryOpType::Add,43 BinaryOpType::Add,82 })80 })83}81}848285pub fn evaluate_compare_op(a: &Val, op: BinaryOpType, b: &Val) -> Result<Ordering> {83pub fn evaluate_compare_op(a: &Val, b: &Val, op: BinaryOpType) -> Result<Ordering> {86 use Val::*;84 use Val::*;87 Ok(match (a, b) {85 Ok(match (a, b) {88 (Str(a), Str(b)) => a.cmp(b),86 (Str(a), Str(b)) => a.cmp(b),92 let bi = b.iter();90 let bi = b.iter();939194 for (a, b) in ai.zip(bi) {92 for (a, b) in ai.zip(bi) {95 let ord = evaluate_compare_op(&a?, op, &b?)?;93 let ord = evaluate_compare_op(&a?, &b?, op)?;96 if !ord.is_eq() {94 if !ord.is_eq() {97 return Ok(ord);95 return Ok(ord);98 }96 }117 (a, Eq, b) => Bool(equals(a, b)?),115 (a, Eq, b) => Bool(equals(a, b)?),118 (a, Neq, b) => Bool(!equals(a, b)?),116 (a, Neq, b) => Bool(!equals(a, b)?),119117120 (a, Lt, b) => Bool(evaluate_compare_op(a, Lt, b)?.is_lt()),118 (a, Lt, b) => Bool(evaluate_compare_op(a, b, Lt)?.is_lt()),121 (a, Gt, b) => Bool(evaluate_compare_op(a, Gt, b)?.is_gt()),119 (a, Gt, b) => Bool(evaluate_compare_op(a, b, Gt)?.is_gt()),122 (a, Lte, b) => Bool(evaluate_compare_op(a, Lte, b)?.is_le()),120 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),123 (a, Gte, b) => Bool(evaluate_compare_op(a, Gte, b)?.is_ge()),121 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),124122125 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),123 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),126 (a, Mod, b) => evaluate_mod_op(a, b)?,124 (a, Mod, b) => evaluate_mod_op(a, b)?,crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth7 Deserialize, Serialize,7 Deserialize, Serialize,8};8};9910use crate::{error::Result, val::ArrValue, ObjValueBuilder, State, Val};10use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};111112impl<'de> Deserialize<'de> for Val {12impl<'de> Deserialize<'de> for Val {13 fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>13 fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>77 where77 where78 E: serde::de::Error,78 E: serde::de::Error,79 {79 {80 Ok(Val::Arr(ArrValue::Bytes(v.into())))80 Ok(Val::Arr(ArrValue::bytes(v.into())))81 }81 }828283 fn visit_none<E>(self) -> Result<Self::Value, E>83 fn visit_none<E>(self) -> Result<Self::Value, E>117 out.push(val);117 out.push(val);118 }118 }119119120 Ok(Val::Arr(ArrValue::Eager(Cc::new(out))))120 Ok(Val::Arr(ArrValue::eager(Cc::new(out))))121 }121 }122122123 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>123 fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1//! jsonnet interpreter implementation1//! jsonnet interpreter implementation2#![cfg_attr(feature = "nightly", feature(thread_local))]2#![cfg_attr(feature = "nightly", feature(thread_local))]3#![feature(type_alias_impl_trait)]3#![deny(unsafe_op_in_unsafe_fn)]4#![deny(unsafe_op_in_unsafe_fn)]4#![warn(5#![warn(5 clippy::all,6 clippy::all,43// For jrsonnet-macros44// For jrsonnet-macros44extern crate self as jrsonnet_evaluator;45extern crate self as jrsonnet_evaluator;454647mod arr;46mod ctx;48mod ctx;47mod dynamic;49mod dynamic;48pub mod error;50pub mod error;crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth14 || format!("std.format of {str}"),14 || format!("std.format of {str}"),15 || {15 || {16 Ok(match vals {16 Ok(match vals {17 Val::Arr(vals) => format_arr(&str, &vals.evaluated()?)?,17 Val::Arr(vals) => format_arr(&str, &vals.evaluatedcc()?)?,18 Val::Obj(obj) => format_obj(&str, &obj)?,18 Val::Obj(obj) => format_obj(&str, &obj)?,19 o => format_arr(&str, &[o])?,19 o => format_arr(&str, &[o])?,20 })20 })crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth6use jrsonnet_types::{ComplexValType, ValType};6use jrsonnet_types::{ComplexValType, ValType};778use crate::{8use crate::{9 arr::ArrValue,9 error::Result,10 error::Result,10 function::{native::NativeDesc, FuncDesc, FuncVal},11 function::{native::NativeDesc, FuncDesc, FuncVal},11 throw,12 throw,12 typed::CheckType,13 typed::CheckType,13 val::{ArrValue, IndexableVal},14 val::IndexableVal,14 ObjValue, ObjValueBuilder, Val,15 ObjValue, ObjValueBuilder, Val,15};16};161730 fn from_untyped(untyped: Val) -> Result<Self>;31 fn from_untyped(untyped: Val) -> Result<Self>;31}32}3334const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS + 1)) - 1) as f64;323533macro_rules! impl_int {36macro_rules! impl_int {34 ($($ty:ty)*) => {$(37 ($($ty:ty)*) => {$(155 }158 }156}159}157impl Typed for usize {160impl Typed for usize {158 // It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility159 const TYPE: &'static ComplexValType =161 const TYPE: &'static ComplexValType =160 &ComplexValType::BoundedNumber(Some(0.0), Some(u32::MAX as f64));162 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));161163162 fn into_untyped(value: Self) -> Result<Val> {164 fn into_untyped(value: Self) -> Result<Val> {163 if value > u32::MAX as Self {165 if value > MAX_SAFE_INTEGER as Self {164 throw!("number is too large")166 throw!("number is too large")165 }167 }166 Ok(Val::Num(value as f64))168 Ok(Val::Num(value as f64))282 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);284 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);283285284 fn into_untyped(value: Self) -> Result<Val> {286 fn into_untyped(value: Self) -> Result<Val> {285 Ok(Val::Arr(ArrValue::Eager(value.0)))287 Ok(Val::Arr(ArrValue::eager(value.0)))286 }288 }287289288 fn from_untyped(value: Val) -> Result<Self> {290 fn from_untyped(value: Val) -> Result<Self> {289 <Self as Typed>::TYPE.check(&value)?;291 <Self as Typed>::TYPE.check(&value)?;290 match value {292 match value {291 Val::Arr(a) => Ok(Self(a.evaluated()?)),293 Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),292 _ => unreachable!(),294 _ => unreachable!(),293 }295 }294 }296 }300 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));302 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));301303302 fn into_untyped(value: Self) -> Result<Val> {304 fn into_untyped(value: Self) -> Result<Val> {303 Ok(Val::Arr(ArrValue::Bytes(value)))305 Ok(Val::Arr(ArrValue::bytes(value)))304 }306 }305307306 fn from_untyped(value: Val) -> Result<Self> {308 fn from_untyped(value: Val) -> Result<Self> {307 if let Val::Arr(ArrValue::Bytes(bytes)) = value {309 if let Val::Arr(ArrValue::Bytes(bytes)) = value {308 return Ok(bytes);310 return Ok(bytes.0);309 }311 }310 <Self as Typed>::TYPE.check(&value)?;312 <Self as Typed>::TYPE.check(&value)?;311 match value {313 match value {crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth1use std::{cell::RefCell, fmt::Debug};1use std::{cell::RefCell, fmt::Debug, mem::replace};223use jrsonnet_gcmodule::{Cc, Trace};3use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};4use jrsonnet_interner::IStr;5use jrsonnet_types::ValType;5use jrsonnet_types::ValType;667pub use crate::arr::ArrValue;7use crate::{8use crate::{8 error::{Error, ErrorKind::*},9 error::{Error, ErrorKind::*},9 function::FuncVal,10 function::FuncVal,31#[derive(Clone, Trace)]32#[derive(Clone, Trace)]32pub struct Thunk<T: Trace>(Cc<RefCell<ThunkInner<T>>>);33pub struct Thunk<T: Trace>(Cc<RefCell<ThunkInner<T>>>);333434impl<T> Thunk<T>35impl<T: Trace> Thunk<T> {35where36 pub fn evaluated(val: T) -> Self {36 T: Clone + Trace,37 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))37{38 }38 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {39 pub fn new(f: TraceBox<dyn ThunkValue<Output = T>>) -> Self {39 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))40 Self(Cc::new(RefCell::new(ThunkInner::Waiting(f))))40 }41 }41 pub fn evaluated(val: T) -> Self {42 pub fn errored(e: Error) -> Self {42 Self(Cc::new(RefCell::new(ThunkInner::Computed(val))))43 Self(Cc::new(RefCell::new(ThunkInner::Errored(e))))43 }44 }45}4647impl<T> Thunk<T>48where49 T: Clone + Trace,50{44 pub fn force(&self) -> Result<()> {51 pub fn force(&self) -> Result<()> {45 self.evaluate()?;52 self.evaluate()?;46 Ok(())53 Ok(())52 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),59 ThunkInner::Pending => return Err(InfiniteRecursionDetected.into()),53 ThunkInner::Waiting(..) => (),60 ThunkInner::Waiting(..) => (),54 };61 };55 let ThunkInner::Waiting(value) = std::mem::replace(&mut *self.0.borrow_mut(), ThunkInner::Pending) else {62 let ThunkInner::Waiting(value) = replace(&mut *self.0.borrow_mut(), ThunkInner::Pending) else {56 unreachable!();63 unreachable!();57 };64 };58 let new_value = match value.0.get() {65 let new_value = match value.0.get() {65 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());72 *self.0.borrow_mut() = ThunkInner::Computed(new_value.clone());66 Ok(new_value)73 Ok(new_value)67 }74 }68}75}697670type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);77type CacheKey = (Option<WeakObjValue>, Option<WeakObjValue>);7178120 }127 }121}128}122123#[derive(Debug, Clone, Trace)]124pub struct Slice {125 pub(crate) inner: ArrValue,126 pub(crate) from: u32,127 pub(crate) to: u32,128 pub(crate) step: u32,129}130impl Slice {131 const fn from(&self) -> usize {132 self.from as usize133 }134 const fn to(&self) -> usize {135 self.to as usize136 }137 const fn step(&self) -> usize {138 self.step as usize139 }140 const fn len(&self) -> usize {141 // TODO: use div_ceil142 let diff = self.to() - self.from();143 let rem = diff % self.step();144 let div = diff / self.step();145146 if rem == 0 {147 div148 } else {149 div + 1150 }151 }152}153154/// Represents a Jsonnet array value.155#[derive(Debug, Clone, Trace)]156// may contrain other ArrValue157#[trace(tracking(force))]158pub enum ArrValue {159 /// Layout optimized byte array.160 Bytes(#[trace(skip)] IBytes),161 /// Every element is lazy evaluated.162 Lazy(Cc<Vec<Thunk<Val>>>),163 /// Every field is already evaluated.164 Eager(Cc<Vec<Val>>),165 /// Concatenation of two arrays of any kind.166 Extended(Box<(Self, Self)>),167 /// Represents a integer array in form `[start, start + 1, ... end - 1, end]`.168 /// This kind of arrays is generated by `std.range(start, end)` call, and used for loops.169 Range(i32, i32),170 /// Sliced array view.171 Slice(Box<Slice>),172 /// Reversed array view.173 /// Returned by `std.reverse(other)` call174 Reversed(Box<Self>),175}176177#[cfg(target_pointer_width = "64")]178static_assertions::assert_eq_size!(ArrValue, [u8; 16]);179180impl ArrValue {181 pub fn new_eager() -> Self {182 Self::Eager(Cc::new(Vec::new()))183 }184 pub fn empty() -> Self {185 Self::new_range(0, 0)186 }187188 /// # Panics189 /// If a > b190 #[inline]191 pub fn new_range(a: i32, b: i32) -> Self {192 assert!(a <= b);193 Self::Range(a, b)194 }195196 /// # Panics197 /// If passed numbers are incorrect198 #[must_use]199 pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {200 let len = self.len();201 let from = from.unwrap_or(0);202 let to = to.unwrap_or(len).min(len);203 let step = step.unwrap_or(1);204 assert!(from < to);205 assert!(step > 0);206207 Self::Slice(Box::new(Slice {208 inner: self,209 from: from as u32,210 to: to as u32,211 step: step as u32,212 }))213 }214215 /// Array length.216 pub fn len(&self) -> usize {217 match self {218 Self::Bytes(i) => i.len(),219 Self::Lazy(l) => l.len(),220 Self::Eager(e) => e.len(),221 Self::Extended(v) => v.0.len() + v.1.len(),222 Self::Range(a, b) => a.abs_diff(*b) as usize + 1,223 Self::Reversed(i) => i.len(),224 Self::Slice(s) => s.len(),225 }226 }227228 /// Is array contains no elements?229 pub fn is_empty(&self) -> bool {230 self.len() == 0231 }232233 /// Get array element by index, evaluating it, if it is lazy.234 ///235 /// Returns `None` on out-of-bounds condition.236 pub fn get(&self, index: usize) -> Result<Option<Val>> {237 match self {238 Self::Bytes(i) => i239 .get(index)240 .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),241 Self::Lazy(vec) => {242 if let Some(v) = vec.get(index) {243 Ok(Some(v.evaluate()?))244 } else {245 Ok(None)246 }247 }248 Self::Eager(vec) => Ok(vec.get(index).cloned()),249 Self::Extended(v) => {250 let a_len = v.0.len();251 if a_len > index {252 v.0.get(index)253 } else {254 v.1.get(index - a_len)255 }256 }257 Self::Range(a, _) => {258 if index >= self.len() {259 return Ok(None);260 }261 Ok(Some(Val::Num(((*a as isize) + index as isize) as f64)))262 }263 Self::Reversed(v) => {264 let len = v.len();265 if index >= len {266 return Ok(None);267 }268 v.get(len - index - 1)269 }270 Self::Slice(v) => {271 let index = v.from() + index * v.step();272 if index >= v.to() {273 return Ok(None);274 }275 v.inner.get(index)276 }277 }278 }279280 /// Get array element by index, without evaluation.281 ///282 /// Returns `None` on out-of-bounds condition.283 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {284 match self {285 Self::Bytes(i) => i286 .get(index)287 .map(|b| Thunk::evaluated(Val::Num(f64::from(*b)))),288 Self::Lazy(vec) => vec.get(index).cloned(),289 Self::Eager(vec) => vec.get(index).cloned().map(Thunk::evaluated),290 Self::Extended(v) => {291 let a_len = v.0.len();292 if a_len > index {293 v.0.get_lazy(index)294 } else {295 v.1.get_lazy(index - a_len)296 }297 }298 Self::Range(a, _) => {299 if index >= self.len() {300 return None;301 }302 Some(Thunk::evaluated(Val::Num(303 ((*a as isize) + index as isize) as f64,304 )))305 }306 Self::Reversed(v) => {307 let len = v.len();308 if index >= len {309 return None;310 }311 v.get_lazy(len - index - 1)312 }313 Self::Slice(s) => {314 let index = s.from() + index * s.step();315 if index >= s.to() {316 return None;317 }318 s.inner.get_lazy(index)319 }320 }321 }322323 /// Evaluate all array elements, returning new array.324 pub fn evaluated(&self) -> Result<Cc<Vec<Val>>> {325 Ok(match self {326 Self::Bytes(i) => {327 let mut out = Vec::with_capacity(i.len());328 for v in i.iter() {329 out.push(Val::Num(f64::from(*v)));330 }331 Cc::new(out)332 }333 Self::Lazy(vec) => {334 let mut out = Vec::with_capacity(vec.len());335 for item in vec.iter() {336 out.push(item.evaluate()?);337 }338 Cc::new(out)339 }340 Self::Eager(vec) => vec.clone(),341 Self::Extended(_v) => {342 let mut out = Vec::with_capacity(self.len());343 for item in self.iter() {344 out.push(item?);345 }346 Cc::new(out)347 }348 Self::Range(a, b) => {349 let mut out = Vec::with_capacity(self.len());350 for i in *a..=*b {351 out.push(Val::Num(f64::from(i)));352 }353 Cc::new(out)354 }355 Self::Reversed(r) => {356 let mut r = r.evaluated()?;357 Cc::update_with(&mut r, |v| v.reverse());358 r359 }360 Self::Slice(v) => {361 let mut out = Vec::with_capacity(v.inner.len());362 for v in v363 .inner364 .iter_lazy()365 .skip(v.from())366 .take(v.to() - v.from())367 .step_by(v.step())368 {369 out.push(v.evaluate()?);370 }371 Cc::new(out)372 }373 })374 }375376 /// Iterate over elements, evaluating them.377 pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {378 (0..self.len()).map(move |idx| match self {379 Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),380 Self::Lazy(l) => l[idx].evaluate(),381 Self::Eager(e) => Ok(e[idx].clone()),382 Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {383 self.get(idx).map(|e| e.expect("idx < len"))384 }385 })386 }387388 /// Iterate over elements, returning lazy values.389 pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = Thunk<Val>> + '_ {390 (0..self.len()).map(move |idx| match self {391 Self::Bytes(b) => Thunk::evaluated(Val::Num(f64::from(b[idx]))),392 Self::Lazy(l) => l[idx].clone(),393 Self::Eager(e) => Thunk::evaluated(e[idx].clone()),394 Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {395 self.get_lazy(idx).expect("idx < len")396 }397 })398 }399400 /// Return a reversed view on current array.401 #[must_use]402 pub fn reversed(self) -> Self {403 Self::Reversed(Box::new(self))404 }405406 /// Return a new array, produced by passing every element of current array to specified callback function.407 pub fn map(self, mapper: impl Fn(Val) -> Result<Val>) -> Result<Self> {408 let mut out = Vec::with_capacity(self.len());409410 for value in self.iter() {411 out.push(mapper(value?)?);412 }413414 Ok(Self::Eager(Cc::new(out)))415 }416417 /// Return a new array, produced from current array by removing every value, for which specified callback function returns false.418 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {419 let mut out = Vec::with_capacity(self.len());420421 for value in self.iter() {422 let value = value?;423 if filter(&value)? {424 out.push(value);425 }426 }427428 Ok(Self::Eager(Cc::new(out)))429 }430431 pub fn ptr_eq(a: &Self, b: &Self) -> bool {432 match (a, b) {433 (Self::Lazy(a), Self::Lazy(b)) => Cc::ptr_eq(a, b),434 (Self::Eager(a), Self::Eager(b)) => Cc::ptr_eq(a, b),435 _ => false,436 }437 }438}439440impl From<Vec<Thunk<Val>>> for ArrValue {441 fn from(v: Vec<Thunk<Val>>) -> Self {442 Self::Lazy(Cc::new(v))443 }444}445446impl From<Vec<Val>> for ArrValue {447 fn from(v: Vec<Val>) -> Self {448 Self::Eager(Cc::new(v))449 }450}451129452/// Represents a Jsonnet value, which can be spliced or indexed (string or array).130/// Represents a Jsonnet value, which can be spliced or indexed (string or array).453#[allow(clippy::module_name_repetitions)]131#[allow(clippy::module_name_repetitions)]496 let step = step.as_deref().copied().unwrap_or(1);174 let step = step.as_deref().copied().unwrap_or(1);497175498 if index >= end {176 if index >= end {499 return Ok(Self::Arr(ArrValue::new_eager()));177 return Ok(Self::Arr(ArrValue::empty()));500 }178 }501179502 Ok(Self::Arr(ArrValue::Slice(Box::new(Slice {180 Ok(Self::Arr(503 inner: arr.clone(),181 arr.clone()504 from: index as u32,182 .slice(Some(index), Some(end), Some(step))505 to: end as u32,183 .expect("arguments checked"),506 step: step as u32,507 }))))184 ))508 }185 }509 }186 }510 }187 }540 }217 }541}218}542543// Broken between stable and nightly, as there is new layout size optimization544// #[cfg(target_pointer_width = "64")]545// static_assertions::assert_eq_size!(Val, [u8; 24]);546219547impl Val {220impl Val {548 pub const fn as_bool(&self) -> Option<bool> {221 pub const fn as_bool(&self) -> Option<bool> {crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth28}28}292930#[builtin]30#[builtin]31pub fn builtin_map(func: NativeFn<((Any,), Any)>, arr: ArrValue) -> Result<ArrValue> {31pub fn builtin_map(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {32 arr.map(|val| Ok(func(Any(val))?.0))32 Ok(arr.map(func))33}33}343435#[builtin]35#[builtin]94#[builtin]94#[builtin]95pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {95pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {96 if to < from {96 if to < from {97 return Ok(ArrValue::new_eager());97 return Ok(ArrValue::empty());98 }98 }99 Ok(ArrValue::new_range(from, to))99 Ok(ArrValue::range_inclusive(from, to))100}100}101101102#[builtin]102#[builtin]crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth77 } else if b.len() == a.len() {77 } else if b.len() == a.len() {78 return equals(&Val::Arr(a), &Val::Arr(b));78 return equals(&Val::Arr(a), &Val::Arr(b));79 } else {79 } else {80 for (a, b) in a.slice(None, Some(b.len()), None).iter().zip(b.iter()) {80 for (a, b) in a.iter().take(b.len()).zip(b.iter()) {81 let a = a?;81 let a = a?;82 let b = b?;82 let b = b?;83 if !equals(&a, &b)? {83 if !equals(&a, &b)? {103 return equals(&Val::Arr(a), &Val::Arr(b));103 return equals(&Val::Arr(a), &Val::Arr(b));104 } else {104 } else {105 let a_len = a.len();105 let a_len = a.len();106 for (a, b) in a106 for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {107 .slice(Some(a_len - b.len()), None, None)108 .iter()109 .zip(b.iter())110 {111 let a = a?;107 let a = a?;crates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth106 if arr.len() <= 1 {106 if arr.len() <= 1 {107 return Ok(arr);107 return Ok(arr);108 }108 }109 Ok(ArrValue::Eager(super::sort::sort(109 Ok(ArrValue::eager(super::sort::sort(110 ctx,110 ctx,111 arr.evaluated()?,111 arr.evaluatedcc()?,112 keyF.unwrap_or_else(FuncVal::identity),112 keyF.unwrap_or_else(FuncVal::identity),113 )?))113 )?))114}114}