difftreelog
refactor use PreparedFunction for NativeFn
in: master
9 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
@@ -1,14 +1,15 @@
use std::{
any::Any,
fmt::{self},
- num::NonZeroU32, rc::Rc,
+ num::NonZeroU32,
+ rc::Rc,
};
use jrsonnet_gcmodule::{cc_dyn, Cc};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{function::FuncVal, Context, Result, Thunk, Val};
+use crate::{typed::NativeFn, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
@@ -61,13 +62,13 @@
}
#[must_use]
- pub fn map(self, mapper: FuncVal) -> Self {
- Self::new(<MappedArray<false>>::new(self, mapper))
+ pub fn map(self, mapper: NativeFn!((Val) -> Val)) -> Self {
+ Self::new(<MappedArray>::new(self, ArrayMapper::Plain(mapper)))
}
#[must_use]
- pub fn map_with_index(self, mapper: FuncVal) -> Self {
- Self::new(<MappedArray<true>>::new(self, mapper))
+ pub fn map_with_index(self, mapper: NativeFn!((u32, Val) -> Val)) -> Self {
+ Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))
}
pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -6,9 +6,11 @@
use jrsonnet_parser::{Expr, Spanned};
use super::ArrValue;
+use crate::typed::NativeFn;
+use crate::val::NumValue;
use crate::{
- error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, typed::Typed,
- val::ThunkValue, Context, Error, ObjValue, Result, Thunk, Val,
+ error::ErrorKind::InfiniteRecursionDetected, evaluate, typed::Typed, val::ThunkValue, Context,
+ Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
@@ -404,14 +406,20 @@
}
}
+#[derive(Trace, Clone, Debug)]
+pub enum ArrayMapper {
+ Plain(NativeFn!((Val) -> Val)),
+ WithIndex(NativeFn!((u32, Val) -> Val)),
+}
+
#[derive(Trace, Debug, Clone)]
-pub struct MappedArray<const WITH_INDEX: bool> {
+pub struct MappedArray {
inner: ArrValue,
cached: Cc<RefCell<Vec<ArrayThunk>>>,
- mapper: FuncVal,
+ mapper: ArrayMapper,
}
-impl<const WITH_INDEX: bool> MappedArray<WITH_INDEX> {
- pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
+impl MappedArray {
+ pub fn new(inner: ArrValue, mapper: ArrayMapper) -> Self {
let len = inner.len();
Self {
inner,
@@ -420,14 +428,13 @@
}
}
fn evaluate(&self, index: usize, value: Val) -> Result<Val> {
- if WITH_INDEX {
- self.mapper.evaluate_simple(&(index, value), false)
- } else {
- self.mapper.evaluate_simple(&(value,), false)
+ match &self.mapper {
+ ArrayMapper::Plain(f) => f.call(value),
+ ArrayMapper::WithIndex(f) => f.call(index as u32, value),
}
}
}
-impl<const WITH_INDEX: bool> ArrayLike for MappedArray<WITH_INDEX> {
+impl ArrayLike for MappedArray {
fn len(&self) -> usize {
self.cached.borrow().len()
}
@@ -468,11 +475,11 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
#[derive(Trace)]
- struct MappedArrayThunk<const WITH_INDEX: bool> {
- arr: MappedArray<WITH_INDEX>,
+ struct MappedArrayThunk {
+ arr: MappedArray,
index: usize,
}
- impl<const WITH_INDEX: bool> ThunkValue for MappedArrayThunk<WITH_INDEX> {
+ impl ThunkValue for MappedArrayThunk {
type Output = Val;
fn get(&self) -> Result<Self::Output> {
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -8,15 +8,13 @@
use jrsonnet_parser::{Destruct, Expr, ExprParams, Span, Spanned};
use self::{
- arglike::OptionalContext,
builtin::{Builtin, StaticBuiltin},
- native::NativeDesc,
parse::{parse_builtin_call, parse_default_function_call, parse_function_call},
prepared::{parse_prepared_builtin_call, parse_prepared_function_call, PreparedCall},
};
use crate::{
bail, error::ErrorKind::*, evaluate, evaluate_trivial, function::builtin::BuiltinFunc, Context,
- ContextBuilder, Result, Thunk, Val,
+ Result, Thunk, Val,
};
pub mod arglike;
@@ -199,18 +197,6 @@
b.call(loc, &args)
}
}
- }
- pub fn evaluate_simple<A: ArgsLike + OptionalContext>(
- &self,
- args: &A,
- tailstrict: bool,
- ) -> Result<Val> {
- self.evaluate(
- ContextBuilder::new().build(),
- CallLocation::native(),
- args,
- tailstrict,
- )
}
pub(crate) fn evaluate_prepared(
@@ -246,10 +232,6 @@
b.call(loc, &args)
}
}
- }
- /// Convert jsonnet function to plain `Fn` value.
- pub fn into_native<D: NativeDesc>(self) -> D::Value {
- D::into_native(self)
}
/// Is this function an indentity function.
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -1,43 +1,2 @@
-use super::{
- arglike::{ArgLike, OptionalContext},
- FuncVal,
-};
-use crate::{typed::Typed, Result};
-
-pub trait NativeDesc {
- type Value;
- fn into_native(val: FuncVal) -> Self::Value;
-}
-macro_rules! impl_native_desc {
- ($($gen:ident)*) => {
- impl<$($gen,)* O> NativeDesc for (($($gen,)*), O)
- where
- $($gen: ArgLike + OptionalContext,)*
- O: Typed,
- {
- type Value = Box<dyn Fn($($gen,)*) -> Result<O>>;
-
- #[allow(non_snake_case)]
- fn into_native(val: FuncVal) -> Self::Value {
- Box::new(move |$($gen),*| {
- let val = val.evaluate_simple(
- &($($gen,)*),
- false,
- )?;
- O::from_untyped(val)
- })
- }
- }
- };
- ($($cur:ident)* @ $c:ident $($rest:ident)*) => {
- impl_native_desc!($($cur)*);
- impl_native_desc!($($cur)* $c @ $($rest)*);
- };
- ($($cur:ident)* @) => {
- impl_native_desc!($($cur)*);
- }
-}
-
-impl_native_desc! {
- @ A B C D E F G H I J K L
-}
+use super::PreparedFuncVal;
+use crate::{typed::Typed, CallLocation, Result, Thunk};
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -8,7 +8,7 @@
use crate::{
arr::{ArrValue, BytesArray},
bail,
- function::{native::NativeDesc, FuncDesc, FuncVal},
+ function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
typed::CheckType,
val::{IndexableVal, NumValue, StrValue, ThunkMapper},
ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
@@ -675,30 +675,65 @@
}
}
-pub struct NativeFn<D: NativeDesc>(D::Value);
-impl<D: NativeDesc> Deref for NativeFn<D> {
- type Target = D::Value;
+#[derive(Debug, Trace, Clone)]
+pub struct NativeFn<D: 'static>(pub(crate) PreparedFuncVal, PhantomData<D>);
+macro_rules! impl_native_desc {
+ ($i:expr; $($gen:ident)*) => {
+ impl<$($gen,)* O> NativeFn<($($gen,)* O,)>
+ where
+ $($gen: Typed,)*
+ O: Typed,
+ {
+ pub fn call(
+ &self,
+ $($gen: $gen,)*
+ ) -> Result<O> {
+ let val = self.0.call(
+ CallLocation::native(),
+ &[$(Typed::into_lazy_untyped($gen),)*],
+ &[],
+ )?;
+ O::from_untyped(val)
+ }
+ }
+ impl<$($gen,)* O> Typed for NativeFn<($($gen,)* O,)> {
+ const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+
+ fn into_untyped(_typed: Self) -> Result<Val> {
+ bail!("can only convert functions from jsonnet to native")
+ }
- fn deref(&self) -> &Self::Target {
- &self.0
+ fn from_untyped(untyped: Val) -> Result<Self> {
+ let func = FuncVal::from_untyped(untyped)?;
+ Ok(Self(
+ PreparedFuncVal::new(func, $i, &[])?,
+ PhantomData,
+ ))
+ }
+ }
+ };
+ ($i:expr; $($cur:ident)* @ $c:ident $($rest:ident)*) => {
+ impl_native_desc!($i; $($cur)*);
+ impl_native_desc!($i + 1; $($cur)* $c @ $($rest)*);
+ };
+ ($i:expr; $($cur:ident)* @) => {
+ impl_native_desc!($i; $($cur)*);
}
}
-impl<D: NativeDesc> Typed for NativeFn<D> {
- const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
- fn into_untyped(_typed: Self) -> Result<Val> {
- bail!("can only convert functions from jsonnet to native")
- }
+impl_native_desc! {
+ 0; @ A B C D E F G H I J K L
+}
- fn from_untyped(untyped: Val) -> Result<Self> {
- Ok(Self(
- untyped
- .as_func()
- .expect("shape is checked")
- .into_native::<D>(),
- ))
+mod native_macro {
+ #[macro_export]
+ macro_rules! NativeFn {
+ (($($t:ty),* $(,)?) -> $res:ty) => {
+ NativeFn<($($t,)* $res)>
+ }
}
}
+pub use crate::NativeFn;
impl Typed for NumValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -3,7 +3,14 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{
- Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn, LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type, parenthesized, parse::{Parse, ParseStream}, parse_macro_input, punctuated::Punctuated, spanned::Spanned, token::{self, Comma}
+ parenthesized,
+ parse::{Parse, ParseStream},
+ parse_macro_input,
+ punctuated::Punctuated,
+ spanned::Spanned,
+ token::{self, Comma},
+ Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,
+ LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4 bail,5 function::{builtin, FuncVal},6 runtime_error,7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8 val::{equals, ArrValue, IndexableVal},9 Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13 if let Some(on_empty) = on_empty {14 on_empty.evaluate()15 } else {16 bail!("expected non-empty array")17 }18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22 if *sz == 0 {23 return Ok(ArrValue::empty());24 }25 func.evaluate_trivial().map_or_else(26 || Ok(ArrValue::range_exclusive(0, *sz).map(func)),27 |trivial| {28 let mut out = Vec::with_capacity(*sz as usize);29 for _ in 0..*sz {30 out.push(trivial.clone());31 }32 Ok(ArrValue::eager(out))33 },34 )35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39 Ok(match what {40 Either2::A(s) => Val::string(s.repeat(count)),41 Either2::B(arr) => Val::Arr(42 ArrValue::repeated(arr, count)43 .ok_or_else(|| runtime_error!("repeated length overflow"))?,44 ),45 })46}4748#[builtin]49pub fn builtin_slice(50 indexable: IndexableVal,51 index: Option<Option<i32>>,52 end: Option<Option<i32>>,53 step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,54) -> Result<Val> {55 indexable56 .slice(index.flatten(), end.flatten(), step.flatten())57 .map(Val::from)58}5960#[builtin]61pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {62 let arr = arr.to_array();63 arr.map(func)64}6566#[builtin]67pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {68 let arr = arr.to_array();69 arr.map_with_index(func)70}7172#[builtin]73pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {74 let mut out = ObjValueBuilder::new();75 for (k, v) in obj.iter(76 // Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).77 // The thrown error might be different, but jsonnet78 // does not specify the evaluation order.79 #[cfg(feature = "exp-preserve-order")]80 true,81 ) {82 let v = v?;83 out.field(k.clone())84 .value(func.evaluate_simple(&(k, v), false)?);85 }86 Ok(out.build())87}8889#[builtin]90pub fn builtin_flatmap(91 func: NativeFn<((Either![String, Val],), Val)>,92 arr: IndexableVal,93) -> Result<IndexableVal> {94 use std::fmt::Write;95 match arr {96 IndexableVal::Str(str) => {97 let mut out = String::new();98 for c in str.chars() {99 match func(Either2::A(c.to_string()))? {100 Val::Str(o) => write!(out, "{o}").unwrap(),101 Val::Null => {},102 _ => bail!("in std.join all items should be strings"),103 }104 }105 Ok(IndexableVal::Str(out.into()))106 }107 IndexableVal::Arr(a) => {108 let mut out = Vec::new();109 for el in a.iter() {110 let el = el?;111 match func(Either2::B(el))? {112 Val::Arr(o) => {113 for oe in o.iter() {114 out.push(oe?);115 }116 }117 Val::Null => {},118 _ => bail!("in std.join all items should be arrays"),119 }120 }121 Ok(IndexableVal::Arr(out.into()))122 }123 }124}125126#[builtin]127pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {128 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))129}130131#[builtin]132pub fn builtin_filter_map(133 filter_func: FuncVal,134 map_func: FuncVal,135 arr: ArrValue,136) -> Result<ArrValue> {137 Ok(builtin_filter(filter_func, arr)?.map(map_func))138}139140#[builtin]141pub fn builtin_foldl(func: FuncVal, arr: Either![ArrValue, IStr], init: Val) -> Result<Val> {142 let mut acc = init;143 match arr {144 Either2::A(arr) => {145 for i in arr.iter() {146 acc = func.evaluate_simple(&(acc, i?), false)?;147 }148 }149 Either2::B(arr) => {150 for i in arr.chars() {151 acc = func.evaluate_simple(&(acc, Val::string(i)), false)?;152 }153 }154 }155 Ok(acc)156}157158#[builtin]159pub fn builtin_foldr(func: FuncVal, arr: Either![ArrValue, IStr], init: Val) -> Result<Val> {160 let mut acc = init;161 match arr {162 Either2::A(arr) => {163 for i in arr.iter().rev() {164 acc = func.evaluate_simple(&(i?, acc), false)?;165 }166 }167 Either2::B(arr) => {168 for i in arr.chars().rev() {169 acc = func.evaluate_simple(&(Val::string(i), acc), false)?;170 }171 }172 }173 Ok(acc)174}175176#[builtin]177pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {178 if to < from {179 return Ok(ArrValue::empty());180 }181 Ok(ArrValue::range_inclusive(from, to))182}183184#[builtin]185pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {186 use std::fmt::Write;187 Ok(match sep {188 IndexableVal::Arr(joiner_items) => {189 let mut out = Vec::new();190191 let mut first = true;192 for item in arr.iter() {193 let item = item?.clone();194 if let Val::Arr(items) = item {195 if !first {196 out.reserve(joiner_items.len());197 // TODO: extend198 for item in joiner_items.iter() {199 out.push(item?);200 }201 }202 first = false;203 out.reserve(items.len());204 for item in items.iter() {205 out.push(item?);206 }207 } else if matches!(item, Val::Null) {208 } else {209 bail!("in std.join all items should be arrays");210 }211 }212213 IndexableVal::Arr(out.into())214 }215 IndexableVal::Str(sep) => {216 let mut out = String::new();217218 let mut first = true;219 for item in arr.iter() {220 let item = item?.clone();221 if let Val::Str(item) = item {222 if !first {223 out += &sep;224 }225 first = false;226 write!(out, "{item}").unwrap();227 } else if matches!(item, Val::Null) {228 } else {229 bail!("in std.join all items should be strings");230 }231 }232233 IndexableVal::Str(out.into())234 }235 })236}237238#[builtin]239pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {240 builtin_join(241 IndexableVal::Str("\n".into()),242 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),243 )244}245246#[builtin]247pub fn builtin_resolve_path(f: String, r: String) -> String {248 let Some(pos) = f.rfind('/') else {249 return r;250 };251 format!("{}{}", &f[..=pos], r)252}253254pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {255 use std::fmt::Write;256 match arr {257 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),258 IndexableVal::Arr(arr) => {259 for ele in arr.iter() {260 let indexable = IndexableVal::from_untyped(ele?)?;261 deep_join_inner(out, indexable)?;262 }263 }264 }265 Ok(())266}267268#[builtin]269pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {270 let mut out = String::new();271 deep_join_inner(&mut out, arr)?;272 Ok(out)273}274275#[builtin]276pub fn builtin_reverse(arr: ArrValue) -> ArrValue {277 arr.reversed()278}279280#[builtin]281pub fn builtin_any(arr: ArrValue) -> Result<bool> {282 for v in arr.iter() {283 let v = bool::from_untyped(v?)?;284 if v {285 return Ok(true);286 }287 }288 Ok(false)289}290291#[builtin]292pub fn builtin_all(arr: ArrValue) -> Result<bool> {293 for v in arr.iter() {294 let v = bool::from_untyped(v?)?;295 if !v {296 return Ok(false);297 }298 }299 Ok(true)300}301302#[builtin]303pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {304 match arr {305 IndexableVal::Str(str) => {306 let x: IStr = IStr::from_untyped(x)?;307 Ok(!x.is_empty() && str.contains(&*x))308 }309 IndexableVal::Arr(a) => {310 for item in a.iter() {311 let item = item?;312 if equals(&item, &x)? {313 return Ok(true);314 }315 }316 Ok(false)317 }318 }319}320321#[builtin]322pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {323 let mut out = Vec::new();324 for (i, ele) in arr.iter().enumerate() {325 let ele = ele?;326 if equals(&ele, &value)? {327 out.push(i);328 }329 }330 Ok(out)331}332333#[builtin]334pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {335 builtin_member(arr, elem)336}337338#[builtin]339pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {340 let mut count = 0;341 for item in arr.iter() {342 if equals(&item?, &x)? {343 count += 1;344 }345 }346 Ok(count)347}348349#[builtin]350pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {351 if arr.is_empty() {352 return eval_on_empty(onEmpty);353 }354 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)355}356357#[builtin]358pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {359 let newArrLeft = arr.clone().slice(None, Some(at), None);360 let newArrRight = arr.slice(Some(at + 1), None, None);361362 Ok(ArrValue::extended(newArrLeft, newArrRight))363}364365#[builtin]366pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {367 for (index, item) in arr.iter().enumerate() {368 if equals(&item?, &elem)? {369 return builtin_remove_at(arr.clone(), index as i32);370 }371 }372 Ok(arr)373}374375#[builtin]376pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {377 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {378 if values.len() == 1 {379 return values[0].clone();380 } else if values.len() == 2 {381 return ArrValue::extended(values[0].clone(), values[1].clone());382 }383 let (a, b) = values.split_at(values.len() / 2);384 ArrValue::extended(flatten_inner(a), flatten_inner(b))385 }386 if arrs.is_empty() {387 return ArrValue::empty();388 } else if arrs.len() == 1 {389 return arrs.into_iter().next().expect("single");390 }391 flatten_inner(&arrs)392}393394#[builtin]395pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {396 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {397 match value {398 Val::Arr(arr) => {399 for ele in arr.iter() {400 process(ele?, out)?;401 }402 }403 _ => out.push(value),404 }405 Ok(())406 }407 let mut out = Vec::new();408 process(value, &mut out)?;409 Ok(out)410}411412#[builtin]413pub fn builtin_prune(414 a: Val,415416 #[default(false)]417 #[cfg(feature = "exp-preserve-order")]418 preserve_order: bool,419) -> Result<Val> {420 fn is_content(val: &Val) -> bool {421 match val {422 Val::Null => false,423 Val::Arr(a) => !a.is_empty(),424 Val::Obj(o) => !o.is_empty(),425 _ => true,426 }427 }428 Ok(match a {429 Val::Arr(a) => {430 let mut out = Vec::new();431 for (i, ele) in a.iter().enumerate() {432 let ele = ele433 .and_then(|v| {434 builtin_prune(435 v,436 #[cfg(feature = "exp-preserve-order")]437 preserve_order,438 )439 })440 .with_description(|| format!("elem <{i}> pruning"))?;441 if is_content(&ele) {442 out.push(ele);443 }444 }445 Val::Arr(ArrValue::eager(out))446 }447 Val::Obj(o) => {448 let mut out = ObjValueBuilder::new();449 for (name, value) in o.iter(450 #[cfg(feature = "exp-preserve-order")]451 preserve_order,452 ) {453 let value = value454 .and_then(|v| {455 builtin_prune(456 v,457 #[cfg(feature = "exp-preserve-order")]458 preserve_order,459 )460 })461 .with_description(|| format!("field <{name}> pruning"))?;462 if !is_content(&value) {463 continue;464 }465 out.field(name).value(value);466 }467 Val::Obj(out.build())468 }469 _ => a,470 })471}1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4 bail,5 function::{builtin, FuncVal},6 runtime_error,7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8 val::{equals, ArrValue, IndexableVal},9 Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13 if let Some(on_empty) = on_empty {14 on_empty.evaluate()15 } else {16 bail!("expected non-empty array")17 }18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22 if *sz == 0 {23 return Ok(ArrValue::empty());24 }25 func.evaluate_trivial().map_or_else(26 // TODO: Different mapped array impl avoiding allocating unnecessary vals27 || Ok(ArrValue::range_exclusive(0, *sz).map(Typed::from_untyped(Val::Func(func))?)),28 |trivial| {29 let mut out = Vec::with_capacity(*sz as usize);30 for _ in 0..*sz {31 out.push(trivial.clone());32 }33 Ok(ArrValue::eager(out))34 },35 )36}3738#[builtin]39pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {40 Ok(match what {41 Either2::A(s) => Val::string(s.repeat(count)),42 Either2::B(arr) => Val::Arr(43 ArrValue::repeated(arr, count)44 .ok_or_else(|| runtime_error!("repeated length overflow"))?,45 ),46 })47}4849#[builtin]50pub fn builtin_slice(51 indexable: IndexableVal,52 index: Option<Option<i32>>,53 end: Option<Option<i32>>,54 step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,55) -> Result<Val> {56 indexable57 .slice(index.flatten(), end.flatten(), step.flatten())58 .map(Val::from)59}6061#[builtin]62pub fn builtin_map(func: NativeFn!((Val) -> Val), arr: IndexableVal) -> ArrValue {63 let arr = arr.to_array();64 arr.map(func)65}6667#[builtin]68pub fn builtin_map_with_index(func: NativeFn!((u32, Val) -> Val), arr: IndexableVal) -> ArrValue {69 let arr = arr.to_array();70 arr.map_with_index(func)71}7273#[builtin]74pub fn builtin_map_with_key(75 func: NativeFn!((IStr, Val) -> Val),76 obj: ObjValue,77) -> Result<ObjValue> {78 let mut out = ObjValueBuilder::new();79 for (k, v) in obj.iter(80 // Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).81 // The thrown error might be different, but jsonnet82 // does not specify the evaluation order.83 #[cfg(feature = "exp-preserve-order")]84 true,85 ) {86 let v = v?;87 out.field(k.clone()).value(func.call(k, v)?);88 }89 Ok(out.build())90}9192#[builtin]93pub fn builtin_flatmap(94 func: NativeFn!((Either![String, Val]) -> Val),95 arr: IndexableVal,96) -> Result<IndexableVal> {97 use std::fmt::Write;98 match arr {99 IndexableVal::Str(str) => {100 let mut out = String::new();101 for c in str.chars() {102 match func.call(Either2::A(c.to_string()))? {103 Val::Str(o) => write!(out, "{o}").unwrap(),104 Val::Null => {}105 _ => bail!("in std.join all items should be strings"),106 }107 }108 Ok(IndexableVal::Str(out.into()))109 }110 IndexableVal::Arr(a) => {111 let mut out = Vec::new();112 for el in a.iter() {113 let el = el?;114 match func.call(Either2::B(el))? {115 Val::Arr(o) => {116 for oe in o.iter() {117 out.push(oe?);118 }119 }120 Val::Null => {}121 _ => bail!("in std.join all items should be arrays"),122 }123 }124 Ok(IndexableVal::Arr(out.into()))125 }126 }127}128129type FilterFunc = NativeFn!((Val) -> bool);130131#[builtin]132pub fn builtin_filter(func: FilterFunc, arr: ArrValue) -> Result<ArrValue> {133 arr.filter(|val| func.call(val.clone()))134}135136#[builtin]137pub fn builtin_filter_map(138 filter_func: FilterFunc,139 map_func: NativeFn!((Val) -> Val),140 arr: ArrValue,141) -> Result<ArrValue> {142 Ok(builtin_filter(filter_func, arr)?.map(map_func))143}144145#[builtin]146pub fn builtin_foldl(147 func: NativeFn!((Val, Either![Val, char]) -> Val),148 arr: Either![ArrValue, IStr],149 init: Val,150) -> Result<Val> {151 let mut acc = init;152 match arr {153 Either2::A(arr) => {154 for i in arr.iter() {155 acc = func.call(acc, Either2::A(i?))?;156 }157 }158 Either2::B(arr) => {159 for c in arr.chars() {160 acc = func.call(acc, Either2::B(c))?;161 }162 }163 }164 Ok(acc)165}166167#[builtin]168pub fn builtin_foldr(169 func: NativeFn!((Either![Val, char], Val) -> Val),170 arr: Either![ArrValue, IStr],171 init: Val,172) -> Result<Val> {173 let mut acc = init;174 match arr {175 Either2::A(arr) => {176 for i in arr.iter().rev() {177 acc = func.call(Either2::A(i?), acc)?;178 }179 }180 Either2::B(arr) => {181 for c in arr.chars().rev() {182 acc = func.call(Either2::B(c), acc)?;183 }184 }185 }186 Ok(acc)187}188189#[builtin]190pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {191 if to < from {192 return Ok(ArrValue::empty());193 }194 Ok(ArrValue::range_inclusive(from, to))195}196197#[builtin]198pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {199 use std::fmt::Write;200 Ok(match sep {201 IndexableVal::Arr(joiner_items) => {202 let mut out = Vec::new();203204 let mut first = true;205 for item in arr.iter() {206 let item = item?.clone();207 if let Val::Arr(items) = item {208 if !first {209 out.reserve(joiner_items.len());210 // TODO: extend211 for item in joiner_items.iter() {212 out.push(item?);213 }214 }215 first = false;216 out.reserve(items.len());217 for item in items.iter() {218 out.push(item?);219 }220 } else if matches!(item, Val::Null) {221 } else {222 bail!("in std.join all items should be arrays");223 }224 }225226 IndexableVal::Arr(out.into())227 }228 IndexableVal::Str(sep) => {229 let mut out = String::new();230231 let mut first = true;232 for item in arr.iter() {233 let item = item?.clone();234 if let Val::Str(item) = item {235 if !first {236 out += &sep;237 }238 first = false;239 write!(out, "{item}").unwrap();240 } else if matches!(item, Val::Null) {241 } else {242 bail!("in std.join all items should be strings");243 }244 }245246 IndexableVal::Str(out.into())247 }248 })249}250251#[builtin]252pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {253 builtin_join(254 IndexableVal::Str("\n".into()),255 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),256 )257}258259#[builtin]260pub fn builtin_resolve_path(f: String, r: String) -> String {261 let Some(pos) = f.rfind('/') else {262 return r;263 };264 format!("{}{}", &f[..=pos], r)265}266267pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {268 use std::fmt::Write;269 match arr {270 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),271 IndexableVal::Arr(arr) => {272 for ele in arr.iter() {273 let indexable = IndexableVal::from_untyped(ele?)?;274 deep_join_inner(out, indexable)?;275 }276 }277 }278 Ok(())279}280281#[builtin]282pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {283 let mut out = String::new();284 deep_join_inner(&mut out, arr)?;285 Ok(out)286}287288#[builtin]289pub fn builtin_reverse(arr: ArrValue) -> ArrValue {290 arr.reversed()291}292293#[builtin]294pub fn builtin_any(arr: ArrValue) -> Result<bool> {295 for v in arr.iter() {296 let v = bool::from_untyped(v?)?;297 if v {298 return Ok(true);299 }300 }301 Ok(false)302}303304#[builtin]305pub fn builtin_all(arr: ArrValue) -> Result<bool> {306 for v in arr.iter() {307 let v = bool::from_untyped(v?)?;308 if !v {309 return Ok(false);310 }311 }312 Ok(true)313}314315#[builtin]316pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {317 match arr {318 IndexableVal::Str(str) => {319 let x: IStr = IStr::from_untyped(x)?;320 Ok(!x.is_empty() && str.contains(&*x))321 }322 IndexableVal::Arr(a) => {323 for item in a.iter() {324 let item = item?;325 if equals(&item, &x)? {326 return Ok(true);327 }328 }329 Ok(false)330 }331 }332}333334#[builtin]335pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {336 let mut out = Vec::new();337 for (i, ele) in arr.iter().enumerate() {338 let ele = ele?;339 if equals(&ele, &value)? {340 out.push(i);341 }342 }343 Ok(out)344}345346#[builtin]347pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {348 builtin_member(arr, elem)349}350351#[builtin]352pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {353 let mut count = 0;354 for item in arr.iter() {355 if equals(&item?, &x)? {356 count += 1;357 }358 }359 Ok(count)360}361362#[builtin]363pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {364 if arr.is_empty() {365 return eval_on_empty(onEmpty);366 }367 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)368}369370#[builtin]371pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {372 let newArrLeft = arr.clone().slice(None, Some(at), None);373 let newArrRight = arr.slice(Some(at + 1), None, None);374375 Ok(ArrValue::extended(newArrLeft, newArrRight))376}377378#[builtin]379pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {380 for (index, item) in arr.iter().enumerate() {381 if equals(&item?, &elem)? {382 return builtin_remove_at(arr.clone(), index as i32);383 }384 }385 Ok(arr)386}387388#[builtin]389pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {390 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {391 if values.len() == 1 {392 return values[0].clone();393 } else if values.len() == 2 {394 return ArrValue::extended(values[0].clone(), values[1].clone());395 }396 let (a, b) = values.split_at(values.len() / 2);397 ArrValue::extended(flatten_inner(a), flatten_inner(b))398 }399 if arrs.is_empty() {400 return ArrValue::empty();401 } else if arrs.len() == 1 {402 return arrs.into_iter().next().expect("single");403 }404 flatten_inner(&arrs)405}406407#[builtin]408pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {409 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {410 match value {411 Val::Arr(arr) => {412 for ele in arr.iter() {413 process(ele?, out)?;414 }415 }416 _ => out.push(value),417 }418 Ok(())419 }420 let mut out = Vec::new();421 process(value, &mut out)?;422 Ok(out)423}424425#[builtin]426pub fn builtin_prune(427 a: Val,428429 #[default(false)]430 #[cfg(feature = "exp-preserve-order")]431 preserve_order: bool,432) -> Result<Val> {433 fn is_content(val: &Val) -> bool {434 match val {435 Val::Null => false,436 Val::Arr(a) => !a.is_empty(),437 Val::Obj(o) => !o.is_empty(),438 _ => true,439 }440 }441 Ok(match a {442 Val::Arr(a) => {443 let mut out = Vec::new();444 for (i, ele) in a.iter().enumerate() {445 let ele = ele446 .and_then(|v| {447 builtin_prune(448 v,449 #[cfg(feature = "exp-preserve-order")]450 preserve_order,451 )452 })453 .with_description(|| format!("elem <{i}> pruning"))?;454 if is_content(&ele) {455 out.push(ele);456 }457 }458 Val::Arr(ArrValue::eager(out))459 }460 Val::Obj(o) => {461 let mut out = ObjValueBuilder::new();462 for (name, value) in o.iter(463 #[cfg(feature = "exp-preserve-order")]464 preserve_order,465 ) {466 let value = value467 .and_then(|v| {468 builtin_prune(469 v,470 #[cfg(feature = "exp-preserve-order")]471 preserve_order,472 )473 })474 .with_description(|| format!("field <{name}> pruning"))?;475 if !is_content(&value) {476 continue;477 }478 out.field(name).value(value);479 }480 Val::Obj(out.build())481 }482 _ => a,483 })484}crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -38,6 +38,7 @@
mod compat;
mod encoding;
mod hash;
+mod keyf;
mod manifest;
mod math;
mod misc;
@@ -50,7 +51,6 @@
mod sort;
mod strings;
mod types;
-mod keyf;
#[allow(clippy::too_many_lines)]
pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {
tests/tests/as_native.rsdiffbeforeafterboth--- a/tests/tests/as_native.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-use jrsonnet_evaluator::{FileImportResolver, Result, State, trace::PathResolver};
-use jrsonnet_stdlib::ContextInitializer;
-
-mod common;
-
-#[test]
-fn as_native() -> Result<()> {
- let mut s = State::builder();
- s.context_initializer(ContextInitializer::new(PathResolver::new_cwd_fallback()))
- .import_resolver(FileImportResolver::default());
- let s = s.build();
-
- let val = s.evaluate_snippet("snip".to_owned(), r"function(a, b) a + b")?;
- let func = val.as_func().expect("this is function");
-
- let native = func.into_native::<((u32, u32), u32)>();
-
- ensure_eq!(native(1, 2)?, 3);
- ensure_eq!(native(3, 4)?, 7);
-
- Ok(())
-}