git.delta.rocks / jrsonnet / refs/commits / e3e648366fcf

difftreelog

feat enable std.thisFile by default

Yaroslav Bolyukin2024-06-18parent: #4f26f96.patch.diff
in: master

6 files changed

modifiedcmds/jrsonnet/Cargo.tomldiffbeforeafterboth
--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -44,9 +44,6 @@
 # --exp-apply
 exp-apply = []
 
-# std.thisFile support
-legacy-this-file = ["jrsonnet-cli/legacy-this-file"]
-
 nightly = ["jrsonnet-evaluator/nightly"]
 
 [dependencies]
modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -170,7 +170,7 @@
 	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);
 	}
modifiedcrates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-cli/Cargo.toml
+++ b/crates/jrsonnet-cli/Cargo.toml
@@ -26,7 +26,6 @@
 exp-regex = [
     "jrsonnet-stdlib/exp-regex",
 ]
-legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]
 
 [dependencies]
 jrsonnet-evaluator = { workspace = true, features = ["explaining-traces"] }
modifiedcrates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth
--- 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 @@
 	ext_code_file: Vec<ExtFile>,
 }
 impl StdOpts {
-	pub fn context_initializer(&self, s: &State) -> Result<Option<ContextInitializer>> {
+	pub fn context_initializer(&self) -> Result<Option<ContextInitializer>> {
 		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());
 		}
modifiedcrates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/Cargo.toml
+++ b/crates/jrsonnet-stdlib/Cargo.toml
@@ -11,8 +11,6 @@
 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
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
13use jrsonnet_evaluator::{13use jrsonnet_evaluator::{
14 error::{ErrorKind::*, Result},14 error::{ErrorKind::*, Result},
15 function::{CallLocation, FuncVal, TlaArg},15 function::{CallLocation, FuncVal, TlaArg},
16 tb,
17 trace::PathResolver,16 trace::PathResolver,
18 ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,17 ContextBuilder, IStr, ObjValue, ObjValueBuilder, Thunk, Val,
19};18};
20use jrsonnet_gcmodule::Trace;19use jrsonnet_gcmodule::Trace;
21use jrsonnet_parser::Source;20use jrsonnet_parser::Source;
328327
329#[derive(Trace, Clone)]328#[derive(Trace, Clone)]
330pub struct ContextInitializer {329pub struct ContextInitializer {
331 /// When we don't need to support legacy-this-file, we can reuse same context for all files330 /// std without applied thisFile overlay
332 #[cfg(not(feature = "legacy-this-file"))]
333 context: jrsonnet_evaluator::Context,
334 /// For `populate`
335 #[cfg(not(feature = "legacy-this-file"))]
336 stdlib_thunk: Thunk<Val>,
337 /// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
338 #[cfg(feature = "legacy-this-file")]
339 stdlib_obj: ObjValue,331 stdlib_obj: ObjValue,
340 settings: Rc<RefCell<Settings>>,332 settings: Rc<RefCell<Settings>>,
341}333}
342impl ContextInitializer {334impl ContextInitializer {
343 pub fn new(s: State, resolver: PathResolver) -> Self {335 pub fn new(resolver: PathResolver) -> Self {
344 let settings = Settings {336 let settings = Settings {
345 ext_vars: HashMap::new(),337 ext_vars: HashMap::new(),
346 ext_natives: HashMap::new(),338 ext_natives: HashMap::new(),
349 };341 };
350 let settings = Rc::new(RefCell::new(settings));342 let settings = Rc::new(RefCell::new(settings));
351 let stdlib_obj = stdlib_uncached(settings.clone());343 let stdlib_obj = stdlib_uncached(settings.clone());
352 #[cfg(not(feature = "legacy-this-file"))]
353 let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));
354 #[cfg(feature = "legacy-this-file")]
355 let _ = s;
356 Self {344 Self {
357 #[cfg(not(feature = "legacy-this-file"))]
358 context: {
359 let mut context = ContextBuilder::with_capacity(s, 1);
360 context.bind("std", stdlib_thunk.clone());
361 context.build()
362 },
363 #[cfg(not(feature = "legacy-this-file"))]
364 stdlib_thunk,
365 #[cfg(feature = "legacy-this-file")]
366 stdlib_obj,345 stdlib_obj,
367 settings,346 settings,
368 }347 }
412 fn reserve_vars(&self) -> usize {391 fn reserve_vars(&self) -> usize {
413 1392 1
414 }393 }
415 #[cfg(not(feature = "legacy-this-file"))]
416 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {
417 self.context.clone()
418 }
419 #[cfg(not(feature = "legacy-this-file"))]
420 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
421 builder.bind("std", self.stdlib_thunk.clone());
422 }
423 #[cfg(feature = "legacy-this-file")]
424 fn populate(&self, source: Source, builder: &mut ContextBuilder) {394 fn populate(&self, source: Source, builder: &mut ContextBuilder) {
425 let mut std = ObjValueBuilder::new();395 let mut std = ObjValueBuilder::new();
426 std.with_super(self.stdlib_obj.clone());396 std.with_super(self.stdlib_obj.clone());
440 }410 }
441}411}
442
443pub trait StateExt {
444 /// This method was previously implemented in jrsonnet-evaluator itself
445 fn with_stdlib(&self);
446}
447
448impl StateExt for State {
449 fn with_stdlib(&self) {
450 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
451 self.settings_mut().context_initializer = tb!(initializer);
452 }
453}
454412