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 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}7};89fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>10where11 Ident: PartialEq<I>,12{13 let attrs = attrs14 .iter()15 .filter(|a| a.path().is_ident(&ident))16 .collect::<Vec<_>>();17 if attrs.len() > 1 {18 return Err(Error::new(19 attrs[1].span(),20 "this attribute may be specified only once",21 ));22 } else if attrs.is_empty() {23 return Ok(false);24 }25 let attr = attrs[0];2627 match attr.meta {28 Meta::Path(_) => Ok(true),29 _ => Ok(false),30 }31}32fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>33where34 Ident: PartialEq<I>,35{36 let attrs = attrs37 .iter()38 .filter(|a| a.path().is_ident(&ident))39 .collect::<Vec<_>>();40 if attrs.len() > 1 {41 return Err(Error::new(42 attrs[1].span(),43 "this attribute may be specified only once",44 ));45 } else if attrs.is_empty() {46 return Ok(None);47 }48 let attr = attrs[0];49 let attr = attr.parse_args::<A>()?;5051 Ok(Some(attr))52}53fn remove_attr<I>(attrs: &mut Vec<Attribute>, ident: I)54where55 Ident: PartialEq<I>,56{57 attrs.retain(|a| !a.path().is_ident(&ident));58}5960fn path_is(path: &Path, needed: &str) -> bool {61 path.leading_colon.is_none()62 && !path.segments.is_empty()63 && path.segments.iter().last().unwrap().ident == needed64}6566fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {67 match ty {68 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {69 let args = &path.path.segments.iter().last().unwrap().arguments;70 Some(args)71 }72 _ => None,73 }74}7576fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {77 let Some(args) = type_is_path(ty, "Option") else {78 return Ok(None);79 };80 // It should have only on angle-bracketed param ("<String>"):81 let PathArguments::AngleBracketed(params) = args else {82 return Err(Error::new(args.span(), "missing option generic"));83 };84 let generic_arg = params.args.iter().next().unwrap();85 // This argument must be a type:86 let GenericArgument::Type(ty) = generic_arg else {87 return Err(Error::new(88 generic_arg.span(),89 "option generic should be a type",90 ));91 };92 Ok(Some(ty))93}9495struct Field {96 attrs: Vec<Attribute>,97 name: Ident,98 _colon: Token![:],99 ty: Type,100}101impl Parse for Field {102 fn parse(input: ParseStream) -> syn::Result<Self> {103 Ok(Self {104 attrs: input.call(Attribute::parse_outer)?,105 name: input.parse()?,106 _colon: input.parse()?,107 ty: input.parse()?,108 })109 }110}111112mod kw {113 syn::custom_keyword!(fields);114 syn::custom_keyword!(rename);115 syn::custom_keyword!(alias);116 syn::custom_keyword!(flatten);117 syn::custom_keyword!(add);118 syn::custom_keyword!(hide);119 syn::custom_keyword!(ok);120}121122struct BuiltinAttrs {123 fields: Vec<Field>,124}125impl Parse for BuiltinAttrs {126 fn parse(input: ParseStream) -> syn::Result<Self> {127 if input.is_empty() {128 return Ok(Self { fields: Vec::new() });129 }130 input.parse::<kw::fields>()?;131 let fields;132 parenthesized!(fields in input);133 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;134 Ok(Self {135 fields: p.into_iter().collect(),136 })137 }138}139140enum Optionality {141 Required,142 Optional,143 Default(Expr),144 TypeDefault,145}146147#[allow(148 clippy::large_enum_variant,149 reason = "this macro is not that hot for it to matter"150)]151enum ArgInfo {152 Normal {153 ty: Box<Type>,154 optionality: Optionality,155 name: Option<String>,156 cfg_attrs: Vec<Attribute>,157 },158 Lazy {159 is_option: bool,160 name: Option<String>,161 },162 Context,163 Location,164 This,165}166167impl ArgInfo {168 fn parse(name: &str, arg: &mut FnArg) -> Result<Self> {169 let FnArg::Typed(arg) = arg else {170 unreachable!()171 };172 let ident = match &arg.pat as &Pat {173 Pat::Ident(i) => Some(i.ident.clone()),174 _ => None,175 };176 let ty = &arg.ty;177 if type_is_path(ty, "Context").is_some() {178 return Ok(Self::Context);179 } else if type_is_path(ty, "CallLocation").is_some() {180 return Ok(Self::Location);181 } else if type_is_path(ty, "Thunk").is_some() {182 return Ok(Self::Lazy {183 is_option: false,184 name: ident.map(|v| v.to_string()),185 });186 }187188 match ty as &Type {189 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),190 _ => {}191 }192193 let (optionality, ty) = if try_parse_attr_noargs(&mut arg.attrs, "default")? {194 remove_attr(&mut arg.attrs, "default");195 (Optionality::TypeDefault, ty.clone())196 } else if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {197 remove_attr(&mut arg.attrs, "default");198 (Optionality::Default(default), ty.clone())199 } else if let Some(ty) = extract_type_from_option(ty)? {200 if type_is_path(ty, "Thunk").is_some() {201 return Ok(Self::Lazy {202 is_option: true,203 name: ident.map(|v| v.to_string()),204 });205 }206207 (Optionality::Optional, Box::new(ty.clone()))208 } else {209 (Optionality::Required, ty.clone())210 };211212 let cfg_attrs = arg213 .attrs214 .iter()215 .filter(|a| a.path().is_ident("cfg"))216 .cloned()217 .collect();218219 Ok(Self::Normal {220 ty,221 optionality,222 name: ident.map(|v| v.to_string()),223 cfg_attrs,224 })225 }226}227228#[proc_macro_attribute]229pub fn builtin(230 attr: proc_macro::TokenStream,231 item: proc_macro::TokenStream,232) -> proc_macro::TokenStream {233 let attr = parse_macro_input!(attr as BuiltinAttrs);234 let item_fn = parse_macro_input!(item as ItemFn);235236 match builtin_inner(attr, item_fn) {237 Ok(v) => v.into(),238 Err(e) => e.into_compile_error().into(),239 }240}241242#[allow(clippy::too_many_lines)]243fn builtin_inner(attr: BuiltinAttrs, mut fun: ItemFn) -> syn::Result<TokenStream> {244 let ReturnType::Type(_, result) = &fun.sig.output else {245 return Err(Error::new(246 fun.sig.span(),247 "builtin should return something",248 ));249 };250251 let name = fun.sig.ident.to_string();252 let args = fun253 .sig254 .inputs255 .iter_mut()256 .map(|arg| ArgInfo::parse(&name, arg))257 .collect::<Result<Vec<_>>>()?;258259 let params_desc = args.iter().filter_map(|a| match a {260 ArgInfo::Normal {261 optionality,262 name,263 cfg_attrs,264 ..265 } => {266 let name = name267 .as_ref()268 .map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});269 let default = match optionality {270 Optionality::Required => quote!(ParamDefault::None),271 Optionality::Optional | Optionality::TypeDefault => quote!(ParamDefault::Exists),272 Optionality::Default(e) => quote!(ParamDefault::Literal(stringify!(#e))),273 };274 Some(quote! {275 #(#cfg_attrs)*276 [#name => #default],277 })278 }279 ArgInfo::Lazy { is_option, name } => {280 let name = name281 .as_ref()282 .map_or_else(|| quote! {unnamed}, |n| quote! {named(#n)});283 Some(quote! {284 [#name => ParamDefault::exists(#is_option)],285 })286 }287 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => None,288 });289290 let mut id = 0usize;291 let pass = args292 .iter()293 .map(|a| match a {294 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {295 let cid = id;296 id += 1;297 (quote! {#cid}, a)298 }299 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {300 (quote! {compile_error!("should not use id")}, a)301 }302 })303 .map(|(id, a)| match a {304 ArgInfo::Normal {305 ty,306 optionality,307 name,308 cfg_attrs,309 } => {310 let name = name.as_ref().map_or("<unnamed>", String::as_str);311 let eval = quote! {jrsonnet_evaluator::in_description_frame(312 || format!("argument <{}> evaluation", #name),313 || <#ty>::from_untyped(value.evaluate()?),314 )?};315 let value = match optionality {316 Optionality::Required => quote! {{317 let value = parsed[#id].as_ref().expect("args shape is checked");318 #eval319 },},320 Optionality::Optional => quote! {if let Some(value) = &parsed[#id] {321 Some(#eval)322 } else {323 None324 },},325 Optionality::Default(expr) => quote! {if let Some(value) = &parsed[#id] {326 #eval327 } else {328 let v: #ty = #expr;329 v330 },},331 Optionality::TypeDefault => quote! {if let Some(value) = &parsed[#id] {332 #eval333 } else {334 let v: #ty = Default::default();335 v336 },},337 };338 quote! {339 #(#cfg_attrs)*340 #value341 }342 }343 ArgInfo::Lazy { is_option, .. } => {344 if *is_option {345 quote! {if let Some(value) = &parsed[#id] {346 Some(value.clone())347 } else {348 None349 },}350 } else {351 quote! {352 parsed[#id].as_ref().expect("args shape is correct").clone(),353 }354 }355 }356 ArgInfo::Context => quote! {ctx.clone(),},357 ArgInfo::Location => quote! {location,},358 ArgInfo::This => quote! {self,},359 });360361 let fields = attr.fields.iter().map(|field| {362 let attrs = &field.attrs;363 let name = &field.name;364 let ty = &field.ty;365 quote! {366 #(#attrs)*367 pub #name: #ty,368 }369 });370371 let name = &fun.sig.ident;372 let vis = &fun.vis;373 let static_ext = if attr.fields.is_empty() {374 quote! {375 impl #name {376 pub const INST: &'static dyn StaticBuiltin = &#name {};377 }378 impl StaticBuiltin for #name {}379 }380 } else {381 quote! {}382 };383 let static_derive_copy = if attr.fields.is_empty() {384 quote! {, Copy}385 } else {386 quote! {}387 };388389 Ok(quote! {390 #fun391392 #[doc(hidden)]393 #[allow(non_camel_case_types)]394 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]395 #vis struct #name {396 #(#fields)*397 }398 const _: () = {399 use ::jrsonnet_evaluator::{400 State, Val,401 function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation, ArgsLike, parse::parse_builtin_call},402 Result, Context, typed::Typed,403 parser::Span, params, Thunk,404 };405 params!(406 #(#params_desc)*407 );408409 #static_ext410 impl Builtin for #name411 where412 Self: 'static413 {414 fn name(&self) -> &str {415 stringify!(#name)416 }417 fn params(&self) -> FunctionSignature {418 PARAMS.with(|p| p.clone())419 }420 #[allow(unused_variables)]421 fn call(&self, location: CallLocation<'_>, parsed: &[Option<Thunk<Val>>]) -> Result<Val> {422 let result: #result = #name(#(#pass)*);423 <_ as Typed>::into_result(result)424 }425 fn as_any(&self) -> &dyn ::std::any::Any {426 self427 }428 }429 };430 })431}432433#[derive(Default)]434#[allow(clippy::struct_excessive_bools)]435struct TypedAttr {436 rename: Option<String>,437 aliases: Vec<String>,438 flatten: bool,439 /// flatten(ok) strategy for flattened optionals440 /// field would be None in case of any parsing error (as in serde)441 flatten_ok: bool,442 // Should it be `field+:` instead of `field:`443 add: bool,444 // Should it be `field::` instead of `field:`445 hide: bool,446}447impl Parse for TypedAttr {448 fn parse(input: ParseStream) -> syn::Result<Self> {449 let mut out = Self::default();450 loop {451 let lookahead = input.lookahead1();452 if lookahead.peek(kw::rename) {453 input.parse::<kw::rename>()?;454 input.parse::<Token![=]>()?;455 let name = input.parse::<LitStr>()?;456 if out.rename.is_some() {457 return Err(Error::new(458 name.span(),459 "rename attribute may only be specified once",460 ));461 }462 out.rename = Some(name.value());463 } else if lookahead.peek(kw::alias) {464 input.parse::<kw::alias>()?;465 input.parse::<Token![=]>()?;466 let alias = input.parse::<LitStr>()?;467 out.aliases.push(alias.value());468 } else if lookahead.peek(kw::flatten) {469 input.parse::<kw::flatten>()?;470 out.flatten = true;471 if input.peek(token::Paren) {472 let content;473 parenthesized!(content in input);474 let lookahead = content.lookahead1();475 if lookahead.peek(kw::ok) {476 content.parse::<kw::ok>()?;477 out.flatten_ok = true;478 } else {479 return Err(lookahead.error());480 }481 }482 } else if lookahead.peek(kw::add) {483 input.parse::<kw::add>()?;484 out.add = true;485 } else if lookahead.peek(kw::hide) {486 input.parse::<kw::hide>()?;487 out.hide = true;488 } else if input.is_empty() {489 break;490 } else {491 return Err(lookahead.error());492 }493 if input.peek(Token![,]) {494 input.parse::<Token![,]>()?;495 } else {496 break;497 }498 }499 Ok(out)500 }501}502503struct TypedField {504 attr: TypedAttr,505 ident: Ident,506 ty: Type,507 is_option: bool,508 is_lazy: bool,509}510impl TypedField {511 fn parse(field: &syn::Field) -> Result<Self> {512 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();513 let Some(ident) = field.ident.clone() else {514 return Err(Error::new(515 field.span(),516 "this field should appear in output object, but it has no visible name",517 ));518 };519 let (is_option, ty) = extract_type_from_option(&field.ty)?520 .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));521 if is_option && attr.flatten {522 if !attr.flatten_ok {523 return Err(Error::new(524 field.span(),525 "strategy should be set when flattening Option",526 ));527 }528 } else if attr.flatten_ok {529 return Err(Error::new(530 field.span(),531 "flatten(ok) is only useable on optional fields",532 ));533 }534535 let is_lazy = type_is_path(&ty, "Thunk").is_some();536537 Ok(Self {538 attr,539 ident,540 ty,541 is_option,542 is_lazy,543 })544 }545 /// None if this field is flattened in jsonnet output546 fn name(&self) -> Option<String> {547 if self.attr.flatten {548 return None;549 }550 Some(551 self.attr552 .rename553 .clone()554 .unwrap_or_else(|| self.ident.to_string()),555 )556 }557558 fn expand_field(&self) -> Option<TokenStream> {559 if self.is_option {560 return None;561 }562 let name = self.name()?;563 let ty = &self.ty;564 Some(quote! {565 (#name, <#ty as Typed>::TYPE)566 })567 }568569 fn expand_parse(&self) -> TokenStream {570 if self.is_option {571 self.expand_parse_optional()572 } else {573 self.expand_parse_mandatory()574 }575 }576577 fn expand_parse_optional(&self) -> TokenStream {578 let ident = &self.ident;579 let ty = &self.ty;580581 // optional flatten is handled in same way as serde582 if self.attr.flatten {583 return quote! {584 #ident: <#ty as TypedObj>::parse(&obj).ok(),585 };586 }587588 let name = self.name().unwrap();589 let aliases = &self.attr.aliases;590591 quote! {592 #ident: {593 let __value = if let Some(__v) = obj.get(#name.into())? {594 Some(__v)595 } #(else if let Some(__v) = obj.get(#aliases.into())? {596 Some(__v)597 })* else {598 None599 };600601 __value.map(<#ty as Typed>::from_untyped).transpose()?602 },603 }604 }605606 fn expand_parse_mandatory(&self) -> TokenStream {607 let ident = &self.ident;608 let ty = &self.ty;609610 // optional flatten is handled in same way as serde611 if self.attr.flatten {612 return quote! {613 #ident: <#ty as TypedObj>::parse(&obj)?,614 };615 }616617 let name = self.name().unwrap();618 let aliases = &self.attr.aliases;619620 let error_text = if aliases.is_empty() {621 // clippy does not understand name variable usage in quote! macro622 #[allow(clippy::redundant_clone)]623 name.clone()624 } else {625 format!("{name} (alias {})", aliases.join(", "))626 };627628 quote! {629 #ident: {630 let __value = if let Some(__v) = obj.get(#name.into())? {631 __v632 } #(else if let Some(__v) = obj.get(#aliases.into())? {633 __v634 })* else {635 return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());636 };637638 <#ty as Typed>::from_untyped(__value)?639 },640 }641 }642643 fn expand_serialize(&self) -> TokenStream {644 let ident = &self.ident;645 let ty = &self.ty;646 self.name().map_or_else(647 || {648 if self.is_option {649 quote! {650 if let Some(value) = self.#ident {651 <#ty as TypedObj>::serialize(value, out)?;652 }653 }654 } else {655 quote! {656 <#ty as TypedObj>::serialize(self.#ident, out)?;657 }658 }659 },660 |name| {661 let hide = if self.attr.hide {662 quote! {.hide()}663 } else {664 quote! {}665 };666 let add = if self.attr.add {667 quote! {.add()}668 } else {669 quote! {}670 };671 let value = if self.is_lazy {672 quote! {673 out.field(#name)674 #hide675 #add676 .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;677 }678 } else {679 quote! {680 out.field(#name)681 #hide682 #add683 .try_value(<#ty as Typed>::into_untyped(value)?)?;684 }685 };686 if self.is_option {687 quote! {688 if let Some(value) = self.#ident {689 #value690 }691 }692 } else {693 quote! {694 {695 let value = self.#ident;696 #value697 }698 }699 }700 },701 )702 }703}704705#[proc_macro_derive(Typed, attributes(typed))]706pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {707 let input = parse_macro_input!(item as DeriveInput);708709 match derive_typed_inner(input) {710 Ok(v) => v.into(),711 Err(e) => e.to_compile_error().into(),712 }713}714715fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {716 let syn::Data::Struct(data) = &input.data else {717 return Err(Error::new(input.span(), "only structs supported"));718 };719720 let ident = &input.ident;721 let fields = data722 .fields723 .iter()724 .map(TypedField::parse)725 .collect::<Result<Vec<_>>>()?;726727 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();728729 let typed = {730 let fields = fields731 .iter()732 .filter_map(TypedField::expand_field)733 .collect::<Vec<_>>();734 quote! {735 impl #impl_generics Typed for #ident #ty_generics #where_clause {736 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[737 #(#fields,)*738 ]);739740 fn from_untyped(value: Val) -> JrResult<Self> {741 let obj = value.as_obj().expect("shape is correct");742 Self::parse(&obj)743 }744745 fn into_untyped(value: Self) -> JrResult<Val> {746 let mut out = ObjValueBuilder::new();747 value.serialize(&mut out)?;748 Ok(Val::Obj(out.build()))749 }750751 }752 }753 };754755 let fields_parse = fields.iter().map(TypedField::expand_parse);756 let fields_serialize = fields757 .iter()758 .map(TypedField::expand_serialize)759 .collect::<Vec<_>>();760761 Ok(quote! {762 const _: () = {763 use ::jrsonnet_evaluator::{764 typed::{ComplexValType, Typed, TypedObj, CheckType},765 Val, State,766 error::{ErrorKind, Result as JrResult},767 ObjValueBuilder, ObjValue,768 };769770 #typed771772 impl #impl_generics TypedObj for #ident #ty_generics #where_clause {773 fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {774 #(#fields_serialize)*775776 Ok(())777 }778 fn parse(obj: &ObjValue) -> JrResult<Self> {779 Ok(Self {780 #(#fields_parse)*781 })782 }783 }784 };785 })786}787788struct FormatInput {789 formatting: LitStr,790 arguments: Vec<Expr>,791}792impl Parse for FormatInput {793 fn parse(input: ParseStream) -> Result<Self> {794 let formatting = input.parse()?;795 let mut arguments = Vec::new();796797 while input.peek(Token![,]) {798 input.parse::<Token![,]>()?;799 if input.is_empty() {800 // Trailing comma801 break;802 }803 let expr = input.parse()?;804 arguments.push(expr);805 }806807 if !input.is_empty() {808 return Err(syn::Error::new(input.span(), "unexpected trailing input"));809 }810811 Ok(Self {812 formatting,813 arguments,814 })815 }816}817fn is_format_str(i: &str) -> bool {818 let mut is_plain = true;819 // -1 = {820 // +1 = }821 let mut is_bracket = 0i8;822 for ele in i.chars() {823 match ele {824 '{' if is_bracket == -1 => {825 is_bracket = 0;826 }827 '}' if is_bracket == -1 => {828 is_plain = false;829 break;830 }831 '}' if is_bracket == 1 => {832 is_bracket = 0;833 }834 '{' if is_bracket == 1 => {835 is_plain = false;836 break;837 }838 '{' => {839 is_bracket = -1;840 }841 '}' => {842 is_bracket = 1;843 }844 _ if is_bracket != 0 => {845 is_plain = false;846 break;847 }848 _ => {}849 }850 }851 !is_plain || is_bracket != 0852}853impl FormatInput {854 fn expand(self) -> TokenStream {855 let format = self.formatting;856 if is_format_str(&format.value()) {857 let args = self.arguments;858 quote! {859 ::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))860 }861 } else {862 if let Some(first) = self.arguments.first() {863 return syn::Error::new(864 first.span(),865 "string has no formatting codes, it should not have the arguments",866 )867 .into_compile_error();868 }869 quote! {870 ::jrsonnet_evaluator::IStr::from(#format)871 }872 }873 }874}875876/// `IStr` formatting helper877///878/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`879/// This macro looks for formatting codes in the input string, and uses880/// `format!()` only when necessary881#[proc_macro]882pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {883 let input = parse_macro_input!(input as FormatInput);884 input.expand().into()885}886887/// Create Thunk using closure syntax888#[proc_macro]889#[allow(non_snake_case)]890pub fn Thunk(input: proc_macro::TokenStream) -> proc_macro::TokenStream {891 let input = parse_macro_input!(input as ExprClosure);892893 let span = input.inputs.span();894 let move_check = input.capture.is_none().then(|| {895 quote_spanned! {span => {896 compile_error!("Thunk! needs to be called with move closure");897 }}898 });899900 let (env, closure, args) = syn_dissect_closure::split_env(input);901902 let trace_check = args.iter().map(|el| {903 let span = el.span();904 quote_spanned! {span => ::jrsonnet_evaluator::gc::assert_trace(&#el);}905 });906907 quote! {{908 #move_check909 #(#trace_check)*910 ::jrsonnet_evaluator::Thunk::new(::jrsonnet_evaluator::val::MemoizedClosureThunk::new(#env, #closure))911 }}.into()912}1use 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(())
-}