difftreelog
feat composable ContextInitializer
in: master
5 files changed
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth126/// During import, this trait will be called to create initial context for file.126/// During import, this trait will be called to create initial context for file.127/// It may initialize global variables, stdlib for example.127/// It may initialize global variables, stdlib for example.128pub trait ContextInitializer: Trace {128pub trait ContextInitializer: Trace {129 /// For which size the builder should be preallocated130 fn reserve_vars(&self) -> usize {131 0132 }129 /// Initialize default file context.133 /// Initialize default file context.134 /// Has default implementation, which calls `populate`.135 /// Prefer to always implement `populate` instead.130 fn initialize(&self, state: State, for_file: Source) -> Context;136 fn initialize(&self, state: State, for_file: Source) -> Context {137 let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());138 self.populate(for_file, &mut builder);139 builder.build()140 }141 /// For composability: extend builder. May panic if this initialization is not supported,142 /// and the context may only be created via `initialize`.143 fn populate(&self, for_file: Source, builder: &mut ContextBuilder);131 /// Allows upcasting from abstract to concrete context initializer.144 /// Allows upcasting from abstract to concrete context initializer.132 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.145 /// jrsonnet by itself doesn't use this method, it is allowed for it to panic.133 fn as_any(&self) -> &dyn Any;146 fn as_any(&self) -> &dyn Any;134}147}135148136/// Context initializer which adds nothing.149/// Context initializer which adds nothing.137#[derive(Trace)]138pub struct DummyContextInitializer;139impl ContextInitializer for DummyContextInitializer {150impl ContextInitializer for () {140 fn initialize(&self, state: State, _for_file: Source) -> Context {151 fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}141 ContextBuilder::new(state).build()142 }143 fn as_any(&self) -> &dyn Any {152 fn as_any(&self) -> &dyn Any {144 self153 self157impl Default for EvaluationSettings {166impl Default for EvaluationSettings {158 fn default() -> Self {167 fn default() -> Self {159 Self {168 Self {160 context_initializer: tb!(DummyContextInitializer),169 context_initializer: tb!(()),161 import_resolver: tb!(DummyImportResolver),170 import_resolver: tb!(DummyImportResolver),162 }171 }163 }172 }396 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {405 pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {397 self.0.settings.borrow_mut()406 self.0.settings.borrow_mut()398 }407 }408 pub fn add_global(&self, name: IStr, value: Thunk<Val>) {409 #[derive(Trace)]410 struct GlobalsCtx {411 globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,412 inner: TraceBox<dyn ContextInitializer>,413 }414 impl ContextInitializer for GlobalsCtx {415 fn reserve_vars(&self) -> usize {416 self.inner.reserve_vars() + self.globals.borrow().len()417 }418 fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {419 self.inner.populate(for_file, builder);420 for (name, val) in self.globals.borrow().iter() {421 builder.bind(name.clone(), val.clone());422 }423 }424425 fn as_any(&self) -> &dyn Any {426 self427 }428 }429 let mut settings = self.settings_mut();430 let initializer = &mut settings.context_initializer;431 match initializer.as_any().downcast_ref::<GlobalsCtx>() {432 Some(glob) => {433 glob.globals.borrow_mut().insert(name, value);434 }435 None => {436 let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));437 settings.context_initializer = tb!(GlobalsCtx {438 globals: {439 let mut out = GcHashMap::with_capacity(1);440 out.insert(name, value);441 RefCell::new(out)442 },443 inner444 })445 }446 }447 }399}448}400449401/// Raw methods evaluate passed values but don't perform TLA execution450/// Raw methods evaluate passed values but don't perform TLA executioncrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth7use jrsonnet_evaluator::{7use jrsonnet_evaluator::{8 error::{ErrorKind::*, Result},8 error::{ErrorKind::*, Result},9 function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},9 function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},10 gc::{GcHashMap, TraceBox},10 gc::TraceBox,11 tb,11 tb,12 trace::PathResolver,12 trace::PathResolver,13 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,13 ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,14};14};15use jrsonnet_gcmodule::{Cc, Trace};15use jrsonnet_gcmodule::{Cc, Trace};16use jrsonnet_parser::Source;16use jrsonnet_parser::Source;231 pub ext_vars: HashMap<IStr, TlaArg>,231 pub ext_vars: HashMap<IStr, TlaArg>,232 /// Used for `std.native`232 /// Used for `std.native`233 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,233 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,234 /// Helper to add globals without implementing custom ContextInitializer235 pub globals: GcHashMap<IStr, Thunk<Val>>,236 /// Used for `std.trace`234 /// Used for `std.trace`237 pub trace_printer: Box<dyn TracePrinter>,235 pub trace_printer: Box<dyn TracePrinter>,238 /// Used for `std.thisFile`236 /// Used for `std.thisFile`246244247#[derive(Trace, Clone)]245#[derive(Trace, Clone)]248pub struct ContextInitializer {246pub struct ContextInitializer {249 // When we don't need to support legacy-this-file, we can reuse same context for all files247 /// When we don't need to support legacy-this-file, we can reuse same context for all files250 #[cfg(not(feature = "legacy-this-file"))]248 #[cfg(not(feature = "legacy-this-file"))]251 context: Context,249 context: jrsonnet_evaluator::Context,250 /// For `populate`251 #[cfg(not(feature = "legacy-this-file"))]252 stdlib_thunk: Thunk<Val>,252 // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it253 /// Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it253 #[cfg(feature = "legacy-this-file")]254 #[cfg(feature = "legacy-this-file")]254 stdlib_obj: ObjValue,255 stdlib_obj: ObjValue,255 settings: Rc<RefCell<Settings>>,256 settings: Rc<RefCell<Settings>>,259 let settings = Settings {260 let settings = Settings {260 ext_vars: Default::default(),261 ext_vars: Default::default(),261 ext_natives: Default::default(),262 ext_natives: Default::default(),262 globals: Default::default(),263 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),263 trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),264 path_resolver: resolver,264 path_resolver: resolver,265 };265 };266 let settings = Rc::new(RefCell::new(settings));266 let settings = Rc::new(RefCell::new(settings));267 let stdlib_obj = stdlib_uncached(settings.clone());268 #[cfg(not(feature = "legacy-this-file"))]269 let stdlib_thunk = Thunk::evaluated(Val::Obj(stdlib_obj));267 Self {270 Self {268 #[cfg(not(feature = "legacy-this-file"))]271 #[cfg(not(feature = "legacy-this-file"))]269 context: {272 context: {270 let mut context = ContextBuilder::with_capacity(_s, 1);273 let mut context = ContextBuilder::with_capacity(_s, 1);271 context.bind(274 context.bind("std".into(), stdlib_thunk.clone());272 "std".into(),273 Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),274 );275 context.build()275 context.build()276 },276 },277 #[cfg(not(feature = "legacy-this-file"))]278 stdlib_thunk,277 #[cfg(feature = "legacy-this-file")]279 #[cfg(feature = "legacy-this-file")]278 stdlib_obj: stdlib_uncached(settings.clone()),280 stdlib_obj,279 settings,281 settings,280 }282 }281 }283 }321 }323 }322}324}323impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {325impl jrsonnet_evaluator::ContextInitializer for ContextInitializer {326 fn reserve_vars(&self) -> usize {327 1328 }324 #[cfg(not(feature = "legacy-this-file"))]329 #[cfg(not(feature = "legacy-this-file"))]325 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {330 fn initialize(&self, _s: State, _source: Source) -> jrsonnet_evaluator::Context {331 self.context.clone()332 }326 let out = self.context.clone();333 #[cfg(not(feature = "legacy-this-file"))]327 let globals = &self.settings().globals;334 fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {328 if globals.is_empty() {329 return out;330 }331332 let mut out = ContextBuilder::extend(out);333 for (k, v) in globals.iter() {334 out.bind(k.clone(), v.clone());335 builder.bind("std".into(), self.stdlib_thunk.clone());335 }336 }336 out.build()337 }338 #[cfg(feature = "legacy-this-file")]337 #[cfg(feature = "legacy-this-file")]339 fn initialize(&self, s: State, source: Source) -> Context {338 fn populate(&self, source: Source, builder: &mut ContextBuilder) {340 use jrsonnet_evaluator::val::StrValue;339 use jrsonnet_evaluator::val::StrValue;341340342 let mut builder = ObjValueBuilder::new();341 let mut std = ObjValueBuilder::new();343 builder.with_super(self.stdlib_obj.clone());342 std.with_super(self.stdlib_obj.clone());344 builder345 .member("thisFile".into())343 std.member("thisFile".into())346 .hide()344 .hide()347 .value(Val::Str(StrValue::Flat(345 .value(Val::Str(StrValue::Flat(348 match source.source_path().path() {346 match source.source_path().path() {351 },349 },352 )))350 )))353 .expect("this object builder is empty");351 .expect("this object builder is empty");354 let stdlib_with_this_file = builder.build();352 let stdlib_with_this_file = std.build();355353356 let mut context = ContextBuilder::with_capacity(s, 1);357 context.bind(354 builder.bind(358 "std".into(),355 "std".into(),359 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),356 Thunk::evaluated(Val::Obj(stdlib_with_this_file)),360 );357 );361 for (k, v) in self.settings().globals.iter() {362 context.bind(k.clone(), v.clone());363 }364 context.build()365 }358 }366 fn as_any(&self) -> &dyn std::any::Any {359 fn as_any(&self) -> &dyn std::any::Any {367 self360 self371pub trait StateExt {364pub trait StateExt {372 /// This method was previously implemented in jrsonnet-evaluator itself365 /// This method was previously implemented in jrsonnet-evaluator itself373 fn with_stdlib(&self);366 fn with_stdlib(&self);374 fn add_global(&self, name: IStr, value: Thunk<Val>);375}367}376368377impl StateExt for State {369impl StateExt for State {378 fn with_stdlib(&self) {370 fn with_stdlib(&self) {379 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());371 let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());380 self.settings_mut().context_initializer = tb!(initializer)372 self.settings_mut().context_initializer = tb!(initializer)381 }373 }382 fn add_global(&self, name: IStr, value: Thunk<Val>) {383 self.settings()384 .context_initializer385 .as_any()386 .downcast_ref::<ContextInitializer>()387 .expect("not standard context initializer")388 .settings_mut()389 .globals390 .insert(name, value);391 }392}374}393375flake.lockdiffbeforeafterboth5 "systems": "systems"5 "systems": "systems"6 },6 },7 "locked": {7 "locked": {8 "lastModified": 1681202837,8 "lastModified": 1689068808,9 "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",9 "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",10 "owner": "numtide",10 "owner": "numtide",11 "repo": "flake-utils",11 "repo": "flake-utils",12 "rev": "cfacdce06f30d2b68473a46042957675eebb3401",12 "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",13 "type": "github"13 "type": "github"14 },14 },15 "original": {15 "original": {20 },20 },21 "nixpkgs": {21 "nixpkgs": {22 "locked": {22 "locked": {23 "lastModified": 1683574088,23 "lastModified": 1689162265,24 "narHash": "sha256-RjE7UXfyYBV3vkpjL5irZOF+4ZgTQlvEWYJsFL2Hig0=",24 "narHash": "sha256-kdW79sfwX2TTX8yFBNUsEYOG+gQuAOHU+WcUtxMUnlc=",25 "owner": "nixos",25 "owner": "nixos",26 "repo": "nixpkgs",26 "repo": "nixpkgs",27 "rev": "05b1a97381588ba98d98f8725b2137fce0ab45cb",27 "rev": "1941c7d8f1219c615a1d6dae826e0d6fab89acca",28 "type": "github"28 "type": "github"29 },29 },30 "original": {30 "original": {50 ]50 ]51 },51 },52 "locked": {52 "locked": {53 "lastModified": 1683512408,53 "lastModified": 1689129196,54 "narHash": "sha256-QMJGp/37En+d5YocJuSU89GL14bBYkIJQ6mqhRfqkkc=",54 "narHash": "sha256-/z/Al4sFcIh5oPQWA9MclQmJR9g3RO8UDiHGaj/T9R8=",55 "owner": "oxalica",55 "owner": "oxalica",56 "repo": "rust-overlay",56 "repo": "rust-overlay",57 "rev": "75b07756c3feb22cf230e75fb064c1b4c725b9bc",57 "rev": "db8d909c9526d4406579ee7343bf2d7de3d15eac",58 "type": "github"58 "type": "github"59 },59 },60 "original": {60 "original": {flake.nixdiffbeforeafterboth16 inherit system;16 inherit system;17 overlays = [ rust-overlay.overlays.default ];17 overlays = [ rust-overlay.overlays.default ];18 };18 };19 rust = ((pkgs.rustChannelOf { date = "2023-05-07"; channel = "nightly"; }).default.override {19 rust = ((pkgs.rustChannelOf { date = "2023-06-26"; channel = "nightly"; }).default.override {20 extensions = [ "rust-src" "miri" "rust-analyzer" ];20 extensions = [ "rust-src" "miri" "rust-analyzer" ];21 });21 });22 in22 intests/tests/common.rsdiffbeforeafterboth5 function::{builtin, FuncVal},5 function::{builtin, FuncVal},6 throw, ObjValueBuilder, State, Thunk, Val,6 throw, ObjValueBuilder, State, Thunk, Val,7};7};8use jrsonnet_stdlib::StateExt;9810#[macro_export]9#[macro_export]11macro_rules! ensure_eq {10macro_rules! ensure_eq {