1use jrsonnet_gcmodule::Trace;2use jrsonnet_interner::IStr;3use jrsonnet_parser::{BindSpec, Destruct, LocExpr, ParamsDesc};45use crate::{6 bail,7 error::{ErrorKind::*, Result},8 evaluate, evaluate_method, evaluate_named,9 gc::GcHashMap,10 val::ThunkValue,11 Context, Pending, Thunk, Val,12};1314#[allow(clippy::too_many_lines)]15#[allow(unused_variables)]16pub fn destruct(17 d: &Destruct,18 parent: Thunk<Val>,19 fctx: Pending<Context>,20 new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,21) -> Result<()> {22 match d {23 Destruct::Full(v) => {24 let old = new_bindings.insert(v.clone(), parent);25 if old.is_some() {26 bail!(DuplicateLocalVar(v.clone()))27 }28 }29 #[cfg(feature = "exp-destruct")]30 Destruct::Skip => {}31 #[cfg(feature = "exp-destruct")]32 Destruct::Array { start, rest, end } => {33 use jrsonnet_parser::DestructRest;3435 use crate::arr::ArrValue;3637 #[derive(Trace)]38 struct DataThunk {39 parent: Thunk<Val>,40 min_len: usize,41 has_rest: bool,42 }43 impl ThunkValue for DataThunk {44 type Output = ArrValue;4546 fn get(self: Box<Self>) -> Result<Self::Output> {47 let v = self.parent.evaluate()?;48 let Val::Arr(arr) = v else {49 bail!("expected array");50 };51 if !self.has_rest {52 if arr.len() != self.min_len {53 bail!("expected {} elements, got {}", self.min_len, arr.len())54 }55 } else if arr.len() < self.min_len {56 bail!(57 "expected at least {} elements, but array was only {}",58 self.min_len,59 arr.len()60 )61 }62 Ok(arr)63 }64 }6566 let full = Thunk::new(DataThunk {67 min_len: start.len() + end.len(),68 has_rest: rest.is_some(),69 parent,70 });7172 {73 #[derive(Trace)]74 struct BaseThunk {75 full: Thunk<ArrValue>,76 index: usize,77 }78 impl ThunkValue for BaseThunk {79 type Output = Val;8081 fn get(self: Box<Self>) -> Result<Self::Output> {82 let full = self.full.evaluate()?;83 Ok(full.get(self.index)?.expect("length is checked"))84 }85 }86 for (i, d) in start.iter().enumerate() {87 destruct(88 d,89 Thunk::new(BaseThunk {90 full: full.clone(),91 index: i,92 }),93 fctx.clone(),94 new_bindings,95 )?;96 }97 }9899 match rest {100 Some(DestructRest::Keep(v)) => {101 #[derive(Trace)]102 struct RestThunk {103 full: Thunk<ArrValue>,104 start: usize,105 end: usize,106 }107 impl ThunkValue for RestThunk {108 type Output = Val;109110 fn get(self: Box<Self>) -> Result<Self::Output> {111 let full = self.full.evaluate()?;112 let to = full.len() - self.end;113 Ok(Val::Arr(full.slice(Some(self.start as i32), Some(to as i32), None)))114 }115 }116117 destruct(118 &Destruct::Full(v.clone()),119 Thunk::new(RestThunk {120 full: full.clone(),121 start: start.len(),122 end: end.len(),123 }),124 fctx.clone(),125 new_bindings,126 )?;127 }128 Some(DestructRest::Drop) | None => {}129 }130131 {132 #[derive(Trace)]133 struct EndThunk {134 full: Thunk<ArrValue>,135 index: usize,136 end: usize,137 }138 impl ThunkValue for EndThunk {139 type Output = Val;140141 fn get(self: Box<Self>) -> Result<Self::Output> {142 let full = self.full.evaluate()?;143 Ok(full144 .get(full.len() - self.end + self.index)?145 .expect("length is checked"))146 }147 }148 for (i, d) in end.iter().enumerate() {149 destruct(150 d,151 Thunk::new(EndThunk {152 full: full.clone(),153 index: i,154 end: end.len(),155 }),156 fctx.clone(),157 new_bindings,158 )?;159 }160 }161 }162 #[cfg(feature = "exp-destruct")]163 Destruct::Object { fields, rest } => {164 use crate::obj::ObjValue;165166 #[derive(Trace)]167 struct DataThunk {168 parent: Thunk<Val>,169 field_names: Vec<IStr>,170 has_rest: bool,171 }172 impl ThunkValue for DataThunk {173 type Output = ObjValue;174175 fn get(self: Box<Self>) -> Result<Self::Output> {176 let v = self.parent.evaluate()?;177 let Val::Obj(obj) = v else {178 bail!("expected object");179 };180 for field in &self.field_names {181 if !obj.has_field_ex(field.clone(), true) {182 bail!("missing field: {field}");183 }184 }185 if !self.has_rest {186 let len = obj.len();187 if len != self.field_names.len() {188 bail!("too many fields, and rest not found");189 }190 }191 Ok(obj)192 }193 }194 let field_names: Vec<_> = fields195 .iter()196 .filter(|f| f.2.is_none())197 .map(|f| f.0.clone())198 .collect();199 let full = Thunk::new(DataThunk {200 parent,201 field_names,202 has_rest: rest.is_some(),203 });204205 for (field, d, default) in fields {206 #[derive(Trace)]207 struct FieldThunk {208 full: Thunk<ObjValue>,209 field: IStr,210 default: Option<(Pending<Context>, LocExpr)>,211 }212 impl ThunkValue for FieldThunk {213 type Output = Val;214215 fn get(self: Box<Self>) -> Result<Self::Output> {216 let full = self.full.evaluate()?;217 if let Some(field) = full.get(self.field)? {218 Ok(field)219 } else {220 let (fctx, expr) = self.default.as_ref().expect("shape is checked");221 Ok(evaluate(fctx.clone().unwrap(), expr)?)222 }223 }224 }225 let value = Thunk::new(FieldThunk {226 full: full.clone(),227 field: field.clone(),228 default: default.clone().map(|e| (fctx.clone(), e)),229 });230 if let Some(d) = d {231 destruct(d, value, fctx.clone(), new_bindings)?;232 } else {233 destruct(234 &Destruct::Full(field.clone()),235 value,236 fctx.clone(),237 new_bindings,238 )?;239 }240 }241 }242 }243 Ok(())244}245246pub fn evaluate_dest(247 d: &BindSpec,248 fctx: Pending<Context>,249 new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,250) -> Result<()> {251 match d {252 BindSpec::Field { into, value } => {253 #[derive(Trace)]254 struct EvaluateThunkValue {255 name: Option<IStr>,256 fctx: Pending<Context>,257 expr: LocExpr,258 }259 impl ThunkValue for EvaluateThunkValue {260 type Output = Val;261 fn get(self: Box<Self>) -> Result<Self::Output> {262 self.name.map_or_else(263 || evaluate(self.fctx.unwrap(), &self.expr),264 |name| evaluate_named(self.fctx.unwrap(), &self.expr, name),265 )266 }267 }268 let data = Thunk::new(EvaluateThunkValue {269 name: into.name(),270 fctx: fctx.clone(),271 expr: value.clone(),272 });273 destruct(into, data, fctx, new_bindings)?;274 }275 BindSpec::Function {276 name,277 params,278 value,279 } => {280 #[derive(Trace)]281 struct MethodThunk {282 fctx: Pending<Context>,283 name: IStr,284 params: ParamsDesc,285 value: LocExpr,286 }287 impl ThunkValue for MethodThunk {288 type Output = Val;289290 fn get(self: Box<Self>) -> Result<Self::Output> {291 Ok(evaluate_method(292 self.fctx.unwrap(),293 self.name,294 self.params,295 self.value,296 ))297 }298 }299300 let old = new_bindings.insert(301 name.clone(),302 Thunk::new(MethodThunk {303 fctx,304 name: name.clone(),305 params: params.clone(),306 value: value.clone(),307 }),308 );309 if old.is_some() {310 bail!(DuplicateLocalVar(name.clone()))311 }312 }313 }314 Ok(())315}