difftreelog
refactor move state to global
in: master
9 files changed
bindings/jsonnet/src/lib.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/lib.rs
+++ b/bindings/jsonnet/src/lib.rs
@@ -247,7 +247,7 @@
match vm
.state
.import(filename)
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val.manifest(&vm.manifest_format))
{
Ok(v) => {
@@ -282,7 +282,7 @@
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val.manifest(&vm.manifest_format))
{
Ok(v) => {
@@ -340,7 +340,7 @@
match vm
.state
.import(filename)
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val_to_multi(val, &vm.manifest_format))
{
Ok(v) => {
@@ -369,7 +369,7 @@
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val_to_multi(val, &vm.manifest_format))
{
Ok(v) => {
@@ -422,7 +422,7 @@
match vm
.state
.import(filename)
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val_to_stream(val, &vm.manifest_format))
{
Ok(v) => {
@@ -451,7 +451,7 @@
match vm
.state
.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())
- .and_then(|val| apply_tla(vm.state.clone(), &vm.tla_args, val))
+ .and_then(|val| apply_tla(&vm.tla_args, val))
.and_then(|val| val_to_stream(val, &vm.manifest_format))
{
Ok(v) => {
cmds/jrsonnet/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -173,6 +173,7 @@
let mut s = State::builder();
s.import_resolver(import_resolver).context_initializer(std);
let s = s.build();
+ let _s = s.enter();
let input = opts.input.input.ok_or(Error::MissingInputArgument)?;
let val = if opts.input.exec {
@@ -192,7 +193,7 @@
unused_mut,
clippy::redundant_clone,
)]
- let mut val = apply_tla(s.clone(), &tla, val)?;
+ let mut val = apply_tla(&tla, val)?;
#[cfg(feature = "exp-apply")]
for apply in opts.input.exp_apply {
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -6,12 +6,11 @@
use crate::{
error::ErrorKind::*, gc::WithCapacityExt as _, map::LayeredHashMap, ObjValue, Pending, Result,
- State, Thunk, Val,
+ Thunk, Val,
};
#[derive(Trace)]
struct ContextInternals {
- state: Option<State>,
dollar: Option<ObjValue>,
sup: Option<ObjValue>,
this: Option<ObjValue>,
@@ -33,13 +32,6 @@
Pending::new()
}
- pub fn state(&self) -> &State {
- self.0
- .state
- .as_ref()
- .expect("used state from dummy context")
- }
-
pub fn dollar(&self) -> Option<&ObjValue> {
self.0.dollar.as_ref()
}
@@ -112,7 +104,6 @@
ctx.bindings.clone().extend(new_bindings)
};
Self(Cc::new(ContextInternals {
- state: ctx.state.clone(),
dollar,
sup,
this,
@@ -128,34 +119,22 @@
}
pub struct ContextBuilder {
- state: Option<State>,
bindings: FxHashMap<IStr, Thunk<Val>>,
extend: Option<Context>,
}
impl ContextBuilder {
- /// # Panics
- /// Panics aren't directly caused by this function, but if state from resulting context is used
- pub fn dangerous_empty_state() -> Self {
- Self {
- state: None,
- bindings: FxHashMap::new(),
- extend: None,
- }
+ pub fn new() -> Self {
+ Self::with_capacity(0)
}
- pub fn new(state: State) -> Self {
- Self::with_capacity(state, 0)
- }
- pub fn with_capacity(state: State, capacity: usize) -> Self {
+ pub fn with_capacity(capacity: usize) -> Self {
Self {
- state: Some(state),
bindings: FxHashMap::with_capacity(capacity),
extend: None,
}
}
pub fn extend(parent: Context) -> Self {
Self {
- state: parent.0.state.clone(),
bindings: FxHashMap::new(),
extend: Some(parent),
}
@@ -173,7 +152,6 @@
parent.extend(self.bindings, None, None, None)
} else {
Context(Cc::new(ContextInternals {
- state: self.state,
bindings: LayeredHashMap::new(self.bindings),
dollar: None,
sup: None,
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,7 @@
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};
-use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};
+use crate::{evaluate, typed::Typed, with_state, Context, Result, Thunk, Val};
/// Marker for arguments, which can be evaluated with context set to None
pub trait OptionalContext {}
@@ -47,28 +47,59 @@
ImportStr(String),
InlineCode(String),
}
-impl ArgLike for TlaArg {
- fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {
+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) => {
- let resolved = ctx.state().resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
- }
- Self::ImportStr(p) => {
- let resolved = ctx.state().resolve_from_default(&p.as_str())?;
- Ok(Thunk!(move || ctx
- .state()
+ 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) => {
+ }),
+ Self::InlineCode(p) => with_state(|s| {
let resolved =
SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));
- Ok(Thunk!(move || ctx.state().import_resolved(resolved)))
- }
+ Ok(Thunk!(move || s.import_resolved(resolved)))
+ }),
+ }
+ }
+}
+
+// TODO: Is this implementation really required, as there is no Context to use?
+// Maybe something a bit stricter is possible to add, especially with precompiled calls?
+impl ArgLike for TlaArg {
+ fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
+ if tailstrict {
+ self.evaluate_tailstrict().map(Thunk::evaluated)
+ } else {
+ self.evaluate()
}
}
}
crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth1use std::fmt::Debug;23pub use arglike::{ArgLike, ArgsLike, TlaArg};4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6pub use jrsonnet_macros::builtin;7use jrsonnet_parser::{Destruct, Expr, LocExpr, ParamsDesc, Span};89use self::{10 arglike::OptionalContext,11 builtin::{Builtin, BuiltinParam, ParamDefault, ParamName, StaticBuiltin},12 native::NativeDesc,13 parse::{parse_default_function_call, parse_function_call},14};15use crate::{16 bail, error::ErrorKind::*, evaluate, evaluate_trivial, function::builtin::BuiltinFunc, Context,17 ContextBuilder, Result, Thunk, Val,18};1920pub mod arglike;21pub mod builtin;22pub mod native;23pub mod parse;2425/// Function callsite location.26/// Either from other jsonnet code, specified by expression location, or from native (without location).27#[derive(Clone, Copy)]28pub struct CallLocation<'l>(pub Option<&'l Span>);29impl<'l> CallLocation<'l> {30 /// Construct new location for calls coming from specified jsonnet expression location.31 pub const fn new(loc: &'l Span) -> Self {32 Self(Some(loc))33 }34}35impl CallLocation<'static> {36 /// Construct new location for calls coming from native code.37 pub const fn native() -> Self {38 Self(None)39 }40}4142/// Represents Jsonnet function defined in code.43#[derive(Debug, PartialEq, Trace)]44pub struct FuncDesc {45 /// # Example46 ///47 /// In expressions like this, deducted to `a`, unspecified otherwise.48 /// ```jsonnet49 /// local a = function() ...50 /// local a() ...51 /// { a: function() ... }52 /// { a() = ... }53 /// ```54 pub name: IStr,55 /// Context, in which this function was evaluated.56 ///57 /// # Example58 /// In59 /// ```jsonnet60 /// local a = 2;61 /// function() ...62 /// ```63 /// context will contain `a`.64 pub ctx: Context,6566 /// Function parameter definition67 pub params: ParamsDesc,68 /// Function body69 pub body: LocExpr,70}71impl FuncDesc {72 /// Create body context, but fill arguments without defaults with lazy error73 pub fn default_body_context(&self) -> Result<Context> {74 parse_default_function_call(self.ctx.clone(), &self.params)75 }7677 /// Create context, with which body code will run78 pub fn call_body_context(79 &self,80 call_ctx: Context,81 args: &dyn ArgsLike,82 tailstrict: bool,83 ) -> Result<Context> {84 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)85 }8687 pub fn evaluate_trivial(&self) -> Option<Val> {88 evaluate_trivial(&self.body)89 }90}9192/// Represents a Jsonnet function value, including plain functions and user-provided builtins.93#[allow(clippy::module_name_repetitions)]94#[derive(Trace, Clone)]95pub enum FuncVal {96 /// Identity function, kept this way for comparsions.97 Id,98 /// Plain function implemented in jsonnet.99 Normal(Cc<FuncDesc>),100 /// Function without arguments works just as a fancy thunk value.101 Thunk(Thunk<Val>),102 /// Standard library function.103 StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),104 /// User-provided function.105 Builtin(BuiltinFunc),106}107108impl Debug for FuncVal {109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {110 match self {111 Self::Id => f.debug_tuple("Id").finish(),112 Self::Thunk(arg0) => f.debug_tuple("Thunk").field(arg0).finish(),113 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),114 Self::StaticBuiltin(arg0) => {115 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()116 }117 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),118 }119 }120}121122#[allow(clippy::unnecessary_wraps)]123#[builtin]124const fn builtin_id(x: Val) -> Val {125 x126}127static ID: &builtin_id = &builtin_id {};128129impl FuncVal {130 pub fn builtin(builtin: impl Builtin) -> Self {131 Self::Builtin(BuiltinFunc::new(builtin))132 }133 pub fn static_builtin(static_builtin: &'static dyn StaticBuiltin) -> Self {134 Self::StaticBuiltin(static_builtin)135 }136137 pub fn params(&self) -> Vec<BuiltinParam> {138 match self {139 Self::Id => ID.params().to_vec(),140 Self::StaticBuiltin(i) => i.params().to_vec(),141 Self::Builtin(i) => i.params().to_vec(),142 Self::Normal(p) => p143 .params144 .iter()145 .map(|p| {146 BuiltinParam::new(147 p.0.name()148 .as_ref()149 .map(IStr::to_string)150 .map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),151 ParamDefault::exists(p.1.is_some()),152 )153 })154 .collect(),155 Self::Thunk(_) => vec![],156 }157 }158 /// Amount of non-default required arguments159 pub fn params_len(&self) -> usize {160 match self {161 Self::Id => 1,162 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),163 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),164 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),165 Self::Thunk(_) => 0,166 }167 }168 /// Function name, as defined in code.169 pub fn name(&self) -> IStr {170 match self {171 Self::Id => "id".into(),172 Self::Normal(normal) => normal.name.clone(),173 Self::StaticBuiltin(builtin) => builtin.name().into(),174 Self::Builtin(builtin) => builtin.name().into(),175 Self::Thunk(_) => "thunk".into(),176 }177 }178 /// Call function using arguments evaluated in specified `call_ctx` [`Context`].179 ///180 /// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.181 pub fn evaluate(182 &self,183 call_ctx: Context,184 loc: CallLocation<'_>,185 args: &dyn ArgsLike,186 tailstrict: bool,187 ) -> Result<Val> {188 match self {189 Self::Id => ID.call(call_ctx, loc, args),190 Self::Normal(func) => {191 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;192 evaluate(body_ctx, &func.body)193 }194 Self::Thunk(thunk) => {195 if args.is_empty() {196 bail!(TooManyArgsFunctionHas(0, vec![],))197 }198 thunk.evaluate()199 }200 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),201 Self::Builtin(b) => b.call(call_ctx, loc, args),202 }203 }204 pub fn evaluate_simple<A: ArgsLike + OptionalContext>(205 &self,206 args: &A,207 tailstrict: bool,208 ) -> Result<Val> {209 self.evaluate(210 ContextBuilder::dangerous_empty_state().build(),211 CallLocation::native(),212 args,213 tailstrict,214 )215 }216 /// Convert jsonnet function to plain `Fn` value.217 pub fn into_native<D: NativeDesc>(self) -> D::Value {218 D::into_native(self)219 }220221 /// Is this function an indentity function.222 ///223 /// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.224 ///225 /// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too226 pub fn is_identity(&self) -> bool {227 match self {228 Self::Id => true,229 Self::Normal(desc) => {230 if desc.params.len() != 1 {231 return false;232 }233 let param = &desc.params[0];234 if param.1.is_some() {235 return false;236 }237 #[allow(clippy::infallible_destructuring_match)]238 let id = match ¶m.0 {239 Destruct::Full(id) => id,240 #[cfg(feature = "exp-destruct")]241 _ => return false,242 };243 desc.body.expr() == &Expr::Var(id.clone())244 }245 _ => false,246 }247 }248 /// Identity function value.249 pub const fn identity() -> Self {250 Self::Id251 }252253 pub fn evaluate_trivial(&self) -> Option<Val> {254 match self {255 Self::Normal(n) => n.evaluate_trivial(),256 _ => None,257 }258 }259}260261impl<T> From<T> for FuncVal262where263 T: Builtin,264{265 fn from(value: T) -> Self {266 Self::builtin(value)267 }268}269impl From<&'static dyn StaticBuiltin> for FuncVal {270 fn from(value: &'static dyn StaticBuiltin) -> Self {271 Self::static_builtin(value)272 }273}1use std::fmt::Debug;23pub use arglike::{ArgLike, ArgsLike, TlaArg};4use jrsonnet_gcmodule::{Cc, Trace};5use jrsonnet_interner::IStr;6pub use jrsonnet_macros::builtin;7use jrsonnet_parser::{Destruct, Expr, LocExpr, ParamsDesc, Span};89use self::{10 arglike::OptionalContext,11 builtin::{Builtin, BuiltinParam, ParamDefault, ParamName, StaticBuiltin},12 native::NativeDesc,13 parse::{parse_default_function_call, parse_function_call},14};15use crate::{16 bail, error::ErrorKind::*, evaluate, evaluate_trivial, function::builtin::BuiltinFunc, Context,17 ContextBuilder, Result, Thunk, Val,18};1920pub mod arglike;21pub mod builtin;22pub mod native;23pub mod parse;2425/// Function callsite location.26/// Either from other jsonnet code, specified by expression location, or from native (without location).27#[derive(Clone, Copy)]28pub struct CallLocation<'l>(pub Option<&'l Span>);29impl<'l> CallLocation<'l> {30 /// Construct new location for calls coming from specified jsonnet expression location.31 pub const fn new(loc: &'l Span) -> Self {32 Self(Some(loc))33 }34}35impl CallLocation<'static> {36 /// Construct new location for calls coming from native code.37 pub const fn native() -> Self {38 Self(None)39 }40}4142/// Represents Jsonnet function defined in code.43#[derive(Debug, PartialEq, Trace)]44pub struct FuncDesc {45 /// # Example46 ///47 /// In expressions like this, deducted to `a`, unspecified otherwise.48 /// ```jsonnet49 /// local a = function() ...50 /// local a() ...51 /// { a: function() ... }52 /// { a() = ... }53 /// ```54 pub name: IStr,55 /// Context, in which this function was evaluated.56 ///57 /// # Example58 /// In59 /// ```jsonnet60 /// local a = 2;61 /// function() ...62 /// ```63 /// context will contain `a`.64 pub ctx: Context,6566 /// Function parameter definition67 pub params: ParamsDesc,68 /// Function body69 pub body: LocExpr,70}71impl FuncDesc {72 /// Create body context, but fill arguments without defaults with lazy error73 pub fn default_body_context(&self) -> Result<Context> {74 parse_default_function_call(self.ctx.clone(), &self.params)75 }7677 /// Create context, with which body code will run78 pub fn call_body_context(79 &self,80 call_ctx: Context,81 args: &dyn ArgsLike,82 tailstrict: bool,83 ) -> Result<Context> {84 parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)85 }8687 pub fn evaluate_trivial(&self) -> Option<Val> {88 evaluate_trivial(&self.body)89 }90}9192/// Represents a Jsonnet function value, including plain functions and user-provided builtins.93#[allow(clippy::module_name_repetitions)]94#[derive(Trace, Clone)]95pub enum FuncVal {96 /// Identity function, kept this way for comparsions.97 Id,98 /// Plain function implemented in jsonnet.99 Normal(Cc<FuncDesc>),100 /// Function without arguments works just as a fancy thunk value.101 Thunk(Thunk<Val>),102 /// Standard library function.103 StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),104 /// User-provided function.105 Builtin(BuiltinFunc),106}107108impl Debug for FuncVal {109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {110 match self {111 Self::Id => f.debug_tuple("Id").finish(),112 Self::Thunk(arg0) => f.debug_tuple("Thunk").field(arg0).finish(),113 Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),114 Self::StaticBuiltin(arg0) => {115 f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()116 }117 Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),118 }119 }120}121122#[allow(clippy::unnecessary_wraps)]123#[builtin]124const fn builtin_id(x: Val) -> Val {125 x126}127static ID: &builtin_id = &builtin_id {};128129impl FuncVal {130 pub fn builtin(builtin: impl Builtin) -> Self {131 Self::Builtin(BuiltinFunc::new(builtin))132 }133 pub fn static_builtin(static_builtin: &'static dyn StaticBuiltin) -> Self {134 Self::StaticBuiltin(static_builtin)135 }136137 pub fn params(&self) -> Vec<BuiltinParam> {138 match self {139 Self::Id => ID.params().to_vec(),140 Self::StaticBuiltin(i) => i.params().to_vec(),141 Self::Builtin(i) => i.params().to_vec(),142 Self::Normal(p) => p143 .params144 .iter()145 .map(|p| {146 BuiltinParam::new(147 p.0.name()148 .as_ref()149 .map(IStr::to_string)150 .map_or(ParamName::ANONYMOUS, ParamName::new_dynamic),151 ParamDefault::exists(p.1.is_some()),152 )153 })154 .collect(),155 Self::Thunk(_) => vec![],156 }157 }158 /// Amount of non-default required arguments159 pub fn params_len(&self) -> usize {160 match self {161 Self::Id => 1,162 Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),163 Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default()).count(),164 Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default()).count(),165 Self::Thunk(_) => 0,166 }167 }168 /// Function name, as defined in code.169 pub fn name(&self) -> IStr {170 match self {171 Self::Id => "id".into(),172 Self::Normal(normal) => normal.name.clone(),173 Self::StaticBuiltin(builtin) => builtin.name().into(),174 Self::Builtin(builtin) => builtin.name().into(),175 Self::Thunk(_) => "thunk".into(),176 }177 }178 /// Call function using arguments evaluated in specified `call_ctx` [`Context`].179 ///180 /// If `tailstrict` is specified - then arguments will be evaluated before being passed to function body.181 pub fn evaluate(182 &self,183 call_ctx: Context,184 loc: CallLocation<'_>,185 args: &dyn ArgsLike,186 tailstrict: bool,187 ) -> Result<Val> {188 match self {189 Self::Id => ID.call(call_ctx, loc, args),190 Self::Normal(func) => {191 let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;192 evaluate(body_ctx, &func.body)193 }194 Self::Thunk(thunk) => {195 if args.is_empty() {196 bail!(TooManyArgsFunctionHas(0, vec![],))197 }198 thunk.evaluate()199 }200 Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),201 Self::Builtin(b) => b.call(call_ctx, loc, args),202 }203 }204 pub fn evaluate_simple<A: ArgsLike + OptionalContext>(205 &self,206 args: &A,207 tailstrict: bool,208 ) -> Result<Val> {209 self.evaluate(210 ContextBuilder::new().build(),211 CallLocation::native(),212 args,213 tailstrict,214 )215 }216 /// Convert jsonnet function to plain `Fn` value.217 pub fn into_native<D: NativeDesc>(self) -> D::Value {218 D::into_native(self)219 }220221 /// Is this function an indentity function.222 ///223 /// Currently only works for builtin `std.id`, aka `Self::Id` value, and `function(x) x`.224 ///225 /// This function should only be used for optimization, not for the conditional logic, i.e code should work with syntetic identity function too226 pub fn is_identity(&self) -> bool {227 match self {228 Self::Id => true,229 Self::Normal(desc) => {230 if desc.params.len() != 1 {231 return false;232 }233 let param = &desc.params[0];234 if param.1.is_some() {235 return false;236 }237 #[allow(clippy::infallible_destructuring_match)]238 let id = match ¶m.0 {239 Destruct::Full(id) => id,240 #[cfg(feature = "exp-destruct")]241 _ => return false,242 };243 desc.body.expr() == &Expr::Var(id.clone())244 }245 _ => false,246 }247 }248 /// Identity function value.249 pub const fn identity() -> Self {250 Self::Id251 }252253 pub fn evaluate_trivial(&self) -> Option<Val> {254 match self {255 Self::Normal(n) => n.evaluate_trivial(),256 _ => None,257 }258 }259}260261impl<T> From<T> for FuncVal262where263 T: Builtin,264{265 fn from(value: T) -> Self {266 Self::builtin(value)267 }268}269impl From<&'static dyn StaticBuiltin> for FuncVal {270 fn from(value: &'static dyn StaticBuiltin) -> Self {271 Self::static_builtin(value)272 }273}crates/jrsonnet-evaluator/src/tla.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/tla.rs
+++ b/crates/jrsonnet-evaluator/src/tla.rs
@@ -1,21 +1,24 @@
use jrsonnet_interner::IStr;
use jrsonnet_parser::Source;
+use rustc_hash::FxHashMap;
use crate::{
- function::{ArgsLike, CallLocation},
- in_description_frame, Result, State, Val,
+ function::{CallLocation, TlaArg},
+ in_description_frame, with_state, Result, Val,
};
-pub fn apply_tla<A: ArgsLike>(s: State, args: &A, val: Val) -> Result<Val> {
+pub fn apply_tla(args: &FxHashMap<IStr, TlaArg>, val: Val) -> Result<Val> {
Ok(if let Val::Func(func) = val {
in_description_frame(
|| "during TLA call".to_owned(),
|| {
func.evaluate(
- s.create_default_context(Source::new_virtual(
- "<top-level-arg>".into(),
- IStr::empty(),
- )),
+ with_state(|s| {
+ s.create_default_context(Source::new_virtual(
+ "<top-level-arg>".into(),
+ IStr::empty(),
+ ))
+ }),
CallLocation::native(),
args,
false,
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -335,11 +335,6 @@
pub path_resolver: PathResolver,
}
-fn extvar_source(name: &str, code: impl Into<IStr>) -> Source {
- let source_name = format!("<extvar:{name}>");
- Source::new_virtual(source_name.into(), code.into())
-}
-
#[derive(Trace, Clone)]
pub struct ContextInitializer {
/// std without applied thisFile overlay
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -3,15 +3,15 @@
use jrsonnet_evaluator::{
bail,
error::{ErrorKind::*, Result},
- function::{builtin, ArgLike, CallLocation, FuncVal},
+ function::{builtin, CallLocation, FuncVal},
manifest::JsonFormat,
typed::{Either2, Either4},
val::{equals, ArrValue},
- Context, Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
+ Either, IStr, ObjValue, ObjValueBuilder, ResultExt, Thunk, Val,
};
use jrsonnet_gcmodule::Cc;
-use crate::{extvar_source, Settings};
+use crate::Settings;
#[builtin]
pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {
@@ -50,16 +50,14 @@
#[builtin(fields(
settings: Cc<RefCell<Settings>>,
))]
-pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {
- let ctx = ctx.state().create_default_context(extvar_source(&x, ""));
+pub fn builtin_ext_var(this: &builtin_ext_var, x: IStr) -> Result<Val> {
this.settings
.borrow()
.ext_vars
.get(&x)
.cloned()
.ok_or_else(|| UndefinedExternalVariable(x))?
- .evaluate_arg(ctx, true)?
- .evaluate()
+ .evaluate_tailstrict()
}
#[builtin(fields(
tests/tests/builtin.rsdiffbeforeafterboth--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -19,7 +19,7 @@
fn basic_function() -> Result<()> {
let a: a = a {};
let v = u32::from_untyped(a.call(
- ContextBuilder::dangerous_empty_state().build(),
+ ContextBuilder::new().build(),
CallLocation::native(),
&(),
)?)?;