Skip to content

Commit

Permalink
feat: enable std.thisFile by default
Browse files Browse the repository at this point in the history
  • Loading branch information
CertainLach committed May 19, 2024
1 parent 71206de commit d6c7d6b
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 55 deletions.
3 changes: 0 additions & 3 deletions cmds/jrsonnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion cmds/jrsonnet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion crates/jrsonnet-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 3 additions & 3 deletions crates/jrsonnet-cli/src/stdlib.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -104,11 +104,11 @@ pub struct StdOpts {
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());
}
Expand Down
2 changes: 0 additions & 2 deletions crates/jrsonnet-stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 3 additions & 45 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -328,19 +327,12 @@ fn extvar_source(name: &str, code: impl Into<IStr>) -> 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<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(),
Expand All @@ -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,
}
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}
}

0 comments on commit d6c7d6b

Please sign in to comment.