--- a/crates/jrsonnet-evaluator/src/ctx.rs +++ b/crates/jrsonnet-evaluator/src/ctx.rs @@ -138,3 +138,30 @@ Cc::ptr_eq(&self.0, &other.0) } } + +#[derive(Default)] +pub struct ContextBuilder { + bindings: GcHashMap>, +} +impl ContextBuilder { + pub fn new() -> Self { + Self::default() + } + pub fn with_capacity(capacity: usize) -> Self { + Self { + bindings: GcHashMap::with_capacity(capacity), + } + } + pub fn bind(&mut self, name: IStr, value: Thunk) -> &mut Self { + self.bindings.insert(name, value); + self + } + pub fn build(self) -> Context { + Context(Cc::new(ContextInternals { + bindings: LayeredHashMap::new(self.bindings), + dollar: None, + sup: None, + this: None, + })) + } +} --- a/crates/jrsonnet-evaluator/src/map.rs +++ b/crates/jrsonnet-evaluator/src/map.rs @@ -23,6 +23,13 @@ } } + pub(crate) fn new(layer: GcHashMap>) -> Self { + Self(Cc::new(LayeredHashMapInternals { + parent: None, + current: layer, + })) + } + pub fn extend(self, new_layer: GcHashMap>) -> Self { Self(Cc::new(LayeredHashMapInternals { parent: Some(self),