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.rsdiffbeforeafterboth1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{CommandFactory, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{GcOpts, ManifestOpts, MiscOpts, OutputOpts, StdOpts, TlaOpts, TraceOpts};9use jrsonnet_evaluator::{10 apply_tla, bail,11 error::{Error as JrError, ErrorKind},12 ResultExt, State, Val,13};14use jrsonnet_parser::{SourceDefaultIgnoreJpath, SourcePath};1516#[cfg(feature = "mimalloc")]17#[global_allocator]18static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1920#[derive(Parser)]21enum SubOpts {22 /// Generate completions for specified shell23 Generate {24 /// Target shell name25 shell: Shell,26 },27}2829#[derive(Parser)]30#[clap(next_help_heading = "DEBUG")]31struct DebugOpts {32 /// Required OS stack size.33 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.34 #[clap(long, name = "size")]35 pub os_stack: Option<usize>,36}3738#[derive(Parser)]39#[clap(next_help_heading = "INPUT")]40struct InputOpts {41 /// Treat input as code, evaluate it instead of reading file.42 #[clap(long, short = 'e')]43 pub exec: bool,4445 /// Path to the file to be compiled if `--exec` is unset, otherwise code itself.46 pub input: Option<String>,4748 /// After executing input, apply specified code.49 /// Output of the initial input will be accessible using `_`.50 #[cfg(feature = "exp-apply")]51 #[clap(long)]52 pub exp_apply: Vec<String>,53}5455/// Jsonnet commandline interpreter (Rust implementation)56#[derive(Parser)]57#[clap(58 args_conflicts_with_subcommands = true,59 disable_version_flag = true,60 version,61 author62)]63struct Opts {64 #[clap(subcommand)]65 sub: Option<SubOpts>,66 /// Print version67 #[clap(long)]68 version: bool,6970 #[clap(flatten)]71 input: InputOpts,72 #[clap(flatten)]73 misc: MiscOpts,74 #[clap(flatten)]75 tla: TlaOpts,76 #[clap(flatten)]77 std: StdOpts,78 #[clap(flatten)]79 gc: GcOpts,8081 #[clap(flatten)]82 trace: TraceOpts,83 #[clap(flatten)]84 manifest: ManifestOpts,85 #[clap(flatten)]86 output: OutputOpts,87 #[clap(flatten)]88 debug: DebugOpts,89}9091// TODO: Add unix_sigpipe = "sig_dfl"92fn main() {93 let opts: Opts = Opts::parse();9495 if opts.version {96 print!("{}", Opts::command().render_version());97 std::process::exit(0)98 }99100 if let Some(sub) = opts.sub {101 match sub {102 SubOpts::Generate { shell } => {103 use clap_complete::generate;104 let app = &mut Opts::command();105 let buf = &mut std::io::stdout();106 generate(shell, app, "jrsonnet", buf);107 std::process::exit(0)108 }109 }110 }111112 let success = if let Some(size) = opts.debug.os_stack {113 std::thread::Builder::new()114 .stack_size(size * 1024 * 1024)115 .spawn(|| main_catch(opts))116 .expect("new thread spawned")117 .join()118 .expect("thread finished successfully")119 } else {120 main_catch(opts)121 };122 if !success {123 std::process::exit(1);124 }125}126127#[derive(thiserror::Error, Debug)]128enum Error {129 // Handled differently130 #[error("evaluation error")]131 Evaluation(JrError),132 #[error("io error")]133 Io(#[from] std::io::Error),134 #[error("input is not utf8 encoded")]135 Utf8(#[from] std::str::Utf8Error),136 #[error("missing input argument")]137 MissingInputArgument,138}139impl From<JrError> for Error {140 fn from(e: JrError) -> Self {141 Self::Evaluation(e)142 }143}144impl From<ErrorKind> for Error {145 fn from(e: ErrorKind) -> Self {146 Self::from(JrError::from(e))147 }148}149150fn main_catch(opts: Opts) -> bool {151 let trace = opts.trace.trace_format();152 if let Err(e) = main_real(opts) {153 if let Error::Evaluation(e) = e {154 let mut out = String::new();155 trace.write_trace(&mut out, &e).expect("format error");156 eprintln!("{out}");157 } else {158 eprintln!("{e}");159 }160 return false;161 }162 true163}164165fn main_real(opts: Opts) -> Result<(), Error> {166 let _gc_leak_guard = opts.gc.leak_on_exit();167 let _gc_print_stats = opts.gc.stats_printer();168 let _stack_depth_override = opts.misc.stack_size_override();169170 let import_resolver = opts.misc.import_resolver();171 let std = opts.std.context_initializer()?;172173 let mut s = State::builder();174 s.import_resolver(import_resolver).context_initializer(std);175 let s = s.build();176177 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;178 let val = if opts.input.exec {179 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?180 } else if input == "-" {181 let mut input = Vec::new();182 std::io::stdin().read_to_end(&mut input)?;183 let input_str = std::str::from_utf8(&input)?;184 s.evaluate_snippet("<stdin>".to_owned(), input_str)?185 } else {186 s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?187 };188189 let tla = opts.tla.tla_opts()?;190 #[allow(191 // It is not redundant/unused in exp-apply192 unused_mut,193 clippy::redundant_clone,194 )]195 let mut val = apply_tla(s.clone(), &tla, val)?;196197 #[cfg(feature = "exp-apply")]198 for apply in opts.input.exp_apply {199 use jrsonnet_evaluator::{InitialUnderscore, Thunk};200 val = s.evaluate_snippet_with(201 "<exp_apply>".to_owned(),202 &apply,203 InitialUnderscore(Thunk::evaluated(val)),204 )?;205 }206207 let manifest_format = opts.manifest.manifest_format();208 if let Some(multi) = opts.output.multi {209 if opts.output.create_output_dirs {210 let mut dir = multi.clone();211 dir.pop();212 create_dir_all(dir)?;213 }214 let Val::Obj(obj) = val else {215 bail!(216 "value should be object for --multi manifest, got {}",217 val.value_type()218 )219 };220 for (field, data) in obj.iter(221 #[cfg(feature = "exp-preserve-order")]222 opts.manifest.preserve_order,223 ) {224 let data = data.with_description(|| format!("getting field {field} for manifest"))?;225226 let mut path = multi.clone();227 path.push(&field as &str);228 if opts.output.create_output_dirs {229 let mut dir = path.clone();230 dir.pop();231 create_dir_all(dir)?;232 }233 println!("{}", path.to_str().expect("path"));234 let mut file = File::create(path)?;235 write!(236 file,237 "{}",238 data.manifest(&manifest_format)239 .with_description(|| format!("manifesting {field}"))?,240 )?;241 if manifest_format.file_trailing_newline() {242 writeln!(file)?;243 }244 file.flush()?;245 }246 } else if let Some(path) = opts.output.output_file {247 if opts.output.create_output_dirs {248 let mut dir = path.clone();249 dir.pop();250 create_dir_all(dir)?;251 }252 let mut file = File::create(path)?;253 writeln!(file, "{}", val.manifest(manifest_format)?)?;254 } else {255 let output = val.manifest(manifest_format)?;256 if !output.is_empty() {257 println!("{output}");258 }259 }260261 Ok(())262}1use std::{2 fs::{create_dir_all, File},3 io::{Read, Write},4};56use clap::{CommandFactory, Parser};7use clap_complete::Shell;8use jrsonnet_cli::{GcOpts, ManifestOpts, MiscOpts, OutputOpts, StdOpts, TlaOpts, TraceOpts};9use jrsonnet_evaluator::{10 apply_tla, bail,11 error::{Error as JrError, ErrorKind},12 ResultExt, State, Val,13};14use jrsonnet_parser::{SourceDefaultIgnoreJpath, SourcePath};1516#[cfg(feature = "mimalloc")]17#[global_allocator]18static GLOBAL: mimallocator::Mimalloc = mimallocator::Mimalloc;1920#[derive(Parser)]21enum SubOpts {22 /// Generate completions for specified shell23 Generate {24 /// Target shell name25 shell: Shell,26 },27}2829#[derive(Parser)]30#[clap(next_help_heading = "DEBUG")]31struct DebugOpts {32 /// Required OS stack size.33 /// This shouldn't be changed unless jrsonnet is failing with stack overflow error.34 #[clap(long, name = "size")]35 pub os_stack: Option<usize>,36}3738#[derive(Parser)]39#[clap(next_help_heading = "INPUT")]40struct InputOpts {41 /// Treat input as code, evaluate it instead of reading file.42 #[clap(long, short = 'e')]43 pub exec: bool,4445 /// Path to the file to be compiled if `--exec` is unset, otherwise code itself.46 pub input: Option<String>,4748 /// After executing input, apply specified code.49 /// Output of the initial input will be accessible using `_`.50 #[cfg(feature = "exp-apply")]51 #[clap(long)]52 pub exp_apply: Vec<String>,53}5455/// Jsonnet commandline interpreter (Rust implementation)56#[derive(Parser)]57#[clap(58 args_conflicts_with_subcommands = true,59 disable_version_flag = true,60 version,61 author62)]63struct Opts {64 #[clap(subcommand)]65 sub: Option<SubOpts>,66 /// Print version67 #[clap(long)]68 version: bool,6970 #[clap(flatten)]71 input: InputOpts,72 #[clap(flatten)]73 misc: MiscOpts,74 #[clap(flatten)]75 tla: TlaOpts,76 #[clap(flatten)]77 std: StdOpts,78 #[clap(flatten)]79 gc: GcOpts,8081 #[clap(flatten)]82 trace: TraceOpts,83 #[clap(flatten)]84 manifest: ManifestOpts,85 #[clap(flatten)]86 output: OutputOpts,87 #[clap(flatten)]88 debug: DebugOpts,89}9091// TODO: Add unix_sigpipe = "sig_dfl"92fn main() {93 let opts: Opts = Opts::parse();9495 if opts.version {96 print!("{}", Opts::command().render_version());97 std::process::exit(0)98 }99100 if let Some(sub) = opts.sub {101 match sub {102 SubOpts::Generate { shell } => {103 use clap_complete::generate;104 let app = &mut Opts::command();105 let buf = &mut std::io::stdout();106 generate(shell, app, "jrsonnet", buf);107 std::process::exit(0)108 }109 }110 }111112 let success = if let Some(size) = opts.debug.os_stack {113 std::thread::Builder::new()114 .stack_size(size * 1024 * 1024)115 .spawn(|| main_catch(opts))116 .expect("new thread spawned")117 .join()118 .expect("thread finished successfully")119 } else {120 main_catch(opts)121 };122 if !success {123 std::process::exit(1);124 }125}126127#[derive(thiserror::Error, Debug)]128enum Error {129 // Handled differently130 #[error("evaluation error")]131 Evaluation(JrError),132 #[error("io error")]133 Io(#[from] std::io::Error),134 #[error("input is not utf8 encoded")]135 Utf8(#[from] std::str::Utf8Error),136 #[error("missing input argument")]137 MissingInputArgument,138}139impl From<JrError> for Error {140 fn from(e: JrError) -> Self {141 Self::Evaluation(e)142 }143}144impl From<ErrorKind> for Error {145 fn from(e: ErrorKind) -> Self {146 Self::from(JrError::from(e))147 }148}149150fn main_catch(opts: Opts) -> bool {151 let trace = opts.trace.trace_format();152 if let Err(e) = main_real(opts) {153 if let Error::Evaluation(e) = e {154 let mut out = String::new();155 trace.write_trace(&mut out, &e).expect("format error");156 eprintln!("{out}");157 } else {158 eprintln!("{e}");159 }160 return false;161 }162 true163}164165fn main_real(opts: Opts) -> Result<(), Error> {166 let _gc_leak_guard = opts.gc.leak_on_exit();167 let _gc_print_stats = opts.gc.stats_printer();168 let _stack_depth_override = opts.misc.stack_size_override();169170 let import_resolver = opts.misc.import_resolver();171 let std = opts.std.context_initializer()?;172173 let mut s = State::builder();174 s.import_resolver(import_resolver).context_initializer(std);175 let s = s.build();176 let _s = s.enter();177178 let input = opts.input.input.ok_or(Error::MissingInputArgument)?;179 let val = if opts.input.exec {180 s.evaluate_snippet("<cmdline>".to_owned(), &input as &str)?181 } else if input == "-" {182 let mut input = Vec::new();183 std::io::stdin().read_to_end(&mut input)?;184 let input_str = std::str::from_utf8(&input)?;185 s.evaluate_snippet("<stdin>".to_owned(), input_str)?186 } else {187 s.import_from(&SourcePath::new(SourceDefaultIgnoreJpath), input.as_str())?188 };189190 let tla = opts.tla.tla_opts()?;191 #[allow(192 // It is not redundant/unused in exp-apply193 unused_mut,194 clippy::redundant_clone,195 )]196 let mut val = apply_tla(&tla, val)?;197198 #[cfg(feature = "exp-apply")]199 for apply in opts.input.exp_apply {200 use jrsonnet_evaluator::{InitialUnderscore, Thunk};201 val = s.evaluate_snippet_with(202 "<exp_apply>".to_owned(),203 &apply,204 InitialUnderscore(Thunk::evaluated(val)),205 )?;206 }207208 let manifest_format = opts.manifest.manifest_format();209 if let Some(multi) = opts.output.multi {210 if opts.output.create_output_dirs {211 let mut dir = multi.clone();212 dir.pop();213 create_dir_all(dir)?;214 }215 let Val::Obj(obj) = val else {216 bail!(217 "value should be object for --multi manifest, got {}",218 val.value_type()219 )220 };221 for (field, data) in obj.iter(222 #[cfg(feature = "exp-preserve-order")]223 opts.manifest.preserve_order,224 ) {225 let data = data.with_description(|| format!("getting field {field} for manifest"))?;226227 let mut path = multi.clone();228 path.push(&field as &str);229 if opts.output.create_output_dirs {230 let mut dir = path.clone();231 dir.pop();232 create_dir_all(dir)?;233 }234 println!("{}", path.to_str().expect("path"));235 let mut file = File::create(path)?;236 write!(237 file,238 "{}",239 data.manifest(&manifest_format)240 .with_description(|| format!("manifesting {field}"))?,241 )?;242 if manifest_format.file_trailing_newline() {243 writeln!(file)?;244 }245 file.flush()?;246 }247 } else if let Some(path) = opts.output.output_file {248 if opts.output.create_output_dirs {249 let mut dir = path.clone();250 dir.pop();251 create_dir_all(dir)?;252 }253 let mut file = File::create(path)?;254 writeln!(file, "{}", val.manifest(manifest_format)?)?;255 } else {256 let output = val.manifest(manifest_format)?;257 if !output.is_empty() {258 println!("{output}");259 }260 }261262 Ok(())263}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.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(),
&(),
)?)?;