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.rsdiffbeforeafterboth1use std::string::String;23use proc_macro2::TokenStream;4use quote::{quote, quote_spanned};5use syn::{6 parenthesized,7 parse::{Parse, ParseStream},8 parse_macro_input,9 punctuated::Punctuated,10 spanned::Spanned,11 token::{self, Comma},12 Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,13 LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,14};1516fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>17where18 Ident: PartialEq<I>,19{20 let attrs = attrs21 .iter()22 .filter(|a| a.path().is_ident(&ident))23 .collect::<Vec<_>>();24 if attrs.len() > 1 {25 return Err(Error::new(26 attrs[1].span(),27 "this attribute may be specified only once",28 ));29 } else if attrs.is_empty() {30 return Ok(false);31 }32 let attr = attrs[0];3334 match attr.meta {35 Meta::Path(_) => Ok(true),36 _ => Ok(false),37 }38}39fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>40where41 Ident: PartialEq<I>,42{43 let attrs = attrs44 .iter()45 .filter(|a| a.path().is_ident(&ident))46 .collect::<Vec<_>>();47 if attrs.len() > 1 {48 return Err(Error::new(49 attrs[1].span(),50 "this attribute may be specified only once",51 ));52 } else if attrs.is_empty() {53 return Ok(None);54 }55 let attr = attrs[0];56 let attr = attr.parse_args::<A>()?;5758 Ok(Some(attr))59}60fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)61where62 Ident: PartialEq<I>,63{64 attrs.retain(|a| !a.path().is_ident(&ident));65}6667fn path_is(path: &Path, needed: &str) -> bool {68 path.leading_colon.is_none()69 && !path.segments.is_empty()70 && path.segments.iter().last().unwrap().ident == needed71}7273fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {74 match ty {75 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {76 let args = &path.path.segments.iter().last().unwrap().arguments;77 Some(args)78 }79 _ => None,80 }81}8283fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {84 let Some(args) = type_is_path(ty, "Option") else {85 return Ok(None);86 };87 // It should have only on angle-bracketed param ("<String>"):88 let PathArguments::AngleBracketed(params) = args else {89 return Err(Error::new(args.span(), "missing option generic"));90 };91 let generic_arg = params.args.iter().next().unwrap();92 // This argument must be a type:93 let GenericArgument::Type(ty) = generic_arg else {94 return Err(Error::new(95 generic_arg.span(),96 "option generic should be a type",97 ));98 };99 Ok(Some(ty))100}101102struct Field {103 attrs: Vec<Attribute>,104 name: Ident,105 _colon: Token![:],106 ty: Type,107}108impl Parse for Field {109 fn parse(input: ParseStream) -> syn::Result<Self> {110 Ok(Self {111 attrs: input.call(Attribute::parse_outer)?,112 name: input.parse()?,113 _colon: input.parse()?,114 ty: input.parse()?,115 })116 }117}118119mod kw {120 syn::custom_keyword!(fields);121 syn::custom_keyword!(rename);122 syn::custom_keyword!(alias);123 syn::custom_keyword!(flatten);124 syn::custom_keyword!(add);125 syn::custom_keyword!(hide);126 syn::custom_keyword!(ok);127}128129struct BuiltinAttrs {130 fields: Vec<Field>,131}132impl Parse for BuiltinAttrs {133 fn parse(input: ParseStream) -> syn::Result<Self> {134 if input.is_empty() {135 return Ok(Self { fields: Vec::new() });136 }137 input.parse::<kw::fields>()?;138 let fields;139 parenthesized!(fields in input);140 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;141 Ok(Self {142 fields: p.into_iter().collect(),143 })144 }145}146147enum Optionality {148 Required,149 Optional,150 Default(Expr),151 TypeDefault,152}153154#[allow(155 clippy::large_enum_variant,156 reason = "this macro is not that hot for it to matter"157)]158enum ArgInfo {159 Normal {160 ty: Box<Type>,161 optionality: Optionality,162 name: Option<String>,163 cfg_attrs: Vec<Attribute>,164 },165 Lazy {166 is_option: bool,167 name: Option<String>,168 },169 Context,170 Location,171 This,172}173174impl ArgInfo {175 fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {176 let FnArg::Typed(arg) = arg else {177 unreachable!()178 };179 let ident = match &arg.pat as &Pat {180 Pat::Ident(i) => Some(i.ident.clone()),181 _ => None,182 };183 let ty = &arg.ty;184 if type_is_path(ty, "Context").is_some() {185 return Ok(Self::Context);186 } else if type_is_path(ty, "CallLocation").is_some() {187 return Ok(Self::Location);188 } else if type_is_path(ty, "Thunk").is_some() {189 return Ok(Self::Lazy {190 is_option: false,191 name: ident.map(|v| v.to_string()),192 });193 }194195 match ty as &Type {196 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),197 _ => {}198 }199200 let (optionality, ty) = if try_parse_attr_noargs(&mut arg.attrs, "default")? {201 remove_attr(&mut arg.attrs, "default");202 (Optionality::TypeDefault, ty.clone())203 } else if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {204 remove_attr(&mut arg.attrs, "default");205 (Optionality::Default(default), ty.clone())206 } else if let Some(ty) = extract_type_from_option(ty)? {207 if type_is_path(ty, "Thunk").is_some() {208 return Ok(Self::Lazy {209 is_option: true,210 name: ident.map(|v| v.to_string()),211 });212 }213214 (Optionality::Optional, Box::new(ty.clone()))215 } else {216 (Optionality::Required, ty.clone())217 };218219 let cfg_attrs = arg220 .attrs221 .iter()222 .filter(|a| a.path().is_ident("cfg"))223 .cloned()224 .collect();225226 Ok(Self::Normal {227 ty,228 optionality,229 name: ident.map(|v| v.to_string()),230 cfg_attrs,231 })232 }233}234235#[proc_macro_attribute]236pub fn builtin(237 attr: proc_macro::TokenStream,238 item: proc_macro::TokenStream,239) -> proc_macro::TokenStream {240 let attr = parse_macro_input!(attr as BuiltinAttrs);241 let item_fn = parse_macro_input!(item as ItemFn);242243 match builtin_inner(attr, item_fn) {244 Ok(v) => v.into(),245 Err(e) => e.into_compile_error().into(),246 }247}248249#[allow(clippy::too_many_lines)]250fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {251 let ReturnType::Type(_, result) = &fun.sig.output else {252 return Err(Error::new(253 fun.sig.span(),254 "builtin should return something",255 ));256 };257258 let name = fun.sig.ident.to_string();259 let args = fun260 .sig261 .inputs262 .iter_mut()263 .map(|arg| ArgInfo::parse(&name, arg))264 .collect::<Result<Vec<_>>>()?;265266 let params_desc = args.iter().filter_map(|a| match a {267 ArgInfo::Normal {268 optionality,269 name,270 cfg_attrs,271 ..272 } => {273 let name = name274 .as_ref()275 .map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});276 let default = match optionality {277 Optionality::Required => quote!(ParamDefault::None),278 Optionality::Optional | Optionality::TypeDefault => quote!(ParamDefault::Exists),279 Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),280 };281 Some(quote! {282 #(#cfg_attrs)*283 [#name => #default],284 })285 }286 ArgInfo::Lazy { is_option, name } => {287 let name = name288 .as_ref()289 .map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});290 Some(quote! {291 [#name => ParamDefault::exists(#is_option)],292 })293 }294 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,295 });296297 let mut id = 0usize;298 let pass = args299 .iter()300 .map(|a| match a {301 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {302 let cid = id;303 id += 1;304 (quote! {#cid}, a)305 }306 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {307 (quote! {compile_error!("should not use id")}, a)308 }309 })310 .map(|(id, a)| match a {311 ArgInfo::Normal {312 ty,313 optionality,314 name,315 cfg_attrs,316 } => {317 let name = name.as_ref().map_or("<unnamed>", String::as_str);318 let eval = quote! {jrsonnet_evaluator::in_description_frame(319 || format!("argument <{}> evaluation", #name),320 || <#ty>::from_untyped(value.evaluate()?),321 )?};322 let value = match optionality {323 Optionality::Required => quote! {{324 let value = parsed[#id].as_ref().expect("args shape is checked");325 #eval326 },},327 Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {328 Some(#eval)329 } else {330 None331 },},332 Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {333 #eval334 } else {335 let v: #ty = #expr;336 v337 },},338 Optionality::TypeDefault => quote! {if let Some(value) = &parsed[#id] {339 #eval340 } else {341 let v: #ty = Default::default();342 v343 },},344 };345 quote! {346 #(#cfg_attrs)*347 #value348 }349 }350 ArgInfo::Lazy { is_option, .. } => {351 if *is_option {352 quote! {if let Some(value) = &parsed[#id] {353 Some(value.clone())354 } else {355 None356 },}357 } else {358 quote! {359 parsed[#id].as_ref().expect("args shape is correct").clone(),360 }361 }362 }363 ArgInfo::Context => quote! {ctx.clone(),},364 ArgInfo::Location => quote! {location,},365 ArgInfo::This => quote! {self,},366 });367368 let fields = attr.fields.iter().map(|field| {369 let attrs = &field.attrs;370 let name = &field.name;371 let ty = &field.ty;372 quote! {373 #(#attrs)*374 pub #name: #ty,375 }376 });377378 let name = &fun.sig.ident;379 let vis = &fun.vis;380 let static_ext = if attr.fields.is_empty() {381 quote! {382 impl #name {383 pub const INST: &'static dyn StaticBuiltin = &#name {};384 }385 impl StaticBuiltin for #name {}386 }387 } else {388 quote! {}389 };390 let static_derive_copy = if attr.fields.is_empty() {391 quote! {, Copy}392 } else {393 quote! {}394 };395396 Ok(quote! {397 #fun398399 #[doc(hidden)]400 #[allow(non_camel_case_types)]401 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]402 #vis struct #name {403 #(#fields)*404 }405 const _: () = {406 use ::jrsonnet_evaluator::{407 State, Val,408 function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation, ArgsLike, parse::parse_builtin_call},409 Result, Context, typed::Typed,410 parser::Span, params, Thunk,411 };412 params!(413 #(#params_desc)*414 );415416 #static_ext417 impl Builtin for #name418 where419 Self: 'static420 {421 fn name(&self) -> &str {422 stringify!(#name)423 }424 fn params(&self) -> FunctionSignature {425 PARAMS.with(|p| p.clone())426 }427 #[allow(unused_variables)]428 fn call(&self, location: CallLocation<'_>, parsed: &[Option<Thunk<Val>>]) -> Result<Val> {429 let result: #result = #name(#(#pass)*);430 <_ as Typed>::into_result(result)431 }432 fn as_any(&self) -> &dyn ::std::any::Any {433 self434 }435 }436 };437 })438}439440#[derive(Default)]441#[allow(clippy::struct_excessive_bools)]442struct TypedAttr {443 rename: Option<String>,444 aliases: Vec<String>,445 flatten: bool,446 /// flatten(ok) strategy for flattened optionals447 /// field would be None in case of any parsing error (as in serde)448 flatten_ok: bool,449 // Should it be `field+:` instead of `field:`450 add: bool,451 // Should it be `field::` instead of `field:`452 hide: bool,453}454impl Parse for TypedAttr {455 fn parse(input: ParseStream) -> syn::Result<Self> {456 let mut out = Self::default();457 loop {458 let lookahead = input.lookahead1();459 if lookahead.peek(kw::rename) {460 input.parse::<kw::rename>()?;461 input.parse::<Token![=]>()?;462 let name = input.parse::<LitStr>()?;463 if out.rename.is_some() {464 return Err(Error::new(465 name.span(),466 "rename attribute may only be specified once",467 ));468 }469 out.rename = Some(name.value());470 } else if lookahead.peek(kw::alias) {471 input.parse::<kw::alias>()?;472 input.parse::<Token![=]>()?;473 let alias = input.parse::<LitStr>()?;474 out.aliases.push(alias.value());475 } else if lookahead.peek(kw::flatten) {476 input.parse::<kw::flatten>()?;477 out.flatten = true;478 if input.peek(token::Paren) {479 let content;480 parenthesized!(content in input);481 let lookahead = content.lookahead1();482 if lookahead.peek(kw::ok) {483 content.parse::<kw::ok>()?;484 out.flatten_ok = true;485 } else {486 return Err(lookahead.error());487 }488 }489 } else if lookahead.peek(kw::add) {490 input.parse::<kw::add>()?;491 out.add = true;492 } else if lookahead.peek(kw::hide) {493 input.parse::<kw::hide>()?;494 out.hide = true;495 } else if input.is_empty() {496 break;497 } else {498 return Err(lookahead.error());499 }500 if input.peek(Token![,]) {501 input.parse::<Token![,]>()?;502 } else {503 break;504 }505 }506 Ok(out)507 }508}509510struct TypedField {511 attr: TypedAttr,512 ident: Ident,513 ty: Type,514 is_option: bool,515 is_lazy: bool,516}517impl TypedField {518 fn parse(field: &syn::Field) -> Result<Self> {519 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();520 let Some(ident) = field.ident.clone() else {521 return Err(Error::new(522 field.span(),523 "this field should appear in output object, but it has no visible name",524 ));525 };526 let (is_option, ty) = extract_type_from_option(&field.ty)?527 .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));528 if is_option && attr.flatten {529 if !attr.flatten_ok {530 return Err(Error::new(531 field.span(),532 "strategy should be set when flattening Option",533 ));534 }535 } else if attr.flatten_ok {536 return Err(Error::new(537 field.span(),538 "flatten(ok) is only useable on optional fields",539 ));540 }541542 let is_lazy = type_is_path(&ty, "Thunk").is_some();543544 Ok(Self {545 attr,546 ident,547 ty,548 is_option,549 is_lazy,550 })551 }552 /// None if this field is flattened in jsonnet output553 fn name(&self) -> Option<String> {554 if self.attr.flatten {555 return None;556 }557 Some(558 self.attr559 .rename560 .clone()561 .unwrap_or_else(|| self.ident.to_string()),562 )563 }564565 fn expand_field(&self) -> Option<TokenStream> {566 if self.is_option {567 return None;568 }569 let name = self.name()?;570 let ty = &self.ty;571 Some(quote! {572 (#name, <#ty as Typed>::TYPE)573 })574 }575576 fn expand_parse(&self) -> TokenStream {577 if self.is_option {578 self.expand_parse_optional()579 } else {580 self.expand_parse_mandatory()581 }582 }583584 fn expand_parse_optional(&self) -> TokenStream {585 let ident = &self.ident;586 let ty = &self.ty;587588 // optional flatten is handled in same way as serde589 if self.attr.flatten {590 return quote! {591 #ident: <#ty as TypedObj>::parse(&obj).ok(),592 };593 }594595 let name = self.name().unwrap();596 let aliases = &self.attr.aliases;597598 quote! {599 #ident: {600 let __value = if let Some(__v) = obj.get(#name.into())? {601 Some(__v)602 } #(else if let Some(__v) = obj.get(#aliases.into())? {603 Some(__v)604 })* else {605 None606 };607608 __value.map(<#ty as Typed>::from_untyped).transpose()?609 },610 }611 }612613 fn expand_parse_mandatory(&self) -> TokenStream {614 let ident = &self.ident;615 let ty = &self.ty;616617 // optional flatten is handled in same way as serde618 if self.attr.flatten {619 return quote! {620 #ident: <#ty as TypedObj>::parse(&obj)?,621 };622 }623624 let name = self.name().unwrap();625 let aliases = &self.attr.aliases;626627 let error_text = if aliases.is_empty() {628 // clippy does not understand name variable usage in quote! macro629 #[allow(clippy::redundant_clone)]630 name.clone()631 } else {632 format!("{name} (alias {})", aliases.join(", "))633 };634635 quote! {636 #ident: {637 let __value = if let Some(__v) = obj.get(#name.into())? {638 __v639 } #(else if let Some(__v) = obj.get(#aliases.into())? {640 __v641 })* else {642 return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());643 };644645 <#ty as Typed>::from_untyped(__value)?646 },647 }648 }649650 fn expand_serialize(&self) -> TokenStream {651 let ident = &self.ident;652 let ty = &self.ty;653 self.name().map_or_else(654 || {655 if self.is_option {656 quote! {657 if let Some(value) = self.#ident {658 <#ty as TypedObj>::serialize(value, out)?;659 }660 }661 } else {662 quote! {663 <#ty as TypedObj>::serialize(self.#ident, out)?;664 }665 }666 },667 |name| {668 let hide = if self.attr.hide {669 quote! {.hide()}670 } else {671 quote! {}672 };673 let add = if self.attr.add {674 quote! {.add()}675 } else {676 quote! {}677 };678 let value = if self.is_lazy {679 quote! {680 out.field(#name)681 #hide682 #add683 .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;684 }685 } else {686 quote! {687 out.field(#name)688 #hide689 #add690 .try_value(<#ty as Typed>::into_untyped(value)?)?;691 }692 };693 if self.is_option {694 quote! {695 if let Some(value) = self.#ident {696 #value697 }698 }699 } else {700 quote! {701 {702 let value = self.#ident;703 #value704 }705 }706 }707 },708 )709 }710}711712#[proc_macro_derive(Typed, attributes(typed))]713pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {714 let input = parse_macro_input!(item as DeriveInput);715716 match derive_typed_inner(input) {717 Ok(v) => v.into(),718 Err(e) => e.to_compile_error().into(),719 }720}721722fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {723 let syn::Data::Struct(data) = &input.data else {724 return Err(Error::new(input.span(), "only structs supported"));725 };726727 let ident = &input.ident;728 let fields = data729 .fields730 .iter()731 .map(TypedField::parse)732 .collect::<Result<Vec<_>>>()?;733734 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();735736 let typed = {737 let fields = fields738 .iter()739 .filter_map(TypedField::expand_field)740 .collect::<Vec<_>>();741 quote! {742 impl #impl_generics Typed for #ident #ty_generics #where_clause {743 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[744 #(#fields,)*745 ]);746747 fn from_untyped(value: Val) -> JrResult<Self> {748 let obj = value.as_obj().expect("shape is correct");749 Self::parse(&obj)750 }751752 fn into_untyped(value: Self) -> JrResult<Val> {753 let mut out = ObjValueBuilder::new();754 value.serialize(&mut out)?;755 Ok(Val::Obj(out.build()))756 }757758 }759 }760 };761762 let fields_parse = fields.iter().map(TypedField::expand_parse);763 let fields_serialize = fields764 .iter()765 .map(TypedField::expand_serialize)766 .collect::<Vec<_>>();767768 Ok(quote! {769 const _: () = {770 use ::jrsonnet_evaluator::{771 typed::{ComplexValType, Typed, TypedObj, CheckType},772 Val, State,773 error::{ErrorKind, Result as JrResult},774 ObjValueBuilder, ObjValue,775 };776777 #typed778779 impl #impl_generics TypedObj for #ident #ty_generics #where_clause {780 fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {781 #(#fields_serialize)*782783 Ok(())784 }785 fn parse(obj: &ObjValue) -> JrResult<Self> {786 Ok(Self {787 #(#fields_parse)*788 })789 }790 }791 };792 })793}794795struct FormatInput {796 formatting: LitStr,797 arguments: Vec<Expr>,798}799impl Parse for FormatInput {800 fn parse(input: ParseStream) -> Result<Self> {801 let formatting = input.parse()?;802 let mut arguments = Vec::new();803804 while input.peek(Token![,]) {805 input.parse::<Token![,]>()?;806 if input.is_empty() {807 // Trailing comma808 break;809 }810 let expr = input.parse()?;811 arguments.push(expr);812 }813814 if !input.is_empty() {815 return Err(syn::Error::new(input.span(), "unexpected trailing input"));816 }817818 Ok(Self {819 formatting,820 arguments,821 })822 }823}824fn is_format_str(i: &str) -> bool {825 let mut is_plain = true;826 // -1 = {827 // +1 = }828 let mut is_bracket = 0i8;829 for ele in i.chars() {830 match ele {831 '{' if is_bracket == -1 => {832 is_bracket = 0;833 }834 '}' if is_bracket == -1 => {835 is_plain = false;836 break;837 }838 '}' if is_bracket == 1 => {839 is_bracket = 0;840 }841 '{' if is_bracket == 1 => {842 is_plain = false;843 break;844 }845 '{' => {846 is_bracket = -1;847 }848 '}' => {849 is_bracket = 1;850 }851 _ if is_bracket != 0 => {852 is_plain = false;853 break;854 }855 _ => {}856 }857 }858 !is_plain || is_bracket != 0859}860impl FormatInput {861 fn expand(self) -> TokenStream {862 let format = self.formatting;863 if is_format_str(&format.value()) {864 let args = self.arguments;865 quote! {866 ::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))867 }868 } else {869 if let Some(first) = self.arguments.first() {870 return syn::Error::new(871 first.span(),872 "string has no formatting codes, it should not have the arguments",873 )874 .into_compile_error();875 }876 quote! {877 ::jrsonnet_evaluator::IStr::from(#format)878 }879 }880 }881}882883/// `IStr` formatting helper884///885/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`886/// This macro looks for formatting codes in the input string, and uses887/// `format!()` only when necessary888#[proc_macro]889pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {890 let input = parse_macro_input!(input as FormatInput);891 input.expand().into()892}893894/// Create Thunk using closure syntax895#[proc_macro]896#[allow(non_snake_case)]897pub fn Thunk(input: proc_macro::TokenStream) -> proc_macro::TokenStream {898 let input = parse_macro_input!(input as ExprClosure);899900 let span = input.inputs.span();901 let move_check = input.capture.is_none().then(|| {902 quote_spanned! {span => {903 compile_error!("Thunk! needs to be called with move closure");904 }}905 });906907 let (env, closure, args) = syn_dissect_closure::split_env(input);908909 let trace_check = args.iter().map(|el| {910 let span = el.span();911 quote_spanned! {span => ::jrsonnet_evaluator::gc::assert_trace(&#el);}912 });913914 quote! {{915 #move_check916 #(#trace_check)*917 ::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::MemoizedClosureThunk::new(#env, #closure))918 }}.into()919}crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -23,7 +23,8 @@
return Ok(ArrValue::empty());
}
func.evaluate_trivial().map_or_else(
- || Ok(ArrValue::range_exclusive(0, *sz).map(func)),
+ // TODO: Different mapped array impl avoiding allocating unnecessary vals
+ || Ok(ArrValue::range_exclusive(0, *sz).map(Typed::from_untyped(Val::Func(func))?)),
|trivial| {
let mut out = Vec::with_capacity(*sz as usize);
for _ in 0..*sz {
@@ -58,19 +59,22 @@
}
#[builtin]
-pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {
+pub fn builtin_map(func: NativeFn!((Val) -> Val), arr: IndexableVal) -> ArrValue {
let arr = arr.to_array();
arr.map(func)
}
#[builtin]
-pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {
+pub fn builtin_map_with_index(func: NativeFn!((u32, Val) -> Val), arr: IndexableVal) -> ArrValue {
let arr = arr.to_array();
arr.map_with_index(func)
}
#[builtin]
-pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {
+pub fn builtin_map_with_key(
+ func: NativeFn!((IStr, Val) -> Val),
+ obj: ObjValue,
+) -> Result<ObjValue> {
let mut out = ObjValueBuilder::new();
for (k, v) in obj.iter(
// Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).
@@ -80,15 +84,14 @@
true,
) {
let v = v?;
- out.field(k.clone())
- .value(func.evaluate_simple(&(k, v), false)?);
+ out.field(k.clone()).value(func.call(k, v)?);
}
Ok(out.build())
}
#[builtin]
pub fn builtin_flatmap(
- func: NativeFn<((Either![String, Val],), Val)>,
+ func: NativeFn!((Either![String, Val]) -> Val),
arr: IndexableVal,
) -> Result<IndexableVal> {
use std::fmt::Write;
@@ -96,9 +99,9 @@
IndexableVal::Str(str) => {
let mut out = String::new();
for c in str.chars() {
- match func(Either2::A(c.to_string()))? {
+ match func.call(Either2::A(c.to_string()))? {
Val::Str(o) => write!(out, "{o}").unwrap(),
- Val::Null => {},
+ Val::Null => {}
_ => bail!("in std.join all items should be strings"),
}
}
@@ -108,13 +111,13 @@
let mut out = Vec::new();
for el in a.iter() {
let el = el?;
- match func(Either2::B(el))? {
+ match func.call(Either2::B(el))? {
Val::Arr(o) => {
for oe in o.iter() {
out.push(oe?);
}
}
- Val::Null => {},
+ Val::Null => {}
_ => bail!("in std.join all items should be arrays"),
}
}
@@ -123,32 +126,38 @@
}
}
+type FilterFunc = NativeFn!((Val) -> bool);
+
#[builtin]
-pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {
- arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))
+pub fn builtin_filter(func: FilterFunc, arr: ArrValue) -> Result<ArrValue> {
+ arr.filter(|val| func.call(val.clone()))
}
#[builtin]
pub fn builtin_filter_map(
- filter_func: FuncVal,
- map_func: FuncVal,
+ filter_func: FilterFunc,
+ map_func: NativeFn!((Val) -> Val),
arr: ArrValue,
) -> Result<ArrValue> {
Ok(builtin_filter(filter_func, arr)?.map(map_func))
}
#[builtin]
-pub fn builtin_foldl(func: FuncVal, arr: Either![ArrValue, IStr], init: Val) -> Result<Val> {
+pub fn builtin_foldl(
+ func: NativeFn!((Val, Either![Val, char]) -> Val),
+ arr: Either![ArrValue, IStr],
+ init: Val,
+) -> Result<Val> {
let mut acc = init;
match arr {
Either2::A(arr) => {
for i in arr.iter() {
- acc = func.evaluate_simple(&(acc, i?), false)?;
+ acc = func.call(acc, Either2::A(i?))?;
}
}
Either2::B(arr) => {
- for i in arr.chars() {
- acc = func.evaluate_simple(&(acc, Val::string(i)), false)?;
+ for c in arr.chars() {
+ acc = func.call(acc, Either2::B(c))?;
}
}
}
@@ -156,17 +165,21 @@
}
#[builtin]
-pub fn builtin_foldr(func: FuncVal, arr: Either![ArrValue, IStr], init: Val) -> Result<Val> {
+pub fn builtin_foldr(
+ func: NativeFn!((Either![Val, char], Val) -> Val),
+ arr: Either![ArrValue, IStr],
+ init: Val,
+) -> Result<Val> {
let mut acc = init;
match arr {
Either2::A(arr) => {
for i in arr.iter().rev() {
- acc = func.evaluate_simple(&(i?, acc), false)?;
+ acc = func.call(Either2::A(i?), acc)?;
}
}
Either2::B(arr) => {
- for i in arr.chars().rev() {
- acc = func.evaluate_simple(&(Val::string(i), acc), false)?;
+ for c in arr.chars().rev() {
+ acc = func.call(Either2::B(c), acc)?;
}
}
}
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(())
-}