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.rsdiffbeforeafterboth1//! jsonnet interpreter implementation2#![cfg_attr(nightly, feature(thread_local, type_alias_impl_trait))]34// For jrsonnet-macros5extern crate self as jrsonnet_evaluator;67mod arr;8pub mod async_import;9mod ctx;10mod dynamic;11pub mod error;12mod evaluate;13pub mod function;14pub mod gc;15mod import;16mod integrations;17pub mod manifest;18mod map;19mod obj;20pub mod stack;21pub mod stdlib;22mod tla;23pub mod trace;24pub mod typed;25pub mod val;2627use std::{28 any::Any,29 cell::{RefCell, RefMut},30 clone::Clone,31 collections::hash_map::Entry,32 fmt::{self, Debug},33 marker::PhantomData,34 rc::Rc,35};3637pub use ctx::*;38pub use dynamic::*;39pub use error::{Error, ErrorKind::*, Result, ResultExt};40pub use evaluate::*;41use function::CallLocation;42pub use import::*;43use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};44pub use jrsonnet_interner::{IBytes, IStr};45#[doc(hidden)]46pub use jrsonnet_macros;47pub use jrsonnet_parser as parser;48use jrsonnet_parser::{Expr, ParserSettings, Source, SourcePath, Spanned};49pub use obj::*;50pub use rustc_hash;51use rustc_hash::FxHashMap;52use stack::check_depth;53pub use tla::apply_tla;54pub use val::{Thunk, Val};5556use crate::gc::WithCapacityExt as _;5758cc_dyn!(59 #[derive(Clone)]60 CcUnbound<V>,61 Unbound<Bound = V>62);6364/// Thunk without bound `super`/`this`65/// object inheritance may be overriden multiple times, and will be fixed only on field read66pub trait Unbound: Trace {67 /// Type of value after object context is bound68 type Bound;69 /// Create value bound to specified object context70 fn bind(&self, sup_this: SupThis) -> Result<Self::Bound>;71}7273/// Object fields may, or may not depend on `this`/`super`, this enum allows cheaper reuse of object-independent fields for native code74/// Standard jsonnet fields are always unbound75#[derive(Clone, Trace)]76pub enum MaybeUnbound {77 /// Value needs to be bound to `this`/`super`78 Unbound(CcUnbound<Val>),79 /// Value is object-independent80 Bound(Thunk<Val>),81}8283impl Debug for MaybeUnbound {84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {85 write!(f, "MaybeUnbound")86 }87}88impl MaybeUnbound {89 /// Attach object context to value, if required90 pub fn evaluate(&self, sup_this: SupThis) -> Result<Val> {91 match self {92 Self::Unbound(v) => v.0.bind(sup_this),93 Self::Bound(v) => Ok(v.evaluate()?),94 }95 }96}9798cc_dyn!(CcContextInitializer, ContextInitializer);99100/// During import, this trait will be called to create initial context for file.101/// It may initialize global variables, stdlib for example.102pub trait ContextInitializer: Trace {103 /// For which size the builder should be preallocated104 fn reserve_vars(&self) -> usize {105 0106 }107 /// Initialize default file context.108 /// Has default implementation, which calls `populate`.109 /// Prefer to always implement `populate` instead.110 fn initialize(&self, for_file: Source) -> Context {111 let mut builder = ContextBuilder::with_capacity(self.reserve_vars());112 self.populate(for_file, &mut builder);113 builder.build()114 }115 /// For composability: extend builder. May panic if this initialization is not supported,116 /// and the context may only be created via `initialize`.117 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);118 /// Allows upcasting from abstract to concrete context initializer.119 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.120 fn as_any(&self) -> &dyn Any;121}122123/// Context initializer which adds nothing.124impl ContextInitializer for () {125 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}126 fn as_any(&self) -> &dyn Any {127 self128 }129}130131impl<T> ContextInitializer for Option<T>132where133 T: ContextInitializer,134{135 fn initialize(&self, for_file: Source) -> Context {136 if let Some(ctx) = self {137 ctx.initialize(for_file)138 } else {139 ().initialize(for_file)140 }141 }142143 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {144 if let Some(ctx) = self {145 ctx.populate(for_file, builder);146 }147 }148149 fn as_any(&self) -> &dyn Any {150 self151 }152}153154macro_rules! impl_context_initializer {155 ($($gen:ident)*) => {156 #[allow(non_snake_case)]157 impl<$($gen: ContextInitializer + Trace,)*> ContextInitializer for ($($gen,)*) {158 fn reserve_vars(&self) -> usize {159 let mut out = 0;160 let ($($gen,)*) = self;161 $(out += $gen.reserve_vars();)*162 out163 }164 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {165 let ($($gen,)*) = self;166 $($gen.populate(for_file.clone(), builder);)*167 }168 fn as_any(&self) -> &dyn Any {169 self170 }171 }172 };173 ($($cur:ident)* @ $c:ident $($rest:ident)*) => {174 impl_context_initializer!($($cur)*);175 impl_context_initializer!($($cur)* $c @ $($rest)*);176 };177 ($($cur:ident)* @) => {178 impl_context_initializer!($($cur)*);179 }180}181impl_context_initializer! {182 A @ B C D E F G183}184185#[derive(Trace)]186struct FileData {187 string: Option<IStr>,188 bytes: Option<IBytes>,189 parsed: Option<Rc<Spanned<Expr>>>,190 evaluated: Option<Val>,191192 evaluating: bool,193}194impl FileData {195 fn new_string(data: IStr) -> Self {196 Self {197 string: Some(data),198 bytes: None,199 parsed: None,200 evaluated: None,201 evaluating: false,202 }203 }204 fn new_bytes(data: IBytes) -> Self {205 Self {206 string: None,207 bytes: Some(data),208 parsed: None,209 evaluated: None,210 evaluating: false,211 }212 }213 pub(crate) fn get_string(&mut self) -> Option<IStr> {214 if self.string.is_none() {215 self.string = Some(216 self.bytes217 .as_ref()218 .expect("either string or bytes should be set")219 .clone()220 .cast_str()?,221 );222 }223 Some(self.string.clone().expect("just set"))224 }225}226227#[derive(Trace)]228pub struct EvaluationStateInternals {229 /// Internal state230 file_cache: RefCell<FxHashMap<SourcePath, FileData>>,231 /// Context initializer, which will be used for imports and everything232 /// [`NoopContextInitializer`] is used by default, most likely you want to have `jrsonnet-stdlib`233 context_initializer: CcContextInitializer,234 /// Used to resolve file locations/contents235 import_resolver: Rc<dyn ImportResolver>,236}237238/// Maintains stack trace and import resolution239#[derive(Clone, Trace)]240pub struct State(Cc<EvaluationStateInternals>);241242thread_local! {243 pub static DEFAULT_STATE: State = State::builder().build();244 pub static STATE: RefCell<Option<State>> = const {RefCell::new(None)};245}246pub struct StateEnterGuard(PhantomData<()>);247impl Drop for StateEnterGuard {248 fn drop(&mut self) {249 STATE.with_borrow_mut(|v| *v = None);250 }251}252253pub fn with_state<V>(v: impl FnOnce(State) -> V) -> V {254 if let Some(state) = STATE.with_borrow(Clone::clone) {255 v(state)256 } else {257 let s = DEFAULT_STATE.with(Clone::clone);258 v(s)259 }260}261262impl State {263 pub fn enter(&self) -> StateEnterGuard {264 self.try_enter().expect("entered state already exists")265 }266 pub fn try_enter(&self) -> Option<StateEnterGuard> {267 STATE.with_borrow_mut(|v| {268 if v.is_none() {269 *v = Some(self.clone());270 Some(StateEnterGuard(PhantomData))271 } else {272 None273 }274 })275 }276 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise277 pub fn import_resolved_str(&self, path: SourcePath) -> Result<IStr> {278 let mut file_cache = self.file_cache();279 let mut file = file_cache.entry(path.clone());280281 let file = match file {282 Entry::Occupied(ref mut d) => d.get_mut(),283 Entry::Vacant(v) => {284 let data = self.import_resolver().load_file_contents(&path)?;285 v.insert(FileData::new_string(286 std::str::from_utf8(&data)287 .map_err(|_| ImportBadFileUtf8(path.clone()))?288 .into(),289 ))290 }291 };292 Ok(file293 .get_string()294 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?)295 }296 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise297 pub fn import_resolved_bin(&self, path: SourcePath) -> Result<IBytes> {298 let mut file_cache = self.file_cache();299 let mut file = file_cache.entry(path.clone());300301 let file = match file {302 Entry::Occupied(ref mut d) => d.get_mut(),303 Entry::Vacant(v) => {304 let data = self.import_resolver().load_file_contents(&path)?;305 v.insert(FileData::new_bytes(data.as_slice().into()))306 }307 };308 if let Some(str) = &file.bytes {309 return Ok(str.clone());310 }311 if file.bytes.is_none() {312 file.bytes = Some(313 file.string314 .as_ref()315 .expect("either string or bytes should be set")316 .clone()317 .cast_bytes(),318 );319 }320 Ok(file.bytes.as_ref().expect("just set").clone())321 }322 /// Should only be called with path retrieved from [`resolve_path`], may panic otherwise323 pub fn import_resolved(&self, path: SourcePath) -> Result<Val> {324 let mut file_cache = self.file_cache();325 let mut file = file_cache.entry(path.clone());326327 let file = match file {328 Entry::Occupied(ref mut d) => d.get_mut(),329 Entry::Vacant(v) => {330 let data = self.import_resolver().load_file_contents(&path)?;331 v.insert(FileData::new_string(332 std::str::from_utf8(&data)333 .map_err(|_| ImportBadFileUtf8(path.clone()))?334 .into(),335 ))336 }337 };338 if let Some(val) = &file.evaluated {339 return Ok(val.clone());340 }341 let code = file342 .get_string()343 .ok_or_else(|| ImportBadFileUtf8(path.clone()))?;344 let file_name = Source::new(path.clone(), code.clone());345 if file.parsed.is_none() {346 file.parsed = Some(347 jrsonnet_parser::parse(348 &code,349 &ParserSettings {350 source: file_name.clone(),351 },352 )353 .map(Rc::new)354 .map_err(|e| ImportSyntaxError {355 path: file_name.clone(),356 error: Box::new(e),357 })?,358 );359 }360 let parsed = file.parsed.as_ref().expect("just set").clone();361 if file.evaluating {362 bail!(InfiniteRecursionDetected)363 }364 file.evaluating = true;365 // Dropping file cache guard here, as evaluation may use this map too366 drop(file_cache);367 let res = evaluate(self.create_default_context(file_name), &parsed);368369 let mut file_cache = self.file_cache();370 let mut file = file_cache.entry(path);371372 let Entry::Occupied(file) = &mut file else {373 unreachable!("this file was just here")374 };375 let file = file.get_mut();376 file.evaluating = false;377 match res {378 Ok(v) => {379 file.evaluated = Some(v.clone());380 Ok(v)381 }382 Err(e) => Err(e),383 }384 }385386 /// Has same semantics as `import 'path'` called from `from` file387 pub fn import_from(&self, from: &SourcePath, path: impl AsPathLike) -> Result<Val> {388 let resolved = self.resolve_from(from, &path)?;389 self.import_resolved(resolved)390 }391 pub fn import(&self, path: impl AsPathLike) -> Result<Val> {392 let resolved = self.resolve_from_default(&path)?;393 self.import_resolved(resolved)394 }395396 /// Creates context with all passed global variables397 pub fn create_default_context(&self, source: Source) -> Context {398 self.context_initializer().initialize(source)399 }400401 /// Creates context with all passed global variables, calling custom modifier402 pub fn create_default_context_with(403 &self,404 source: Source,405 context_initializer: impl ContextInitializer,406 ) -> Context {407 let default_initializer = self.context_initializer();408 let mut builder = ContextBuilder::with_capacity(409 default_initializer.reserve_vars() + context_initializer.reserve_vars(),410 );411 default_initializer.populate(source.clone(), &mut builder);412 context_initializer.populate(source, &mut builder);413414 builder.build()415 }416}417418/// Internals419impl State {420 fn file_cache(&self) -> RefMut<'_, FxHashMap<SourcePath, FileData>> {421 self.0.file_cache.borrow_mut()422 }423}424/// Executes code creating a new stack frame, to be replaced with try{}425pub fn in_frame<T>(426 e: CallLocation<'_>,427 frame_desc: impl FnOnce() -> String,428 f: impl FnOnce() -> Result<T>,429) -> Result<T> {430 let _guard = check_depth()?;431432 f().with_description_src(e, frame_desc)433}434435/// Executes code creating a new stack frame, to be replaced with try{}436pub fn in_description_frame<T>(437 frame_desc: impl FnOnce() -> String,438 f: impl FnOnce() -> Result<T>,439) -> Result<T> {440 let _guard = check_depth()?;441442 f().with_description(frame_desc)443}444445#[derive(Trace)]446pub struct InitialUnderscore(pub Thunk<Val>);447impl ContextInitializer for InitialUnderscore {448 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {449 builder.bind("_", self.0.clone());450 }451452 fn as_any(&self) -> &dyn Any {453 self454 }455}456457/// Raw methods evaluate passed values but don't perform TLA execution458impl State {459 /// Parses and evaluates the given snippet460 pub fn evaluate_snippet(&self, name: impl Into<IStr>, code: impl Into<IStr>) -> Result<Val> {461 let code = code.into();462 let source = Source::new_virtual(name.into(), code.clone());463 let parsed = jrsonnet_parser::parse(464 &code,465 &ParserSettings {466 source: source.clone(),467 },468 )469 .map_err(|e| ImportSyntaxError {470 path: source.clone(),471 error: Box::new(e),472 })?;473 evaluate(self.create_default_context(source), &parsed)474 }475 /// Parses and evaluates the given snippet with custom context modifier476 pub fn evaluate_snippet_with(477 &self,478 name: impl Into<IStr>,479 code: impl Into<IStr>,480 context_initializer: impl ContextInitializer,481 ) -> Result<Val> {482 let code = code.into();483 let source = Source::new_virtual(name.into(), code.clone());484 let parsed = jrsonnet_parser::parse(485 &code,486 &ParserSettings {487 source: source.clone(),488 },489 )490 .map_err(|e| ImportSyntaxError {491 path: source.clone(),492 error: Box::new(e),493 })?;494 evaluate(495 self.create_default_context_with(source, context_initializer),496 &parsed,497 )498 }499}500501/// Settings utilities502impl State {503 // Only panics in case of [`ImportResolver`] contract violation504 #[allow(clippy::missing_panics_doc)]505 pub fn resolve_from(&self, from: &SourcePath, path: &dyn AsPathLike) -> Result<SourcePath> {506 self.import_resolver().resolve_from(from, path)507 }508 #[allow(clippy::missing_panics_doc)]509 pub fn resolve_from_default(&self, path: &dyn AsPathLike) -> Result<SourcePath> {510 self.import_resolver().resolve_from_default(path)511 }512 pub fn import_resolver(&self) -> &dyn ImportResolver {513 &*self.0.import_resolver514 }515 pub fn context_initializer(&self) -> &dyn ContextInitializer {516 &*self.0.context_initializer.0517 }518}519520impl State {521 pub fn builder() -> StateBuilder {522 StateBuilder::default()523 }524}525526impl Default for State {527 fn default() -> Self {528 Self::builder().build()529 }530}531532#[derive(Default)]533pub struct StateBuilder {534 import_resolver: Option<Rc<dyn ImportResolver>>,535 context_initializer: Option<CcContextInitializer>,536}537impl StateBuilder {538 pub fn import_resolver(&mut self, import_resolver: impl ImportResolver) -> &mut Self {539 let _ = self.import_resolver.insert(Rc::new(import_resolver));540 self541 }542 pub fn context_initializer(543 &mut self,544 context_initializer: impl ContextInitializer,545 ) -> &mut Self {546 let _ = self547 .context_initializer548 .insert(CcContextInitializer::new(context_initializer));549 self550 }551 pub fn build(mut self) -> State {552 State(Cc::new(EvaluationStateInternals {553 file_cache: RefCell::new(FxHashMap::new()),554 context_initializer: self555 .context_initializer556 .take()557 .unwrap_or_else(|| CcContextInitializer::new(())),558 import_resolver: self559 .import_resolver560 .take()561 .unwrap_or_else(|| Rc::new(DummyImportResolver)),562 }))563 }564}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.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::{CallLocation, FuncDesc, FuncVal, PreparedFuncVal},
+ function::{FuncDesc, FuncVal},
typed::CheckType,
val::{IndexableVal, NumValue, StrValue, ThunkMapper},
ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
@@ -671,69 +671,9 @@
Ok(None)
} else {
T::from_untyped(untyped).map(Some)
- }
- }
-}
-
-#[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 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;
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
@@ -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;