difftreelog
feat composable ContextInitializer
in: master
5 files changed
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -126,20 +126,29 @@
/// During import, this trait will be called to create initial context for file.
/// It may initialize global variables, stdlib for example.
pub trait ContextInitializer: Trace {
+ /// For which size the builder should be preallocated
+ fn reserve_vars(&self) -> usize {
+ 0
+ }
/// Initialize default file context.
- fn initialize(&self, state: State, for_file: Source) -> Context;
+ /// Has default implementation, which calls `populate`.
+ /// Prefer to always implement `populate` instead.
+ fn initialize(&self, state: State, for_file: Source) -> Context {
+ let mut builder = ContextBuilder::with_capacity(state, self.reserve_vars());
+ self.populate(for_file, &mut builder);
+ builder.build()
+ }
+ /// For composability: extend builder. May panic if this initialization is not supported,
+ /// and the context may only be created via `initialize`.
+ fn populate(&self, for_file: Source, builder: &mut ContextBuilder);
/// Allows upcasting from abstract to concrete context initializer.
/// jrsonnet by itself doesn't use this method, it is allowed for it to panic.
fn as_any(&self) -> &dyn Any;
}
/// Context initializer which adds nothing.
-#[derive(Trace)]
-pub struct DummyContextInitializer;
-impl ContextInitializer for DummyContextInitializer {
- fn initialize(&self, state: State, _for_file: Source) -> Context {
- ContextBuilder::new(state).build()
- }
+impl ContextInitializer for () {
+ fn populate(&self, _for_file: Source, _builder: &mut ContextBuilder) {}
fn as_any(&self) -> &dyn Any {
self
}
@@ -157,7 +166,7 @@
impl Default for EvaluationSettings {
fn default() -> Self {
Self {
- context_initializer: tb!(DummyContextInitializer),
+ context_initializer: tb!(()),
import_resolver: tb!(DummyImportResolver),
}
}
@@ -396,6 +405,46 @@
pub fn settings_mut(&self) -> RefMut<'_, EvaluationSettings> {
self.0.settings.borrow_mut()
}
+ pub fn add_global(&self, name: IStr, value: Thunk<Val>) {
+ #[derive(Trace)]
+ struct GlobalsCtx {
+ globals: RefCell<GcHashMap<IStr, Thunk<Val>>>,
+ inner: TraceBox<dyn ContextInitializer>,
+ }
+ impl ContextInitializer for GlobalsCtx {
+ fn reserve_vars(&self) -> usize {
+ self.inner.reserve_vars() + self.globals.borrow().len()
+ }
+ fn populate(&self, for_file: Source, builder: &mut ContextBuilder) {
+ self.inner.populate(for_file, builder);
+ for (name, val) in self.globals.borrow().iter() {
+ builder.bind(name.clone(), val.clone());
+ }
+ }
+
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+ }
+ let mut settings = self.settings_mut();
+ let initializer = &mut settings.context_initializer;
+ match initializer.as_any().downcast_ref::<GlobalsCtx>() {
+ Some(glob) => {
+ glob.globals.borrow_mut().insert(name, value);
+ }
+ None => {
+ let inner = std::mem::replace(&mut settings.context_initializer, tb!(()));
+ settings.context_initializer = tb!(GlobalsCtx {
+ globals: {
+ let mut out = GcHashMap::with_capacity(1);
+ out.insert(name, value);
+ RefCell::new(out)
+ },
+ inner
+ })
+ }
+ }
+ }
}
/// Raw methods evaluate passed values but don't perform TLA execution
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -7,10 +7,10 @@
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
function::{builtin::Builtin, CallLocation, FuncVal, TlaArg},
- gc::{GcHashMap, TraceBox},
+ gc::TraceBox,
tb,
trace::PathResolver,
- Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
+ ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_parser::Source;
@@ -231,8 +231,6 @@
pub ext_vars: HashMap<IStr, TlaArg>,
/// Used for `std.native`
pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,
- /// Helper to add globals without implementing custom ContextInitializer
- pub globals: GcHashMap<IStr, Thunk<Val>>,
/// Used for `std.trace`
pub trace_printer: Box<dyn TracePrinter>,
/// Used for `std.thisFile`
@@ -246,10 +244,13 @@
#[derive(Trace, Clone)]
pub struct ContextInitializer {
- // When we don't need to support legacy-this-file, we can reuse same context for all files
+ /// 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: Context,
- // Otherwise, we can only keep first stdlib layer, and then stack thisFile on top of it
+ 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")]
stdlib_obj: ObjValue,
settings: Rc<RefCell<Settings>>,
@@ -259,23 +260,24 @@
let settings = Settings {
ext_vars: Default::default(),
ext_natives: Default::default(),
- globals: Default::default(),
trace_printer: Box::new(StdTracePrinter::new(resolver.clone())),
path_resolver: resolver,
};
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));
Self {
#[cfg(not(feature = "legacy-this-file"))]
context: {
let mut context = ContextBuilder::with_capacity(_s, 1);
- context.bind(
- "std".into(),
- Thunk::evaluated(Val::Obj(stdlib_uncached(settings.clone()))),
- );
+ context.bind("std".into(), stdlib_thunk.clone());
context.build()
},
+ #[cfg(not(feature = "legacy-this-file"))]
+ stdlib_thunk,
#[cfg(feature = "legacy-this-file")]
- stdlib_obj: stdlib_uncached(settings.clone()),
+ stdlib_obj,
settings,
}
}
@@ -321,28 +323,24 @@
}
}
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 {
- let out = self.context.clone();
- let globals = &self.settings().globals;
- if globals.is_empty() {
- return out;
- }
-
- let mut out = ContextBuilder::extend(out);
- for (k, v) in globals.iter() {
- out.bind(k.clone(), v.clone());
- }
- out.build()
+ self.context.clone()
+ }
+ #[cfg(not(feature = "legacy-this-file"))]
+ fn populate(&self, _for_file: Source, builder: &mut ContextBuilder) {
+ builder.bind("std".into(), self.stdlib_thunk.clone());
}
#[cfg(feature = "legacy-this-file")]
- fn initialize(&self, s: State, source: Source) -> Context {
+ fn populate(&self, source: Source, builder: &mut ContextBuilder) {
use jrsonnet_evaluator::val::StrValue;
- let mut builder = ObjValueBuilder::new();
- builder.with_super(self.stdlib_obj.clone());
- builder
- .member("thisFile".into())
+ let mut std = ObjValueBuilder::new();
+ std.with_super(self.stdlib_obj.clone());
+ std.member("thisFile".into())
.hide()
.value(Val::Str(StrValue::Flat(
match source.source_path().path() {
@@ -351,17 +349,12 @@
},
)))
.expect("this object builder is empty");
- let stdlib_with_this_file = builder.build();
+ let stdlib_with_this_file = std.build();
- let mut context = ContextBuilder::with_capacity(s, 1);
- context.bind(
+ builder.bind(
"std".into(),
Thunk::evaluated(Val::Obj(stdlib_with_this_file)),
);
- for (k, v) in self.settings().globals.iter() {
- context.bind(k.clone(), v.clone());
- }
- context.build()
}
fn as_any(&self) -> &dyn std::any::Any {
self
@@ -371,22 +364,11 @@
pub trait StateExt {
/// This method was previously implemented in jrsonnet-evaluator itself
fn with_stdlib(&self);
- fn add_global(&self, name: IStr, value: Thunk<Val>);
}
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)
- }
- fn add_global(&self, name: IStr, value: Thunk<Val>) {
- self.settings()
- .context_initializer
- .as_any()
- .downcast_ref::<ContextInitializer>()
- .expect("not standard context initializer")
- .settings_mut()
- .globals
- .insert(name, value);
}
}
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
- "lastModified": 1681202837,
- "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
+ "lastModified": 1689068808,
+ "narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
"owner": "numtide",
"repo": "flake-utils",
- "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
+ "rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
"type": "github"
},
"original": {
@@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 1683574088,
- "narHash": "sha256-RjE7UXfyYBV3vkpjL5irZOF+4ZgTQlvEWYJsFL2Hig0=",
+ "lastModified": 1689162265,
+ "narHash": "sha256-kdW79sfwX2TTX8yFBNUsEYOG+gQuAOHU+WcUtxMUnlc=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "05b1a97381588ba98d98f8725b2137fce0ab45cb",
+ "rev": "1941c7d8f1219c615a1d6dae826e0d6fab89acca",
"type": "github"
},
"original": {
@@ -50,11 +50,11 @@
]
},
"locked": {
- "lastModified": 1683512408,
- "narHash": "sha256-QMJGp/37En+d5YocJuSU89GL14bBYkIJQ6mqhRfqkkc=",
+ "lastModified": 1689129196,
+ "narHash": "sha256-/z/Al4sFcIh5oPQWA9MclQmJR9g3RO8UDiHGaj/T9R8=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "75b07756c3feb22cf230e75fb064c1b4c725b9bc",
+ "rev": "db8d909c9526d4406579ee7343bf2d7de3d15eac",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth1{2 description = "Jrsonnet";3 inputs = {4 nixpkgs.url = "github:nixos/nixpkgs";5 flake-utils.url = "github:numtide/flake-utils";6 rust-overlay = {7 url = "github:oxalica/rust-overlay";8 inputs.nixpkgs.follows = "nixpkgs";9 inputs.flake-utils.follows = "flake-utils";10 };11 };12 outputs = { nixpkgs, flake-utils, rust-overlay, ... }:13 flake-utils.lib.eachDefaultSystem (system:14 let15 pkgs = import nixpkgs {16 inherit system;17 overlays = [ rust-overlay.overlays.default ];18 };19 rust = ((pkgs.rustChannelOf { date = "2023-05-07"; channel = "nightly"; }).default.override {20 extensions = [ "rust-src" "miri" "rust-analyzer" ];21 });22 in23 rec {24 packages = rec {25 go-jsonnet = pkgs.callPackage ./nix/go-jsonnet.nix { };26 sjsonnet = pkgs.callPackage ./nix/sjsonnet.nix { };27 jsonnet = pkgs.callPackage ./nix/jsonnet.nix { };28 # I didn't managed to build it, and nixpkgs version is marked as broken29 # haskell-jsonnet = pkgs.callPackage ./nix/haskell-jsonnet.nix { };30 jrsonnet = pkgs.callPackage ./nix/jrsonnet.nix {31 rustPlatform = pkgs.makeRustPlatform {32 rustc = rust;33 cargo = rust;34 };35 };36 jrsonnet-nightly = pkgs.callPackage ./nix/jrsonnet.nix {37 rustPlatform = pkgs.makeRustPlatform {38 rustc = rust;39 cargo = rust;40 };41 withNightlyFeatures = true;42 };43 jrsonnet-release = pkgs.callPackage ./nix/jrsonnet-release.nix {44 rustPlatform = pkgs.makeRustPlatform {45 rustc = rust;46 cargo = rust;47 };48 };4950 benchmarks = pkgs.callPackage ./nix/benchmarks.nix {51 inherit go-jsonnet sjsonnet jsonnet;52 jrsonnetVariants = [53 { drv = jrsonnet; name = ""; }54 ];55 };56 benchmarks-quick = pkgs.callPackage ./nix/benchmarks.nix {57 inherit go-jsonnet sjsonnet jsonnet;58 quick = true;59 jrsonnetVariants = [60 { drv = jrsonnet; name = ""; }61 ];62 };63 benchmarks-against-release = pkgs.callPackage ./nix/benchmarks.nix {64 inherit go-jsonnet sjsonnet jsonnet;65 jrsonnetVariants = [66 { drv = jrsonnet; name = "current"; }67 { drv = jrsonnet-nightly; name = "current-nightly"; }68 { drv = jrsonnet-release; name = "before-str-extend"; }69 ];70 };71 benchmarks-quick-against-release = pkgs.callPackage ./nix/benchmarks.nix {72 inherit go-jsonnet sjsonnet jsonnet;73 quick = true;74 jrsonnetVariants = [75 { drv = jrsonnet; name = "current"; }76 { drv = jrsonnet-nightly; name = "current-nightly"; }77 { drv = jrsonnet-release; name = "before-str-extend"; }78 ];79 };80 };81 devShell = pkgs.mkShell {82 nativeBuildInputs = with pkgs;[83 rust84 cargo-edit85 cargo-asm86 cargo-outdated87 lld88 hyperfine89 valgrind90 kcachegrind91 graphviz92 ];93 };94 }95 );96}1{2 description = "Jrsonnet";3 inputs = {4 nixpkgs.url = "github:nixos/nixpkgs";5 flake-utils.url = "github:numtide/flake-utils";6 rust-overlay = {7 url = "github:oxalica/rust-overlay";8 inputs.nixpkgs.follows = "nixpkgs";9 inputs.flake-utils.follows = "flake-utils";10 };11 };12 outputs = { nixpkgs, flake-utils, rust-overlay, ... }:13 flake-utils.lib.eachDefaultSystem (system:14 let15 pkgs = import nixpkgs {16 inherit system;17 overlays = [ rust-overlay.overlays.default ];18 };19 rust = ((pkgs.rustChannelOf { date = "2023-06-26"; channel = "nightly"; }).default.override {20 extensions = [ "rust-src" "miri" "rust-analyzer" ];21 });22 in23 rec {24 packages = rec {25 go-jsonnet = pkgs.callPackage ./nix/go-jsonnet.nix { };26 sjsonnet = pkgs.callPackage ./nix/sjsonnet.nix { };27 jsonnet = pkgs.callPackage ./nix/jsonnet.nix { };28 # I didn't managed to build it, and nixpkgs version is marked as broken29 # haskell-jsonnet = pkgs.callPackage ./nix/haskell-jsonnet.nix { };30 jrsonnet = pkgs.callPackage ./nix/jrsonnet.nix {31 rustPlatform = pkgs.makeRustPlatform {32 rustc = rust;33 cargo = rust;34 };35 };36 jrsonnet-nightly = pkgs.callPackage ./nix/jrsonnet.nix {37 rustPlatform = pkgs.makeRustPlatform {38 rustc = rust;39 cargo = rust;40 };41 withNightlyFeatures = true;42 };43 jrsonnet-release = pkgs.callPackage ./nix/jrsonnet-release.nix {44 rustPlatform = pkgs.makeRustPlatform {45 rustc = rust;46 cargo = rust;47 };48 };4950 benchmarks = pkgs.callPackage ./nix/benchmarks.nix {51 inherit go-jsonnet sjsonnet jsonnet;52 jrsonnetVariants = [53 { drv = jrsonnet; name = ""; }54 ];55 };56 benchmarks-quick = pkgs.callPackage ./nix/benchmarks.nix {57 inherit go-jsonnet sjsonnet jsonnet;58 quick = true;59 jrsonnetVariants = [60 { drv = jrsonnet; name = ""; }61 ];62 };63 benchmarks-against-release = pkgs.callPackage ./nix/benchmarks.nix {64 inherit go-jsonnet sjsonnet jsonnet;65 jrsonnetVariants = [66 { drv = jrsonnet; name = "current"; }67 { drv = jrsonnet-nightly; name = "current-nightly"; }68 { drv = jrsonnet-release; name = "before-str-extend"; }69 ];70 };71 benchmarks-quick-against-release = pkgs.callPackage ./nix/benchmarks.nix {72 inherit go-jsonnet sjsonnet jsonnet;73 quick = true;74 jrsonnetVariants = [75 { drv = jrsonnet; name = "current"; }76 { drv = jrsonnet-nightly; name = "current-nightly"; }77 { drv = jrsonnet-release; name = "before-str-extend"; }78 ];79 };80 };81 devShell = pkgs.mkShell {82 nativeBuildInputs = with pkgs;[83 rust84 cargo-edit85 cargo-asm86 cargo-outdated87 lld88 hyperfine89 valgrind90 kcachegrind91 graphviz92 ];93 };94 }95 );96}tests/tests/common.rsdiffbeforeafterboth--- a/tests/tests/common.rs
+++ b/tests/tests/common.rs
@@ -5,7 +5,6 @@
function::{builtin, FuncVal},
throw, ObjValueBuilder, State, Thunk, Val,
};
-use jrsonnet_stdlib::StateExt;
#[macro_export]
macro_rules! ensure_eq {