difftreelog
feat enable std.thisFile by default
in: master
6 files changed
cmds/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]
cmds/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);
}
crates/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"] }
crates/jrsonnet-cli/src/stdlib.rsdiffbeforeafterboth1use std::{fs::read_to_string, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{trace::PathResolver, Result, State};5use jrsonnet_stdlib::ContextInitializer;67#[derive(Clone)]8pub struct ExtStr {9 pub name: String,10 pub value: String,11}1213/// Parses a string like `name=<value>`, or `name` and reads value from env variable.14/// With no value it will be read from env variable.15/// If env variable is not found then it will be an error.16/// Value can contain `=` symbol.17///18/// ```19/// use std::str::FromStr;20/// use jrsonnet_cli::ExtStr;21///22/// let ext = ExtStr::from_str("name=value").unwrap();23/// assert_eq!(ext.name, "name");24/// assert_eq!(ext.value, "value");25///26/// std::env::set_var("name", "value");27///28/// let ext = ExtStr::from_str("name").unwrap();29/// assert_eq!(ext.name, "name");30/// assert_eq!(ext.value, "value");31///32/// let ext = ExtStr::from_str("name=value=with=equals").unwrap();33/// assert_eq!(ext.name, "name");34/// assert_eq!(ext.value, "value=with=equals");35/// ```36///37impl FromStr for ExtStr {38 type Err = &'static str;3940 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {41 match s.find('=') {42 Some(idx) => Ok(Self {43 name: s[..idx].to_owned(),44 value: s[idx + 1..].to_owned(),45 }),46 None => Ok(Self {47 name: s.to_owned(),48 value: std::env::var(s).or(Err("missing env var"))?,49 }),50 }51 }52}5354#[derive(Clone)]55pub struct ExtFile {56 pub name: String,57 pub value: String,58}5960impl FromStr for ExtFile {61 type Err = String;6263 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {64 let out: Vec<&str> = s.split('=').collect();65 if out.len() != 2 {66 return Err("bad ext-file syntax".to_owned());67 }68 let file = read_to_string(out[1]);69 match file {70 Ok(content) => Ok(Self {71 name: out[0].into(),72 value: content,73 }),74 Err(e) => Err(format!("{e}")),75 }76 }77}7879#[derive(Parser)]80#[clap(next_help_heading = "STANDARD LIBRARY")]81pub struct StdOpts {82 /// Disable standard library.83 /// By default standard library will be available via global `std` variable.84 #[clap(long)]85 no_stdlib: bool,86 /// Add string external variable.87 /// External variables are globally available so it is preferred88 /// to use top level arguments whenever it's possible.89 /// If [=data] is not set then it will be read from `name` env variable.90 /// Can be accessed from code via `std.extVar("name")`.91 #[clap(long, short = 'V', name = "name[=var data]", number_of_values = 1)]92 ext_str: Vec<ExtStr>,93 /// Read string external variable from file.94 /// See also `--ext-str`95 #[clap(long, name = "name=var path", number_of_values = 1)]96 ext_str_file: Vec<ExtFile>,97 /// Add external variable from code.98 /// See also `--ext-str`99 #[clap(long, name = "name[=var source]", number_of_values = 1)]100 ext_code: Vec<ExtStr>,101 /// Read string external variable from file.102 /// See also `--ext-str`103 #[clap(long, name = "name=var code path", number_of_values = 1)]104 ext_code_file: Vec<ExtFile>,105}106impl StdOpts {107 pub fn context_initializer(&self, s: &State) -> Result<Option<ContextInitializer>> {108 if self.no_stdlib {109 return Ok(None);110 }111 let ctx = ContextInitializer::new(s.clone(), PathResolver::new_cwd_fallback());112 for ext in &self.ext_str {113 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());114 }115 for ext in &self.ext_str_file {116 ctx.add_ext_str((&ext.name as &str).into(), (&ext.value as &str).into());117 }118 for ext in &self.ext_code {119 ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;120 }121 for ext in &self.ext_code_file {122 ctx.add_ext_code(&ext.name as &str, &ext.value as &str)?;123 }124 Ok(Some(ctx))125 }126}crates/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
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -13,9 +13,8 @@
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 @@
#[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<Val>,
- /// 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<RefCell<Settings>>,
}
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 @@
};
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,
}
@@ -411,16 +390,7 @@
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());
@@ -437,17 +407,5 @@
}
fn as_any(&self) -> &dyn std::any::Any {
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);
}
}