diff --git a/src/org/mozilla/javascript/NativeReflect.java b/src/org/mozilla/javascript/NativeReflect.java index ada3b48467..d7f1a05618 100644 --- a/src/org/mozilla/javascript/NativeReflect.java +++ b/src/org/mozilla/javascript/NativeReflect.java @@ -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])) { diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeReflectTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeReflectTest.java index 5651d80853..bc853bb331 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeReflectTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeReflectTest.java @@ -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 @@ -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; + }); + } }