Skip to content

Commit

Permalink
ScriptRuntime#toString(java.lang.Object) uses toPrimitive()
Browse files Browse the repository at this point in the history
fix s stupid bug in the toPrimitive() implementation introduced in mozilla#1661
  • Loading branch information
rbri committed Oct 5, 2024
1 parent 9de5f41 commit a2e6272
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
20 changes: 9 additions & 11 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,24 +1014,22 @@ public static String toString(Object val) {
return val.toString();
}
if (val instanceof BigInteger) {
return val.toString();
return ((BigInteger) val).toString(10);
}
if (val instanceof Number) {
// XXX should we just teach NativeNumber.stringValue()
// about Numbers?
return numberToString(((Number) val).doubleValue(), 10);
}
if (val instanceof Symbol) {
throw typeErrorById("msg.not.a.string");
if (val instanceof Boolean) {
return val.toString();
}
if (val instanceof Scriptable) {
val = ((Scriptable) val).getDefaultValue(StringClass);
if ((val instanceof Scriptable) && !isSymbol(val)) {
throw errorWithClassName("msg.primitive.expected", val);
}
continue;
if (isSymbol(val)) {
throw typeErrorById("msg.not.a.string");
}
return val.toString();
// Assert: val is an Object
val = toPrimitive(val, StringClass);
// Assert: val is a primitive
}
}

Expand Down Expand Up @@ -3581,7 +3579,7 @@ public static Object toPrimitive(Object input, Class<?> preferredType) {
&& !Undefined.isUndefined(exoticToPrim)) {
throw notFunctionError(exoticToPrim);
}
final Class<?> defaultValueHint = preferredType == null ? preferredType : NumberClass;
final Class<?> defaultValueHint = preferredType == null ? NumberClass : preferredType;
final Object result = s.getDefaultValue(defaultValueHint);
if ((result instanceof Scriptable) && !isSymbol(result))
throw typeErrorById("msg.bad.default.value");
Expand Down
18 changes: 3 additions & 15 deletions tests/testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1063,11 +1063,9 @@ built-ins/Math 51/327 (15.6%)

built-ins/NaN 0/6 (0.0%)

built-ins/NativeErrors 31/123 (25.2%)
built-ins/NativeErrors 29/123 (23.58%)
AggregateError/errors-iterabletolist-failures.js
AggregateError/is-a-constructor.js {unsupported: [Reflect.construct]}
AggregateError/message-tostring-abrupt.js
AggregateError/message-tostring-abrupt-symbol.js
AggregateError/newtarget-proto-custom.js {unsupported: [Reflect.construct]}
AggregateError/newtarget-proto-fallback.js
AggregateError/proto-from-ctor-realm.js {unsupported: [Reflect]}
Expand Down Expand Up @@ -1118,7 +1116,7 @@ built-ins/Number 24/335 (7.16%)
S9.3.1_A3_T1_U180E.js {unsupported: [u180e]}
S9.3.1_A3_T2_U180E.js {unsupported: [u180e]}

built-ins/Object 217/3408 (6.37%)
built-ins/Object 216/3408 (6.34%)
assign/assignment-to-readonly-property-of-target-must-throw-a-typeerror-exception.js
assign/not-a-constructor.js {unsupported: [Reflect.construct]}
assign/source-own-prop-desc-missing.js {unsupported: [Proxy]}
Expand Down Expand Up @@ -1188,7 +1186,6 @@ built-ins/Object 217/3408 (6.37%)
freeze/throws-when-false.js
freeze/typedarray-backed-by-resizable-buffer.js {unsupported: [resizable-arraybuffer]}
fromEntries/not-a-constructor.js {unsupported: [Reflect.construct]}
fromEntries/to-property-key.js
fromEntries/uses-keys-not-iterator.js
getOwnPropertyDescriptors/not-a-constructor.js {unsupported: [Reflect.construct]}
getOwnPropertyDescriptors/observable-operations.js {unsupported: [Proxy]}
Expand Down Expand Up @@ -2184,7 +2181,7 @@ built-ins/SetIteratorPrototype 0/11 (0.0%)

~built-ins/SharedArrayBuffer

built-ins/String 125/1182 (10.58%)
built-ins/String 116/1182 (9.81%)
fromCharCode/not-a-constructor.js {unsupported: [Reflect.construct]}
fromCodePoint/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/charAt/not-a-constructor.js {unsupported: [Reflect.construct]}
Expand All @@ -2198,9 +2195,6 @@ built-ins/String 125/1182 (10.58%)
prototype/indexOf/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/indexOf/position-tointeger-errors.js
prototype/indexOf/position-tointeger-wrapped-values.js
prototype/indexOf/searchstring-tostring-bigint.js
prototype/indexOf/searchstring-tostring-errors.js
prototype/indexOf/searchstring-tostring-toprimitive.js
prototype/indexOf/searchstring-tostring-wrapped-values.js
prototype/isWellFormed/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/isWellFormed/to-string-primitive.js
Expand All @@ -2223,7 +2217,6 @@ built-ins/String 125/1182 (10.58%)
prototype/replaceAll/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/replaceAll/replaceValue-call-each-match-position.js
prototype/replaceAll/replaceValue-call-matching-empty.js
prototype/replaceAll/replaceValue-value-tostring.js
prototype/replaceAll/searchValue-flags-no-g-throws.js
prototype/replaceAll/searchValue-flags-null-undefined-throws.js
prototype/replaceAll/searchValue-flags-toString-abrupt.js
Expand All @@ -2236,7 +2229,6 @@ built-ins/String 125/1182 (10.58%)
prototype/replaceAll/searchValue-replacer-RegExp-call.js {unsupported: [class]}
prototype/replaceAll/searchValue-replacer-RegExp-call-fn.js {unsupported: [class]}
prototype/replaceAll/searchValue-tostring-regexp.js
prototype/replaceAll/this-tostring.js
prototype/replace/cstm-replace-get-err.js
prototype/replace/cstm-replace-invocation.js
prototype/replace/not-a-constructor.js {unsupported: [Reflect.construct]}
Expand Down Expand Up @@ -2272,16 +2264,12 @@ built-ins/String 125/1182 (10.58%)
prototype/toWellFormed/to-string-primitive.js
prototype/trimEnd/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/trimEnd/this-value-object-toprimitive-call-err.js
prototype/trimEnd/this-value-object-toprimitive-meth-err.js
prototype/trimEnd/this-value-object-toprimitive-meth-priority.js
prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js
prototype/trimEnd/this-value-object-tostring-meth-priority.js
prototype/trimEnd/this-value-object-valueof-meth-priority.js
prototype/trimStart/not-a-constructor.js {unsupported: [Reflect.construct]}
prototype/trimStart/this-value-object-toprimitive-call-err.js
prototype/trimStart/this-value-object-toprimitive-meth-err.js
prototype/trimStart/this-value-object-toprimitive-meth-priority.js
prototype/trimStart/this-value-object-toprimitive-returns-object-err.js
prototype/trimStart/this-value-object-tostring-meth-priority.js
prototype/trimStart/this-value-object-valueof-meth-priority.js
prototype/trim/not-a-constructor.js {unsupported: [Reflect.construct]}
Expand Down

0 comments on commit a2e6272

Please sign in to comment.