Skip to content

Commit

Permalink
Update compare to use toPrimitive. Improve symbol and bigint handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonygermano committed Sep 5, 2024
1 parent 9664e77 commit e17cc25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
28 changes: 21 additions & 7 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3926,18 +3926,32 @@ public static boolean compare(Object val1, Object val2, int op) {
if (val1 instanceof Number && val2 instanceof Number) {
return compare((Number) val1, (Number) val2, op);
} else {
if ((val1 instanceof Symbol) || (val2 instanceof Symbol)) {
if ((isSymbol(val1)) || (isSymbol(val2))) {
throw typeErrorById("msg.compare.symbol");
}
if (val1 instanceof Scriptable) {
val1 = ((Scriptable) val1).getDefaultValue(NumberClass);
}
if (val2 instanceof Scriptable) {
val2 = ((Scriptable) val2).getDefaultValue(NumberClass);
}
val1 = toPrimitive(val1, Optional.of(NumberClass));
val2 = toPrimitive(val2, Optional.of(NumberClass));
if (val1 instanceof CharSequence && val2 instanceof CharSequence) {
return compareTo(val1.toString(), val2.toString(), op);
}
if (val1 instanceof BigInteger && val2 instanceof CharSequence) {
final BigInteger ny;
try {
ny = toBigInt(val2.toString());
} catch (EcmaError e) {
return false;
}
return compareTo((BigInteger) val1, ny, op);
}
if (val1 instanceof CharSequence && val2 instanceof BigInteger) {
final BigInteger nx;
try {
nx = toBigInt(val1.toString());
} catch (EcmaError e) {
return false;
}
return compareTo(nx, (BigInteger) val2, op);
}
return compare(toNumeric(val1), toNumeric(val2), op);
}
}
Expand Down
14 changes: 4 additions & 10 deletions tests/testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5112,12 +5112,9 @@ language/expressions/generators 232/290 (80.0%)
yield-star-after-newline.js
yield-star-before-newline.js

language/expressions/greater-than 2/49 (4.08%)
bigint-and-incomparable-string.js
bigint-and-string.js
language/expressions/greater-than 0/49 (0.0%)

language/expressions/greater-than-or-equal 1/43 (2.33%)
bigint-and-incomparable-string.js
language/expressions/greater-than-or-equal 0/43 (0.0%)

language/expressions/grouping 0/9 (0.0%)

Expand Down Expand Up @@ -5160,12 +5157,9 @@ language/expressions/left-shift 4/45 (8.89%)
bigint-wrapped-values.js
order-of-evaluation.js

language/expressions/less-than 1/45 (2.22%)
bigint-and-incomparable-string.js
language/expressions/less-than 0/45 (0.0%)

language/expressions/less-than-or-equal 2/47 (4.26%)
bigint-and-incomparable-string.js
bigint-and-string.js
language/expressions/less-than-or-equal 0/47 (0.0%)

language/expressions/logical-and 1/18 (5.56%)
tco-right.js {unsupported: [tail-call-optimization]}
Expand Down

0 comments on commit e17cc25

Please sign in to comment.