difftreelog
style fix clippy warnings
in: master
4 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -192,8 +192,6 @@
inc_hidden: bool,
#[cfg(feature = "exp-preserve-order")] preserve_order: Option<bool>,
) -> Result<VecVal> {
- #[cfg(not(feature = "exp-preserve-order"))]
- let preserve_order = false;
#[cfg(feature = "exp-preserve-order")]
let preserve_order = preserve_order.unwrap_or(false);
let out = obj.fields_ex(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use std::convert::TryFrom;23use gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,7 LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12 builtin::{std_slice, BUILTINS},13 error::Error::*,14 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15 function::CallLocation,16 gc::TraceBox,17 push_frame, throw,18 typed::BoundedUsize,19 val::{ArrValue, FuncDesc, FuncVal, LazyValValue},20 with_state, Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal,21 ObjValue, ObjValueBuilder, ObjectAssertion, Result, Val,22};23pub mod operator;2425pub fn evaluate_binding_in_future(26 b: &BindSpec,27 context_creator: FutureWrapper<Context>,28) -> LazyVal {29 let b = b.clone();30 if let Some(params) = &b.params {31 let params = params.clone();3233 #[derive(Trace)]34 struct LazyMethodBinding {35 context_creator: FutureWrapper<Context>,36 name: IStr,37 params: ParamsDesc,38 value: LocExpr,39 }40 impl LazyValValue for LazyMethodBinding {41 fn get(self: Box<Self>) -> Result<Val> {42 Ok(evaluate_method(43 self.context_creator.unwrap(),44 self.name,45 self.params,46 self.value,47 ))48 }49 }5051 LazyVal::new(TraceBox(Box::new(LazyMethodBinding {52 context_creator,53 name: b.name.clone(),54 params,55 value: b.value.clone(),56 })))57 } else {58 #[derive(Trace)]59 struct LazyNamedBinding {60 context_creator: FutureWrapper<Context>,61 name: IStr,62 value: LocExpr,63 }64 impl LazyValValue for LazyNamedBinding {65 fn get(self: Box<Self>) -> Result<Val> {66 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)67 }68 }69 LazyVal::new(TraceBox(Box::new(LazyNamedBinding {70 context_creator,71 name: b.name.clone(),72 value: b.value,73 })))74 }75}7677pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {78 let b = b.clone();79 if let Some(params) = &b.params {80 let params = params.clone();8182 #[derive(Trace)]83 struct BindableMethodLazyVal {84 this: Option<ObjValue>,85 super_obj: Option<ObjValue>,8687 context_creator: ContextCreator,88 name: IStr,89 params: ParamsDesc,90 value: LocExpr,91 }92 impl LazyValValue for BindableMethodLazyVal {93 fn get(self: Box<Self>) -> Result<Val> {94 Ok(evaluate_method(95 self.context_creator.create(self.this, self.super_obj)?,96 self.name,97 self.params,98 self.value,99 ))100 }101 }102103 #[derive(Trace)]104 struct BindableMethod {105 context_creator: ContextCreator,106 name: IStr,107 params: ParamsDesc,108 value: LocExpr,109 }110 impl Bindable for BindableMethod {111 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {112 Ok(LazyVal::new(TraceBox(Box::new(BindableMethodLazyVal {113 this,114 super_obj,115116 context_creator: self.context_creator.clone(),117 name: self.name.clone(),118 params: self.params.clone(),119 value: self.value.clone(),120 }))))121 }122 }123124 (125 b.name.clone(),126 LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {127 context_creator,128 name: b.name.clone(),129 params,130 value: b.value.clone(),131 })))),132 )133 } else {134 #[derive(Trace)]135 struct BindableNamedLazyVal {136 this: Option<ObjValue>,137 super_obj: Option<ObjValue>,138139 context_creator: ContextCreator,140 name: IStr,141 value: LocExpr,142 }143 impl LazyValValue for BindableNamedLazyVal {144 fn get(self: Box<Self>) -> Result<Val> {145 evaluate_named(146 self.context_creator.create(self.this, self.super_obj)?,147 &self.value,148 self.name,149 )150 }151 }152153 #[derive(Trace)]154 struct BindableNamed {155 context_creator: ContextCreator,156 name: IStr,157 value: LocExpr,158 }159 impl Bindable for BindableNamed {160 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {161 Ok(LazyVal::new(TraceBox(Box::new(BindableNamedLazyVal {162 this,163 super_obj,164165 context_creator: self.context_creator.clone(),166 name: self.name.clone(),167 value: self.value.clone(),168 }))))169 }170 }171172 (173 b.name.clone(),174 LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableNamed {175 context_creator,176 name: b.name.clone(),177 value: b.value.clone(),178 })))),179 )180 }181}182183pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {184 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {185 name,186 ctx,187 params,188 body,189 })))190}191192pub fn evaluate_field_name(193 context: Context,194 field_name: &jrsonnet_parser::FieldName,195) -> Result<Option<IStr>> {196 Ok(match field_name {197 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),198 jrsonnet_parser::FieldName::Dyn(expr) => push_frame(199 CallLocation::new(&expr.1),200 || "evaluating field name".to_string(),201 || {202 let value = evaluate(context, expr)?;203 if matches!(value, Val::Null) {204 Ok(None)205 } else {206 Ok(Some(IStr::try_from(value)?))207 }208 },209 )?,210 })211}212213pub fn evaluate_comp(214 context: Context,215 specs: &[CompSpec],216 callback: &mut impl FnMut(Context) -> Result<()>,217) -> Result<()> {218 match specs.get(0) {219 None => callback(context)?,220 Some(CompSpec::IfSpec(IfSpecData(cond))) => {221 if bool::try_from(evaluate(context.clone(), cond)?)? {222 evaluate_comp(context, &specs[1..], callback)?223 }224 }225 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {226 Val::Arr(list) => {227 for item in list.iter() {228 evaluate_comp(229 context.clone().with_var(var.clone(), item?.clone()),230 &specs[1..],231 callback,232 )?233 }234 }235 _ => throw!(InComprehensionCanOnlyIterateOverArray),236 },237 }238 Ok(())239}240241pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {242 let new_bindings = FutureWrapper::new();243 let future_this = FutureWrapper::new();244 let context_creator = ContextCreator(context.clone(), new_bindings.clone());245 {246 let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());247 for (n, b) in members248 .iter()249 .filter_map(|m| match m {250 Member::BindStmt(b) => Some(b.clone()),251 _ => None,252 })253 .map(|b| evaluate_binding(&b, context_creator.clone()))254 {255 bindings.insert(n, b);256 }257 new_bindings.fill(bindings);258 }259260 let mut builder = ObjValueBuilder::new();261 for member in members.iter() {262 match member {263 Member::Field(FieldMember {264 name,265 plus,266 params: None,267 visibility,268 value,269 }) => {270 let name = evaluate_field_name(context.clone(), name)?;271 if name.is_none() {272 continue;273 }274 let name = name.unwrap();275276 #[derive(Trace)]277 struct ObjMemberBinding {278 context_creator: ContextCreator,279 value: LocExpr,280 name: IStr,281 }282 impl Bindable for ObjMemberBinding {283 fn bind(284 &self,285 this: Option<ObjValue>,286 super_obj: Option<ObjValue>,287 ) -> Result<LazyVal> {288 Ok(LazyVal::new_resolved(evaluate_named(289 self.context_creator.create(this, super_obj)?,290 &self.value,291 self.name.clone(),292 )?))293 }294 }295 builder296 .member(name.clone())297 .with_add(*plus)298 .with_visibility(*visibility)299 .with_location(value.1.clone())300 .bindable(TraceBox(Box::new(ObjMemberBinding {301 context_creator: context_creator.clone(),302 value: value.clone(),303 name,304 })))?;305 }306 Member::Field(FieldMember {307 name,308 params: Some(params),309 value,310 ..311 }) => {312 let name = evaluate_field_name(context.clone(), name)?;313 if name.is_none() {314 continue;315 }316 let name = name.unwrap();317 #[derive(Trace)]318 struct ObjMemberBinding {319 context_creator: ContextCreator,320 value: LocExpr,321 params: ParamsDesc,322 name: IStr,323 }324 impl Bindable for ObjMemberBinding {325 fn bind(326 &self,327 this: Option<ObjValue>,328 super_obj: Option<ObjValue>,329 ) -> Result<LazyVal> {330 Ok(LazyVal::new_resolved(evaluate_method(331 self.context_creator.create(this, super_obj)?,332 self.name.clone(),333 self.params.clone(),334 self.value.clone(),335 )))336 }337 }338 builder339 .member(name.clone())340 .hide()341 .with_location(value.1.clone())342 .bindable(TraceBox(Box::new(ObjMemberBinding {343 context_creator: context_creator.clone(),344 value: value.clone(),345 params: params.clone(),346 name,347 })))?;348 }349 Member::BindStmt(_) => {}350 Member::AssertStmt(stmt) => {351 #[derive(Trace)]352 struct ObjectAssert {353 context_creator: ContextCreator,354 assert: AssertStmt,355 }356 impl ObjectAssertion for ObjectAssert {357 fn run(358 &self,359 this: Option<ObjValue>,360 super_obj: Option<ObjValue>,361 ) -> Result<()> {362 let ctx = self.context_creator.create(this, super_obj)?;363 evaluate_assert(ctx, &self.assert)364 }365 }366 builder.assert(TraceBox(Box::new(ObjectAssert {367 context_creator: context_creator.clone(),368 assert: stmt.clone(),369 })));370 }371 }372 }373 let this = builder.build();374 future_this.fill(this.clone());375 Ok(this)376}377378pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {379 Ok(match object {380 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,381 ObjBody::ObjComp(obj) => {382 let future_this = FutureWrapper::new();383 let mut builder = ObjValueBuilder::new();384 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {385 let new_bindings = FutureWrapper::new();386 let context_creator = ContextCreator(context.clone(), new_bindings.clone());387 let mut bindings: GcHashMap<IStr, LazyBinding> =388 GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());389 for (n, b) in obj390 .pre_locals391 .iter()392 .chain(obj.post_locals.iter())393 .map(|b| evaluate_binding(b, context_creator.clone()))394 {395 bindings.insert(n, b);396 }397 new_bindings.fill(bindings.clone());398 let ctx = ctx.extend_unbound(bindings, None, None, None)?;399 let key = evaluate(ctx.clone(), &obj.key)?;400401 match key {402 Val::Null => {}403 Val::Str(n) => {404 #[derive(Trace)]405 struct ObjCompBinding {406 context: Context,407 value: LocExpr,408 }409 impl Bindable for ObjCompBinding {410 fn bind(411 &self,412 this: Option<ObjValue>,413 _super_obj: Option<ObjValue>,414 ) -> Result<LazyVal> {415 Ok(LazyVal::new_resolved(evaluate(416 self.context417 .clone()418 .extend(GcHashMap::new(), None, this, None),419 &self.value,420 )?))421 }422 }423 builder424 .member(n)425 .with_location(obj.value.1.clone())426 .with_add(obj.plus)427 .bindable(TraceBox(Box::new(ObjCompBinding {428 context: ctx,429 value: obj.value.clone(),430 })))?;431 }432 v => throw!(FieldMustBeStringGot(v.value_type())),433 }434435 Ok(())436 })?;437438 let this = builder.build();439 future_this.fill(this.clone());440 this441 }442 })443}444445pub fn evaluate_apply(446 context: Context,447 value: &LocExpr,448 args: &ArgsDesc,449 loc: CallLocation,450 tailstrict: bool,451) -> Result<Val> {452 let value = evaluate(context.clone(), value)?;453 Ok(match value {454 Val::Func(f) => {455 let body = || f.evaluate(context, loc, args, tailstrict);456 if tailstrict {457 body()?458 } else {459 push_frame(loc, || format!("function <{}> call", f.name()), body)?460 }461 }462 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),463 })464}465466pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {467 let value = &assertion.0;468 let msg = &assertion.1;469 let assertion_result = push_frame(470 CallLocation::new(&value.1),471 || "assertion condition".to_owned(),472 || bool::try_from(evaluate(context.clone(), value)?),473 )?;474 if !assertion_result {475 push_frame(476 CallLocation::new(&value.1),477 || "assertion failure".to_owned(),478 || {479 if let Some(msg) = msg {480 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));481 } else {482 throw!(AssertionFailed(Val::Null.to_string()?));483 }484 },485 )?486 }487 Ok(())488}489490pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {491 use Expr::*;492 let LocExpr(expr, _loc) = lexpr;493 Ok(match &**expr {494 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),495 _ => evaluate(context, lexpr)?,496 })497}498499pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {500 use Expr::*;501 let LocExpr(expr, loc) = expr;502 // let bp = with_state(|s| s.0.stop_at.borrow().clone());503 Ok(match &**expr {504 Literal(LiteralType::This) => {505 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)506 }507 Literal(LiteralType::Super) => Val::Obj(508 context509 .super_obj()510 .clone()511 .ok_or(NoSuperFound)?512 .with_this(context.this().clone().unwrap()),513 ),514 Literal(LiteralType::Dollar) => {515 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)516 }517 Literal(LiteralType::True) => Val::Bool(true),518 Literal(LiteralType::False) => Val::Bool(false),519 Literal(LiteralType::Null) => Val::Null,520 Parened(e) => evaluate(context, e)?,521 Str(v) => Val::Str(v.clone()),522 Num(v) => Val::new_checked_num(*v)?,523 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,524 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,525 Var(name) => push_frame(526 CallLocation::new(loc),527 || format!("variable <{}> access", name),528 || context.binding(name.clone())?.evaluate(),529 )?,530 Index(value, index) => {531 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {532 (Val::Obj(v), Val::Str(s)) => {533 let sn = s.clone();534 push_frame(535 CallLocation::new(loc),536 || format!("field <{}> access", sn),537 || {538 if let Some(v) = v.get(s.clone())? {539 Ok(v)540 } else {541 throw!(NoSuchField(s))542 }543 },544 )?545 }546 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(547 ValType::Obj,548 ValType::Str,549 n.value_type(),550 )),551552 (Val::Arr(v), Val::Num(n)) => {553 if n.fract() > f64::EPSILON {554 throw!(FractionalIndex)555 }556 v.get(n as usize)?557 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?558 }559 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),560 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(561 ValType::Arr,562 ValType::Num,563 n.value_type(),564 )),565566 (Val::Str(s), Val::Num(n)) => Val::Str(567 s.chars()568 .skip(n as usize)569 .take(1)570 .collect::<String>()571 .into(),572 ),573 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(574 ValType::Str,575 ValType::Num,576 n.value_type(),577 )),578579 (v, _) => throw!(CantIndexInto(v.value_type())),580 }581 }582 LocalExpr(bindings, returned) => {583 let mut new_bindings: GcHashMap<IStr, LazyVal> =584 GcHashMap::with_capacity(bindings.len());585 let future_context = Context::new_future();586 for b in bindings {587 new_bindings.insert(588 b.name.clone(),589 evaluate_binding_in_future(b, future_context.clone()),590 );591 }592 let context = context593 .extend_bound(new_bindings)594 .into_future(future_context);595 evaluate(context, &returned.clone())?596 }597 Arr(items) => {598 let mut out = Vec::with_capacity(items.len());599 for item in items {600 // TODO: Implement ArrValue::Lazy with same context for every element?601 #[derive(Trace)]602 struct ArrayElement {603 context: Context,604 item: LocExpr,605 }606 impl LazyValValue for ArrayElement {607 fn get(self: Box<Self>) -> Result<Val> {608 evaluate(self.context, &self.item)609 }610 }611 out.push(LazyVal::new(TraceBox(Box::new(ArrayElement {612 context: context.clone(),613 item: item.clone(),614 }))));615 }616 Val::Arr(out.into())617 }618 ArrComp(expr, comp_specs) => {619 let mut out = Vec::new();620 evaluate_comp(context, comp_specs, &mut |ctx| {621 out.push(evaluate(ctx, expr)?);622 Ok(())623 })?;624 Val::Arr(ArrValue::Eager(Cc::new(out)))625 }626 Obj(body) => Val::Obj(evaluate_object(context, body)?),627 ObjExtend(s, t) => evaluate_add_op(628 &evaluate(context.clone(), s)?,629 &Val::Obj(evaluate_object(context, t)?),630 )?,631 Apply(value, args, tailstrict) => {632 evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?633 }634 Function(params, body) => {635 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())636 }637 Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(638 BUILTINS639 .with(|b| b.get(name).copied())640 .ok_or_else(|| IntrinsicNotFound(name.clone()))?,641 )),642 AssertExpr(assert, returned) => {643 evaluate_assert(context.clone(), assert)?;644 evaluate(context, returned)?645 }646 ErrorStmt(e) => push_frame(647 CallLocation::new(loc),648 || "error statement".to_owned(),649 || throw!(RuntimeError(evaluate(context, e)?.to_string()?,)),650 )?,651 IfElse {652 cond,653 cond_then,654 cond_else,655 } => {656 if push_frame(657 CallLocation::new(loc),658 || "if condition".to_owned(),659 || bool::try_from(evaluate(context.clone(), &cond.0)?),660 )? {661 evaluate(context, cond_then)?662 } else {663 match cond_else {664 Some(v) => evaluate(context, v)?,665 None => Val::Null,666 }667 }668 }669 Slice(value, desc) => {670 let indexable = evaluate(context.clone(), value)?;671 let loc = CallLocation::new(loc);672673 fn parse_idx<const MIN: usize>(674 loc: CallLocation,675 context: &Context,676 expr: &Option<LocExpr>,677 desc: &'static str,678 ) -> Result<Option<BoundedUsize<MIN, { i32::MAX as usize }>>> {679 if let Some(value) = expr {680 Ok(Some(push_frame(681 loc,682 || format!("slice {}", desc),683 || Ok(evaluate(context.clone(), value)?.try_into()?),684 )?))685 } else {686 Ok(None)687 }688 }689690 let start = parse_idx(loc, &context, &desc.start, "start")?;691 let end = parse_idx(loc, &context, &desc.end, "end")?;692 let step = parse_idx(loc, &context, &desc.step, "step")?;693694 std_slice(indexable.into_indexable()?, start, end, step)?695 }696 Import(path) => {697 let tmp = loc.clone().0;698 let mut import_location = tmp.to_path_buf();699 import_location.pop();700 push_frame(701 CallLocation::new(loc),702 || format!("import {:?}", path),703 || with_state(|s| s.import_file(&import_location, path)),704 )?705 }706 ImportStr(path) => {707 let tmp = loc.clone().0;708 let mut import_location = tmp.to_path_buf();709 import_location.pop();710 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)711 }712 ImportBin(path) => {713 let tmp = loc.clone().0;714 let mut import_location = tmp.to_path_buf();715 import_location.pop();716 let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;717 Val::Arr(ArrValue::Bytes(bytes))718 }719 })720}1use std::convert::TryFrom;23use gcmodule::{Cc, Trace};4use jrsonnet_interner::IStr;5use jrsonnet_parser::{6 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, ForSpecData, IfSpecData,7 LiteralType, LocExpr, Member, ObjBody, ParamsDesc,8};9use jrsonnet_types::ValType;1011use crate::{12 builtin::{std_slice, BUILTINS},13 error::Error::*,14 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},15 function::CallLocation,16 gc::TraceBox,17 push_frame, throw,18 typed::BoundedUsize,19 val::{ArrValue, FuncDesc, FuncVal, LazyValValue},20 with_state, Bindable, Context, ContextCreator, FutureWrapper, GcHashMap, LazyBinding, LazyVal,21 ObjValue, ObjValueBuilder, ObjectAssertion, Result, Val,22};23pub mod operator;2425pub fn evaluate_binding_in_future(26 b: &BindSpec,27 context_creator: FutureWrapper<Context>,28) -> LazyVal {29 let b = b.clone();30 if let Some(params) = &b.params {31 let params = params.clone();3233 #[derive(Trace)]34 struct LazyMethodBinding {35 context_creator: FutureWrapper<Context>,36 name: IStr,37 params: ParamsDesc,38 value: LocExpr,39 }40 impl LazyValValue for LazyMethodBinding {41 fn get(self: Box<Self>) -> Result<Val> {42 Ok(evaluate_method(43 self.context_creator.unwrap(),44 self.name,45 self.params,46 self.value,47 ))48 }49 }5051 LazyVal::new(TraceBox(Box::new(LazyMethodBinding {52 context_creator,53 name: b.name.clone(),54 params,55 value: b.value.clone(),56 })))57 } else {58 #[derive(Trace)]59 struct LazyNamedBinding {60 context_creator: FutureWrapper<Context>,61 name: IStr,62 value: LocExpr,63 }64 impl LazyValValue for LazyNamedBinding {65 fn get(self: Box<Self>) -> Result<Val> {66 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)67 }68 }69 LazyVal::new(TraceBox(Box::new(LazyNamedBinding {70 context_creator,71 name: b.name.clone(),72 value: b.value,73 })))74 }75}7677pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {78 let b = b.clone();79 if let Some(params) = &b.params {80 let params = params.clone();8182 #[derive(Trace)]83 struct BindableMethodLazyVal {84 this: Option<ObjValue>,85 super_obj: Option<ObjValue>,8687 context_creator: ContextCreator,88 name: IStr,89 params: ParamsDesc,90 value: LocExpr,91 }92 impl LazyValValue for BindableMethodLazyVal {93 fn get(self: Box<Self>) -> Result<Val> {94 Ok(evaluate_method(95 self.context_creator.create(self.this, self.super_obj)?,96 self.name,97 self.params,98 self.value,99 ))100 }101 }102103 #[derive(Trace)]104 struct BindableMethod {105 context_creator: ContextCreator,106 name: IStr,107 params: ParamsDesc,108 value: LocExpr,109 }110 impl Bindable for BindableMethod {111 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {112 Ok(LazyVal::new(TraceBox(Box::new(BindableMethodLazyVal {113 this,114 super_obj,115116 context_creator: self.context_creator.clone(),117 name: self.name.clone(),118 params: self.params.clone(),119 value: self.value.clone(),120 }))))121 }122 }123124 (125 b.name.clone(),126 LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {127 context_creator,128 name: b.name.clone(),129 params,130 value: b.value.clone(),131 })))),132 )133 } else {134 #[derive(Trace)]135 struct BindableNamedLazyVal {136 this: Option<ObjValue>,137 super_obj: Option<ObjValue>,138139 context_creator: ContextCreator,140 name: IStr,141 value: LocExpr,142 }143 impl LazyValValue for BindableNamedLazyVal {144 fn get(self: Box<Self>) -> Result<Val> {145 evaluate_named(146 self.context_creator.create(self.this, self.super_obj)?,147 &self.value,148 self.name,149 )150 }151 }152153 #[derive(Trace)]154 struct BindableNamed {155 context_creator: ContextCreator,156 name: IStr,157 value: LocExpr,158 }159 impl Bindable for BindableNamed {160 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {161 Ok(LazyVal::new(TraceBox(Box::new(BindableNamedLazyVal {162 this,163 super_obj,164165 context_creator: self.context_creator.clone(),166 name: self.name.clone(),167 value: self.value.clone(),168 }))))169 }170 }171172 (173 b.name.clone(),174 LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableNamed {175 context_creator,176 name: b.name.clone(),177 value: b.value.clone(),178 })))),179 )180 }181}182183pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {184 Val::Func(FuncVal::Normal(Cc::new(FuncDesc {185 name,186 ctx,187 params,188 body,189 })))190}191192pub fn evaluate_field_name(193 context: Context,194 field_name: &jrsonnet_parser::FieldName,195) -> Result<Option<IStr>> {196 Ok(match field_name {197 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),198 jrsonnet_parser::FieldName::Dyn(expr) => push_frame(199 CallLocation::new(&expr.1),200 || "evaluating field name".to_string(),201 || {202 let value = evaluate(context, expr)?;203 if matches!(value, Val::Null) {204 Ok(None)205 } else {206 Ok(Some(IStr::try_from(value)?))207 }208 },209 )?,210 })211}212213pub fn evaluate_comp(214 context: Context,215 specs: &[CompSpec],216 callback: &mut impl FnMut(Context) -> Result<()>,217) -> Result<()> {218 match specs.get(0) {219 None => callback(context)?,220 Some(CompSpec::IfSpec(IfSpecData(cond))) => {221 if bool::try_from(evaluate(context.clone(), cond)?)? {222 evaluate_comp(context, &specs[1..], callback)?223 }224 }225 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {226 Val::Arr(list) => {227 for item in list.iter() {228 evaluate_comp(229 context.clone().with_var(var.clone(), item?.clone()),230 &specs[1..],231 callback,232 )?233 }234 }235 _ => throw!(InComprehensionCanOnlyIterateOverArray),236 },237 }238 Ok(())239}240241pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {242 let new_bindings = FutureWrapper::new();243 let future_this = FutureWrapper::new();244 let context_creator = ContextCreator(context.clone(), new_bindings.clone());245 {246 let mut bindings: GcHashMap<IStr, LazyBinding> = GcHashMap::with_capacity(members.len());247 for (n, b) in members248 .iter()249 .filter_map(|m| match m {250 Member::BindStmt(b) => Some(b.clone()),251 _ => None,252 })253 .map(|b| evaluate_binding(&b, context_creator.clone()))254 {255 bindings.insert(n, b);256 }257 new_bindings.fill(bindings);258 }259260 let mut builder = ObjValueBuilder::new();261 for member in members.iter() {262 match member {263 Member::Field(FieldMember {264 name,265 plus,266 params: None,267 visibility,268 value,269 }) => {270 let name = evaluate_field_name(context.clone(), name)?;271 if name.is_none() {272 continue;273 }274 let name = name.unwrap();275276 #[derive(Trace)]277 struct ObjMemberBinding {278 context_creator: ContextCreator,279 value: LocExpr,280 name: IStr,281 }282 impl Bindable for ObjMemberBinding {283 fn bind(284 &self,285 this: Option<ObjValue>,286 super_obj: Option<ObjValue>,287 ) -> Result<LazyVal> {288 Ok(LazyVal::new_resolved(evaluate_named(289 self.context_creator.create(this, super_obj)?,290 &self.value,291 self.name.clone(),292 )?))293 }294 }295 builder296 .member(name.clone())297 .with_add(*plus)298 .with_visibility(*visibility)299 .with_location(value.1.clone())300 .bindable(TraceBox(Box::new(ObjMemberBinding {301 context_creator: context_creator.clone(),302 value: value.clone(),303 name,304 })))?;305 }306 Member::Field(FieldMember {307 name,308 params: Some(params),309 value,310 ..311 }) => {312 let name = evaluate_field_name(context.clone(), name)?;313 if name.is_none() {314 continue;315 }316 let name = name.unwrap();317 #[derive(Trace)]318 struct ObjMemberBinding {319 context_creator: ContextCreator,320 value: LocExpr,321 params: ParamsDesc,322 name: IStr,323 }324 impl Bindable for ObjMemberBinding {325 fn bind(326 &self,327 this: Option<ObjValue>,328 super_obj: Option<ObjValue>,329 ) -> Result<LazyVal> {330 Ok(LazyVal::new_resolved(evaluate_method(331 self.context_creator.create(this, super_obj)?,332 self.name.clone(),333 self.params.clone(),334 self.value.clone(),335 )))336 }337 }338 builder339 .member(name.clone())340 .hide()341 .with_location(value.1.clone())342 .bindable(TraceBox(Box::new(ObjMemberBinding {343 context_creator: context_creator.clone(),344 value: value.clone(),345 params: params.clone(),346 name,347 })))?;348 }349 Member::BindStmt(_) => {}350 Member::AssertStmt(stmt) => {351 #[derive(Trace)]352 struct ObjectAssert {353 context_creator: ContextCreator,354 assert: AssertStmt,355 }356 impl ObjectAssertion for ObjectAssert {357 fn run(358 &self,359 this: Option<ObjValue>,360 super_obj: Option<ObjValue>,361 ) -> Result<()> {362 let ctx = self.context_creator.create(this, super_obj)?;363 evaluate_assert(ctx, &self.assert)364 }365 }366 builder.assert(TraceBox(Box::new(ObjectAssert {367 context_creator: context_creator.clone(),368 assert: stmt.clone(),369 })));370 }371 }372 }373 let this = builder.build();374 future_this.fill(this.clone());375 Ok(this)376}377378pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {379 Ok(match object {380 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,381 ObjBody::ObjComp(obj) => {382 let future_this = FutureWrapper::new();383 let mut builder = ObjValueBuilder::new();384 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {385 let new_bindings = FutureWrapper::new();386 let context_creator = ContextCreator(context.clone(), new_bindings.clone());387 let mut bindings: GcHashMap<IStr, LazyBinding> =388 GcHashMap::with_capacity(obj.pre_locals.len() + obj.post_locals.len());389 for (n, b) in obj390 .pre_locals391 .iter()392 .chain(obj.post_locals.iter())393 .map(|b| evaluate_binding(b, context_creator.clone()))394 {395 bindings.insert(n, b);396 }397 new_bindings.fill(bindings.clone());398 let ctx = ctx.extend_unbound(bindings, None, None, None)?;399 let key = evaluate(ctx.clone(), &obj.key)?;400401 match key {402 Val::Null => {}403 Val::Str(n) => {404 #[derive(Trace)]405 struct ObjCompBinding {406 context: Context,407 value: LocExpr,408 }409 impl Bindable for ObjCompBinding {410 fn bind(411 &self,412 this: Option<ObjValue>,413 _super_obj: Option<ObjValue>,414 ) -> Result<LazyVal> {415 Ok(LazyVal::new_resolved(evaluate(416 self.context417 .clone()418 .extend(GcHashMap::new(), None, this, None),419 &self.value,420 )?))421 }422 }423 builder424 .member(n)425 .with_location(obj.value.1.clone())426 .with_add(obj.plus)427 .bindable(TraceBox(Box::new(ObjCompBinding {428 context: ctx,429 value: obj.value.clone(),430 })))?;431 }432 v => throw!(FieldMustBeStringGot(v.value_type())),433 }434435 Ok(())436 })?;437438 let this = builder.build();439 future_this.fill(this.clone());440 this441 }442 })443}444445pub fn evaluate_apply(446 context: Context,447 value: &LocExpr,448 args: &ArgsDesc,449 loc: CallLocation,450 tailstrict: bool,451) -> Result<Val> {452 let value = evaluate(context.clone(), value)?;453 Ok(match value {454 Val::Func(f) => {455 let body = || f.evaluate(context, loc, args, tailstrict);456 if tailstrict {457 body()?458 } else {459 push_frame(loc, || format!("function <{}> call", f.name()), body)?460 }461 }462 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),463 })464}465466pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {467 let value = &assertion.0;468 let msg = &assertion.1;469 let assertion_result = push_frame(470 CallLocation::new(&value.1),471 || "assertion condition".to_owned(),472 || bool::try_from(evaluate(context.clone(), value)?),473 )?;474 if !assertion_result {475 push_frame(476 CallLocation::new(&value.1),477 || "assertion failure".to_owned(),478 || {479 if let Some(msg) = msg {480 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));481 } else {482 throw!(AssertionFailed(Val::Null.to_string()?));483 }484 },485 )?486 }487 Ok(())488}489490pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {491 use Expr::*;492 let LocExpr(expr, _loc) = lexpr;493 Ok(match &**expr {494 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),495 _ => evaluate(context, lexpr)?,496 })497}498499pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {500 use Expr::*;501 let LocExpr(expr, loc) = expr;502 // let bp = with_state(|s| s.0.stop_at.borrow().clone());503 Ok(match &**expr {504 Literal(LiteralType::This) => {505 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)506 }507 Literal(LiteralType::Super) => Val::Obj(508 context509 .super_obj()510 .clone()511 .ok_or(NoSuperFound)?512 .with_this(context.this().clone().unwrap()),513 ),514 Literal(LiteralType::Dollar) => {515 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)516 }517 Literal(LiteralType::True) => Val::Bool(true),518 Literal(LiteralType::False) => Val::Bool(false),519 Literal(LiteralType::Null) => Val::Null,520 Parened(e) => evaluate(context, e)?,521 Str(v) => Val::Str(v.clone()),522 Num(v) => Val::new_checked_num(*v)?,523 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,524 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,525 Var(name) => push_frame(526 CallLocation::new(loc),527 || format!("variable <{}> access", name),528 || context.binding(name.clone())?.evaluate(),529 )?,530 Index(value, index) => {531 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {532 (Val::Obj(v), Val::Str(s)) => {533 let sn = s.clone();534 push_frame(535 CallLocation::new(loc),536 || format!("field <{}> access", sn),537 || {538 if let Some(v) = v.get(s.clone())? {539 Ok(v)540 } else {541 throw!(NoSuchField(s))542 }543 },544 )?545 }546 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(547 ValType::Obj,548 ValType::Str,549 n.value_type(),550 )),551552 (Val::Arr(v), Val::Num(n)) => {553 if n.fract() > f64::EPSILON {554 throw!(FractionalIndex)555 }556 v.get(n as usize)?557 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?558 }559 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),560 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(561 ValType::Arr,562 ValType::Num,563 n.value_type(),564 )),565566 (Val::Str(s), Val::Num(n)) => Val::Str(567 s.chars()568 .skip(n as usize)569 .take(1)570 .collect::<String>()571 .into(),572 ),573 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(574 ValType::Str,575 ValType::Num,576 n.value_type(),577 )),578579 (v, _) => throw!(CantIndexInto(v.value_type())),580 }581 }582 LocalExpr(bindings, returned) => {583 let mut new_bindings: GcHashMap<IStr, LazyVal> =584 GcHashMap::with_capacity(bindings.len());585 let future_context = Context::new_future();586 for b in bindings {587 new_bindings.insert(588 b.name.clone(),589 evaluate_binding_in_future(b, future_context.clone()),590 );591 }592 let context = context593 .extend_bound(new_bindings)594 .into_future(future_context);595 evaluate(context, &returned.clone())?596 }597 Arr(items) => {598 let mut out = Vec::with_capacity(items.len());599 for item in items {600 // TODO: Implement ArrValue::Lazy with same context for every element?601 #[derive(Trace)]602 struct ArrayElement {603 context: Context,604 item: LocExpr,605 }606 impl LazyValValue for ArrayElement {607 fn get(self: Box<Self>) -> Result<Val> {608 evaluate(self.context, &self.item)609 }610 }611 out.push(LazyVal::new(TraceBox(Box::new(ArrayElement {612 context: context.clone(),613 item: item.clone(),614 }))));615 }616 Val::Arr(out.into())617 }618 ArrComp(expr, comp_specs) => {619 let mut out = Vec::new();620 evaluate_comp(context, comp_specs, &mut |ctx| {621 out.push(evaluate(ctx, expr)?);622 Ok(())623 })?;624 Val::Arr(ArrValue::Eager(Cc::new(out)))625 }626 Obj(body) => Val::Obj(evaluate_object(context, body)?),627 ObjExtend(s, t) => evaluate_add_op(628 &evaluate(context.clone(), s)?,629 &Val::Obj(evaluate_object(context, t)?),630 )?,631 Apply(value, args, tailstrict) => {632 evaluate_apply(context, value, args, CallLocation::new(loc), *tailstrict)?633 }634 Function(params, body) => {635 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())636 }637 Intrinsic(name) => Val::Func(FuncVal::StaticBuiltin(638 BUILTINS639 .with(|b| b.get(name).copied())640 .ok_or_else(|| IntrinsicNotFound(name.clone()))?,641 )),642 AssertExpr(assert, returned) => {643 evaluate_assert(context.clone(), assert)?;644 evaluate(context, returned)?645 }646 ErrorStmt(e) => push_frame(647 CallLocation::new(loc),648 || "error statement".to_owned(),649 || throw!(RuntimeError(evaluate(context, e)?.to_string()?,)),650 )?,651 IfElse {652 cond,653 cond_then,654 cond_else,655 } => {656 if push_frame(657 CallLocation::new(loc),658 || "if condition".to_owned(),659 || bool::try_from(evaluate(context.clone(), &cond.0)?),660 )? {661 evaluate(context, cond_then)?662 } else {663 match cond_else {664 Some(v) => evaluate(context, v)?,665 None => Val::Null,666 }667 }668 }669 Slice(value, desc) => {670 let indexable = evaluate(context.clone(), value)?;671 let loc = CallLocation::new(loc);672673 fn parse_idx<const MIN: usize>(674 loc: CallLocation,675 context: &Context,676 expr: &Option<LocExpr>,677 desc: &'static str,678 ) -> Result<Option<BoundedUsize<MIN, { i32::MAX as usize }>>> {679 if let Some(value) = expr {680 Ok(Some(push_frame(681 loc,682 || format!("slice {}", desc),683 || evaluate(context.clone(), value)?.try_into(),684 )?))685 } else {686 Ok(None)687 }688 }689690 let start = parse_idx(loc, &context, &desc.start, "start")?;691 let end = parse_idx(loc, &context, &desc.end, "end")?;692 let step = parse_idx(loc, &context, &desc.step, "step")?;693694 std_slice(indexable.into_indexable()?, start, end, step)?695 }696 Import(path) => {697 let tmp = loc.clone().0;698 let mut import_location = tmp.to_path_buf();699 import_location.pop();700 push_frame(701 CallLocation::new(loc),702 || format!("import {:?}", path),703 || with_state(|s| s.import_file(&import_location, path)),704 )?705 }706 ImportStr(path) => {707 let tmp = loc.clone().0;708 let mut import_location = tmp.to_path_buf();709 import_location.pop();710 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)711 }712 ImportBin(path) => {713 let tmp = loc.clone().0;714 let mut import_location = tmp.to_path_buf();715 import_location.pop();716 let bytes = with_state(|s| s.import_file_bin(&import_location, path))?;717 Val::Arr(ArrValue::Bytes(bytes))718 }719 })720}crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -19,13 +19,13 @@
};
#[cfg(not(feature = "exp-preserve-order"))]
-pub(crate) mod ordering {
+mod ordering {
use gcmodule::Trace;
#[derive(Clone, Copy, Default, Debug, Trace)]
pub struct FieldIndex;
impl FieldIndex {
- pub fn next(self) -> Self {
+ pub const fn next(self) -> Self {
Self
}
}
@@ -33,7 +33,7 @@
#[derive(Clone, Copy, Default, Debug, Trace)]
pub struct SuperDepth;
impl SuperDepth {
- pub fn deeper(self) -> Self {
+ pub const fn deeper(self) -> Self {
Self
}
}
@@ -41,7 +41,7 @@
#[derive(Clone, Copy)]
pub struct FieldSortKey;
impl FieldSortKey {
- pub fn new(_: SuperDepth, _: FieldIndex) -> Self {
+ pub const fn new(_: SuperDepth, _: FieldIndex) -> Self {
Self
}
}
@@ -87,7 +87,7 @@
}
}
-pub(crate) use ordering::*;
+use ordering::*;
#[derive(Debug, Trace)]
pub struct ObjMember {
@@ -594,7 +594,7 @@
pub fn bindable(self, bindable: TraceBox<dyn Bindable>) {
self.binding(LazyBinding::Bindable(Cc::new(bindable)))
}
- pub fn binding(self, binding: LazyBinding) -> () {
+ pub fn binding(self, binding: LazyBinding) {
let (receiver, name, member) = self.build_member(binding);
let new = receiver.0.clone();
*receiver.0 = new.extend_with_raw_member(name, member)
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -201,16 +201,16 @@
pub(crate) step: u32,
}
impl Slice {
- fn from(&self) -> usize {
+ const fn from(&self) -> usize {
self.from as usize
}
- fn to(&self) -> usize {
+ const fn to(&self) -> usize {
self.to as usize
}
- fn step(&self) -> usize {
+ const fn step(&self) -> usize {
self.step as usize
}
- fn len(&self) -> usize {
+ const fn len(&self) -> usize {
// TODO: use div_ceil
let diff = self.to() - self.from();
let rem = diff % self.step();