Skip to content

Commit 214b958

Browse files
authored
Merge pull request #370 from killme2008/v5.2.5-dev
5.2.5 dev
2 parents c2456dc + d8b748a commit 214b958

File tree

12 files changed

+510
-57
lines changed

12 files changed

+510
-57
lines changed

src/main/java/com/googlecode/aviator/AviatorEvaluator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.googlecode.aviator.lexer.token.OperatorType;
2525
import com.googlecode.aviator.parser.AviatorClassLoader;
2626
import com.googlecode.aviator.runtime.type.AviatorFunction;
27+
import com.googlecode.aviator.utils.ArrayHashMap;
2728
import com.googlecode.aviator.utils.Utils;
2829

2930

@@ -112,7 +113,10 @@ public static Map<String, Object> newEnv(final Object... args) {
112113
if (args != null && args.length % 2 != 0) {
113114
throw new IllegalArgumentException("Expect arguments number is even.");
114115
}
115-
Map<String, Object> env = new HashMap<String, Object>(args != null ? args.length : 10);
116+
Map<String, Object> env =
117+
(args != null && args.length <= 16) ? new ArrayHashMap<String, Object>()
118+
: new HashMap<String, Object>(args != null ? args.length : 16);
119+
116120
if (args != null) {
117121
for (int i = 0; i < args.length; i += 2) {
118122
String key = (String) args[i];

src/main/java/com/googlecode/aviator/BaseExpression.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public abstract class BaseExpression implements Expression {
4747
protected String sourceFile;
4848

4949

50+
@Override
5051
public String getSourceFile() {
5152
return this.sourceFile;
5253
}

src/main/java/com/googlecode/aviator/runtime/function/internal/ReducerFunction.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public final AviatorObject call(final Map<String, Object> env, final AviatorObje
7171
boolean breakOut = false;
7272
ReducerResult midResult = (ReducerResult) result;
7373
result = midResult.obj;
74+
75+
if (midResult.state == ReducerState.Empty) {
76+
continue;
77+
}
7478
switch (midResult.state) {
7579
case Break:
7680
breakOut = true;
@@ -97,6 +101,10 @@ public final AviatorObject call(final Map<String, Object> env, final AviatorObje
97101
boolean breakOut = false;
98102
ReducerResult midResult = (ReducerResult) result;
99103
result = midResult.obj;
104+
105+
if (midResult.state == ReducerState.Empty) {
106+
continue;
107+
}
100108
switch (midResult.state) {
101109
case Break:
102110
breakOut = true;

src/main/java/com/googlecode/aviator/runtime/type/AviatorDecimal.java

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ public static final AviatorDecimal valueOf(final AviatorEvaluatorInstance instan
4343

4444
@Override
4545
public AviatorObject innerSub(final Map<String, Object> env, final AviatorNumber other) {
46-
switch (other.getAviatorType()) {
47-
case Double:
48-
return AviatorDouble.valueOf(doubleValue() - other.doubleValue());
49-
default:
50-
return AviatorDecimal.valueOf(
51-
toDecimal(env).subtract(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
46+
if (other.getAviatorType() != AviatorType.Double) {
47+
return AviatorDecimal
48+
.valueOf(toDecimal(env).subtract(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
49+
} else {
50+
return AviatorDouble.valueOf(doubleValue() - other.doubleValue());
5251
}
5352
}
5453

@@ -61,61 +60,55 @@ public AviatorObject neg(final Map<String, Object> env) {
6160

6261
@Override
6362
public AviatorObject innerMult(final Map<String, Object> env, final AviatorNumber other) {
64-
switch (other.getAviatorType()) {
65-
case Double:
66-
return AviatorDouble.valueOf(doubleValue() * other.doubleValue());
67-
default:
68-
return AviatorDecimal.valueOf(
69-
toDecimal(env).multiply(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
63+
if (other.getAviatorType() != AviatorType.Double) {
64+
return AviatorDecimal
65+
.valueOf(toDecimal(env).multiply(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
66+
} else {
67+
return AviatorDouble.valueOf(doubleValue() * other.doubleValue());
7068
}
7169
}
7270

7371

7472
@Override
7573
public AviatorObject innerMod(final Map<String, Object> env, final AviatorNumber other) {
76-
switch (other.getAviatorType()) {
77-
case Double:
78-
return AviatorDouble.valueOf(doubleValue() % other.doubleValue());
79-
default:
80-
return AviatorDecimal.valueOf(
81-
toDecimal(env).remainder(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
74+
if (other.getAviatorType() != AviatorType.Double) {
75+
return AviatorDecimal.valueOf(
76+
toDecimal(env).remainder(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
77+
} else {
78+
return AviatorDouble.valueOf(doubleValue() % other.doubleValue());
8279
}
8380
}
8481

8582

8683
@Override
8784
public AviatorObject innerDiv(final Map<String, Object> env, final AviatorNumber other) {
88-
switch (other.getAviatorType()) {
89-
case Double:
90-
return AviatorDouble.valueOf(doubleValue() / other.doubleValue());
91-
default:
92-
return AviatorDecimal
93-
.valueOf(toDecimal(env).divide(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
85+
if (other.getAviatorType() != AviatorType.Double) {
86+
return AviatorDecimal
87+
.valueOf(toDecimal(env).divide(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
88+
} else {
89+
return AviatorDouble.valueOf(doubleValue() / other.doubleValue());
9490
}
9591
}
9692

9793

9894
@Override
9995
public AviatorNumber innerAdd(final Map<String, Object> env, final AviatorNumber other) {
100-
switch (other.getAviatorType()) {
101-
case Double:
102-
return AviatorDouble.valueOf(doubleValue() + other.doubleValue());
103-
default:
104-
return AviatorDecimal
105-
.valueOf(toDecimal(env).add(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
96+
if (other.getAviatorType() != AviatorType.Double) {
97+
return AviatorDecimal
98+
.valueOf(toDecimal(env).add(other.toDecimal(env), RuntimeUtils.getMathContext(env)));
99+
} else {
100+
return AviatorDouble.valueOf(doubleValue() + other.doubleValue());
106101
}
107102
}
108103

109104

110105
@Override
111106
public int innerCompare(final Map<String, Object> env, final AviatorNumber other) {
112-
switch (other.getAviatorType()) {
113-
case Double:
114-
return Double.compare(doubleValue(), other.doubleValue());
115-
default:
116-
return toDecimal(env).compareTo(other.toDecimal(env));
107+
if (other.getAviatorType() != AviatorType.Double) {
108+
return toDecimal(env).compareTo(other.toDecimal(env));
109+
} else {
110+
return Double.compare(doubleValue(), other.doubleValue());
117111
}
118-
119112
}
120113

121114

src/main/java/com/googlecode/aviator/runtime/type/AviatorLong.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ public AviatorObject neg(final Map<String, Object> env) {
7979

8080
@Override
8181
public int innerCompare(final Map<String, Object> env, final AviatorNumber other) {
82+
if (other.getAviatorType() == AviatorType.Long) {
83+
return TypeUtils.comapreLong(longValue(), other.longValue());
84+
}
85+
8286
switch (other.getAviatorType()) {
8387
case BigInt:
8488
return toBigInt().compareTo(other.toBigInt());
8589
case Decimal:
8690
return toDecimal(env).compareTo(other.toDecimal(env));
87-
case Long:
88-
return TypeUtils.comapreLong(longValue(), other.longValue());
8991
case Double:
9092
return Double.compare(doubleValue(), other.doubleValue());
9193
default:

src/main/java/com/googlecode/aviator/runtime/type/AviatorString.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ public AviatorString(final String lexeme, final boolean isLiteral, final boolean
8585
public AviatorObject add(final AviatorObject other, final Map<String, Object> env) {
8686
final StringBuilder sb = new StringBuilder(getLexeme(env));
8787

88-
if (other.getAviatorType() == AviatorType.Pattern) {
88+
if (other.getAviatorType() != AviatorType.Pattern) {
89+
sb.append(other.getValue(env));
90+
} else {
8991
final AviatorPattern otherPatterh = (AviatorPattern) other;
9092
sb.append(otherPatterh.pattern.pattern());
91-
} else {
92-
sb.append(other.getValue(env));
9393
}
9494
return new AviatorStringBuilder(sb);
9595
}
@@ -117,19 +117,22 @@ private int tryCompareDate(final Map<String, Object> env, final Date otherDate)
117117
@Override
118118
public int innerCompare(final AviatorObject other, final Map<String, Object> env) {
119119
final String left = getLexeme(env);
120+
121+
if (other.getAviatorType() == AviatorType.String) {
122+
final AviatorString otherString = (AviatorString) other;
123+
final String right = otherString.getLexeme(env);
124+
if (left != null && right != null) {
125+
return left.compareTo(right);
126+
} else if (left == null && right != null) {
127+
return -1;
128+
} else if (left != null && right == null) {
129+
return 1;
130+
} else {
131+
return 0;
132+
}
133+
}
134+
120135
switch (other.getAviatorType()) {
121-
case String:
122-
final AviatorString otherString = (AviatorString) other;
123-
final String right = otherString.getLexeme(env);
124-
if (left == null && right != null) {
125-
return -1;
126-
} else if (left != null && right == null) {
127-
return 1;
128-
} else if (left == null && right == null) {
129-
return 0;
130-
} else {
131-
return left.compareTo(right);
132-
}
133136
case JavaType:
134137
final AviatorJavaType javaType = (AviatorJavaType) other;
135138
final Object otherJavaValue = javaType.getValue(env);

0 commit comments

Comments
 (0)