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}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(())
-}