git.delta.rocks / jrsonnet / refs/commits / 41ef1e2d3380

difftreelog

fix(evaluator) do not panic in bad operations

Лач2020-06-27parent: #0ba2642.patch.diff
in: master

2 files changed

modifiedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth
before · crates/jsonnet-evaluator/src/error.rs
1use crate::ValType;2use jsonnet_parser::ExprLocation;3use std::{path::PathBuf, rc::Rc};45#[derive(Debug, Clone)]6pub enum Error {7	IntristicNotFound(Rc<str>, Rc<str>),8	IntristicArgumentReorderingIsNotSupportedYet,910	VariableIsNotDefined(String),11	TypeMismatch(&'static str, Vec<ValType>, ValType),12	NoSuchField(Rc<str>),1314	UnknownVariable(Rc<str>),1516	OnlyFunctionsCanBeCalledGot(ValType),17	UnknownFunctionParameter(String),18	BindingParameterASecondTime(Rc<str>),19	TooManyArgsFunctionHas(usize),20	FunctionParameterNotBoundInCall(Rc<str>),2122	UndefinedExternalVariable(Rc<str>),2324	FieldMustBeStringGot(ValType),2526	AttemptedIndexAnArrayWithString(Rc<str>),27	ValueIndexMustBeTypeGot(ValType, ValType, ValType),28	CantIndexInto(ValType),2930	StandaloneSuper,3132	ImportFileNotFound(PathBuf, PathBuf),33	ResolvedFileNotFound(PathBuf),34	ImportBadFileUtf8(PathBuf),35	ImportNotSupported(PathBuf, PathBuf),36	ImportSyntaxError(jsonnet_parser::ParseError),3738	RuntimeError(Rc<str>),39	StackOverflow,40	FractionalIndex,41	DivisionByZero,42}4344#[derive(Clone, Debug)]45pub struct StackTraceElement(pub ExprLocation, pub String);46#[derive(Debug, Clone)]47pub struct StackTrace(pub Vec<StackTraceElement>);4849#[derive(Debug, Clone)]50pub struct LocError(pub Error, pub StackTrace);51pub type Result<V> = std::result::Result<V, LocError>;
after · crates/jsonnet-evaluator/src/error.rs
1use crate::ValType;2use jsonnet_parser::{BinaryOpType, ExprLocation, UnaryOpType};3use std::{path::PathBuf, rc::Rc};45#[derive(Debug, Clone)]6pub enum Error {7	IntristicNotFound(Rc<str>, Rc<str>),8	IntristicArgumentReorderingIsNotSupportedYet,910	UnaryOperatorDoesNotOperateOnType(UnaryOpType, ValType),11	BinaryOperatorDoesNotOperateOnValues(BinaryOpType, ValType, ValType),1213	VariableIsNotDefined(String),14	TypeMismatch(&'static str, Vec<ValType>, ValType),15	NoSuchField(Rc<str>),1617	UnknownVariable(Rc<str>),1819	OnlyFunctionsCanBeCalledGot(ValType),20	UnknownFunctionParameter(String),21	BindingParameterASecondTime(Rc<str>),22	TooManyArgsFunctionHas(usize),23	FunctionParameterNotBoundInCall(Rc<str>),2425	UndefinedExternalVariable(Rc<str>),2627	FieldMustBeStringGot(ValType),2829	AttemptedIndexAnArrayWithString(Rc<str>),30	ValueIndexMustBeTypeGot(ValType, ValType, ValType),31	CantIndexInto(ValType),3233	StandaloneSuper,3435	ImportFileNotFound(PathBuf, PathBuf),36	ResolvedFileNotFound(PathBuf),37	ImportBadFileUtf8(PathBuf),38	ImportNotSupported(PathBuf, PathBuf),39	ImportSyntaxError(jsonnet_parser::ParseError),4041	RuntimeError(Rc<str>),42	StackOverflow,43	FractionalIndex,44	DivisionByZero,45}4647#[derive(Clone, Debug)]48pub struct StackTraceElement(pub ExprLocation, pub String);49#[derive(Debug, Clone)]50pub struct StackTrace(pub Vec<StackTraceElement>);5152#[derive(Debug, Clone)]53pub struct LocError(pub Error, pub StackTrace);54pub type Result<V> = std::result::Result<V, LocError>;
modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -74,7 +74,10 @@
 		(UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),
 		(UnaryOpType::Minus, Val::Num(n)) => Val::Num(-*n),
 		(UnaryOpType::BitNot, Val::Num(n)) => Val::Num(!(*n as i32) as f64),
-		(op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),
+		(op, o) => create_error(Error::UnaryOperatorDoesNotOperateOnType(
+			op,
+			o.value_type()?,
+		))?,
 	})
 }
 
@@ -92,7 +95,11 @@
 		(Val::Obj(v1), Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),
 		(Val::Arr(a), Val::Arr(b)) => Val::Arr(Rc::new([&a[..], &b[..]].concat())),
 		(Val::Num(v1), Val::Num(v2)) => Val::Num(v1 + v2),
-		_ => panic!("can't add: {:?} and {:?}", a, b),
+		_ => create_error(Error::BinaryOperatorDoesNotOperateOnValues(
+			BinaryOpType::Add,
+			a.value_type()?,
+			b.value_type()?,
+		))?,
 	})
 }
 
@@ -161,7 +168,11 @@
 			Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)
 		}
 
-		_ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),
+		_ => create_error(Error::BinaryOperatorDoesNotOperateOnValues(
+			op,
+			a.value_type()?,
+			b.value_type()?,
+		))?,
 	})
 }