From d6c7d6beb4377b1e49dd3074369a758cc922dbc0 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 19 May 2024 21:55:59 +0200 Subject: [PATCH] feat: enable std.thisFile by default --- cmds/jrsonnet/Cargo.toml | 3 -- cmds/jrsonnet/src/main.rs | 2 +- crates/jrsonnet-cli/Cargo.toml | 1 - crates/jrsonnet-cli/src/stdlib.rs | 6 ++-- crates/jrsonnet-stdlib/Cargo.toml | 2 -- crates/jrsonnet-stdlib/src/lib.rs | 48 ++----------------------------- 6 files changed, 7 insertions(+), 55 deletions(-) diff --git a/cmds/jrsonnet/Cargo.toml b/cmds/jrsonnet/Cargo.toml index 811f9a8c..2fde9f57 100644 --- a/cmds/jrsonnet/Cargo.toml +++ b/cmds/jrsonnet/Cargo.toml @@ -44,9 +44,6 @@ exp-null-coaelse = [ # --exp-apply exp-apply = [] -# std.thisFile support -legacy-this-file = ["jrsonnet-cli/legacy-this-file"] - nightly = ["jrsonnet-evaluator/nightly"] [dependencies] diff --git a/cmds/jrsonnet/src/main.rs b/cmds/jrsonnet/src/main.rs index 00e506e5..c0f5d09d 100644 --- a/cmds/jrsonnet/src/main.rs +++ b/cmds/jrsonnet/src/main.rs @@ -170,7 +170,7 @@ fn main_real(s: &State, opts: Opts) -> Result<(), Error> { let import_resolver = opts.misc.import_resolver(); s.set_import_resolver(import_resolver); - let std = opts.std.context_initializer(s)?; + let std = opts.std.context_initializer()?; if let Some(std) = std { s.set_context_initializer(std); } diff --git a/crates/jrsonnet-cli/Cargo.toml b/crates/jrsonnet-cli/Cargo.toml index b3eb92b9..b2bcfd33 100644 --- a/crates/jrsonnet-cli/Cargo.toml +++ b/crates/jrsonnet-cli/Cargo.toml @@ -26,7 +26,6 @@ exp-null-coaelse = [ exp-regex = [ "jrsonnet-stdlib/exp-regex", ] -legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"] [dependencies] jrsonnet-evaluator = { workspace = true, features = ["explaining-traces"] } diff --git a/crates/jrsonnet-cli/src/stdlib.rs b/crates/jrsonnet-cli/src/stdlib.rs index 903b8993..15689e2e 100644 --- a/crates/jrsonnet-cli/src/stdlib.rs +++ b/crates/jrsonnet-cli/src/stdlib.rs @@ -1,7 +1,7 @@ use std::{fs::read_to_string, str::FromStr}; use clap::Parser; -use jrsonnet_evaluator::{trace::PathResolver, Result, State}; +use jrsonnet_evaluator::{trace::PathResolver, Result}; use jrsonnet_stdlib::ContextInitializer; #[derive(Clone)] @@ -104,11 +104,11 @@ pub struct StdOpts { ext_code_file: Vec, } impl StdOpts { - pub fn context_initializer(&self, s: &State) -> Result> { + pub fn context_initializer(&self) -> Result> { if self.no_stdlib { return Ok(None); } - let ctx = ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback()); + let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback()); for ext in &self.ext_str { ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into()); } diff --git a/crates/jrsonnet-stdlib/Cargo.toml b/crates/jrsonnet-stdlib/Cargo.toml index cbd1c482..910afc5e 100644 --- a/crates/jrsonnet-stdlib/Cargo.toml +++ b/crates/jrsonnet-stdlib/Cargo.toml @@ -11,8 +11,6 @@ version.workspace = true workspace = true [features] -# Enables legacy `std.thisFile` support, at the cost of worse caching -legacy-this-file = [] # Add order preservation flag to some functions exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"] # Bigint type diff --git a/crates/jrsonnet-stdlib/src/lib.rs b/crates/jrsonnet-stdlib/src/lib.rs index e0d2dddc..de84777f 100644 --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -13,9 +13,8 @@ pub use hash::*; use jrsonnet_evaluator::{ error::{ErrorKind::*, Result}, function::{CallLocation, FuncVal, TlaArg}, - tb, trace::PathResolver, - ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val, + ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val, }; use jrsonnet_gcmodule::Trace; use jrsonnet_parser::Source; @@ -328,19 +327,12 @@ fn extvar_source(name: &str, code: impl Into) -> Source { #[derive(Trace, Clone)] pub struct ContextInitializer { - /// When we don't need to support legacy-this-file, we can reuse same context for all files - #[cfg(not(feature = "legacy-this-file"))] - context: jrsonnet_evaluator::Context, - /// For `populate` - #[cfg(not(feature = "legacy-this-file"))] - stdlib_thunk: Thunk, - /// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it - #[cfg(feature = "legacy-this-file")] + /// std without applied thisFile overlay stdlib_obj: ObjValue, settings: Rc>, } impl ContextInitializer { - pub fn new(s: State, resolver: PathResolver) -> Self { + pub fn new(resolver: PathResolver) -> Self { let settings = Settings { ext_vars: HashMap::new(), ext_natives: HashMap::new(), @@ -349,20 +341,7 @@ impl ContextInitializer { }; let settings = Rc::new(RefCell::new(settings)); let stdlib_obj = stdlib_uncached(settings.clone()); - #[cfg(not(feature = "legacy-this-file"))] - let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj)); - #[cfg(feature = "legacy-this-file")] - let _ = s; Self { - #[cfg(not(feature = "legacy-this-file"))] - context: { - let mut context = ContextBuilder::with_capacity(s, 1); - context.bind("std", stdlib_thunk.clone()); - context.build() - }, - #[cfg(not(feature = "legacy-this-file"))] - stdlib_thunk, - #[cfg(feature = "legacy-this-file")] stdlib_obj, settings, } @@ -412,15 +391,6 @@ impl jrsonnet_evaluator::ContextInitializer for ContextInitializer { fn reserve_vars(&self) -> usize { 1 } - #[cfg(not(feature = "legacy-this-file"))] - fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context { - self.context.clone() - } - #[cfg(not(feature = "legacy-this-file"))] - fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) { - builder.bind("std", self.stdlib_thunk.clone()); - } - #[cfg(feature = "legacy-this-file")] fn populate(&self, source: Source, builder: &mut ContextBuilder) { let mut std = ObjValueBuilder::new(); std.with_super(self.stdlib_obj.clone()); @@ -439,15 +409,3 @@ impl jrsonnet_evaluator::ContextInitializer for ContextInitializer { self } } - -pub trait StateExt { - /// This method was previously implemented in jrsonnet-evaluator itself - fn with_stdlib(&self); -} - -impl StateExt for State { - fn with_stdlib(&self) { - let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback()); - self.settings_mut().context_initializer = tb!(initializer); - } -}