difftreelog
refactor drop ArgsLike abstraction
in: master
17 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -22,11 +22,11 @@
use jrsonnet_evaluator::{
apply_tla, bail,
- function::TlaArg,
gc::WithCapacityExt as _,
manifest::{JsonFormat, ManifestFormat, ToStringFormat},
rustc_hash::FxHashMap,
stack::set_stack_depth_limit,
+ tla::TlaArg,
trace::{CompactFormat, PathResolver, TraceFormat},
AsPathLike, FileImportResolver, IStr, ImportResolver, Result, State, Val,
};
@@ -40,6 +40,7 @@
pub extern "C" fn _start() {}
/// Return the version string of the Jsonnet interpreter.
+///
/// Conforms to [semantic versioning](http://semver.org/).
/// If this does not match `LIB_JSONNET_VERSION`
/// then there is a mismatch between header and compiled library.
bindings/jsonnet/src/vars_tlas.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/vars_tlas.rs
+++ b/bindings/jsonnet/src/vars_tlas.rs
@@ -2,7 +2,8 @@
use std::{ffi::CStr, os::raw::c_char};
-use jrsonnet_evaluator::{function::TlaArg, IStr};
+use jrsonnet_evaluator::tla::TlaArg;
+use jrsonnet_evaluator::IStr;
use crate::VM;
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/stdlib.rs
+++ b/crates/jrsonnet-cli/src/stdlib.rs
@@ -1,7 +1,8 @@
use std::str::FromStr;
use clap::Parser;
-use jrsonnet_evaluator::{function::TlaArg, trace::PathResolver, Result};
+use jrsonnet_evaluator::tla::TlaArg;
+use jrsonnet_evaluator::{trace::PathResolver, Result};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone)]
crates/jrsonnet-cli/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/tla.rs
+++ b/crates/jrsonnet-cli/src/tla.rs
@@ -1,7 +1,6 @@
use clap::Parser;
-use jrsonnet_evaluator::{
- error::Result, function::TlaArg, gc::WithCapacityExt as _, rustc_hash::FxHashMap, IStr,
-};
+use jrsonnet_evaluator::tla::TlaArg;
+use jrsonnet_evaluator::{error::Result, gc::WithCapacityExt as _, rustc_hash::FxHashMap, IStr};
use crate::{ExtFile, ExtStr};
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -9,7 +9,7 @@
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{typed::NativeFn, Context, Result, Thunk, Val};
+use crate::{function::NativeFn, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -6,8 +6,7 @@
use jrsonnet_parser::{Expr, Spanned};
use super::ArrValue;
-use crate::typed::NativeFn;
-use crate::val::NumValue;
+use crate::function::NativeFn;
use crate::{
error::ErrorKind::InfiniteRecursionDetected, evaluate, typed::Typed, val::ThunkValue, Context,
Error, ObjValue, Result, Thunk, Val,
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ /dev/null
@@ -1,197 +0,0 @@
-use std::collections::HashMap;
-use std::rc::Rc;
-
-use jrsonnet_gcmodule::Trace;
-use jrsonnet_interner::IStr;
-use jrsonnet_parser::{ArgsDesc, Expr, SourceFifo, SourcePath, Spanned};
-
-use crate::{evaluate, typed::Typed, with_state, Context, Result, Thunk, Val};
-
-pub trait ArgLike {
- fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>>;
-}
-
-impl ArgLike for &Rc<Spanned<Expr>> {
- fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
- Ok(if tailstrict {
- Thunk::evaluated(evaluate(ctx, self)?)
- } else {
- let expr = (*self).clone();
- Thunk!(move || evaluate(ctx, &expr))
- })
- }
-}
-
-impl<T> ArgLike for T
-where
- T: Typed + Clone,
-{
- fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
- if T::provides_lazy() && !tailstrict {
- return Ok(T::into_lazy_untyped(self.clone()));
- }
- let val = T::into_untyped(self.clone())?;
- Ok(Thunk::evaluated(val))
- }
-}
-
-#[derive(Clone, Trace)]
-pub enum TlaArg {
- String(IStr),
- Val(Val),
- Lazy(Thunk<Val>),
- Import(String),
- ImportStr(String),
- InlineCode(String),
-}
-impl TlaArg {
- pub fn evaluate_tailstrict(&self) -> Result<Val> {
- match self {
- Self::String(s) => Ok(Val::string(s.clone())),
- Self::Val(val) => Ok(val.clone()),
- Self::Lazy(lazy) => Ok(lazy.evaluate()?),
- Self::Import(p) => with_state(|s| {
- let resolved = s.resolve_from_default(&p.as_str())?;
- s.import_resolved(resolved)
- }),
- Self::ImportStr(p) => with_state(|s| {
- let resolved = s.resolve_from_default(&p.as_str())?;
- s.import_resolved_str(resolved).map(Val::string)
- }),
- Self::InlineCode(p) => with_state(|s| {
- let resolved =
- SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
- s.import_resolved(resolved)
- }),
- }
- }
- pub fn evaluate(&self) -> Result<Thunk<Val>> {
- match self {
- Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
- Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
- Self::Lazy(lazy) => Ok(lazy.clone()),
- Self::Import(p) => with_state(|s| {
- let resolved = s.resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || s.import_resolved(resolved)))
- }),
- Self::ImportStr(p) => with_state(|s| {
- let resolved = s.resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || s
- .import_resolved_str(resolved)
- .map(Val::string)))
- }),
- Self::InlineCode(p) => with_state(|s| {
- let resolved =
- SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
- Ok(Thunk!(move || s.import_resolved(resolved)))
- }),
- }
- }
-}
-
-pub trait ArgsLike {
- fn unnamed_len(&self) -> usize;
- fn unnamed_iter(
- &self,
- ctx: Context,
- tailstrict: bool,
- handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
- ) -> Result<()>;
- fn named_iter(
- &self,
- ctx: Context,
- tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
- ) -> Result<()>;
- fn named_names(&self, handler: &mut dyn FnMut(&IStr));
- fn is_empty(&self) -> bool;
-}
-
-impl ArgsLike for Vec<Val> {
- fn unnamed_len(&self) -> usize {
- self.len()
- }
- fn unnamed_iter(
- &self,
- _ctx: Context,
- _tailstrict: bool,
- handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
- ) -> Result<()> {
- for (idx, el) in self.iter().enumerate() {
- handler(idx, Thunk::evaluated(el.clone()))?;
- }
- Ok(())
- }
- fn named_iter(
- &self,
- _ctx: Context,
- _tailstrict: bool,
- _handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
- ) -> Result<()> {
- Ok(())
- }
- fn named_names(&self, _handler: &mut dyn FnMut(&IStr)) {}
- fn is_empty(&self) -> bool {
- self.is_empty()
- }
-}
-
-impl ArgsLike for ArgsDesc {
- fn unnamed_len(&self) -> usize {
- self.unnamed.len()
- }
-
- fn unnamed_iter(
- &self,
- ctx: Context,
- tailstrict: bool,
- handler: &mut dyn FnMut(usize, Thunk<Val>) -> Result<()>,
- ) -> Result<()> {
- for (id, arg) in self.unnamed.iter().enumerate() {
- handler(
- id,
- if tailstrict {
- Thunk::evaluated(evaluate(ctx.clone(), arg)?)
- } else {
- let ctx = ctx.clone();
- let arg = arg.clone();
-
- Thunk!(move || evaluate(ctx, &arg))
- },
- )?;
- }
- Ok(())
- }
-
- fn named_iter(
- &self,
- ctx: Context,
- tailstrict: bool,
- handler: &mut dyn FnMut(&IStr, Thunk<Val>) -> Result<()>,
- ) -> Result<()> {
- for (name, arg) in &self.named {
- handler(
- name,
- if tailstrict {
- Thunk::evaluated(evaluate(ctx.clone(), arg)?)
- } else {
- let ctx = ctx.clone();
- let arg = arg.clone();
-
- Thunk!(move || evaluate(ctx, &arg))
- },
- )?;
- }
- Ok(())
- }
-
- fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
- for (name, _) in &self.named {
- handler(name);
- }
- }
-
- fn is_empty(&self) -> bool {
- self.unnamed.is_empty() && self.named.is_empty()
- }
-}
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -1,11 +1,10 @@
use std::{fmt::Debug, rc::Rc};
-pub use arglike::{ArgLike, ArgsLike, TlaArg};
use educe::Educe;
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::builtin;
-use jrsonnet_parser::{Destruct, Expr, ExprParams, Span, Spanned};
+use jrsonnet_parser::{ArgsDesc, Destruct, Expr, ExprParams, Span, Spanned};
use self::{
builtin::{Builtin, StaticBuiltin},
@@ -17,12 +16,12 @@
Result, Thunk, Val,
};
-pub mod arglike;
pub mod builtin;
-pub mod native;
-pub mod parse;
+mod native;
+mod parse;
mod prepared;
+pub use native::NativeFn;
pub use prepared::PreparedFuncVal;
pub use jrsonnet_parser::function::*;
@@ -81,10 +80,10 @@
}
/// Create context, with which body code will run
- pub fn call_body_context(
+ pub(crate) fn call_body_context(
&self,
call_ctx: Context,
- args: &dyn ArgsLike,
+ args: &ArgsDesc,
tailstrict: bool,
) -> Result<Context> {
parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)
@@ -170,7 +169,7 @@
&self,
call_ctx: Context,
loc: CallLocation<'_>,
- args: &dyn ArgsLike,
+ args: &ArgsDesc,
tailstrict: bool,
) -> Result<Val> {
match self {
@@ -179,7 +178,7 @@
evaluate(body_ctx, &func.body)
}
Self::Thunk(thunk) => {
- if !args.is_empty() {
+ if !args.named.is_empty() || !args.unnamed.is_empty() {
bail!(TooManyArgsFunctionHas(0, FunctionSignature::empty()))
}
thunk.evaluate()
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -1,2 +1,68 @@
+use std::marker::PhantomData;
+
+use jrsonnet_gcmodule::Trace;
+
use super::PreparedFuncVal;
-use crate::{typed::Typed, CallLocation, Result, Thunk};
+use crate::{bail, function::FuncVal, typed::Typed, CallLocation, Result, Val};
+use jrsonnet_types::{ComplexValType, ValType};
+
+#[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,
+ {
+ #[allow(non_snake_case, clippy::too_many_arguments)]
+ 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 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_native_desc! {
+ 0; @ A B C D E F G H I J K L
+}
+
+mod native_macro {
+ #[macro_export]
+ macro_rules! NativeFn {
+ (($($t:ty),* $(,)?) -> $res:ty) => {
+ NativeFn<($($t,)* $res)>
+ }
+ }
+}
+pub use crate::NativeFn;
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/parse.rs
+++ b/crates/jrsonnet-evaluator/src/function/parse.rs
@@ -1,19 +1,29 @@
+use std::rc::Rc;
+
use jrsonnet_parser::{
function::{FunctionSignature, ParamName},
- ExprParams,
+ ArgsDesc, Expr, ExprParams, Spanned,
};
use rustc_hash::FxHashMap;
-use super::arglike::ArgsLike;
use crate::{
bail,
destructure::destruct,
error::{ErrorKind::*, Result},
- evaluate_named_param,
+ evaluate, evaluate_named_param,
gc::WithCapacityExt as _,
Context, Pending, Thunk, Val,
};
+fn eval_arg(ctx: Context, arg: &Rc<Spanned<Expr>>, tailstrict: bool) -> Result<Thunk<Val>> {
+ if tailstrict {
+ Ok(Thunk::evaluated(evaluate(ctx, arg)?))
+ } else {
+ let arg = arg.clone();
+ Ok(Thunk!(move || evaluate(ctx, &arg)))
+ }
+}
+
/// Creates correct [context](Context) for function body evaluation returning error on invalid call.
///
/// ## Parameters
@@ -22,15 +32,15 @@
/// * `params`: function parameters' definition
/// * `args`: passed function arguments
/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
-pub fn parse_function_call(
+pub(crate) fn parse_function_call(
ctx: Context,
body_ctx: Context,
params: &ExprParams,
- args: &dyn ArgsLike,
+ args: &ArgsDesc,
tailstrict: bool,
) -> Result<Context> {
let mut passed_args = FxHashMap::with_capacity(params.binds_len());
- if args.unnamed_len() > params.signature.len() {
+ if args.unnamed.len() > params.signature.len() {
bail!(TooManyArgsFunctionHas(
params.signature.len(),
params.signature.clone(),
@@ -40,28 +50,29 @@
let mut filled_named = 0;
let mut filled_positionals = 0;
- args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
+ for (id, arg) in args.unnamed.iter().enumerate() {
destruct(
¶ms.exprs[id].destruct,
- arg,
+ eval_arg(ctx.clone(), arg, tailstrict)?,
Pending::new_filled(ctx.clone()),
&mut passed_args,
)?;
filled_positionals += 1;
- Ok(())
- })?;
+ }
- args.named_iter(ctx, tailstrict, &mut |name, value| {
+ for (name, value) in &args.named {
// FIXME: O(n) for arg existence check
if !params.exprs.iter().any(|p| &p.destruct.name() == name) {
bail!(UnknownFunctionParameter(name.clone()));
}
- if passed_args.insert(name.clone(), value).is_some() {
+ if passed_args
+ .insert(name.clone(), eval_arg(ctx.clone(), value, tailstrict)?)
+ .is_some()
+ {
bail!(BindingParameterASecondTime(name.clone()));
}
filled_named += 1;
- Ok(())
- })?;
+ }
if filled_named + filled_positionals < params.len() {
// Some args are unset, but maybe we have defaults for them
@@ -104,13 +115,13 @@
// Some args still weren't filled
if filled_named + filled_positionals != params.len() {
- for param in params.exprs.iter().skip(args.unnamed_len()) {
+ for param in params.exprs.iter().skip(args.unnamed.len()) {
let mut found = false;
- args.named_names(&mut |name| {
+ for (name, _) in &args.named {
if ¶m.destruct.name() == name {
found = true;
}
- });
+ }
if !found {
bail!(FunctionParameterNotBoundInCall(
param.destruct.name(),
@@ -141,34 +152,35 @@
pub fn parse_builtin_call(
ctx: Context,
params: FunctionSignature,
- args: &dyn ArgsLike,
+ args: &ArgsDesc,
tailstrict: bool,
) -> Result<Vec<Option<Thunk<Val>>>> {
let mut passed_args: Vec<Option<Thunk<Val>>> = vec![None; params.len()];
- if args.unnamed_len() > params.len() {
+ if args.unnamed.len() > params.len() {
bail!(TooManyArgsFunctionHas(params.len(), params,))
}
let mut filled_args = 0;
- args.unnamed_iter(ctx.clone(), tailstrict, &mut |id, arg| {
- passed_args[id] = Some(arg);
+ for (id, arg) in args.unnamed.iter().enumerate() {
+ passed_args[id] = Some(eval_arg(ctx.clone(), arg, tailstrict)?);
filled_args += 1;
- Ok(())
- })?;
+ }
- args.named_iter(ctx, tailstrict, &mut |name, arg| {
+ for (name, arg) in &args.named {
// FIXME: O(n) for arg existence check
let id = params
.iter()
.position(|p| p.name() == name)
.ok_or_else(|| UnknownFunctionParameter(name.clone()))?;
- if passed_args[id].replace(arg).is_some() {
+ if passed_args[id]
+ .replace(eval_arg(ctx.clone(), arg, tailstrict)?)
+ .is_some()
+ {
bail!(BindingParameterASecondTime(name.clone()));
}
filled_args += 1;
- Ok(())
- })?;
+ }
if filled_args < params.len() {
for (id, _) in params.iter().enumerate().filter(|(_, p)| p.has_default()) {
@@ -180,13 +192,13 @@
// Some args still wasn't filled
if filled_args != params.len() {
- for param in params.iter().skip(args.unnamed_len()) {
+ for param in params.iter().skip(args.unnamed.len()) {
let mut found = false;
- args.named_names(&mut |name| {
+ for (name, _) in &args.named {
if param.name() == name {
found = true;
}
- });
+ }
if !found {
bail!(FunctionParameterNotBoundInCall(
param.name().clone(),
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -19,7 +19,7 @@
mod obj;
pub mod stack;
pub mod stdlib;
-mod tla;
+pub mod tla;
pub mod trace;
pub mod typed;
pub mod val;
crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -1,12 +1,68 @@
use std::{collections::HashMap, hash::BuildHasher};
+use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::IStr;
+use jrsonnet_parser::{SourceFifo, SourcePath};
use crate::{
- function::{CallLocation, PreparedFuncVal, TlaArg},
- in_description_frame, Result, Val,
+ function::{CallLocation, PreparedFuncVal},
+ in_description_frame, with_state, Result, Thunk, Val,
};
+#[derive(Clone, Trace)]
+pub enum TlaArg {
+ String(IStr),
+ Val(Val),
+ Lazy(Thunk<Val>),
+ Import(String),
+ ImportStr(String),
+ InlineCode(String),
+}
+impl TlaArg {
+ pub fn evaluate_tailstrict(&self) -> Result<Val> {
+ match self {
+ Self::String(s) => Ok(Val::string(s.clone())),
+ Self::Val(val) => Ok(val.clone()),
+ Self::Lazy(lazy) => Ok(lazy.evaluate()?),
+ Self::Import(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ s.import_resolved(resolved)
+ }),
+ Self::ImportStr(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ s.import_resolved_str(resolved).map(Val::string)
+ }),
+ Self::InlineCode(p) => with_state(|s| {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ s.import_resolved(resolved)
+ }),
+ }
+ }
+ pub fn evaluate(&self) -> Result<Thunk<Val>> {
+ match self {
+ Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),
+ Self::Val(val) => Ok(Thunk::evaluated(val.clone())),
+ Self::Lazy(lazy) => Ok(lazy.clone()),
+ Self::Import(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || s.import_resolved(resolved)))
+ }),
+ Self::ImportStr(p) => with_state(|s| {
+ let resolved = s.resolve_from_default(&p.as_str())?;
+ Ok(Thunk!(move || s
+ .import_resolved_str(resolved)
+ .map(Val::string)))
+ }),
+ Self::InlineCode(p) => with_state(|s| {
+ let resolved =
+ SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
+ Ok(Thunk!(move || s.import_resolved(resolved)))
+ }),
+ }
+ }
+}
+
pub fn apply_tla<H: BuildHasher>(args: &HashMap<IStr, TlaArg, H>, val: Val) -> Result<Val> {
Ok(if let Val::Func(func) = val {
in_description_frame(
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 arr::{ArrValue, BytesArray},10 bail,11 function::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},12 typed::CheckType,13 val::{IndexableVal, NumValue, StrValue, ThunkMapper},14 ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,15};1617#[derive(Trace)]18struct FromUntyped<K: Trace>(PhantomData<fn() -> K>);19impl<K> ThunkMapper<Val> for FromUntyped<K>20where21 K: Typed + Trace,22{23 type Output = K;2425 fn map(self, from: Val) -> Result<Self::Output> {26 K::from_untyped(from)27 }28}29impl<K: Trace> Default for FromUntyped<K> {30 fn default() -> Self {31 Self(PhantomData)32 }33}3435pub trait TypedObj: Typed {36 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;37 fn parse(obj: &ObjValue) -> Result<Self>;38 fn into_object(self) -> Result<ObjValue> {39 let mut builder = ObjValueBuilder::new();40 self.serialize(&mut builder)?;41 Ok(builder.build())42 }43}4445pub trait Typed: Sized {46 const TYPE: &'static ComplexValType;47 fn into_untyped(typed: Self) -> Result<Val>;48 fn into_lazy_untyped(typed: Self) -> Thunk<Val> {49 Thunk::from(Self::into_untyped(typed))50 }51 fn from_untyped(untyped: Val) -> Result<Self>;52 fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {53 Self::from_untyped(lazy.evaluate()?)54 }5556 // Whatever caller should use `into_lazy_untyped` instead of `into_untyped`57 fn provides_lazy() -> bool {58 false59 }6061 // Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible62 fn wants_lazy() -> bool {63 false64 }6566 /// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result67 /// This method returns identity in impl Typed for Result, and should not be overriden68 #[doc(hidden)]69 fn into_result(typed: Self) -> Result<Val> {70 let value = Self::into_untyped(typed)?;71 Ok(value)72 }73}7475impl<T> Typed for Thunk<T>76where77 T: Typed + Trace + Clone,78{79 const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);8081 fn into_untyped(typed: Self) -> Result<Val> {82 T::into_untyped(typed.evaluate()?)83 }8485 fn from_untyped(untyped: Val) -> Result<Self> {86 Self::from_lazy_untyped(Thunk::evaluated(untyped))87 }8889 fn provides_lazy() -> bool {90 true91 }9293 fn into_lazy_untyped(inner: Self) -> Thunk<Val> {94 #[derive(Trace)]95 struct IntoUntyped<K: Trace>(PhantomData<fn() -> K>);96 impl<K> ThunkMapper<K> for IntoUntyped<K>97 where98 K: Typed + Trace,99 {100 type Output = Val;101102 fn map(self, from: K) -> Result<Self::Output> {103 K::into_untyped(from)104 }105 }106 impl<K: Trace> Default for IntoUntyped<K> {107 fn default() -> Self {108 Self(PhantomData)109 }110 }111 inner.map(<IntoUntyped<T>>::default())112 }113114 fn wants_lazy() -> bool {115 true116 }117118 fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {119 Ok(inner.map(<FromUntyped<T>>::default()))120 }121}122123pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;124pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;125126macro_rules! impl_int {127 ($($ty:ty)*) => {$(128 impl Typed for $ty {129 const TYPE: &'static ComplexValType =130 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));131 fn from_untyped(value: Val) -> Result<Self> {132 <Self as Typed>::TYPE.check(&value)?;133 match value {134 Val::Num(n) => {135 let n = n.get();136 #[allow(clippy::float_cmp)]137 if n.trunc() != n {138 bail!(139 "cannot convert number with fractional part to {}",140 stringify!($ty)141 )142 }143 Ok(n as Self)144 }145 _ => unreachable!(),146 }147 }148 fn into_untyped(value: Self) -> Result<Val> {149 Ok(Val::Num(value.into()))150 }151 }152 )*};153}154155impl_int!(i8 u8 i16 u16 i32 u32);156157macro_rules! impl_bounded_int {158 ($($name:ident = $ty:ty)*) => {$(159 #[derive(Clone, Copy)]160 pub struct $name<const MIN: $ty, const MAX: $ty>($ty);161 impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {162 pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {163 if value >= MIN && value <= MAX {164 Some(Self(value))165 } else {166 None167 }168 }169 pub const fn value(self) -> $ty {170 self.0171 }172 }173 impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {174 type Target = $ty;175 fn deref(&self) -> &Self::Target {176 &self.0177 }178 }179180 impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {181 const TYPE: &'static ComplexValType =182 &ComplexValType::BoundedNumber(183 Some(MIN as f64),184 Some(MAX as f64),185 );186187 fn from_untyped(value: Val) -> Result<Self> {188 <Self as Typed>::TYPE.check(&value)?;189 match value {190 Val::Num(n) => {191 let n = n.get();192 #[allow(clippy::float_cmp)]193 if n.trunc() != n {194 bail!(195 "cannot convert number with fractional part to {}",196 stringify!($ty)197 )198 }199 Ok(Self(n as $ty))200 }201 _ => unreachable!(),202 }203 }204205 #[allow(clippy::cast_lossless)]206 fn into_untyped(value: Self) -> Result<Val> {207 Ok(Val::try_num(value.0)?)208 }209 }210 )*};211}212213impl_bounded_int!(214 BoundedI8 = i8215 BoundedI16 = i16216 BoundedI32 = i32217 BoundedI64 = i64218 BoundedUsize = usize219);220221impl Typed for f64 {222 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);223224 fn into_untyped(value: Self) -> Result<Val> {225 Ok(Val::try_num(value)?)226 }227228 fn from_untyped(value: Val) -> Result<Self> {229 <Self as Typed>::TYPE.check(&value)?;230 match value {231 Val::Num(n) => Ok(n.get()),232 _ => unreachable!(),233 }234 }235}236237pub struct PositiveF64(pub f64);238impl Typed for PositiveF64 {239 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);240241 fn into_untyped(value: Self) -> Result<Val> {242 Ok(Val::try_num(value.0)?)243 }244245 fn from_untyped(value: Val) -> Result<Self> {246 <Self as Typed>::TYPE.check(&value)?;247 match value {248 Val::Num(n) => Ok(Self(n.get())),249 _ => unreachable!(),250 }251 }252}253impl Typed for usize {254 const TYPE: &'static ComplexValType =255 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));256257 fn into_untyped(value: Self) -> Result<Val> {258 Ok(Val::try_num(value)?)259 }260261 fn from_untyped(value: Val) -> Result<Self> {262 <Self as Typed>::TYPE.check(&value)?;263 match value {264 Val::Num(n) => {265 let n = n.get();266 #[allow(clippy::float_cmp)]267 if n.trunc() != n {268 bail!("cannot convert number with fractional part to usize")269 }270 Ok(n as Self)271 }272 _ => unreachable!(),273 }274 }275}276277impl Typed for IStr {278 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);279280 fn into_untyped(value: Self) -> Result<Val> {281 Ok(Val::string(value))282 }283284 fn from_untyped(value: Val) -> Result<Self> {285 <Self as Typed>::TYPE.check(&value)?;286 match value {287 Val::Str(s) => Ok(s.into_flat()),288 _ => unreachable!(),289 }290 }291}292293impl Typed for String {294 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);295296 fn into_untyped(value: Self) -> Result<Val> {297 Ok(Val::string(value))298 }299300 fn from_untyped(value: Val) -> Result<Self> {301 <Self as Typed>::TYPE.check(&value)?;302 match value {303 Val::Str(s) => Ok(s.to_string()),304 _ => unreachable!(),305 }306 }307}308309impl Typed for StrValue {310 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);311312 fn into_untyped(value: Self) -> Result<Val> {313 Ok(Val::Str(value))314 }315316 fn from_untyped(value: Val) -> Result<Self> {317 <Self as Typed>::TYPE.check(&value)?;318 match value {319 Val::Str(s) => Ok(s),320 _ => unreachable!(),321 }322 }323}324325impl Typed for char {326 const TYPE: &'static ComplexValType = &ComplexValType::Char;327328 fn into_untyped(value: Self) -> Result<Val> {329 Ok(Val::string(value))330 }331332 fn from_untyped(value: Val) -> Result<Self> {333 <Self as Typed>::TYPE.check(&value)?;334 match value {335 Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),336 _ => unreachable!(),337 }338 }339}340341impl<T> Typed for Vec<T>342where343 T: Typed,344{345 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);346347 fn into_untyped(value: Self) -> Result<Val> {348 Ok(Val::Arr(349 value350 .into_iter()351 .map(T::into_untyped)352 .collect::<Result<ArrValue>>()?,353 ))354 }355356 fn from_untyped(value: Val) -> Result<Self> {357 let Val::Arr(a) = value else {358 <Self as Typed>::TYPE.check(&value)?;359 unreachable!("typecheck should fail")360 };361 a.iter()362 .enumerate()363 .map(|(i, r)| {364 r.and_then(|t| {365 T::from_untyped(t).with_description(|| format!("parsing elem <{i}>"))366 })367 })368 .collect::<Result<Self>>()369 }370}371372impl<K: Typed + Ord, V: Typed> Typed for BTreeMap<K, V> {373 const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);374375 fn into_untyped(typed: Self) -> Result<Val> {376 let mut out = ObjValueBuilder::with_capacity(typed.len());377 for (k, v) in typed {378 let Some(key) = K::into_untyped(k)?.as_str() else {379 bail!("map key should serialize to string");380 };381 let value = V::into_untyped(v)?;382 out.field(key).value(value);383 }384 Ok(Val::Obj(out.build()))385 }386387 fn from_untyped(value: Val) -> Result<Self> {388 Self::TYPE.check(&value)?;389 let obj = value.as_obj().expect("typecheck should fail");390391 let mut out = Self::new();392 if V::wants_lazy() {393 for key in obj.fields_ex(394 false,395 #[cfg(feature = "exp-preserve-order")]396 false,397 ) {398 let value = obj.get_lazy(key.clone()).expect("field exists");399 let value = V::from_lazy_untyped(value)?;400 let key = K::from_untyped(Val::Str(key.into()))?;401 let _ = out.insert(key, value);402 }403 } else {404 for (key, value) in obj.iter(405 #[cfg(feature = "exp-preserve-order")]406 false,407 ) {408 let key = K::from_untyped(Val::Str(key.into()))?;409 let value = V::from_untyped(value?)?;410 let _ = out.insert(key, value);411 }412 }413 Ok(out)414 }415}416417impl Typed for Val {418 const TYPE: &'static ComplexValType = &ComplexValType::Any;419420 fn into_untyped(typed: Self) -> Result<Val> {421 Ok(typed)422 }423 fn from_untyped(untyped: Val) -> Result<Self> {424 Ok(untyped)425 }426}427428// Hack429#[doc(hidden)]430impl<T> Typed for Result<T>431where432 T: Typed,433{434 const TYPE: &'static ComplexValType = &ComplexValType::Any;435436 fn into_untyped(_typed: Self) -> Result<Val> {437 panic!("do not use this conversion")438 }439440 fn from_untyped(_untyped: Val) -> Result<Self> {441 panic!("do not use this conversion")442 }443444 fn into_result(typed: Self) -> Result<Val> {445 typed.map(T::into_untyped)?446 }447}448449/// Specialization450impl Typed for IBytes {451 const TYPE: &'static ComplexValType =452 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));453454 fn into_untyped(value: Self) -> Result<Val> {455 Ok(Val::Arr(ArrValue::bytes(value)))456 }457458 fn from_untyped(value: Val) -> Result<Self> {459 let Val::Arr(a) = &value else {460 <Self as Typed>::TYPE.check(&value)?;461 unreachable!()462 };463 if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {464 return Ok(bytes.0.as_slice().into());465 }466 <Self as Typed>::TYPE.check(&value)?;467 // Any::downcast_ref::<ByteArray>(&a);468 let mut out = Vec::with_capacity(a.len());469 for e in a.iter() {470 let r = e?;471 out.push(u8::from_untyped(r)?);472 }473 Ok(out.as_slice().into())474 }475}476477pub struct M1;478impl Typed for M1 {479 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));480481 fn into_untyped(_: Self) -> Result<Val> {482 Ok(Val::Num(NumValue::new(-1.0).expect("finite")))483 }484485 fn from_untyped(value: Val) -> Result<Self> {486 <Self as Typed>::TYPE.check(&value)?;487 Ok(Self)488 }489}490491macro_rules! decl_either {492 ($($name: ident, $($id: ident)*);*) => {$(493 #[derive(Clone)]494 pub enum $name<$($id),*> {495 $($id($id)),*496 }497 impl<$($id),*> Typed for $name<$($id),*>498 where499 $($id: Typed,)*500 {501 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);502503 fn into_untyped(value: Self) -> Result<Val> {504 match value {$(505 $name::$id(v) => $id::into_untyped(v)506 ),*}507 }508509 fn from_untyped(value: Val) -> Result<Self> {510 $(511 if $id::TYPE.check(&value).is_ok() {512 $id::from_untyped(value).map(Self::$id)513 } else514 )* {515 <Self as Typed>::TYPE.check(&value)?;516 unreachable!()517 }518 }519 }520 )*}521}522decl_either!(523 Either1, A;524 Either2, A B;525 Either3, A B C;526 Either4, A B C D;527 Either5, A B C D E;528 Either6, A B C D E F;529 Either7, A B C D E F G530);531#[macro_export]532macro_rules! Either {533 ($a:ty) => {$crate::typed::Either1<$a>};534 ($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};535 ($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};536 ($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};537 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};538 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};539 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};540}541pub use Either;542543pub type MyType = Either![u32, f64, String];544545impl Typed for ArrValue {546 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);547548 fn into_untyped(value: Self) -> Result<Val> {549 Ok(Val::Arr(value))550 }551552 fn from_untyped(value: Val) -> Result<Self> {553 <Self as Typed>::TYPE.check(&value)?;554 match value {555 Val::Arr(a) => Ok(a),556 _ => unreachable!(),557 }558 }559}560561impl Typed for FuncVal {562 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);563564 fn into_untyped(value: Self) -> Result<Val> {565 Ok(Val::Func(value))566 }567568 fn from_untyped(value: Val) -> Result<Self> {569 <Self as Typed>::TYPE.check(&value)?;570 match value {571 Val::Func(a) => Ok(a),572 _ => unreachable!(),573 }574 }575}576577impl Typed for Cc<FuncDesc> {578 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);579580 fn into_untyped(value: Self) -> Result<Val> {581 Ok(Val::Func(FuncVal::Normal(value)))582 }583584 fn from_untyped(value: Val) -> Result<Self> {585 <Self as Typed>::TYPE.check(&value)?;586 match value {587 Val::Func(FuncVal::Normal(desc)) => Ok(desc),588 Val::Func(_) => bail!("expected normal function, not builtin"),589 _ => unreachable!(),590 }591 }592}593594impl Typed for ObjValue {595 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);596597 fn into_untyped(value: Self) -> Result<Val> {598 Ok(Val::Obj(value))599 }600601 fn from_untyped(value: Val) -> Result<Self> {602 <Self as Typed>::TYPE.check(&value)?;603 match value {604 Val::Obj(a) => Ok(a),605 _ => unreachable!(),606 }607 }608}609610impl Typed for bool {611 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);612613 fn into_untyped(value: Self) -> Result<Val> {614 Ok(Val::Bool(value))615 }616617 fn from_untyped(value: Val) -> Result<Self> {618 <Self as Typed>::TYPE.check(&value)?;619 match value {620 Val::Bool(a) => Ok(a),621 _ => unreachable!(),622 }623 }624}625impl Typed for IndexableVal {626 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[627 &ComplexValType::Simple(ValType::Arr),628 &ComplexValType::Simple(ValType::Str),629 ]);630631 fn into_untyped(value: Self) -> Result<Val> {632 match value {633 Self::Str(s) => Ok(Val::string(s)),634 Self::Arr(a) => Ok(Val::Arr(a)),635 }636 }637638 fn from_untyped(value: Val) -> Result<Self> {639 <Self as Typed>::TYPE.check(&value)?;640 value.into_indexable()641 }642}643644pub struct Null;645impl Typed for Null {646 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);647648 fn into_untyped(_: Self) -> Result<Val> {649 Ok(Val::Null)650 }651652 fn from_untyped(value: Val) -> Result<Self> {653 <Self as Typed>::TYPE.check(&value)?;654 Ok(Self)655 }656}657658impl<T> Typed for Option<T>659where660 T: Typed,661{662 const TYPE: &'static ComplexValType =663 &ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);664665 fn into_untyped(typed: Self) -> Result<Val> {666 typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))667 }668669 fn from_untyped(untyped: Val) -> Result<Self> {670 if matches!(untyped, Val::Null) {671 Ok(None)672 } else {673 T::from_untyped(untyped).map(Some)674 }675 }676}677678#[derive(Debug, Trace, Clone)]679pub struct NativeFn<D: 'static>(pub(crate) PreparedFuncVal, PhantomData<D>);680macro_rules! impl_native_desc {681 ($i:expr; $($gen:ident)*) => {682 impl<$($gen,)* O> NativeFn<($($gen,)* O,)>683 where684 $($gen: Typed,)*685 O: Typed,686 {687 pub fn call(688 &self,689 $($gen: $gen,)*690 ) -> Result<O> {691 let val = self.0.call(692 CallLocation::native(),693 &[$(Typed::into_lazy_untyped($gen),)*],694 &[],695 )?;696 O::from_untyped(val)697 }698 }699 impl<$($gen,)* O> Typed for NativeFn<($($gen,)* O,)> {700 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);701702 fn into_untyped(_typed: Self) -> Result<Val> {703 bail!("can only convert functions from jsonnet to native")704 }705706 fn from_untyped(untyped: Val) -> Result<Self> {707 let func = FuncVal::from_untyped(untyped)?;708 Ok(Self(709 PreparedFuncVal::new(func, $i, &[])?,710 PhantomData,711 ))712 }713 }714 };715 ($i:expr; $($cur:ident)* @ $c:ident $($rest:ident)*) => {716 impl_native_desc!($i; $($cur)*);717 impl_native_desc!($i + 1; $($cur)* $c @ $($rest)*);718 };719 ($i:expr; $($cur:ident)* @) => {720 impl_native_desc!($i; $($cur)*);721 }722}723724impl_native_desc! {725 0; @ A B C D E F G H I J K L726}727728mod native_macro {729 #[macro_export]730 macro_rules! NativeFn {731 (($($t:ty),* $(,)?) -> $res:ty) => {732 NativeFn<($($t,)* $res)>733 }734 }735}736pub use crate::NativeFn;737738impl Typed for NumValue {739 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);740741 fn into_untyped(typed: Self) -> Result<Val> {742 Ok(Val::Num(typed))743 }744745 fn from_untyped(untyped: Val) -> Result<Self> {746 Self::TYPE.check(&untyped)?;747 match untyped {748 Val::Num(v) => Ok(v),749 _ => unreachable!(),750 }751 }752}1use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 arr::{ArrValue, BytesArray},10 bail,11 function::{FuncDesc, FuncVal},12 typed::CheckType,13 val::{IndexableVal, NumValue, StrValue, ThunkMapper},14 ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,15};1617#[derive(Trace)]18struct FromUntyped<K: Trace>(PhantomData<fn() -> K>);19impl<K> ThunkMapper<Val> for FromUntyped<K>20where21 K: Typed + Trace,22{23 type Output = K;2425 fn map(self, from: Val) -> Result<Self::Output> {26 K::from_untyped(from)27 }28}29impl<K: Trace> Default for FromUntyped<K> {30 fn default() -> Self {31 Self(PhantomData)32 }33}3435pub trait TypedObj: Typed {36 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;37 fn parse(obj: &ObjValue) -> Result<Self>;38 fn into_object(self) -> Result<ObjValue> {39 let mut builder = ObjValueBuilder::new();40 self.serialize(&mut builder)?;41 Ok(builder.build())42 }43}4445pub trait Typed: Sized {46 const TYPE: &'static ComplexValType;47 fn into_untyped(typed: Self) -> Result<Val>;48 fn into_lazy_untyped(typed: Self) -> Thunk<Val> {49 Thunk::from(Self::into_untyped(typed))50 }51 fn from_untyped(untyped: Val) -> Result<Self>;52 fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {53 Self::from_untyped(lazy.evaluate()?)54 }5556 // Whatever caller should use `into_lazy_untyped` instead of `into_untyped`57 fn provides_lazy() -> bool {58 false59 }6061 // Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible62 fn wants_lazy() -> bool {63 false64 }6566 /// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result67 /// This method returns identity in impl Typed for Result, and should not be overriden68 #[doc(hidden)]69 fn into_result(typed: Self) -> Result<Val> {70 let value = Self::into_untyped(typed)?;71 Ok(value)72 }73}7475impl<T> Typed for Thunk<T>76where77 T: Typed + Trace + Clone,78{79 const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);8081 fn into_untyped(typed: Self) -> Result<Val> {82 T::into_untyped(typed.evaluate()?)83 }8485 fn from_untyped(untyped: Val) -> Result<Self> {86 Self::from_lazy_untyped(Thunk::evaluated(untyped))87 }8889 fn provides_lazy() -> bool {90 true91 }9293 fn into_lazy_untyped(inner: Self) -> Thunk<Val> {94 #[derive(Trace)]95 struct IntoUntyped<K: Trace>(PhantomData<fn() -> K>);96 impl<K> ThunkMapper<K> for IntoUntyped<K>97 where98 K: Typed + Trace,99 {100 type Output = Val;101102 fn map(self, from: K) -> Result<Self::Output> {103 K::into_untyped(from)104 }105 }106 impl<K: Trace> Default for IntoUntyped<K> {107 fn default() -> Self {108 Self(PhantomData)109 }110 }111 inner.map(<IntoUntyped<T>>::default())112 }113114 fn wants_lazy() -> bool {115 true116 }117118 fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {119 Ok(inner.map(<FromUntyped<T>>::default()))120 }121}122123pub const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS)) - 1) as f64;124pub const MIN_SAFE_INTEGER: f64 = (-((1i64 << (f64::MANTISSA_DIGITS)) - 1)) as f64;125126macro_rules! impl_int {127 ($($ty:ty)*) => {$(128 impl Typed for $ty {129 const TYPE: &'static ComplexValType =130 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));131 fn from_untyped(value: Val) -> Result<Self> {132 <Self as Typed>::TYPE.check(&value)?;133 match value {134 Val::Num(n) => {135 let n = n.get();136 #[allow(clippy::float_cmp)]137 if n.trunc() != n {138 bail!(139 "cannot convert number with fractional part to {}",140 stringify!($ty)141 )142 }143 Ok(n as Self)144 }145 _ => unreachable!(),146 }147 }148 fn into_untyped(value: Self) -> Result<Val> {149 Ok(Val::Num(value.into()))150 }151 }152 )*};153}154155impl_int!(i8 u8 i16 u16 i32 u32);156157macro_rules! impl_bounded_int {158 ($($name:ident = $ty:ty)*) => {$(159 #[derive(Clone, Copy)]160 pub struct $name<const MIN: $ty, const MAX: $ty>($ty);161 impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {162 pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {163 if value >= MIN && value <= MAX {164 Some(Self(value))165 } else {166 None167 }168 }169 pub const fn value(self) -> $ty {170 self.0171 }172 }173 impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {174 type Target = $ty;175 fn deref(&self) -> &Self::Target {176 &self.0177 }178 }179180 impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {181 const TYPE: &'static ComplexValType =182 &ComplexValType::BoundedNumber(183 Some(MIN as f64),184 Some(MAX as f64),185 );186187 fn from_untyped(value: Val) -> Result<Self> {188 <Self as Typed>::TYPE.check(&value)?;189 match value {190 Val::Num(n) => {191 let n = n.get();192 #[allow(clippy::float_cmp)]193 if n.trunc() != n {194 bail!(195 "cannot convert number with fractional part to {}",196 stringify!($ty)197 )198 }199 Ok(Self(n as $ty))200 }201 _ => unreachable!(),202 }203 }204205 #[allow(clippy::cast_lossless)]206 fn into_untyped(value: Self) -> Result<Val> {207 Ok(Val::try_num(value.0)?)208 }209 }210 )*};211}212213impl_bounded_int!(214 BoundedI8 = i8215 BoundedI16 = i16216 BoundedI32 = i32217 BoundedI64 = i64218 BoundedUsize = usize219);220221impl Typed for f64 {222 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);223224 fn into_untyped(value: Self) -> Result<Val> {225 Ok(Val::try_num(value)?)226 }227228 fn from_untyped(value: Val) -> Result<Self> {229 <Self as Typed>::TYPE.check(&value)?;230 match value {231 Val::Num(n) => Ok(n.get()),232 _ => unreachable!(),233 }234 }235}236237pub struct PositiveF64(pub f64);238impl Typed for PositiveF64 {239 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);240241 fn into_untyped(value: Self) -> Result<Val> {242 Ok(Val::try_num(value.0)?)243 }244245 fn from_untyped(value: Val) -> Result<Self> {246 <Self as Typed>::TYPE.check(&value)?;247 match value {248 Val::Num(n) => Ok(Self(n.get())),249 _ => unreachable!(),250 }251 }252}253impl Typed for usize {254 const TYPE: &'static ComplexValType =255 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));256257 fn into_untyped(value: Self) -> Result<Val> {258 Ok(Val::try_num(value)?)259 }260261 fn from_untyped(value: Val) -> Result<Self> {262 <Self as Typed>::TYPE.check(&value)?;263 match value {264 Val::Num(n) => {265 let n = n.get();266 #[allow(clippy::float_cmp)]267 if n.trunc() != n {268 bail!("cannot convert number with fractional part to usize")269 }270 Ok(n as Self)271 }272 _ => unreachable!(),273 }274 }275}276277impl Typed for IStr {278 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);279280 fn into_untyped(value: Self) -> Result<Val> {281 Ok(Val::string(value))282 }283284 fn from_untyped(value: Val) -> Result<Self> {285 <Self as Typed>::TYPE.check(&value)?;286 match value {287 Val::Str(s) => Ok(s.into_flat()),288 _ => unreachable!(),289 }290 }291}292293impl Typed for String {294 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);295296 fn into_untyped(value: Self) -> Result<Val> {297 Ok(Val::string(value))298 }299300 fn from_untyped(value: Val) -> Result<Self> {301 <Self as Typed>::TYPE.check(&value)?;302 match value {303 Val::Str(s) => Ok(s.to_string()),304 _ => unreachable!(),305 }306 }307}308309impl Typed for StrValue {310 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);311312 fn into_untyped(value: Self) -> Result<Val> {313 Ok(Val::Str(value))314 }315316 fn from_untyped(value: Val) -> Result<Self> {317 <Self as Typed>::TYPE.check(&value)?;318 match value {319 Val::Str(s) => Ok(s),320 _ => unreachable!(),321 }322 }323}324325impl Typed for char {326 const TYPE: &'static ComplexValType = &ComplexValType::Char;327328 fn into_untyped(value: Self) -> Result<Val> {329 Ok(Val::string(value))330 }331332 fn from_untyped(value: Val) -> Result<Self> {333 <Self as Typed>::TYPE.check(&value)?;334 match value {335 Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),336 _ => unreachable!(),337 }338 }339}340341impl<T> Typed for Vec<T>342where343 T: Typed,344{345 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);346347 fn into_untyped(value: Self) -> Result<Val> {348 Ok(Val::Arr(349 value350 .into_iter()351 .map(T::into_untyped)352 .collect::<Result<ArrValue>>()?,353 ))354 }355356 fn from_untyped(value: Val) -> Result<Self> {357 let Val::Arr(a) = value else {358 <Self as Typed>::TYPE.check(&value)?;359 unreachable!("typecheck should fail")360 };361 a.iter()362 .enumerate()363 .map(|(i, r)| {364 r.and_then(|t| {365 T::from_untyped(t).with_description(|| format!("parsing elem <{i}>"))366 })367 })368 .collect::<Result<Self>>()369 }370}371372impl<K: Typed + Ord, V: Typed> Typed for BTreeMap<K, V> {373 const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);374375 fn into_untyped(typed: Self) -> Result<Val> {376 let mut out = ObjValueBuilder::with_capacity(typed.len());377 for (k, v) in typed {378 let Some(key) = K::into_untyped(k)?.as_str() else {379 bail!("map key should serialize to string");380 };381 let value = V::into_untyped(v)?;382 out.field(key).value(value);383 }384 Ok(Val::Obj(out.build()))385 }386387 fn from_untyped(value: Val) -> Result<Self> {388 Self::TYPE.check(&value)?;389 let obj = value.as_obj().expect("typecheck should fail");390391 let mut out = Self::new();392 if V::wants_lazy() {393 for key in obj.fields_ex(394 false,395 #[cfg(feature = "exp-preserve-order")]396 false,397 ) {398 let value = obj.get_lazy(key.clone()).expect("field exists");399 let value = V::from_lazy_untyped(value)?;400 let key = K::from_untyped(Val::Str(key.into()))?;401 let _ = out.insert(key, value);402 }403 } else {404 for (key, value) in obj.iter(405 #[cfg(feature = "exp-preserve-order")]406 false,407 ) {408 let key = K::from_untyped(Val::Str(key.into()))?;409 let value = V::from_untyped(value?)?;410 let _ = out.insert(key, value);411 }412 }413 Ok(out)414 }415}416417impl Typed for Val {418 const TYPE: &'static ComplexValType = &ComplexValType::Any;419420 fn into_untyped(typed: Self) -> Result<Val> {421 Ok(typed)422 }423 fn from_untyped(untyped: Val) -> Result<Self> {424 Ok(untyped)425 }426}427428// Hack429#[doc(hidden)]430impl<T> Typed for Result<T>431where432 T: Typed,433{434 const TYPE: &'static ComplexValType = &ComplexValType::Any;435436 fn into_untyped(_typed: Self) -> Result<Val> {437 panic!("do not use this conversion")438 }439440 fn from_untyped(_untyped: Val) -> Result<Self> {441 panic!("do not use this conversion")442 }443444 fn into_result(typed: Self) -> Result<Val> {445 typed.map(T::into_untyped)?446 }447}448449/// Specialization450impl Typed for IBytes {451 const TYPE: &'static ComplexValType =452 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));453454 fn into_untyped(value: Self) -> Result<Val> {455 Ok(Val::Arr(ArrValue::bytes(value)))456 }457458 fn from_untyped(value: Val) -> Result<Self> {459 let Val::Arr(a) = &value else {460 <Self as Typed>::TYPE.check(&value)?;461 unreachable!()462 };463 if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {464 return Ok(bytes.0.as_slice().into());465 }466 <Self as Typed>::TYPE.check(&value)?;467 // Any::downcast_ref::<ByteArray>(&a);468 let mut out = Vec::with_capacity(a.len());469 for e in a.iter() {470 let r = e?;471 out.push(u8::from_untyped(r)?);472 }473 Ok(out.as_slice().into())474 }475}476477pub struct M1;478impl Typed for M1 {479 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));480481 fn into_untyped(_: Self) -> Result<Val> {482 Ok(Val::Num(NumValue::new(-1.0).expect("finite")))483 }484485 fn from_untyped(value: Val) -> Result<Self> {486 <Self as Typed>::TYPE.check(&value)?;487 Ok(Self)488 }489}490491macro_rules! decl_either {492 ($($name: ident, $($id: ident)*);*) => {$(493 #[derive(Clone)]494 pub enum $name<$($id),*> {495 $($id($id)),*496 }497 impl<$($id),*> Typed for $name<$($id),*>498 where499 $($id: Typed,)*500 {501 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);502503 fn into_untyped(value: Self) -> Result<Val> {504 match value {$(505 $name::$id(v) => $id::into_untyped(v)506 ),*}507 }508509 fn from_untyped(value: Val) -> Result<Self> {510 $(511 if $id::TYPE.check(&value).is_ok() {512 $id::from_untyped(value).map(Self::$id)513 } else514 )* {515 <Self as Typed>::TYPE.check(&value)?;516 unreachable!()517 }518 }519 }520 )*}521}522decl_either!(523 Either1, A;524 Either2, A B;525 Either3, A B C;526 Either4, A B C D;527 Either5, A B C D E;528 Either6, A B C D E F;529 Either7, A B C D E F G530);531#[macro_export]532macro_rules! Either {533 ($a:ty) => {$crate::typed::Either1<$a>};534 ($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};535 ($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};536 ($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};537 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};538 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};539 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};540}541pub use Either;542543pub type MyType = Either![u32, f64, String];544545impl Typed for ArrValue {546 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);547548 fn into_untyped(value: Self) -> Result<Val> {549 Ok(Val::Arr(value))550 }551552 fn from_untyped(value: Val) -> Result<Self> {553 <Self as Typed>::TYPE.check(&value)?;554 match value {555 Val::Arr(a) => Ok(a),556 _ => unreachable!(),557 }558 }559}560561impl Typed for FuncVal {562 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);563564 fn into_untyped(value: Self) -> Result<Val> {565 Ok(Val::Func(value))566 }567568 fn from_untyped(value: Val) -> Result<Self> {569 <Self as Typed>::TYPE.check(&value)?;570 match value {571 Val::Func(a) => Ok(a),572 _ => unreachable!(),573 }574 }575}576577impl Typed for Cc<FuncDesc> {578 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);579580 fn into_untyped(value: Self) -> Result<Val> {581 Ok(Val::Func(FuncVal::Normal(value)))582 }583584 fn from_untyped(value: Val) -> Result<Self> {585 <Self as Typed>::TYPE.check(&value)?;586 match value {587 Val::Func(FuncVal::Normal(desc)) => Ok(desc),588 Val::Func(_) => bail!("expected normal function, not builtin"),589 _ => unreachable!(),590 }591 }592}593594impl Typed for ObjValue {595 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);596597 fn into_untyped(value: Self) -> Result<Val> {598 Ok(Val::Obj(value))599 }600601 fn from_untyped(value: Val) -> Result<Self> {602 <Self as Typed>::TYPE.check(&value)?;603 match value {604 Val::Obj(a) => Ok(a),605 _ => unreachable!(),606 }607 }608}609610impl Typed for bool {611 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);612613 fn into_untyped(value: Self) -> Result<Val> {614 Ok(Val::Bool(value))615 }616617 fn from_untyped(value: Val) -> Result<Self> {618 <Self as Typed>::TYPE.check(&value)?;619 match value {620 Val::Bool(a) => Ok(a),621 _ => unreachable!(),622 }623 }624}625impl Typed for IndexableVal {626 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[627 &ComplexValType::Simple(ValType::Arr),628 &ComplexValType::Simple(ValType::Str),629 ]);630631 fn into_untyped(value: Self) -> Result<Val> {632 match value {633 Self::Str(s) => Ok(Val::string(s)),634 Self::Arr(a) => Ok(Val::Arr(a)),635 }636 }637638 fn from_untyped(value: Val) -> Result<Self> {639 <Self as Typed>::TYPE.check(&value)?;640 value.into_indexable()641 }642}643644pub struct Null;645impl Typed for Null {646 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);647648 fn into_untyped(_: Self) -> Result<Val> {649 Ok(Val::Null)650 }651652 fn from_untyped(value: Val) -> Result<Self> {653 <Self as Typed>::TYPE.check(&value)?;654 Ok(Self)655 }656}657658impl<T> Typed for Option<T>659where660 T: Typed,661{662 const TYPE: &'static ComplexValType =663 &ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);664665 fn into_untyped(typed: Self) -> Result<Val> {666 typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))667 }668669 fn from_untyped(untyped: Val) -> Result<Self> {670 if matches!(untyped, Val::Null) {671 Ok(None)672 } else {673 T::from_untyped(untyped).map(Some)674 }675 }676}677678impl Typed for NumValue {679 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);680681 fn into_untyped(typed: Self) -> Result<Val> {682 Ok(Val::Num(typed))683 }684685 fn from_untyped(untyped: Val) -> Result<Self> {686 Self::TYPE.check(&untyped)?;687 match untyped {688 Val::Num(v) => Ok(v),689 _ => unreachable!(),690 }691 }692}crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -405,7 +405,7 @@
const _: () = {
use ::jrsonnet_evaluator::{
State, Val,
- function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation, ArgsLike, parse::parse_builtin_call},
+ function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation},
Result, Context, typed::Typed,
parser::Span, params, Thunk,
};
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -2,9 +2,9 @@
use jrsonnet_evaluator::{
bail,
- function::{builtin, FuncVal},
+ function::{builtin, FuncVal, NativeFn},
runtime_error,
- typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
+ typed::{BoundedI32, BoundedUsize, Either2, Typed},
val::{equals, ArrValue, IndexableVal},
Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -13,7 +13,8 @@
pub use hash::*;
use jrsonnet_evaluator::{
error::Result,
- function::{CallLocation, FuncVal, TlaArg},
+ function::{CallLocation, FuncVal},
+ tla::TlaArg,
trace::PathResolver,
val::NumValue,
ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,
tests/tests/cpp_test_suite.rsdiffbeforeafterboth--- a/tests/tests/cpp_test_suite.rs
+++ b/tests/tests/cpp_test_suite.rs
@@ -6,10 +6,10 @@
use jrsonnet_evaluator::{
FileImportResolver, IStr, ObjValueBuilder, State, Val, apply_tla,
- function::TlaArg,
gc::WithCapacityExt as _,
manifest::JsonFormat,
rustc_hash::FxHashMap,
+ tla::TlaArg,
trace::{CompactFormat, PathResolver, TraceFormat},
};
use jrsonnet_stdlib::ContextInitializer;