|
1 | 1 | /****************************************************************************************************** |
2 | 2 | Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator) |
3 | | - Version : 1.4.29.0 |
| 3 | + Version : 1.4.30.0 |
4 | 4 | (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable) |
5 | 5 |
|
6 | 6 | Author : Coding Seb |
@@ -336,6 +336,7 @@ protected enum TryBlockEvaluatedState |
336 | 336 | { "null", null}, |
337 | 337 | { "true", true }, |
338 | 338 | { "false", false }, |
| 339 | + { "this", null } |
339 | 340 | }; |
340 | 341 |
|
341 | 342 | protected IDictionary<string, Func<double, double>> simpleDoubleMathFuncsDictionary = new Dictionary<string, Func<double, double>>(StringComparer.Ordinal) |
@@ -872,10 +873,20 @@ protected virtual BindingFlags StaticBindingFlag |
872 | 873 |
|
873 | 874 | #region Custom and on the fly evaluation |
874 | 875 |
|
| 876 | + private object context; |
| 877 | + |
875 | 878 | /// <summary> |
876 | 879 | /// If set, this object is used to use it's fields, properties and methods as global variables and functions |
877 | 880 | /// </summary> |
878 | | - public object Context { get; set; } |
| 881 | + public object Context |
| 882 | + { |
| 883 | + get { return context; } |
| 884 | + set |
| 885 | + { |
| 886 | + context = value; |
| 887 | + defaultVariables["this"] = context; |
| 888 | + } |
| 889 | + } |
879 | 890 |
|
880 | 891 | private IDictionary<string, object> variables = new Dictionary<string, object>(StringComparer.Ordinal); |
881 | 892 |
|
@@ -2092,6 +2103,15 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi |
2092 | 2103 | { |
2093 | 2104 | throw; |
2094 | 2105 | } |
| 2106 | + catch (NullReferenceException nullException) |
| 2107 | + { |
| 2108 | + stack.Push(new BubbleExceptionContainer() |
| 2109 | + { |
| 2110 | + Exception = nullException |
| 2111 | + }); |
| 2112 | + |
| 2113 | + return true; |
| 2114 | + } |
2095 | 2115 | catch (Exception ex) |
2096 | 2116 | { |
2097 | 2117 | //Transport the exception in stack. |
@@ -2787,7 +2807,7 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack, |
2787 | 2807 |
|
2788 | 2808 | if(type.IsArray && OptionForceIntegerNumbersEvaluationsAsDoubleByDefault) |
2789 | 2809 | { |
2790 | | - oIndexingArgs = oIndexingArgs.Select(o => o is double ? (int)o : o).ToList(); |
| 2810 | + oIndexingArgs = oIndexingArgs.ConvertAll(o => o is double ? (int)o : o); |
2791 | 2811 | } |
2792 | 2812 | else |
2793 | 2813 | { |
@@ -2924,8 +2944,16 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re |
2924 | 2944 |
|
2925 | 2945 | if (expression.Substring(i)[0] == '"') |
2926 | 2946 | { |
2927 | | - endOfString = true; |
2928 | | - stack.Push(resultString.ToString()); |
| 2947 | + if (expression.Substring(i).Length > 1 && expression.Substring(i)[1] == '"') |
| 2948 | + { |
| 2949 | + i += 2; |
| 2950 | + resultString.Append(@""""); |
| 2951 | + } |
| 2952 | + else |
| 2953 | + { |
| 2954 | + endOfString = true; |
| 2955 | + stack.Push(resultString.ToString()); |
| 2956 | + } |
2929 | 2957 | } |
2930 | 2958 | else if (expression.Substring(i)[0] == '{') |
2931 | 2959 | { |
@@ -2979,7 +3007,13 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re |
2979 | 3007 | string beVerb = bracketCount == 1 ? "is" : "are"; |
2980 | 3008 | throw new Exception($"{bracketCount} '}}' character {beVerb} missing in expression : [{expression}]"); |
2981 | 3009 | } |
2982 | | - resultString.Append(Evaluate(innerExp.ToString())); |
| 3010 | + |
| 3011 | + object obj = Evaluate(innerExp.ToString()); |
| 3012 | + |
| 3013 | + if (obj is BubbleExceptionContainer bubbleExceptionContainer) |
| 3014 | + throw bubbleExceptionContainer.Exception; |
| 3015 | + |
| 3016 | + resultString.Append(obj); |
2983 | 3017 | } |
2984 | 3018 | } |
2985 | 3019 | else if (expression.Substring(i, expression.Length - i)[0] == '}') |
@@ -3119,14 +3153,24 @@ void EvaluateFirstPreviousUnaryOp(int j) |
3119 | 3153 | } |
3120 | 3154 | else |
3121 | 3155 | { |
| 3156 | + var left = (dynamic)list[i + 1]; |
| 3157 | + var right = (dynamic)list[i - 1]; |
| 3158 | + |
3122 | 3159 | try |
3123 | 3160 | { |
3124 | | - list[i] = operatorEvalutationsDict[eOp]((dynamic)list[i + 1], (dynamic)list[i - 1]); |
| 3161 | + list[i] = operatorEvalutationsDict[eOp](left, right); |
| 3162 | + |
| 3163 | + if (left is BubbleExceptionContainer && right is string) |
| 3164 | + { |
| 3165 | + list[i] = left; //Bubble up the causing error |
| 3166 | + } |
| 3167 | + else if (right is BubbleExceptionContainer && left is string) |
| 3168 | + { |
| 3169 | + list[i] = right; //Bubble up the causing error |
| 3170 | + } |
3125 | 3171 | } |
3126 | 3172 | catch (Exception ex) |
3127 | 3173 | { |
3128 | | - var left = (dynamic)list[i + 1]; |
3129 | | - var right = (dynamic)list[i - 1]; |
3130 | 3174 | if (left is BubbleExceptionContainer) |
3131 | 3175 | { |
3132 | 3176 | list[i] = left; //Bubble up the causing error |
|
0 commit comments