Skip to content

Commit

Permalink
fix handling of primitive values in Reflect.apply (see HtmlUnit/htmlu…
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 5, 2023
1 parent 1d1c220 commit 19cb929
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/org/mozilla/javascript/NativeReflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private static Object apply(Context cx, Scriptable scope, Scriptable thisObj, Ob

if (args[1] instanceof Scriptable) {
thisObj = (Scriptable) args[1];
} else if (ScriptRuntime.isPrimitive(args[1])) {
thisObj = cx.newObject(scope, "Object", new Object[] {args[1]});
}

if (ScriptRuntime.isSymbol(args[2])) {
Expand Down
36 changes: 36 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeReflectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ public void testToString() {
@Test
public void apply() {
testDouble(1.0, "Reflect.apply(Math.floor, undefined, [1.75])");
testString(
"hello",
"Reflect.apply(String.fromCharCode, undefined, [104, 101, 108, 108, 111])");
testInt(4, "Reflect.apply(RegExp.prototype.exec, /ab/, ['confabulation']).index");
testString("i", "Reflect.apply(''.charAt, 'ponies', [3])");
}

@Test
public void applyString() {
testString("foo", "Reflect.apply(String.prototype.toString, 'foo', [])");
testString("oo", "Reflect.apply(String.prototype.substring, 'foo', [1])");
}

@Test
public void applyNumber() {
testString("1.234567e+3", "Reflect.apply(Number.prototype.toExponential, 1234.567, [])");
testString("1.23e+3", "Reflect.apply(Number.prototype.toExponential, 1234.567, [2])");
}

@Test
public void applyBoolean() {
testString("true", "Reflect.apply(Boolean.prototype.toString, true, [])");
testString("false", "Reflect.apply(Boolean.prototype.toString, false, [])");
}

@Test
Expand Down Expand Up @@ -425,4 +448,17 @@ private static void testDouble(double expected, String js) {
return null;
});
}

private static void testInt(int expected, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
final Scriptable scope = cx.initStandardObjects();

Object result = cx.evaluateString(scope, js, "test", 1, null);
assertEquals(expected, ((Integer) result).intValue());

return null;
});
}
}

0 comments on commit 19cb929

Please sign in to comment.