git.delta.rocks / jrsonnet / refs/commits / e53de5dd7f69

difftreelog

fix make jsonnet tests partially pass

Лач2020-06-04parent: #774a622.patch.diff
in: master

3 files changed

modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
54pub fn evaluate_field_name(54pub fn evaluate_field_name(
55 context: Context,55 context: Context,
56 field_name: &jsonnet_parser::FieldName,56 field_name: &jsonnet_parser::FieldName,
57) -> Result<String> {57) -> Result<Option<String>> {
58 Ok(match field_name {58 Ok(match field_name {
59 jsonnet_parser::FieldName::Fixed(n) => n.clone(),59 jsonnet_parser::FieldName::Fixed(n) => Some(n.clone()),
60 jsonnet_parser::FieldName::Dyn(expr) => {60 jsonnet_parser::FieldName::Dyn(expr) => {
61 evaluate(context, expr)?.try_cast_str("dynamic field name")?61 let value = evaluate(context, expr)?.unwrap_if_lazy()?;
62 if matches!(value, Val::Null) {
63 None
64 } else {
65 Some(value.try_cast_str("dynamic field name")?)
66 }
62 }67 }
63 })68 })
64}69}
234 value,239 value,
235 }) => {240 }) => {
236 let name = evaluate_field_name(context.clone(), &name)?;241 let name = evaluate_field_name(context.clone(), &name)?;
242 if name.is_none() {
243 continue;
244 }
245 let name = name.unwrap();
237 new_members.insert(246 new_members.insert(
238 name.clone(),247 name.clone(),
239 ObjMember {248 ObjMember {
260 ..269 ..
261 }) => {270 }) => {
262 let name = evaluate_field_name(context.clone(), &name)?;271 let name = evaluate_field_name(context.clone(), &name)?;
272 if name.is_none() {
273 continue;
274 }
275 let name = name.unwrap();
263 new_members.insert(276 new_members.insert(
264 name,277 name,
265 ObjMember {278 ObjMember {
299 .clone()312 .clone()
300 .unwrap_or_else(|| panic!("this not found")),313 .unwrap_or_else(|| panic!("this not found")),
301 ),314 ),
302 Literal(LiteralType::Super) => Val::Obj(
303 context
304 .super_obj()
305 .clone()
306 .unwrap_or_else(|| panic!("super not found")),
307 ),
308 Literal(LiteralType::Dollar) => Val::Obj(315 Literal(LiteralType::Dollar) => Val::Obj(
309 context316 context
310 .dollar()317 .dollar()
320 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,327 BinaryOp(v1, o, v2) => evaluate_binary_op_special(context, &v1, *o, &v2)?,
321 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,328 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)?)?,
322 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy()?,329 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy()?,
330 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {
331 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;
332 context
333 .super_obj()
334 .clone()
335 .expect("no super found")
336 .get_raw(&name, &context.this().clone().expect("no this found"))?
337 .expect("value not found")
338 }
323 Index(value, index) => {339 Index(value, index) => {
324 match (340 match (
325 evaluate(context.clone(), value)?.unwrap_if_lazy()?,341 evaluate(context.clone(), value)?.unwrap_if_lazy()?,
381 evaluate_comp(context, expr, compspecs)?.unwrap(),397 evaluate_comp(context, expr, compspecs)?.unwrap(),
382 ),398 ),
383 Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),399 Obj(body) => Val::Obj(evaluate_object(context, body.clone())?),
400 ObjExtend(s, t) => evaluate_add_op(
401 &evaluate(context.clone(), s)?,
402 &Val::Obj(evaluate_object(context, t.clone())?),
403 )?,
384 Apply(value, ArgsDesc(args)) => {404 Apply(value, ArgsDesc(args)) => {
385 let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;405 let value = evaluate(context.clone(), value)?.unwrap_if_lazy()?;
386 match value {406 match value {
408 evaluate(context.clone(), &args[0].1)?,428 evaluate(context.clone(), &args[0].1)?,
409 evaluate(context, &args[1].1)?,429 evaluate(context, &args[1].1)?,
410 ) {430 ) {
411 assert!(v > 0.0);431 assert!(v >= 0.0);
412 let mut out = Vec::with_capacity(v as usize);432 let mut out = Vec::with_capacity(v as usize);
413 for i in 0..v as usize {433 for i in 0..v as usize {
414 out.push(d.evaluate(vec![(None, Val::Num(i as f64))])?)434 out.push(d.evaluate(vec![(None, Val::Num(i as f64))])?)
452 );472 );
453 Val::Bool(a == b)473 Val::Bool(a == b)
454 }474 }
475 ("std", "modulo") => {
476 assert_eq!(args.len(), 2);
477 if let (Val::Num(a), Val::Num(b)) = (
478 evaluate(context.clone(), &args[0].1)?,
479 evaluate(context, &args[1].1)?,
480 ) {
481 Val::Num(a % b)
482 } else {
483 panic!("bad modulo call");
484 }
485 }
486 ("std", "floor") => {
487 assert_eq!(args.len(), 1);
488 if let Val::Num(a) = evaluate(context, &args[0].1)? {
489 Val::Num(a.floor())
490 } else {
491 panic!("bad floor call");
492 }
493 }
455 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),494 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),
456 },495 },
457 Val::Func(f) => push(locexpr, "function call".to_owned(), || {496 Val::Func(f) => push(locexpr, "function call".to_owned(), || {
516 Some(v) => push(v.clone(), "if condition 'else' branch".to_owned(), || {555 Some(v) => push(v.clone(), "if condition 'else' branch".to_owned(), || {
517 evaluate(context, v)556 evaluate(context, v)
518 })?,557 })?,
519 None => Val::Bool(false),558 None => Val::Null,
520 }559 }
521 }560 }
522 }561 }
modifiedcrates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/obj.rs
+++ b/crates/jsonnet-evaluator/src/obj.rs
@@ -83,7 +83,7 @@
 			Ok(None)
 		}
 	}
-	fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {
+	pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {
 		match (self.0.this_entries.get(key), &self.0.super_obj) {
 			(Some(k), None) => Ok(Some(k.invoke.0(
 				Some(real_this.clone()),
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -3,7 +3,7 @@
 	ObjValue, Result,
 };
 use closure::closure;
-use jsonnet_parser::ParamsDesc;
+use jsonnet_parser::{Param, ParamsDesc};
 use std::{
 	cell::RefCell,
 	collections::HashMap,
@@ -70,19 +70,15 @@
 		let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();
 		let future_ctx = Context::new_future();
 
-		// self.params
-		// 	.with_defaults()
-		// 	.into_iter()
-		// 	.for_each(|Param(name, default)| {
-		// 		let default = Rc::new(*default.unwrap());
-		// 		new_bindings.insert(
-		// 			name,
-		// 			binding!(move |_, _| Val::Lazy(lazy_val!(|| self
-		// 				.eval_default
-		// 				.0
-		// 				.default(future_ctx.unwrap(), *default.clone())))),
-		// 		);
-		// 	});
+		for Param(name, default) in self.params.with_defaults() {
+			let default = default.unwrap();
+			let eval_default = self.eval_default.clone();
+			new_bindings.insert(
+				name,
+				lazy_binding!(closure!(clone future_ctx, clone default, clone eval_default, |_, _| Ok(lazy_val!(closure!(clone future_ctx, clone eval_default, clone default, || (eval_default.clone()).0
+					(future_ctx.clone().unwrap(), default.clone())))))),
+			);
+		}
 		for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {
 			new_bindings.insert(
 				name.as_ref().unwrap().clone(),