difftreelog
Merge pull request #62 from CertainLach/fix/missing-language-features
in: master
Missing language features
9 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
@@ -518,7 +518,7 @@
], {
let mut acc = init;
for i in arr.iter().rev() {
- acc = func.evaluate_values(context.clone(), &[acc, i?])?;
+ acc = func.evaluate_values(context.clone(), &[i?, acc])?;
}
Ok(acc)
})
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -73,6 +73,9 @@
.cloned()
.ok_or(VariableIsNotDefined(name))?)
}
+ pub fn contains_binding(&self, name: IStr) -> bool {
+ self.0.bindings.contains_key(&name)
+ }
pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {
{
ctx.0.borrow_mut().replace(self);
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -56,7 +56,7 @@
BindingParameterASecondTime(IStr),
#[error("too many args, function has {0}")]
TooManyArgsFunctionHas(usize),
- #[error("founction argument is not passed: {0}")]
+ #[error("function argument is not passed: {0}")]
FunctionParameterNotBoundInCall(IStr),
#[error("external variable is not defined: {0}")]
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth1use crate::{2 builtin::std_slice,3 error::Error::*,4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},5 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,6 FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,7 Result, Val,8};9use jrsonnet_gc::{Gc, Trace};10use jrsonnet_interner::IStr;11use jrsonnet_parser::{12 ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,13 IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,14};15use jrsonnet_types::ValType;16use rustc_hash::{FxHashMap, FxHasher};17use std::{collections::HashMap, hash::BuildHasherDefault};18pub mod operator;1920pub fn evaluate_binding_in_future(21 b: &BindSpec,22 context_creator: FutureWrapper<Context>,23) -> LazyVal {24 let b = b.clone();25 if let Some(params) = &b.params {26 let params = params.clone();2728 #[derive(Trace)]29 #[trivially_drop]30 struct LazyMethodBinding {31 context_creator: FutureWrapper<Context>,32 name: IStr,33 params: ParamsDesc,34 value: LocExpr,35 }36 impl LazyValValue for LazyMethodBinding {37 fn get(self: Box<Self>) -> Result<Val> {38 Ok(evaluate_method(39 self.context_creator.unwrap(),40 self.name,41 self.params,42 self.value,43 ))44 }45 }4647 LazyVal::new(Box::new(LazyMethodBinding {48 context_creator,49 name: b.name.clone(),50 params,51 value: b.value.clone(),52 }))53 } else {54 #[derive(Trace)]55 #[trivially_drop]56 struct LazyNamedBinding {57 context_creator: FutureWrapper<Context>,58 name: IStr,59 value: LocExpr,60 }61 impl LazyValValue for LazyNamedBinding {62 fn get(self: Box<Self>) -> Result<Val> {63 evaluate_named(self.context_creator.unwrap(), &self.value, self.name)64 }65 }66 LazyVal::new(Box::new(LazyNamedBinding {67 context_creator,68 name: b.name.clone(),69 value: b.value,70 }))71 }72}7374pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (IStr, LazyBinding) {75 let b = b.clone();76 if let Some(params) = &b.params {77 let params = params.clone();7879 #[derive(Trace)]80 #[trivially_drop]81 struct BindableMethodLazyVal {82 this: Option<ObjValue>,83 super_obj: Option<ObjValue>,8485 context_creator: ContextCreator,86 name: IStr,87 params: ParamsDesc,88 value: LocExpr,89 }90 impl LazyValValue for BindableMethodLazyVal {91 fn get(self: Box<Self>) -> Result<Val> {92 Ok(evaluate_method(93 self.context_creator.create(self.this, self.super_obj)?,94 self.name,95 self.params,96 self.value,97 ))98 }99 }100101 #[derive(Trace)]102 #[trivially_drop]103 struct BindableMethod {104 context_creator: ContextCreator,105 name: IStr,106 params: ParamsDesc,107 value: LocExpr,108 }109 impl Bindable for BindableMethod {110 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {111 Ok(LazyVal::new(Box::new(BindableMethodLazyVal {112 this,113 super_obj,114115 context_creator: self.context_creator.clone(),116 name: self.name.clone(),117 params: self.params.clone(),118 value: self.value.clone(),119 })))120 }121 }122123 (124 b.name.clone(),125 LazyBinding::Bindable(Gc::new(Box::new(BindableMethod {126 context_creator,127 name: b.name.clone(),128 params,129 value: b.value.clone(),130 }))),131 )132 } else {133 #[derive(Trace)]134 #[trivially_drop]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 #[trivially_drop]155 struct BindableNamed {156 context_creator: ContextCreator,157 name: IStr,158 value: LocExpr,159 }160 impl Bindable for BindableNamed {161 fn bind(&self, this: Option<ObjValue>, super_obj: Option<ObjValue>) -> Result<LazyVal> {162 Ok(LazyVal::new(Box::new(BindableNamedLazyVal {163 this,164 super_obj,165166 context_creator: self.context_creator.clone(),167 name: self.name.clone(),168 value: self.value.clone(),169 })))170 }171 }172173 (174 b.name.clone(),175 LazyBinding::Bindable(Gc::new(Box::new(BindableNamed {176 context_creator,177 name: b.name.clone(),178 value: b.value.clone(),179 }))),180 )181 }182}183184pub fn evaluate_method(ctx: Context, name: IStr, params: ParamsDesc, body: LocExpr) -> Val {185 Val::Func(Gc::new(FuncVal::Normal(FuncDesc {186 name,187 ctx,188 params,189 body,190 })))191}192193pub fn evaluate_field_name(194 context: Context,195 field_name: &jrsonnet_parser::FieldName,196) -> Result<Option<IStr>> {197 Ok(match field_name {198 jrsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),199 jrsonnet_parser::FieldName::Dyn(expr) => {200 let value = evaluate(context, expr)?;201 if matches!(value, Val::Null) {202 None203 } else {204 Some(value.try_cast_str("dynamic field name")?)205 }206 }207 })208}209210pub fn evaluate_comp(211 context: Context,212 specs: &[CompSpec],213 callback: &mut impl FnMut(Context) -> Result<()>,214) -> Result<()> {215 match specs.get(0) {216 None => callback(context)?,217 Some(CompSpec::IfSpec(IfSpecData(cond))) => {218 if evaluate(context.clone(), cond)?.try_cast_bool("if spec")? {219 evaluate_comp(context, &specs[1..], callback)?220 }221 }222 Some(CompSpec::ForSpec(ForSpecData(var, expr))) => match evaluate(context.clone(), expr)? {223 Val::Arr(list) => {224 for item in list.iter() {225 evaluate_comp(226 context.clone().with_var(var.clone(), item?.clone()),227 &specs[1..],228 callback,229 )?230 }231 }232 _ => throw!(InComprehensionCanOnlyIterateOverArray),233 },234 }235 Ok(())236}237238pub fn evaluate_member_list_object(context: Context, members: &[Member]) -> Result<ObjValue> {239 let new_bindings = FutureWrapper::new();240 let future_this = FutureWrapper::new();241 let context_creator = ContextCreator(context.clone(), new_bindings.clone());242 {243 let mut bindings: FxHashMap<IStr, LazyBinding> =244 FxHashMap::with_capacity_and_hasher(members.len(), BuildHasherDefault::default());245 for (n, b) in members246 .iter()247 .filter_map(|m| match m {248 Member::BindStmt(b) => Some(b.clone()),249 _ => None,250 })251 .map(|b| evaluate_binding(&b, context_creator.clone()))252 {253 bindings.insert(n, b);254 }255 new_bindings.fill(bindings);256 }257258 let mut builder = ObjValueBuilder::new();259 for member in members.iter() {260 match member {261 Member::Field(FieldMember {262 name,263 plus,264 params: None,265 visibility,266 value,267 }) => {268 let name = evaluate_field_name(context.clone(), name)?;269 if name.is_none() {270 continue;271 }272 let name = name.unwrap();273274 #[derive(Trace)]275 #[trivially_drop]276 struct ObjMemberBinding {277 context_creator: ContextCreator,278 value: LocExpr,279 name: IStr,280 }281 impl Bindable for ObjMemberBinding {282 fn bind(283 &self,284 this: Option<ObjValue>,285 super_obj: Option<ObjValue>,286 ) -> Result<LazyVal> {287 Ok(LazyVal::new_resolved(evaluate_named(288 self.context_creator.create(this, super_obj)?,289 &self.value,290 self.name.clone(),291 )?))292 }293 }294 builder295 .member(name.clone())296 .with_add(*plus)297 .with_visibility(*visibility)298 .with_location(value.1.clone())299 .bindable(Box::new(ObjMemberBinding {300 context_creator: context_creator.clone(),301 value: value.clone(),302 name,303 }));304 }305 Member::Field(FieldMember {306 name,307 params: Some(params),308 value,309 ..310 }) => {311 let name = evaluate_field_name(context.clone(), name)?;312 if name.is_none() {313 continue;314 }315 let name = name.unwrap();316 #[derive(Trace)]317 #[trivially_drop]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(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 #[trivially_drop]353 struct ObjectAssert {354 context_creator: ContextCreator,355 assert: AssertStmt,356 }357 impl ObjectAssertion for ObjectAssert {358 fn run(359 &self,360 this: Option<ObjValue>,361 super_obj: Option<ObjValue>,362 ) -> Result<()> {363 let ctx = self.context_creator.create(this, super_obj)?;364 evaluate_assert(ctx, &self.assert)365 }366 }367 builder.assert(Box::new(ObjectAssert {368 context_creator: context_creator.clone(),369 assert: stmt.clone(),370 }));371 }372 }373 }374 let this = builder.build();375 future_this.fill(this.clone());376 Ok(this)377}378379pub fn evaluate_object(context: Context, object: &ObjBody) -> Result<ObjValue> {380 Ok(match object {381 ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,382 ObjBody::ObjComp(obj) => {383 let future_this = FutureWrapper::new();384 let mut builder = ObjValueBuilder::new();385 evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {386 let new_bindings = FutureWrapper::new();387 let context_creator = ContextCreator(context.clone(), new_bindings.clone());388 let mut bindings: FxHashMap<IStr, LazyBinding> =389 FxHashMap::with_capacity_and_hasher(390 obj.pre_locals.len() + obj.post_locals.len(),391 BuildHasherDefault::default(),392 );393 for (n, b) in obj394 .pre_locals395 .iter()396 .chain(obj.post_locals.iter())397 .map(|b| evaluate_binding(b, context_creator.clone()))398 {399 bindings.insert(n, b);400 }401 new_bindings.fill(bindings.clone());402 let ctx = ctx.extend_unbound(bindings, None, None, None)?;403 let key = evaluate(ctx.clone(), &obj.key)?;404405 match key {406 Val::Null => {}407 Val::Str(n) => {408 #[derive(Trace)]409 #[trivially_drop]410 struct ObjCompBinding {411 context: Context,412 value: LocExpr,413 }414 impl Bindable for ObjCompBinding {415 fn bind(416 &self,417 this: Option<ObjValue>,418 _super_obj: Option<ObjValue>,419 ) -> Result<LazyVal> {420 Ok(LazyVal::new_resolved(evaluate(421 self.context.clone().extend(422 FxHashMap::default(),423 None,424 this,425 None,426 ),427 &self.value,428 )?))429 }430 }431 builder432 .member(n)433 .with_location(obj.value.1.clone())434 .bindable(Box::new(ObjCompBinding {435 context: ctx,436 value: obj.value.clone(),437 }));438 }439 v => throw!(FieldMustBeStringGot(v.value_type())),440 }441442 Ok(())443 })?;444445 let this = builder.build();446 future_this.fill(this.clone());447 this448 }449 })450}451452pub fn evaluate_apply(453 context: Context,454 value: &LocExpr,455 args: &ArgsDesc,456 loc: Option<&ExprLocation>,457 tailstrict: bool,458) -> Result<Val> {459 let value = evaluate(context.clone(), value)?;460 Ok(match value {461 Val::Func(f) => {462 let body = || f.evaluate(context, loc, args, tailstrict);463 if tailstrict {464 body()?465 } else {466 push(loc, || format!("function <{}> call", f.name()), body)?467 }468 }469 v => throw!(OnlyFunctionsCanBeCalledGot(v.value_type())),470 })471}472473pub fn evaluate_assert(context: Context, assertion: &AssertStmt) -> Result<()> {474 let value = &assertion.0;475 let msg = &assertion.1;476 let assertion_result = push(477 value.1.as_ref(),478 || "assertion condition".to_owned(),479 || {480 evaluate(context.clone(), value)?481 .try_cast_bool("assertion condition should be of type `boolean`")482 },483 )?;484 if !assertion_result {485 push(486 value.1.as_ref(),487 || "assertion failure".to_owned(),488 || {489 if let Some(msg) = msg {490 throw!(AssertionFailed(evaluate(context, msg)?.to_string()?));491 } else {492 throw!(AssertionFailed(Val::Null.to_string()?));493 }494 },495 )?496 }497 Ok(())498}499500pub fn evaluate_named(context: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {501 use Expr::*;502 let LocExpr(expr, _loc) = lexpr;503 Ok(match &**expr {504 Function(params, body) => evaluate_method(context, name, params.clone(), body.clone()),505 _ => evaluate(context, lexpr)?,506 })507}508509pub fn evaluate(context: Context, expr: &LocExpr) -> Result<Val> {510 use Expr::*;511 let LocExpr(expr, loc) = expr;512 Ok(match &**expr {513 Literal(LiteralType::This) => {514 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)515 }516 Literal(LiteralType::Super) => Val::Obj(517 context518 .super_obj()519 .clone()520 .ok_or(NoSuperFound)?521 .with_this(context.this().clone().unwrap()),522 ),523 Literal(LiteralType::Dollar) => {524 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)525 }526 Literal(LiteralType::True) => Val::Bool(true),527 Literal(LiteralType::False) => Val::Bool(false),528 Literal(LiteralType::Null) => Val::Null,529 Parened(e) => evaluate(context, e)?,530 Str(v) => Val::Str(v.clone()),531 Num(v) => Val::new_checked_num(*v)?,532 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, v1, *o, v2)?,533 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,534 Var(name) => push(535 loc.as_ref(),536 || format!("variable <{}>", name),537 || context.binding(name.clone())?.evaluate(),538 )?,539 Index(value, index) => {540 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {541 (Val::Obj(v), Val::Str(s)) => {542 let sn = s.clone();543 push(544 loc.as_ref(),545 || format!("field <{}> access", sn),546 || {547 if let Some(v) = v.get(s.clone())? {548 Ok(v)549 } else {550 throw!(NoSuchField(s))551 }552 },553 )?554 }555 (Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(556 ValType::Obj,557 ValType::Str,558 n.value_type(),559 )),560561 (Val::Arr(v), Val::Num(n)) => {562 if n.fract() > f64::EPSILON {563 throw!(FractionalIndex)564 }565 v.get(n as usize)?566 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?567 }568 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),569 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(570 ValType::Arr,571 ValType::Num,572 n.value_type(),573 )),574575 (Val::Str(s), Val::Num(n)) => Val::Str(576 s.chars()577 .skip(n as usize)578 .take(1)579 .collect::<String>()580 .into(),581 ),582 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(583 ValType::Str,584 ValType::Num,585 n.value_type(),586 )),587588 (v, _) => throw!(CantIndexInto(v.value_type())),589 }590 }591 LocalExpr(bindings, returned) => {592 let mut new_bindings: FxHashMap<IStr, LazyVal> = HashMap::with_capacity_and_hasher(593 bindings.len(),594 BuildHasherDefault::<FxHasher>::default(),595 );596 let future_context = Context::new_future();597 for b in bindings {598 new_bindings.insert(599 b.name.clone(),600 evaluate_binding_in_future(b, future_context.clone()),601 );602 }603 let context = context604 .extend_bound(new_bindings)605 .into_future(future_context);606 evaluate(context, &returned.clone())?607 }608 Arr(items) => {609 let mut out = Vec::with_capacity(items.len());610 for item in items {611 // TODO: Implement ArrValue::Lazy with same context for every element?612 #[derive(Trace)]613 #[trivially_drop]614 struct ArrayElement {615 context: Context,616 item: LocExpr,617 }618 impl LazyValValue for ArrayElement {619 fn get(self: Box<Self>) -> Result<Val> {620 evaluate(self.context, &self.item)621 }622 }623 out.push(LazyVal::new(Box::new(ArrayElement {624 context: context.clone(),625 item: item.clone(),626 })));627 }628 Val::Arr(out.into())629 }630 ArrComp(expr, comp_specs) => {631 let mut out = Vec::new();632 evaluate_comp(context, comp_specs, &mut |ctx| {633 out.push(evaluate(ctx, expr)?);634 Ok(())635 })?;636 Val::Arr(ArrValue::Eager(Gc::new(out)))637 }638 Obj(body) => Val::Obj(evaluate_object(context, body)?),639 ObjExtend(s, t) => evaluate_add_op(640 &evaluate(context.clone(), s)?,641 &Val::Obj(evaluate_object(context, t)?),642 )?,643 Apply(value, args, tailstrict) => {644 evaluate_apply(context, value, args, loc.as_ref(), *tailstrict)?645 }646 Function(params, body) => {647 evaluate_method(context, "anonymous".into(), params.clone(), body.clone())648 }649 Intrinsic(name) => Val::Func(Gc::new(FuncVal::Intrinsic(name.clone()))),650 AssertExpr(assert, returned) => {651 evaluate_assert(context.clone(), assert)?;652 evaluate(context, returned)?653 }654 ErrorStmt(e) => push(655 loc.as_ref(),656 || "error statement".to_owned(),657 || {658 throw!(RuntimeError(659 evaluate(context, e)?.try_cast_str("error text should be of type `string`")?,660 ))661 },662 )?,663 IfElse {664 cond,665 cond_then,666 cond_else,667 } => {668 if push(669 loc.as_ref(),670 || "if condition".to_owned(),671 || evaluate(context.clone(), &cond.0)?.try_cast_bool("in if condition"),672 )? {673 evaluate(context, cond_then)?674 } else {675 match cond_else {676 Some(v) => evaluate(context, v)?,677 None => Val::Null,678 }679 }680 }681 Slice(value, desc) => {682 let indexable = evaluate(context.clone(), value)?;683684 fn parse_num(685 context: &Context,686 expr: Option<&LocExpr>,687 desc: &'static str,688 ) -> Result<Option<usize>> {689 Ok(match expr {690 Some(s) => evaluate(context.clone(), s)?691 .try_cast_nullable_num(desc)?692 .map(|v| v as usize),693 None => None,694 })695 }696697 let start = parse_num(&context, desc.start.as_ref(), "start")?;698 let end = parse_num(&context, desc.end.as_ref(), "end")?;699 let step = parse_num(&context, desc.step.as_ref(), "step")?;700701 std_slice(indexable.into_indexable()?, start, end, step)?702 }703 Import(path) => {704 let tmp = loc705 .clone()706 .expect("imports cannot be used without loc_data")707 .0;708 let mut import_location = tmp.to_path_buf();709 import_location.pop();710 push(711 loc.as_ref(),712 || format!("import {:?}", path),713 || with_state(|s| s.import_file(&import_location, path)),714 )?715 }716 ImportStr(path) => {717 let tmp = loc718 .clone()719 .expect("imports cannot be used without loc_data")720 .0;721 let mut import_location = tmp.to_path_buf();722 import_location.pop();723 Val::Str(with_state(|s| s.import_file_str(&import_location, path))?)724 }725 })726}crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -1,4 +1,7 @@
-use crate::{error::Error::*, evaluate, throw, Context, LazyVal, LazyValValue, Result, Val};
+use crate::{
+ error::Error::*, evaluate, evaluate_named, throw, Context, FutureWrapper, LazyVal,
+ LazyValValue, Result, Val,
+};
use jrsonnet_gc::Trace;
use jrsonnet_interner::IStr;
use jrsonnet_parser::{ArgsDesc, LocExpr, ParamsDesc};
@@ -8,6 +11,18 @@
const NO_DEFAULT_CONTEXT: &str =
"no default context set for call with defined default parameter value";
+#[derive(Trace)]
+#[trivially_drop]
+struct EvaluateLazyVal {
+ context: Context,
+ expr: LocExpr,
+}
+impl LazyValValue for EvaluateLazyVal {
+ fn get(self: Box<Self>) -> Result<Val> {
+ evaluate(self.context, &self.expr)
+ }
+}
+
/// Creates correct [context](Context) for function body evaluation returning error on invalid call.
///
/// ## Parameters
@@ -18,64 +33,119 @@
/// * `tailstrict`: if set to `true` function arguments are eagerly executed, otherwise - lazily
pub fn parse_function_call(
ctx: Context,
- body_ctx: Option<Context>,
+ body_ctx: Context,
params: &ParamsDesc,
args: &ArgsDesc,
tailstrict: bool,
) -> Result<Context> {
- let mut out = HashMap::with_capacity_and_hasher(params.len(), BuildHasherDefault::default());
- let mut positioned_args = vec![None; params.0.len()];
- for (id, arg) in args.iter().enumerate() {
- let idx = if let Some(name) = &arg.0 {
- params
- .iter()
- .position(|p| *p.0 == *name)
- .ok_or_else(|| UnknownFunctionParameter(name.clone()))?
- } else {
- id
- };
+ let mut passed_args =
+ HashMap::with_capacity_and_hasher(params.len(), BuildHasherDefault::default());
+ if args.unnamed.len() > params.len() {
+ throw!(TooManyArgsFunctionHas(params.len()))
+ }
+
+ let mut filled_args = 0;
- if idx >= params.len() {
- throw!(TooManyArgsFunctionHas(params.len()));
+ for (id, arg) in args.unnamed.iter().enumerate() {
+ let name = params[id].0.clone();
+ passed_args.insert(
+ name,
+ if tailstrict {
+ LazyVal::new_resolved(evaluate(ctx.clone(), arg)?)
+ } else {
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: arg.clone(),
+ }))
+ },
+ );
+ filled_args += 1;
+ }
+
+ for (name, value) in args.named.iter() {
+ // FIXME: O(n) for arg existence check
+ if !params.iter().any(|p| &p.0 == name) {
+ throw!(UnknownFunctionParameter((name as &str).to_owned()));
}
- if positioned_args[idx].is_some() {
- throw!(BindingParameterASecondTime(params[idx].0.clone()));
+ if passed_args
+ .insert(
+ name.clone(),
+ if tailstrict {
+ LazyVal::new_resolved(evaluate(ctx.clone(), value)?)
+ } else {
+ LazyVal::new(Box::new(EvaluateLazyVal {
+ context: ctx.clone(),
+ expr: value.clone(),
+ }))
+ },
+ )
+ .is_some()
+ {
+ throw!(BindingParameterASecondTime(name.clone()));
}
- positioned_args[idx] = Some(arg.1.clone());
+ filled_args += 1;
}
- // Fill defaults
- for (id, p) in params.iter().enumerate() {
- let (ctx, expr) = if let Some(arg) = &positioned_args[id] {
- (ctx.clone(), arg)
- } else if let Some(default) = &p.1 {
- (body_ctx.clone().expect(NO_DEFAULT_CONTEXT), default)
- } else {
- throw!(FunctionParameterNotBoundInCall(p.0.clone()));
- };
- let val = if tailstrict {
- LazyVal::new_resolved(evaluate(ctx, expr)?)
- } else {
+
+ if filled_args < params.len() {
+ // Some args are unset, but maybe we have defaults for them
+ // Default values should be created in newly created context
+ let future_context = FutureWrapper::<Context>::new();
+ let mut defaults = HashMap::with_capacity_and_hasher(
+ params.len() - filled_args,
+ BuildHasherDefault::default(),
+ );
+
+ for param in params.iter().filter(|p| p.1.is_some()) {
+ if passed_args.contains_key(¶m.0.clone()) {
+ continue;
+ }
#[derive(Trace)]
#[trivially_drop]
- struct EvaluateLazyVal {
- context: Context,
- expr: LocExpr,
+ struct LazyNamedBinding {
+ future_context: FutureWrapper<Context>,
+ name: IStr,
+ value: LocExpr,
}
- impl LazyValValue for EvaluateLazyVal {
+ impl LazyValValue for LazyNamedBinding {
fn get(self: Box<Self>) -> Result<Val> {
- evaluate(self.context, &self.expr)
+ evaluate_named(self.future_context.unwrap(), &self.value, self.name)
}
}
+ LazyVal::new(Box::new(LazyNamedBinding {
+ future_context: future_context.clone(),
+ name: param.0.clone(),
+ value: param.1.clone().unwrap(),
+ }));
- LazyVal::new(Box::new(EvaluateLazyVal {
- context: ctx.clone(),
- expr: expr.clone(),
- }))
- };
- out.insert(p.0.clone(), val);
- }
+ defaults.insert(
+ param.0.clone(),
+ LazyVal::new(Box::new(LazyNamedBinding {
+ future_context: future_context.clone(),
+ name: param.0.clone(),
+ value: param.1.clone().unwrap(),
+ })),
+ );
+ filled_args += 1;
+ }
+
+ // Some args still wasn't filled
+ if filled_args != params.len() {
+ for param in params.iter().skip(args.unnamed.len()) {
+ if !args.named.iter().any(|a| a.0 == param.0) {
+ throw!(FunctionParameterNotBoundInCall(param.0.clone()));
+ }
+ }
+ unreachable!();
+ }
- Ok(body_ctx.unwrap_or(ctx).extend(out, None, None, None))
+ Ok(body_ctx
+ .extend(passed_args, None, None, None)
+ .extend_bound(defaults)
+ .into_future(future_context))
+ } else {
+ let body_ctx = body_ctx.extend(passed_args, None, None, None);
+ Ok(body_ctx)
+ }
}
pub fn parse_function_call_map(
@@ -176,21 +246,25 @@
use $crate::{error::Error::*, throw, evaluate, push_stack_frame, typed::CheckType};
let args = $args;
- if args.len() > $total_args {
+ if args.unnamed.len() + args.named.len() > $total_args {
throw!(TooManyArgsFunctionHas($total_args));
}
$(
- if args.len() <= $id {
+ if args.unnamed.len() + args.named.len() <= $id {
throw!(FunctionParameterNotBoundInCall(stringify!($name).into()));
}
- let $name = &args[$id];
- if $name.0.is_some() {
- if $name.0.as_ref().unwrap() != stringify!($name) {
+ // Is named
+ let $name = if $id >= $args.unnamed.len() {
+ let named = &args.named[$id - $args.unnamed.len()];
+ if &named.0 != stringify!($name) {
throw!(IntrinsicArgumentReorderingIsNotSupportedYet);
}
- }
+ &named.1
+ } else {
+ &$args.unnamed[$id]
+ };
let $name = push_stack_frame(None, || format!("evaluating argument"), || {
- let value = evaluate($ctx.clone(), &$name.1)?;
+ let value = evaluate($ctx.clone(), &$name)?;
$ty.check(&value)?;
Ok(value)
})?;
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -29,6 +29,16 @@
.get(key)
.or_else(|| self.0.parent.as_ref().and_then(|p| p.get(key)))
}
+
+ pub fn contains_key(&self, key: &IStr) -> bool {
+ (self.0).current.contains_key(key)
+ || self
+ .0
+ .parent
+ .as_ref()
+ .map(|p| p.contains_key(key))
+ .unwrap_or(false)
+ }
}
impl Clone for LayeredHashMap {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -11,7 +11,7 @@
};
use jrsonnet_gc::{Gc, GcCell, Trace};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{el, Arg, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
+use jrsonnet_parser::{el, ArgsDesc, Expr, ExprLocation, LiteralType, LocExpr, ParamsDesc};
use jrsonnet_types::ValType;
use std::{collections::HashMap, fmt::Debug, rc::Rc};
@@ -127,7 +127,7 @@
Self::Normal(func) => {
let ctx = parse_function_call(
call_ctx,
- Some(func.ctx.clone()),
+ func.ctx.clone(),
&func.params,
args,
tailstrict,
@@ -136,7 +136,8 @@
}
Self::Intrinsic(name) => call_builtin(call_ctx, loc, name, args),
Self::NativeExt(_name, handler) => {
- let args = parse_function_call(call_ctx, None, &handler.params, args, true)?;
+ let args =
+ parse_function_call(call_ctx, Context::new(), &handler.params, args, true)?;
let mut out_args = Vec::with_capacity(handler.params.len());
for p in handler.params.0.iter() {
out_args.push(args.binding(p.0.clone())?.evaluate()?);
@@ -554,17 +555,17 @@
el!(Expr::Var("std".into())),
el!(Expr::Str("manifestYamlDoc".into()))
)),
- ArgsDesc(vec![
- Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),
- Arg(
- None,
+ ArgsDesc::new(
+ vec![
+ el!(Expr::Var("__tmp__to_json__".into())),
el!(Expr::Literal(if padding != 0 {
LiteralType::True
} else {
LiteralType::False
- }))
- )
- ]),
+ })),
+ ],
+ vec![]
+ ),
false
)),
)?
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -194,18 +194,13 @@
#[cfg_attr(feature = "deserialize", derive(Deserialize))]
#[derive(Debug, PartialEq, Trace)]
#[trivially_drop]
-pub struct Arg(pub Option<String>, pub LocExpr);
-
-#[cfg_attr(feature = "serialize", derive(Serialize))]
-#[cfg_attr(feature = "deserialize", derive(Deserialize))]
-#[derive(Debug, PartialEq, Trace)]
-#[trivially_drop]
-pub struct ArgsDesc(pub Vec<Arg>);
-
-impl Deref for ArgsDesc {
- type Target = Vec<Arg>;
- fn deref(&self) -> &Self::Target {
- &self.0
+pub struct ArgsDesc {
+ pub unnamed: Vec<LocExpr>,
+ pub named: Vec<(IStr, LocExpr)>,
+}
+impl ArgsDesc {
+ pub fn new(unnamed: Vec<LocExpr>, named: Vec<(IStr, LocExpr)>) -> Self {
+ Self { unnamed, named }
}
}
@@ -247,6 +242,7 @@
pub struct ObjComp {
pub pre_locals: Vec<BindSpec>,
pub key: LocExpr,
+ pub plus: bool,
pub value: LocExpr,
pub post_locals: Vec<BindSpec>,
pub compspecs: Vec<CompSpec>,
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -7,6 +7,7 @@
};
mod expr;
pub use expr::*;
+pub use jrsonnet_interner::IStr;
pub use peg;
pub struct ParserSettings {
@@ -70,19 +71,29 @@
}
/ { expr::ParamsDesc(Rc::new(Vec::new())) }
- pub rule arg(s: &ParserSettings) -> expr::Arg
- = name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}
- / expr:expr(s) {expr::Arg(None, expr)}
+ pub rule arg(s: &ParserSettings) -> (Option<IStr>, LocExpr)
+ = quiet! { name:(s:$(id()) _ "=" _ {s})? expr:expr(s) {(name.map(Into::into), expr)} }
+ / expected!("<argument>")
+
pub rule args(s: &ParserSettings) -> expr::ArgsDesc
- = args:arg(s) ** comma() comma()? {
+ = args:arg(s)**comma() comma()? {?
+ let unnamed_count = args.iter().take_while(|(n, _)| n.is_none()).count();
+ let mut unnamed = Vec::with_capacity(unnamed_count);
+ let mut named = Vec::with_capacity(args.len() - unnamed_count);
let mut named_started = false;
- for arg in &args {
- named_started = named_started || arg.0.is_some();
- assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");
+ for (name, value) in args {
+ if let Some(name) = name {
+ named_started = true;
+ named.push((name, value));
+ } else {
+ if named_started {
+ return Err("<named argument>")
+ }
+ unnamed.push(value);
+ }
}
- expr::ArgsDesc(args)
+ Ok(expr::ArgsDesc::new(unnamed, named))
}
- / { expr::ArgsDesc(Vec::new()) }
pub rule bind(s: &ParserSettings) -> expr::BindSpec
= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}
@@ -136,12 +147,13 @@
/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}
/ field:field(s) {expr::Member::Field(field)}
pub rule objinside(s: &ParserSettings) -> expr::ObjBody
- = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {
+ = pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ plus:"+"? _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {
let mut compspecs = vec![CompSpec::ForSpec(forspec)];
compspecs.extend(others.unwrap_or_default());
expr::ObjBody::ObjComp(expr::ObjComp{
pre_locals,
key,
+ plus: plus.is_some(),
value,
post_locals,
compspecs,
@@ -492,7 +504,7 @@
el!(ArrComp(
el!(Apply(
el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),
- ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),
+ ArgsDesc::new(vec![el!(Var("x".into()))], vec![]),
false,
)),
vec![CompSpec::ForSpec(ForSpecData(