Skip to content

Commit

Permalink
guard Symbol.toPrimitive lookup in toPrimitive AO by language version…
Browse files Browse the repository at this point in the history
… >= ES6
  • Loading branch information
tonygermano committed Sep 6, 2024
1 parent 835cac3 commit 40b7494
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3499,26 +3499,28 @@ public static Object toPrimitive(Object input, Optional<Class<?>> preferredType)
return input;
}
final Scriptable s = (Scriptable) input;
final Object exoticToPrim = ScriptableObject.getProperty(s, SymbolKey.TO_PRIMITIVE);
if (exoticToPrim instanceof Function) {
final Function func = (Function) exoticToPrim;
final Context cx = Context.getCurrentContext();
final Scriptable scope = func.getParentScope();
final String hint =
preferredType
.map(type -> type == StringClass ? "string" : "number")
.orElse("default");
final Object[] args = new Object[] {hint};
final Object result = func.call(cx, scope, s, args);
if (isObject(result)) {
throw typeErrorById("msg.cant.convert.to.primitive");
final Context cx = Context.getCurrentContext();
if (cx.getLanguageVersion() >= Context.VERSION_ES6) {
final Object exoticToPrim = ScriptableObject.getProperty(s, SymbolKey.TO_PRIMITIVE);
if (exoticToPrim instanceof Function) {
final Function func = (Function) exoticToPrim;
final Scriptable scope = func.getParentScope();
final String hint =
preferredType
.map(type -> type == StringClass ? "string" : "number")
.orElse("default");
final Object[] args = new Object[] {hint};
final Object result = func.call(cx, scope, s, args);
if (isObject(result)) {
throw typeErrorById("msg.cant.convert.to.primitive");
}
return result;
}
if (!Undefined.isUndefined(exoticToPrim)
&& exoticToPrim != null
&& exoticToPrim != Scriptable.NOT_FOUND) {
throw notFunctionError(exoticToPrim);
}
return result;
}
if (!Undefined.isUndefined(exoticToPrim)
&& exoticToPrim != null
&& exoticToPrim != Scriptable.NOT_FOUND) {
throw notFunctionError(exoticToPrim);
}
final Class<?> defaultValueHint = preferredType.orElse(NumberClass);
final Object result = s.getDefaultValue(defaultValueHint);
Expand Down

0 comments on commit 40b7494

Please sign in to comment.