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
83 Ok(None)83 Ok(None)
84 }84 }
85 }85 }
86 fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {86 pub(crate) fn get_raw(&self, key: &str, real_this: &ObjValue) -> Result<Option<Val>> {
87 match (self.0.this_entries.get(key), &self.0.super_obj) {87 match (self.0.this_entries.get(key), &self.0.super_obj) {
88 (Some(k), None) => Ok(Some(k.invoke.0(88 (Some(k), None) => Ok(Some(k.invoke.0(
89 Some(real_this.clone()),89 Some(real_this.clone()),
modifiedcrates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth
3 ObjValue, Result,3 ObjValue, Result,
4};4};
5use closure::closure;5use closure::closure;
6use jsonnet_parser::ParamsDesc;6use jsonnet_parser::{Param, ParamsDesc};
7use std::{7use std::{
8 cell::RefCell,8 cell::RefCell,
9 collections::HashMap,9 collections::HashMap,
70 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();70 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();
71 let future_ctx = Context::new_future();71 let future_ctx = Context::new_future();
7272
73 // self.params73 for Param(name, default) in self.params.with_defaults() {
74 // .with_defaults()74 let default = default.unwrap();
75 // .into_iter()75 let eval_default = self.eval_default.clone();
76 // .for_each(|Param(name, default)| {76 new_bindings.insert(
77 // let default = Rc::new(*default.unwrap());77 name,
78 // new_bindings.insert(78 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
79 // name,79 (future_ctx.clone().unwrap(), default.clone())))))),
80 // binding!(move |_, _| Val::Lazy(lazy_val!(|| self80 );
81 // .eval_default81 }
82 // .0
83 // .default(future_ctx.unwrap(), *default.clone())))),
84 // );
85 // });
86 for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {82 for (name, val) in args.clone().into_iter().filter(|e| e.0.is_some()) {
87 new_bindings.insert(83 new_bindings.insert(
88 name.as_ref().unwrap().clone(),84 name.as_ref().unwrap().clone(),