difftreelog
refactor use PreparedFunction for NativeFn
in: master
9 files changed
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -1,14 +1,15 @@
use std::{
any::Any,
fmt::{self},
- num::NonZeroU32, rc::Rc,
+ num::NonZeroU32,
+ rc::Rc,
};
use jrsonnet_gcmodule::{cc_dyn, Cc};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{function::FuncVal, Context, Result, Thunk, Val};
+use crate::{typed::NativeFn, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
@@ -61,13 +62,13 @@
}
#[must_use]
- pub fn map(self, mapper: FuncVal) -> Self {
- Self::new(<MappedArray<false>>::new(self, mapper))
+ pub fn map(self, mapper: NativeFn!((Val) -> Val)) -> Self {
+ Self::new(<MappedArray>::new(self, ArrayMapper::Plain(mapper)))
}
#[must_use]
- pub fn map_with_index(self, mapper: FuncVal) -> Self {
- Self::new(<MappedArray<true>>::new(self, mapper))
+ pub fn map_with_index(self, mapper: NativeFn!((u32, Val) -> Val)) -> Self {
+ Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))
}
pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -6,9 +6,11 @@
use jrsonnet_parser::{Expr, Spanned};
use super::ArrValue;
+use crate::typed::NativeFn;
+use crate::val::NumValue;
use crate::{
- error::ErrorKind::InfiniteRecursionDetected, evaluate, function::FuncVal, typed::Typed,
- val::ThunkValue, Context, Error, ObjValue, Result, Thunk, Val,
+ error::ErrorKind::InfiniteRecursionDetected, evaluate, typed::Typed, val::ThunkValue, Context,
+ Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
@@ -404,14 +406,20 @@
}
}
+#[derive(Trace, Clone, Debug)]
+pub enum ArrayMapper {
+ Plain(NativeFn!((Val) -> Val)),
+ WithIndex(NativeFn!((u32, Val) -> Val)),
+}
+
#[derive(Trace, Debug, Clone)]
-pub struct MappedArray<const WITH_INDEX: bool> {
+pub struct MappedArray {
inner: ArrValue,
cached: Cc<RefCell<Vec<ArrayThunk>>>,
- mapper: FuncVal,
+ mapper: ArrayMapper,
}
-impl<const WITH_INDEX: bool> MappedArray<WITH_INDEX> {
- pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
+impl MappedArray {
+ pub fn new(inner: ArrValue, mapper: ArrayMapper) -> Self {
let len = inner.len();
Self {
inner,
@@ -420,14 +428,13 @@
}
}
fn evaluate(&self, index: usize, value: Val) -> Result<Val> {
- if WITH_INDEX {
- self.mapper.evaluate_simple(&(index, value), false)
- } else {
- self.mapper.evaluate_simple(&(value,), false)
+ match &self.mapper {
+ ArrayMapper::Plain(f) => f.call(value),
+ ArrayMapper::WithIndex(f) => f.call(index as u32, value),
}
}
}
-impl<const WITH_INDEX: bool> ArrayLike for MappedArray<WITH_INDEX> {
+impl ArrayLike for MappedArray {
fn len(&self) -> usize {
self.cached.borrow().len()
}
@@ -468,11 +475,11 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
#[derive(Trace)]
- struct MappedArrayThunk<const WITH_INDEX: bool> {
- arr: MappedArray<WITH_INDEX>,
+ struct MappedArrayThunk {
+ arr: MappedArray,
index: usize,
}
- impl<const WITH_INDEX: bool> ThunkValue for MappedArrayThunk<WITH_INDEX> {
+ impl ThunkValue for MappedArrayThunk {
type Output = Val;
fn get(&self) -> Result<Self::Output> {
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -8,15 +8,13 @@
use jrsonnet_parser::{Destruct, Expr, ExprParams, Span, Spanned};
use self::{
- arglike::OptionalContext,
builtin::{Builtin, StaticBuiltin},
- native::NativeDesc,
parse::{parse_builtin_call, parse_default_function_call, parse_function_call},
prepared::{parse_prepared_builtin_call, parse_prepared_function_call, PreparedCall},
};
use crate::{
bail, error::ErrorKind::*, evaluate, evaluate_trivial, function::builtin::BuiltinFunc, Context,
- ContextBuilder, Result, Thunk, Val,
+ Result, Thunk, Val,
};
pub mod arglike;
@@ -199,18 +197,6 @@
b.call(loc, &args)
}
}
- }
- pub fn evaluate_simple<A: ArgsLike + OptionalContext>(
- &self,
- args: &A,
- tailstrict: bool,
- ) -> Result<Val> {
- self.evaluate(
- ContextBuilder::new().build(),
- CallLocation::native(),
- args,
- tailstrict,
- )
}
pub(crate) fn evaluate_prepared(
@@ -246,10 +232,6 @@
b.call(loc, &args)
}
}
- }
- /// Convert jsonnet function to plain `Fn` value.
- pub fn into_native<D: NativeDesc>(self) -> D::Value {
- D::into_native(self)
}
/// Is this function an indentity function.
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -1,43 +1,2 @@
-use super::{
- arglike::{ArgLike, OptionalContext},
- FuncVal,
-};
-use crate::{typed::Typed, Result};
-
-pub trait NativeDesc {
- type Value;
- fn into_native(val: FuncVal) -> Self::Value;
-}
-macro_rules! impl_native_desc {
- ($($gen:ident)*) => {
- impl<$($gen,)* O> NativeDesc for (($($gen,)*), O)
- where
- $($gen: ArgLike + OptionalContext,)*
- O: Typed,
- {
- type Value = Box<dyn Fn($($gen,)*) -> Result<O>>;
-
- #[allow(non_snake_case)]
- fn into_native(val: FuncVal) -> Self::Value {
- Box::new(move |$($gen),*| {
- let val = val.evaluate_simple(
- &($($gen,)*),
- false,
- )?;
- O::from_untyped(val)
- })
- }
- }
- };
- ($($cur:ident)* @ $c:ident $($rest:ident)*) => {
- impl_native_desc!($($cur)*);
- impl_native_desc!($($cur)* $c @ $($rest)*);
- };
- ($($cur:ident)* @) => {
- impl_native_desc!($($cur)*);
- }
-}
-
-impl_native_desc! {
- @ A B C D E F G H I J K L
-}
+use super::PreparedFuncVal;
+use crate::{typed::Typed, CallLocation, Result, Thunk};
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -8,7 +8,7 @@
use crate::{
arr::{ArrValue, BytesArray},
bail,
- function::{native::NativeDesc, FuncDesc, FuncVal},
+ function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
typed::CheckType,
val::{IndexableVal, NumValue, StrValue, ThunkMapper},
ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
@@ -675,30 +675,65 @@
}
}
-pub struct NativeFn<D: NativeDesc>(D::Value);
-impl<D: NativeDesc> Deref for NativeFn<D> {
- type Target = D::Value;
+#[derive(Debug, Trace, Clone)]
+pub struct NativeFn<D: 'static>(pub(crate) PreparedFuncVal, PhantomData<D>);
+macro_rules! impl_native_desc {
+ ($i:expr; $($gen:ident)*) => {
+ impl<$($gen,)* O> NativeFn<($($gen,)* O,)>
+ where
+ $($gen: Typed,)*
+ O: Typed,
+ {
+ pub fn call(
+ &self,
+ $($gen: $gen,)*
+ ) -> Result<O> {
+ let val = self.0.call(
+ CallLocation::native(),
+ &[$(Typed::into_lazy_untyped($gen),)*],
+ &[],
+ )?;
+ O::from_untyped(val)
+ }
+ }
+ impl<$($gen,)* O> Typed for NativeFn<($($gen,)* O,)> {
+ const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+
+ fn into_untyped(_typed: Self) -> Result<Val> {
+ bail!("can only convert functions from jsonnet to native")
+ }
- fn deref(&self) -> &Self::Target {
- &self.0
+ fn from_untyped(untyped: Val) -> Result<Self> {
+ let func = FuncVal::from_untyped(untyped)?;
+ Ok(Self(
+ PreparedFuncVal::new(func, $i, &[])?,
+ PhantomData,
+ ))
+ }
+ }
+ };
+ ($i:expr; $($cur:ident)* @ $c:ident $($rest:ident)*) => {
+ impl_native_desc!($i; $($cur)*);
+ impl_native_desc!($i + 1; $($cur)* $c @ $($rest)*);
+ };
+ ($i:expr; $($cur:ident)* @) => {
+ impl_native_desc!($i; $($cur)*);
}
}
-impl<D: NativeDesc> Typed for NativeFn<D> {
- const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
- fn into_untyped(_typed: Self) -> Result<Val> {
- bail!("can only convert functions from jsonnet to native")
- }
+impl_native_desc! {
+ 0; @ A B C D E F G H I J K L
+}
- fn from_untyped(untyped: Val) -> Result<Self> {
- Ok(Self(
- untyped
- .as_func()
- .expect("shape is checked")
- .into_native::<D>(),
- ))
+mod native_macro {
+ #[macro_export]
+ macro_rules! NativeFn {
+ (($($t:ty),* $(,)?) -> $res:ty) => {
+ NativeFn<($($t,)* $res)>
+ }
}
}
+pub use crate::NativeFn;
impl Typed for NumValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -3,7 +3,14 @@
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{
- Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn, LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type, parenthesized, parse::{Parse, ParseStream}, parse_macro_input, punctuated::Punctuated, spanned::Spanned, token::{self, Comma}
+ parenthesized,
+ parse::{Parse, ParseStream},
+ parse_macro_input,
+ punctuated::Punctuated,
+ spanned::Spanned,
+ token::{self, Comma},
+ Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,
+ LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
crates/jrsonnet-stdlib/src/arrays.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.rsdiffbeforeafterboth1#![allow(clippy::similar_names)]23use std::{4 cell::{Ref, RefCell, RefMut},5 collections::HashMap,6 f64,7 rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15 error::Result,16 function::{CallLocation, FuncVal, TlaArg},17 trace::PathResolver,18 val::NumValue,19 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,20};21use jrsonnet_gcmodule::{Acyclic, Cc, Trace};22use jrsonnet_parser::Source;23pub use manifest::*;24pub use math::*;25pub use misc::*;26pub use objects::*;27pub use operator::*;28pub use parse::*;29pub use sets::*;30pub use sort::*;31pub use strings::*;32pub use types::*;3334#[cfg(feature = "exp-regex")]35pub use crate::regex::*;3637mod arrays;38mod compat;39mod encoding;40mod hash;41mod manifest;42mod math;43mod misc;44mod objects;45mod operator;46mod parse;47#[cfg(feature = "exp-regex")]48mod regex;49mod sets;50mod sort;51mod strings;52mod types;53mod keyf;5455#[allow(clippy::too_many_lines)]56pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {57 let mut builder = ObjValueBuilder::new();5859 // FIXME: Use PHF60 for (name, builtin) in [61 // Types62 ("type", builtin_type::INST),63 ("isString", builtin_is_string::INST),64 ("isNumber", builtin_is_number::INST),65 ("isBoolean", builtin_is_boolean::INST),66 ("isObject", builtin_is_object::INST),67 ("isArray", builtin_is_array::INST),68 ("isFunction", builtin_is_function::INST),69 ("isNull", builtin_is_null::INST),70 // Arrays71 ("makeArray", builtin_make_array::INST),72 ("repeat", builtin_repeat::INST),73 ("slice", builtin_slice::INST),74 ("map", builtin_map::INST),75 ("mapWithIndex", builtin_map_with_index::INST),76 ("mapWithKey", builtin_map_with_key::INST),77 ("flatMap", builtin_flatmap::INST),78 ("filter", builtin_filter::INST),79 ("foldl", builtin_foldl::INST),80 ("foldr", builtin_foldr::INST),81 ("range", builtin_range::INST),82 ("join", builtin_join::INST),83 ("lines", builtin_lines::INST),84 ("resolvePath", builtin_resolve_path::INST),85 ("deepJoin", builtin_deep_join::INST),86 ("reverse", builtin_reverse::INST),87 ("any", builtin_any::INST),88 ("all", builtin_all::INST),89 ("member", builtin_member::INST),90 ("find", builtin_find::INST),91 ("contains", builtin_contains::INST),92 ("count", builtin_count::INST),93 ("avg", builtin_avg::INST),94 ("removeAt", builtin_remove_at::INST),95 ("remove", builtin_remove::INST),96 ("flattenArrays", builtin_flatten_arrays::INST),97 ("flattenDeepArray", builtin_flatten_deep_array::INST),98 ("prune", builtin_prune::INST),99 ("filterMap", builtin_filter_map::INST),100 // Math101 ("abs", builtin_abs::INST),102 ("sign", builtin_sign::INST),103 ("max", builtin_max::INST),104 ("min", builtin_min::INST),105 ("clamp", builtin_clamp::INST),106 ("sum", builtin_sum::INST),107 ("modulo", builtin_modulo::INST),108 ("floor", builtin_floor::INST),109 ("ceil", builtin_ceil::INST),110 ("log", builtin_log::INST),111 ("log2", builtin_log2::INST),112 ("log10", builtin_log10::INST),113 ("pow", builtin_pow::INST),114 ("sqrt", builtin_sqrt::INST),115 ("sin", builtin_sin::INST),116 ("cos", builtin_cos::INST),117 ("tan", builtin_tan::INST),118 ("asin", builtin_asin::INST),119 ("acos", builtin_acos::INST),120 ("atan", builtin_atan::INST),121 ("atan2", builtin_atan2::INST),122 ("exp", builtin_exp::INST),123 ("mantissa", builtin_mantissa::INST),124 ("exponent", builtin_exponent::INST),125 ("round", builtin_round::INST),126 ("isEven", builtin_is_even::INST),127 ("isOdd", builtin_is_odd::INST),128 ("isInteger", builtin_is_integer::INST),129 ("isDecimal", builtin_is_decimal::INST),130 ("deg2rad", builtin_deg2rad::INST),131 ("rad2deg", builtin_rad2deg::INST),132 ("hypot", builtin_hypot::INST),133 // Operator134 ("mod", builtin_mod::INST),135 ("primitiveEquals", builtin_primitive_equals::INST),136 ("equals", builtin_equals::INST),137 ("xor", builtin_xor::INST),138 ("xnor", builtin_xnor::INST),139 ("format", builtin_format::INST),140 // Sort141 ("sort", builtin_sort::INST),142 ("uniq", builtin_uniq::INST),143 ("set", builtin_set::INST),144 ("minArray", builtin_min_array::INST),145 ("maxArray", builtin_max_array::INST),146 // Hash147 ("md5", builtin_md5::INST),148 ("sha1", builtin_sha1::INST),149 ("sha256", builtin_sha256::INST),150 ("sha512", builtin_sha512::INST),151 ("sha3", builtin_sha3::INST),152 // Encoding153 ("encodeUTF8", builtin_encode_utf8::INST),154 ("decodeUTF8", builtin_decode_utf8::INST),155 ("base64", builtin_base64::INST),156 ("base64Decode", builtin_base64_decode::INST),157 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),158 // Objects159 ("objectFieldsEx", builtin_object_fields_ex::INST),160 ("objectFields", builtin_object_fields::INST),161 ("objectFieldsAll", builtin_object_fields_all::INST),162 ("objectValues", builtin_object_values::INST),163 ("objectValuesAll", builtin_object_values_all::INST),164 ("objectKeysValues", builtin_object_keys_values::INST),165 ("objectKeysValuesAll", builtin_object_keys_values_all::INST),166 ("objectHasEx", builtin_object_has_ex::INST),167 ("objectHas", builtin_object_has::INST),168 ("objectHasAll", builtin_object_has_all::INST),169 ("objectRemoveKey", builtin_object_remove_key::INST),170 // Manifest171 ("escapeStringJson", builtin_escape_string_json::INST),172 ("escapeStringPython", builtin_escape_string_python::INST),173 ("escapeStringXML", builtin_escape_string_xml::INST),174 ("manifestJsonEx", builtin_manifest_json_ex::INST),175 ("manifestJson", builtin_manifest_json::INST),176 ("manifestJsonMinified", builtin_manifest_json_minified::INST),177 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),178 ("manifestYamlStream", builtin_manifest_yaml_stream::INST),179 ("manifestTomlEx", builtin_manifest_toml_ex::INST),180 ("manifestToml", builtin_manifest_toml::INST),181 ("toString", builtin_to_string::INST),182 ("manifestPython", builtin_manifest_python::INST),183 ("manifestPythonVars", builtin_manifest_python_vars::INST),184 ("manifestXmlJsonml", builtin_manifest_xml_jsonml::INST),185 ("manifestIni", builtin_manifest_ini::INST),186 // Parse187 ("parseJson", builtin_parse_json::INST),188 ("parseYaml", builtin_parse_yaml::INST),189 // Strings190 ("codepoint", builtin_codepoint::INST),191 ("substr", builtin_substr::INST),192 ("char", builtin_char::INST),193 ("strReplace", builtin_str_replace::INST),194 ("escapeStringBash", builtin_escape_string_bash::INST),195 ("escapeStringDollars", builtin_escape_string_dollars::INST),196 ("isEmpty", builtin_is_empty::INST),197 ("equalsIgnoreCase", builtin_equals_ignore_case::INST),198 ("splitLimit", builtin_splitlimit::INST),199 ("splitLimitR", builtin_splitlimitr::INST),200 ("split", builtin_split::INST),201 ("asciiUpper", builtin_ascii_upper::INST),202 ("asciiLower", builtin_ascii_lower::INST),203 ("findSubstr", builtin_find_substr::INST),204 ("parseInt", builtin_parse_int::INST),205 #[cfg(feature = "exp-bigint")]206 ("bigint", builtin_bigint::INST),207 ("parseOctal", builtin_parse_octal::INST),208 ("parseHex", builtin_parse_hex::INST),209 ("stringChars", builtin_string_chars::INST),210 ("lstripChars", builtin_lstrip_chars::INST),211 ("rstripChars", builtin_rstrip_chars::INST),212 ("stripChars", builtin_strip_chars::INST),213 ("trim", builtin_trim::INST),214 // Misc215 ("length", builtin_length::INST),216 ("get", builtin_get::INST),217 ("startsWith", builtin_starts_with::INST),218 ("endsWith", builtin_ends_with::INST),219 ("assertEqual", builtin_assert_equal::INST),220 ("mergePatch", builtin_merge_patch::INST),221 // Sets222 ("setMember", builtin_set_member::INST),223 ("setInter", builtin_set_inter::INST),224 ("setDiff", builtin_set_diff::INST),225 ("setUnion", builtin_set_union::INST),226 // Regex227 #[cfg(feature = "exp-regex")]228 ("regexQuoteMeta", builtin_regex_quote_meta::INST),229 // Compat230 ("__compare", builtin___compare::INST),231 ("__compare_array", builtin___compare_array::INST),232 ("__array_less", builtin___array_less::INST),233 ("__array_greater", builtin___array_greater::INST),234 ("__array_less_or_equal", builtin___array_less_or_equal::INST),235 (236 "__array_greater_or_equal",237 builtin___array_greater_or_equal::INST,238 ),239 ]240 .iter()241 .copied()242 {243 builder.method(name, builtin);244 }245246 builder.method(247 "extVar",248 builtin_ext_var {249 settings: settings.clone(),250 },251 );252 builder.method(253 "native",254 builtin_native {255 settings: settings.clone(),256 },257 );258 builder.method("trace", builtin_trace { settings });259 builder.method("id", FuncVal::Id);260261 builder.field("pi").hide().value(Val::Num(262 NumValue::new(f64::consts::PI).expect("pi is finite"),263 ));264265 #[cfg(feature = "exp-regex")]266 {267 // Regex268 let regex_cache = RegexCache::default();269 builder.method(270 "regexFullMatch",271 builtin_regex_full_match {272 cache: regex_cache.clone(),273 },274 );275 builder.method(276 "regexPartialMatch",277 builtin_regex_partial_match {278 cache: regex_cache.clone(),279 },280 );281 builder.method(282 "regexReplace",283 builtin_regex_replace {284 cache: regex_cache.clone(),285 },286 );287 builder.method(288 "regexGlobalReplace",289 builtin_regex_global_replace { cache: regex_cache },290 );291 };292293 builder.build()294}295296pub trait TracePrinter: Acyclic {297 fn print_trace(&self, loc: CallLocation, value: IStr);298}299300#[derive(Acyclic)]301pub struct StdTracePrinter {302 resolver: PathResolver,303}304impl StdTracePrinter {305 pub fn new(resolver: PathResolver) -> Self {306 Self { resolver }307 }308}309impl TracePrinter for StdTracePrinter {310 fn print_trace(&self, loc: CallLocation, value: IStr) {311 eprint!("TRACE:");312 if let Some(loc) = loc.0 {313 let locs = loc.0.map_source_locations(&[loc.1]);314 eprint!(315 " {}:{}",316 loc.0.source_path().path().map_or_else(317 || loc.0.source_path().to_string(),318 |p| self.resolver.resolve(p)319 ),320 locs[0].line321 );322 }323 eprintln!(" {value}");324 }325}326327#[derive(Clone, Trace)]328pub struct Settings {329 /// Used for `std.extVar`330 pub ext_vars: HashMap<IStr, TlaArg>,331 /// Used for `std.native`332 pub ext_natives: HashMap<IStr, FuncVal>,333 /// Used for `std.trace`334 pub trace_printer: Rc<dyn TracePrinter>,335 /// Used for `std.thisFile`336 pub path_resolver: PathResolver,337}338339#[derive(Trace, Clone)]340pub struct ContextInitializer {341 /// std without applied thisFile overlay342 stdlib_obj: ObjValue,343 settings: Cc<RefCell<Settings>>,344}345impl ContextInitializer {346 pub fn new(resolver: PathResolver) -> Self {347 let settings = Settings {348 ext_vars: HashMap::new(),349 ext_natives: HashMap::new(),350 trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),351 path_resolver: resolver,352 };353 let settings = Cc::new(RefCell::new(settings));354 let stdlib_obj = stdlib_uncached(settings.clone());355 Self {356 stdlib_obj,357 settings,358 }359 }360 pub fn settings(&self) -> Ref<'_, Settings> {361 self.settings.borrow()362 }363 pub fn settings_mut(&self) -> RefMut<'_, Settings> {364 self.settings.borrow_mut()365 }366 pub fn add_ext_var(&self, name: IStr, value: Val) {367 self.settings_mut()368 .ext_vars369 .insert(name, TlaArg::Val(value));370 }371 pub fn add_ext_str(&self, name: IStr, value: IStr) {372 self.settings_mut()373 .ext_vars374 .insert(name, TlaArg::String(value));375 }376 pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {377 // self.data_mut().volatile_files.insert(source_name, code);378 self.settings_mut()379 .ext_vars380 .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));381 Ok(())382 }383 pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {384 self.settings_mut()385 .ext_natives386 .insert(name.into(), cb.into());387 }388}389impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {390 fn reserve_vars(&self) -> usize {391 1392 }393 fn populate(&self, source: Source, builder: &mut ContextBuilder) {394 let mut std = ObjValueBuilder::new();395 std.with_super(self.stdlib_obj.clone());396 std.field("thisFile").hide().value({397 let source_path = source.source_path();398 source_path.path().map_or_else(399 || source_path.to_string(),400 |p| self.settings().path_resolver.resolve(p),401 )402 });403 let stdlib_with_this_file = std.build();404405 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));406 }407 fn as_any(&self) -> &dyn std::any::Any {408 self409 }410}1#![allow(clippy::similar_names)]23use std::{4 cell::{Ref, RefCell, RefMut},5 collections::HashMap,6 f64,7 rc::Rc,8};910pub use arrays::*;11pub use compat::*;12pub use encoding::*;13pub use hash::*;14use jrsonnet_evaluator::{15 error::Result,16 function::{CallLocation, FuncVal, TlaArg},17 trace::PathResolver,18 val::NumValue,19 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,20};21use jrsonnet_gcmodule::{Acyclic, Cc, Trace};22use jrsonnet_parser::Source;23pub use manifest::*;24pub use math::*;25pub use misc::*;26pub use objects::*;27pub use operator::*;28pub use parse::*;29pub use sets::*;30pub use sort::*;31pub use strings::*;32pub use types::*;3334#[cfg(feature = "exp-regex")]35pub use crate::regex::*;3637mod arrays;38mod compat;39mod encoding;40mod hash;41mod keyf;42mod manifest;43mod math;44mod misc;45mod objects;46mod operator;47mod parse;48#[cfg(feature = "exp-regex")]49mod regex;50mod sets;51mod sort;52mod strings;53mod types;5455#[allow(clippy::too_many_lines)]56pub fn stdlib_uncached(settings: Cc<RefCell<Settings>>) -> ObjValue {57 let mut builder = ObjValueBuilder::new();5859 // FIXME: Use PHF60 for (name, builtin) in [61 // Types62 ("type", builtin_type::INST),63 ("isString", builtin_is_string::INST),64 ("isNumber", builtin_is_number::INST),65 ("isBoolean", builtin_is_boolean::INST),66 ("isObject", builtin_is_object::INST),67 ("isArray", builtin_is_array::INST),68 ("isFunction", builtin_is_function::INST),69 ("isNull", builtin_is_null::INST),70 // Arrays71 ("makeArray", builtin_make_array::INST),72 ("repeat", builtin_repeat::INST),73 ("slice", builtin_slice::INST),74 ("map", builtin_map::INST),75 ("mapWithIndex", builtin_map_with_index::INST),76 ("mapWithKey", builtin_map_with_key::INST),77 ("flatMap", builtin_flatmap::INST),78 ("filter", builtin_filter::INST),79 ("foldl", builtin_foldl::INST),80 ("foldr", builtin_foldr::INST),81 ("range", builtin_range::INST),82 ("join", builtin_join::INST),83 ("lines", builtin_lines::INST),84 ("resolvePath", builtin_resolve_path::INST),85 ("deepJoin", builtin_deep_join::INST),86 ("reverse", builtin_reverse::INST),87 ("any", builtin_any::INST),88 ("all", builtin_all::INST),89 ("member", builtin_member::INST),90 ("find", builtin_find::INST),91 ("contains", builtin_contains::INST),92 ("count", builtin_count::INST),93 ("avg", builtin_avg::INST),94 ("removeAt", builtin_remove_at::INST),95 ("remove", builtin_remove::INST),96 ("flattenArrays", builtin_flatten_arrays::INST),97 ("flattenDeepArray", builtin_flatten_deep_array::INST),98 ("prune", builtin_prune::INST),99 ("filterMap", builtin_filter_map::INST),100 // Math101 ("abs", builtin_abs::INST),102 ("sign", builtin_sign::INST),103 ("max", builtin_max::INST),104 ("min", builtin_min::INST),105 ("clamp", builtin_clamp::INST),106 ("sum", builtin_sum::INST),107 ("modulo", builtin_modulo::INST),108 ("floor", builtin_floor::INST),109 ("ceil", builtin_ceil::INST),110 ("log", builtin_log::INST),111 ("log2", builtin_log2::INST),112 ("log10", builtin_log10::INST),113 ("pow", builtin_pow::INST),114 ("sqrt", builtin_sqrt::INST),115 ("sin", builtin_sin::INST),116 ("cos", builtin_cos::INST),117 ("tan", builtin_tan::INST),118 ("asin", builtin_asin::INST),119 ("acos", builtin_acos::INST),120 ("atan", builtin_atan::INST),121 ("atan2", builtin_atan2::INST),122 ("exp", builtin_exp::INST),123 ("mantissa", builtin_mantissa::INST),124 ("exponent", builtin_exponent::INST),125 ("round", builtin_round::INST),126 ("isEven", builtin_is_even::INST),127 ("isOdd", builtin_is_odd::INST),128 ("isInteger", builtin_is_integer::INST),129 ("isDecimal", builtin_is_decimal::INST),130 ("deg2rad", builtin_deg2rad::INST),131 ("rad2deg", builtin_rad2deg::INST),132 ("hypot", builtin_hypot::INST),133 // Operator134 ("mod", builtin_mod::INST),135 ("primitiveEquals", builtin_primitive_equals::INST),136 ("equals", builtin_equals::INST),137 ("xor", builtin_xor::INST),138 ("xnor", builtin_xnor::INST),139 ("format", builtin_format::INST),140 // Sort141 ("sort", builtin_sort::INST),142 ("uniq", builtin_uniq::INST),143 ("set", builtin_set::INST),144 ("minArray", builtin_min_array::INST),145 ("maxArray", builtin_max_array::INST),146 // Hash147 ("md5", builtin_md5::INST),148 ("sha1", builtin_sha1::INST),149 ("sha256", builtin_sha256::INST),150 ("sha512", builtin_sha512::INST),151 ("sha3", builtin_sha3::INST),152 // Encoding153 ("encodeUTF8", builtin_encode_utf8::INST),154 ("decodeUTF8", builtin_decode_utf8::INST),155 ("base64", builtin_base64::INST),156 ("base64Decode", builtin_base64_decode::INST),157 ("base64DecodeBytes", builtin_base64_decode_bytes::INST),158 // Objects159 ("objectFieldsEx", builtin_object_fields_ex::INST),160 ("objectFields", builtin_object_fields::INST),161 ("objectFieldsAll", builtin_object_fields_all::INST),162 ("objectValues", builtin_object_values::INST),163 ("objectValuesAll", builtin_object_values_all::INST),164 ("objectKeysValues", builtin_object_keys_values::INST),165 ("objectKeysValuesAll", builtin_object_keys_values_all::INST),166 ("objectHasEx", builtin_object_has_ex::INST),167 ("objectHas", builtin_object_has::INST),168 ("objectHasAll", builtin_object_has_all::INST),169 ("objectRemoveKey", builtin_object_remove_key::INST),170 // Manifest171 ("escapeStringJson", builtin_escape_string_json::INST),172 ("escapeStringPython", builtin_escape_string_python::INST),173 ("escapeStringXML", builtin_escape_string_xml::INST),174 ("manifestJsonEx", builtin_manifest_json_ex::INST),175 ("manifestJson", builtin_manifest_json::INST),176 ("manifestJsonMinified", builtin_manifest_json_minified::INST),177 ("manifestYamlDoc", builtin_manifest_yaml_doc::INST),178 ("manifestYamlStream", builtin_manifest_yaml_stream::INST),179 ("manifestTomlEx", builtin_manifest_toml_ex::INST),180 ("manifestToml", builtin_manifest_toml::INST),181 ("toString", builtin_to_string::INST),182 ("manifestPython", builtin_manifest_python::INST),183 ("manifestPythonVars", builtin_manifest_python_vars::INST),184 ("manifestXmlJsonml", builtin_manifest_xml_jsonml::INST),185 ("manifestIni", builtin_manifest_ini::INST),186 // Parse187 ("parseJson", builtin_parse_json::INST),188 ("parseYaml", builtin_parse_yaml::INST),189 // Strings190 ("codepoint", builtin_codepoint::INST),191 ("substr", builtin_substr::INST),192 ("char", builtin_char::INST),193 ("strReplace", builtin_str_replace::INST),194 ("escapeStringBash", builtin_escape_string_bash::INST),195 ("escapeStringDollars", builtin_escape_string_dollars::INST),196 ("isEmpty", builtin_is_empty::INST),197 ("equalsIgnoreCase", builtin_equals_ignore_case::INST),198 ("splitLimit", builtin_splitlimit::INST),199 ("splitLimitR", builtin_splitlimitr::INST),200 ("split", builtin_split::INST),201 ("asciiUpper", builtin_ascii_upper::INST),202 ("asciiLower", builtin_ascii_lower::INST),203 ("findSubstr", builtin_find_substr::INST),204 ("parseInt", builtin_parse_int::INST),205 #[cfg(feature = "exp-bigint")]206 ("bigint", builtin_bigint::INST),207 ("parseOctal", builtin_parse_octal::INST),208 ("parseHex", builtin_parse_hex::INST),209 ("stringChars", builtin_string_chars::INST),210 ("lstripChars", builtin_lstrip_chars::INST),211 ("rstripChars", builtin_rstrip_chars::INST),212 ("stripChars", builtin_strip_chars::INST),213 ("trim", builtin_trim::INST),214 // Misc215 ("length", builtin_length::INST),216 ("get", builtin_get::INST),217 ("startsWith", builtin_starts_with::INST),218 ("endsWith", builtin_ends_with::INST),219 ("assertEqual", builtin_assert_equal::INST),220 ("mergePatch", builtin_merge_patch::INST),221 // Sets222 ("setMember", builtin_set_member::INST),223 ("setInter", builtin_set_inter::INST),224 ("setDiff", builtin_set_diff::INST),225 ("setUnion", builtin_set_union::INST),226 // Regex227 #[cfg(feature = "exp-regex")]228 ("regexQuoteMeta", builtin_regex_quote_meta::INST),229 // Compat230 ("__compare", builtin___compare::INST),231 ("__compare_array", builtin___compare_array::INST),232 ("__array_less", builtin___array_less::INST),233 ("__array_greater", builtin___array_greater::INST),234 ("__array_less_or_equal", builtin___array_less_or_equal::INST),235 (236 "__array_greater_or_equal",237 builtin___array_greater_or_equal::INST,238 ),239 ]240 .iter()241 .copied()242 {243 builder.method(name, builtin);244 }245246 builder.method(247 "extVar",248 builtin_ext_var {249 settings: settings.clone(),250 },251 );252 builder.method(253 "native",254 builtin_native {255 settings: settings.clone(),256 },257 );258 builder.method("trace", builtin_trace { settings });259 builder.method("id", FuncVal::Id);260261 builder.field("pi").hide().value(Val::Num(262 NumValue::new(f64::consts::PI).expect("pi is finite"),263 ));264265 #[cfg(feature = "exp-regex")]266 {267 // Regex268 let regex_cache = RegexCache::default();269 builder.method(270 "regexFullMatch",271 builtin_regex_full_match {272 cache: regex_cache.clone(),273 },274 );275 builder.method(276 "regexPartialMatch",277 builtin_regex_partial_match {278 cache: regex_cache.clone(),279 },280 );281 builder.method(282 "regexReplace",283 builtin_regex_replace {284 cache: regex_cache.clone(),285 },286 );287 builder.method(288 "regexGlobalReplace",289 builtin_regex_global_replace { cache: regex_cache },290 );291 };292293 builder.build()294}295296pub trait TracePrinter: Acyclic {297 fn print_trace(&self, loc: CallLocation, value: IStr);298}299300#[derive(Acyclic)]301pub struct StdTracePrinter {302 resolver: PathResolver,303}304impl StdTracePrinter {305 pub fn new(resolver: PathResolver) -> Self {306 Self { resolver }307 }308}309impl TracePrinter for StdTracePrinter {310 fn print_trace(&self, loc: CallLocation, value: IStr) {311 eprint!("TRACE:");312 if let Some(loc) = loc.0 {313 let locs = loc.0.map_source_locations(&[loc.1]);314 eprint!(315 " {}:{}",316 loc.0.source_path().path().map_or_else(317 || loc.0.source_path().to_string(),318 |p| self.resolver.resolve(p)319 ),320 locs[0].line321 );322 }323 eprintln!(" {value}");324 }325}326327#[derive(Clone, Trace)]328pub struct Settings {329 /// Used for `std.extVar`330 pub ext_vars: HashMap<IStr, TlaArg>,331 /// Used for `std.native`332 pub ext_natives: HashMap<IStr, FuncVal>,333 /// Used for `std.trace`334 pub trace_printer: Rc<dyn TracePrinter>,335 /// Used for `std.thisFile`336 pub path_resolver: PathResolver,337}338339#[derive(Trace, Clone)]340pub struct ContextInitializer {341 /// std without applied thisFile overlay342 stdlib_obj: ObjValue,343 settings: Cc<RefCell<Settings>>,344}345impl ContextInitializer {346 pub fn new(resolver: PathResolver) -> Self {347 let settings = Settings {348 ext_vars: HashMap::new(),349 ext_natives: HashMap::new(),350 trace_printer: Rc::new(StdTracePrinter::new(resolver.clone())),351 path_resolver: resolver,352 };353 let settings = Cc::new(RefCell::new(settings));354 let stdlib_obj = stdlib_uncached(settings.clone());355 Self {356 stdlib_obj,357 settings,358 }359 }360 pub fn settings(&self) -> Ref<'_, Settings> {361 self.settings.borrow()362 }363 pub fn settings_mut(&self) -> RefMut<'_, Settings> {364 self.settings.borrow_mut()365 }366 pub fn add_ext_var(&self, name: IStr, value: Val) {367 self.settings_mut()368 .ext_vars369 .insert(name, TlaArg::Val(value));370 }371 pub fn add_ext_str(&self, name: IStr, value: IStr) {372 self.settings_mut()373 .ext_vars374 .insert(name, TlaArg::String(value));375 }376 pub fn add_ext_code(&self, name: &str, code: impl AsRef<str>) -> Result<()> {377 // self.data_mut().volatile_files.insert(source_name, code);378 self.settings_mut()379 .ext_vars380 .insert(name.into(), TlaArg::InlineCode(code.as_ref().to_owned()));381 Ok(())382 }383 pub fn add_native(&self, name: impl Into<IStr>, cb: impl Into<FuncVal>) {384 self.settings_mut()385 .ext_natives386 .insert(name.into(), cb.into());387 }388}389impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {390 fn reserve_vars(&self) -> usize {391 1392 }393 fn populate(&self, source: Source, builder: &mut ContextBuilder) {394 let mut std = ObjValueBuilder::new();395 std.with_super(self.stdlib_obj.clone());396 std.field("thisFile").hide().value({397 let source_path = source.source_path();398 source_path.path().map_or_else(399 || source_path.to_string(),400 |p| self.settings().path_resolver.resolve(p),401 )402 });403 let stdlib_with_this_file = std.build();404405 builder.bind("std", Thunk::evaluated(Val::Obj(stdlib_with_this_file)));406 }407 fn as_any(&self) -> &dyn std::any::Any {408 self409 }410}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(())
-}