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.rsdiffbeforeafterboth4use jrsonnet_interner::IStr;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};5use jrsonnet_parser::{ArgsDesc, LocExpr, SourceFifo, SourcePath};667use crate::{evaluate, typed::Typed, Context, Result, Thunk, Val};7use crate::{evaluate, typed::Typed, with_state, Context, Result, Thunk, Val};889/// Marker for arguments, which can be evaluated with context set to None9/// Marker for arguments, which can be evaluated with context set to None10pub trait OptionalContext {}10pub trait OptionalContext {}47 ImportStr(String),47 ImportStr(String),48 InlineCode(String),48 InlineCode(String),49}49}50impl ArgLike for TlaArg {50impl TlaArg {51 pub fn evaluate_tailstrict(&self) -> Result<Val> {52 match self {53 Self::String(s) => Ok(Val::string(s.clone())),54 Self::Val(val) => Ok(val.clone()),55 Self::Lazy(lazy) => Ok(lazy.evaluate()?),56 Self::Import(p) => with_state(|s| {57 let resolved = s.resolve_from_default(&p.as_str())?;58 s.import_resolved(resolved)59 }),60 Self::ImportStr(p) => with_state(|s| {61 let resolved = s.resolve_from_default(&p.as_str())?;62 s.import_resolved_str(resolved).map(Val::string)63 }),64 Self::InlineCode(p) => with_state(|s| {65 let resolved =66 SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));67 s.import_resolved(resolved)68 }),69 }70 }51 fn evaluate_arg(&self, ctx: Context, _tailstrict: bool) -> Result<Thunk<Val>> {71 pub fn evaluate(&self) -> Result<Thunk<Val>> {52 match self {72 match self {53 Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),73 Self::String(s) => Ok(Thunk::evaluated(Val::string(s.clone()))),54 Self::Val(val) => Ok(Thunk::evaluated(val.clone())),74 Self::Val(val) => Ok(Thunk::evaluated(val.clone())),55 Self::Lazy(lazy) => Ok(lazy.clone()),75 Self::Lazy(lazy) => Ok(lazy.clone()),56 Self::Import(p) => {76 Self::Import(p) => with_state(|s| {57 let resolved = ctx.state().resolve_from_default(&p.as_str())?;77 let resolved = s.resolve_from_default(&p.as_str())?;58 Ok(Thunk!(move || ctx.state().import_resolved(resolved)))78 Ok(Thunk!(move || s.import_resolved(resolved)))59 }79 }),60 Self::ImportStr(p) => {80 Self::ImportStr(p) => with_state(|s| {61 let resolved = ctx.state().resolve_from_default(&p.as_str())?;81 let resolved = s.resolve_from_default(&p.as_str())?;62 Ok(Thunk!(move || ctx82 Ok(Thunk!(move || s63 .state()64 .import_resolved_str(resolved)83 .import_resolved_str(resolved)65 .map(Val::string)))84 .map(Val::string)))66 }85 }),67 Self::InlineCode(p) => {86 Self::InlineCode(p) => with_state(|s| {68 let resolved =87 let resolved =69 SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));88 SourcePath::new(SourceFifo("<inline code>".to_owned(), p.as_bytes().into()));70 Ok(Thunk!(move || ctx.state().import_resolved(resolved)))89 Ok(Thunk!(move || s.import_resolved(resolved)))71 }90 }),72 }91 }73 }92 }74}93}9495// TODO: Is this implementation really required, as there is no Context to use?96// Maybe something a bit stricter is possible to add, especially with precompiled calls?97impl ArgLike for TlaArg {98 fn evaluate_arg(&self, _ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {99 if tailstrict {100 self.evaluate_tailstrict().map(Thunk::evaluated)101 } else {102 self.evaluate()103 }104 }105}7510676pub trait ArgsLike {107pub trait ArgsLike {77 fn unnamed_len(&self) -> usize;108 fn unnamed_len(&self) -> usize;crates/jrsonnet-evaluator/src/function/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/mod.rs
+++ b/crates/jrsonnet-evaluator/src/function/mod.rs
@@ -207,7 +207,7 @@
tailstrict: bool,
) -> Result<Val> {
self.evaluate(
- ContextBuilder::dangerous_empty_state().build(),
+ ContextBuilder::new().build(),
CallLocation::native(),
args,
tailstrict,
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(),
&(),
)?)?;