difftreelog
refactor unwrap Unbound thunk value
in: master
3 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth147 name: IStr,147 name: IStr,148 }148 }149 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {149 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {150 type Bound = Thunk<Val>;150 type Bound = Val;151 fn bind(151 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {152 &self,153 sup: Option<ObjValue>,154 this: Option<ObjValue>,155 ) -> Result<Thunk<Val>> {156 Ok(Thunk::evaluated(evaluate_named(152 Ok(evaluate_named(157 self.uctx.bind(sup, this)?,153 self.uctx.bind(sup, this)?,158 &self.value,154 &self.value,159 self.name.clone(),155 self.name.clone(),160 )?))156 )?)161 }157 }162 }158 }163159191 name: IStr,187 name: IStr,192 }188 }193 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {189 impl<B: Unbound<Bound = Context>> Unbound for UnboundMethod<B> {194 type Bound = Thunk<Val>;190 type Bound = Val;195 fn bind(191 fn bind(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {196 &self,197 sup: Option<ObjValue>,198 this: Option<ObjValue>,199 ) -> Result<Thunk<Val>> {200 Ok(Thunk::evaluated(evaluate_method(192 Ok(evaluate_method(201 self.uctx.bind(sup, this)?,193 self.uctx.bind(sup, this)?,202 self.name.clone(),194 self.name.clone(),203 self.params.clone(),195 self.params.clone(),204 self.value.clone(),196 self.value.clone(),205 )))197 ))206 }198 }207 }199 }208200209 let name = if let Some(name) = evaluate_field_name(ctx.clone(), name)? {201 let Some(name) = evaluate_field_name(ctx.clone(), name)? else {210 name211 } else {212 continue;202 continue;213 };203 };214204276 value: LocExpr,266 value: LocExpr,277 }267 }278 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {268 impl<B: Unbound<Bound = Context>> Unbound for UnboundValue<B> {279 type Bound = Thunk<Val>;269 type Bound = Val;280 fn bind(270 fn bind(281 &self,271 &self,282 sup: Option<ObjValue>,272 sup: Option<ObjValue>,283 this: Option<ObjValue>,273 this: Option<ObjValue>,284 ) -> Result<Thunk<Val>> {274 ) -> Result<Val> {285 Ok(Thunk::evaluated(evaluate(275 Ok(evaluate(286 self.uctx.bind(sup, this.clone())?.extend(276 self.uctx.bind(sup, this.clone())?.extend(287 GcHashMap::new(),277 GcHashMap::new(),288 None,278 None,289 None,279 None,290 this,280 this,291 ),281 ),292 &self.value,282 &self.value,293 )?))283 )?)294 }284 }295 }285 }296 builder286 buildercrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth98#[derive(Clone, Trace)]98#[derive(Clone, Trace)]99pub enum MaybeUnbound {99pub enum MaybeUnbound {100 /// Value needs to be bound to `this`/`super`100 /// Value needs to be bound to `this`/`super`101 Unbound(Cc<TraceBox<dyn Unbound<Bound = Thunk<Val>>>>),101 Unbound(Cc<TraceBox<dyn Unbound<Bound = Val>>>),102 /// Value is object-independent102 /// Value is object-independent103 Bound(Thunk<Val>),103 Bound(Thunk<Val>),104}104}110}110}111impl MaybeUnbound {111impl MaybeUnbound {112 /// Attach object context to value, if required112 /// Attach object context to value, if required113 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Thunk<Val>> {113 pub fn evaluate(&self, sup: Option<ObjValue>, this: Option<ObjValue>) -> Result<Val> {114 match self {114 match self {115 Self::Unbound(v) => v.bind(sup, this),115 Self::Unbound(v) => v.bind(sup, this),116 Self::Bound(v) => Ok(v.clone()),116 Self::Bound(v) => Ok(v.evaluate()?),117 }117 }118 }118 }119}119}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth441 }441 }442 fn evaluate_this(&self, v: &ObjMember, real_this: Self) -> Result<Val> {442 fn evaluate_this(&self, v: &ObjMember, real_this: Self) -> Result<Val> {443 v.invoke443 v.invoke.evaluate(self.0.sup.clone(), Some(real_this))444 .evaluate(self.0.sup.clone(), Some(real_this))?445 .evaluate()446 }444 }447445448 fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {446 fn run_assertions_raw(&self, real_this: &Self) -> Result<()> {605 pub fn thunk(self, value: Thunk<Val>) -> Result<()> {603 pub fn thunk(self, value: Thunk<Val>) -> Result<()> {606 self.binding(MaybeUnbound::Bound(value))604 self.binding(MaybeUnbound::Bound(value))607 }605 }608 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) -> Result<()> {606 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) -> Result<()> {609 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))607 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)))610 }608 }611 pub fn binding(self, binding: MaybeUnbound) -> Result<()> {609 pub fn binding(self, binding: MaybeUnbound) -> Result<()> {628 pub fn value(self, value: Val) {626 pub fn value(self, value: Val) {629 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));627 self.binding(MaybeUnbound::Bound(Thunk::evaluated(value)));630 }628 }631 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Thunk<Val>>>) {629 pub fn bindable(self, bindable: TraceBox<dyn Unbound<Bound = Val>>) {632 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));630 self.binding(MaybeUnbound::Unbound(Cc::new(bindable)));633 }631 }634 pub fn binding(self, binding: MaybeUnbound) {632 pub fn binding(self, binding: MaybeUnbound) {