difftreelog
feat enable std.thisFile by default
in: master
6 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth44# --exp-apply44# --exp-apply45exp-apply = []45exp-apply = []4647# std.thisFile support48legacy-this-file = ["jrsonnet-cli/legacy-this-file"]494650nightly = ["jrsonnet-evaluator/nightly"]47nightly = ["jrsonnet-evaluator/nightly"]5148cmds/jrsonnet/src/main.rsdiffbeforeafterboth170 let import_resolver = opts.misc.import_resolver();170 let import_resolver = opts.misc.import_resolver();171 s.set_import_resolver(import_resolver);171 s.set_import_resolver(import_resolver);172172173 let std = opts.std.context_initializer(s)?;173 let std = opts.std.context_initializer()?;174 if let Some(std) = std {174 if let Some(std) = std {175 s.set_context_initializer(std);175 s.set_context_initializer(std);176 }176 }crates/jrsonnet-cli/Cargo.tomldiffbeforeafterboth26exp-regex = [26exp-regex = [27 "jrsonnet-stdlib/exp-regex",27 "jrsonnet-stdlib/exp-regex",28]28]29legacy-this-file = ["jrsonnet-stdlib/legacy-this-file"]302931[dependencies]30[dependencies]32jrsonnet-evaluator = { workspace = true, features = ["explaining-traces"] }31jrsonnet-evaluator = { workspace = true, features = ["explaining-traces"] }crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth1use std::{fs::read_to_string, str::FromStr};1use std::{fs::read_to_string, str::FromStr};223use clap::Parser;3use clap::Parser;4use jrsonnet_evaluator::{trace::PathResolver, Result, State};4use jrsonnet_evaluator::{trace::PathResolver, Result};5use jrsonnet_stdlib::ContextInitializer;5use jrsonnet_stdlib::ContextInitializer;667#[derive(Clone)]7#[derive(Clone)]104 ext_code_file: Vec<ExtFile>,104 ext_code_file: Vec<ExtFile>,105}105}106impl StdOpts {106impl StdOpts {107 pub fn context_initializer(&self, s: &State) -> Result<Option<ContextInitializer>> {107 pub fn context_initializer(&self) -> Result<Option<ContextInitializer>> {108 if self.no_stdlib {108 if self.no_stdlib {109 return Ok(None);109 return Ok(None);110 }110 }111 let ctx = ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback());111 let ctx = ContextInitializer::new(PathResolver::new_cwd_fallback());112 for ext in &self.ext_str {112 for ext in &self.ext_str {113 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());113 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());114 }114 }crates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth11workspace = true11workspace = true121213[features]13[features]14# Enables legacy `std.thisFile` support, at the cost of worse caching15legacy-this-file = []16# Add order preservation flag to some functions14# Add order preservation flag to some functions17exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]15exp-preserve-order = ["jrsonnet-evaluator/exp-preserve-order"]18# Bigint type16# Bigint typecrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth13use 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;328327329#[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 overlay332 #[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 it338 #[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 1414 }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}442443pub trait StateExt {444 /// This method was previously implemented in jrsonnet-evaluator itself445 fn with_stdlib(&self);446}447448impl 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