Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error in coercion to boolean for legacy versions (#1402) #1403

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ public Object getDefaultValue(Class<?> hint) {
Context cx = Context.getContext();
if (cx.getLanguageVersion() == Context.VERSION_1_2) return Long.valueOf(length);
}
if (hint == ScriptRuntime.BooleanClass) {
Context cx = Context.getContext();
if (cx.getLanguageVersion() == Context.VERSION_1_0
|| cx.getLanguageVersion() == Context.VERSION_1_1) return length != 0;
}
return super.getDefaultValue(hint);
}

Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ScriptableObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ public Object[] getAllIds() {
*/
@Override
public Object getDefaultValue(Class<?> typeHint) {
if (typeHint == ScriptRuntime.BooleanClass) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change required?

return Boolean.TRUE;
}
return getDefaultValue(this, typeHint);
}

Expand Down
68 changes: 68 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Issue1402Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class Issue1402Test {
@Test
public void codeCanBeRunWithoutRaisingErrorInLegacyMode() {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_1_0);

Scriptable scope = cx.initStandardObjects(null);
Object result =
cx.evaluateString(
scope,
"var Base = function() {};\n"
+ "var Extending = function() {};\n"
+ "Extending.prototype = Base;\n"
+ "var x = new Extending();\n"
+ "!!x\n",
"test",
1,
null);
assertEquals(true, result);
return null;
});
}

@Test
public void emptyArraysCoerceToFalseInV10() {
assertEmptyArrayCoercesTo(Context.VERSION_1_0, false);
}

@Test
public void emptyArraysCoerceToFalseInV11() {
assertEmptyArrayCoercesTo(Context.VERSION_1_1, false);
}

@Test
public void emptyArraysCoerceToTrueInV12() {
assertEmptyArrayCoercesTo(Context.VERSION_1_2, true);
}

@Test
public void emptyArraysCoerceToTrueInNormalVersions() {
assertEmptyArrayCoercesTo(Context.VERSION_1_3, true);
}

private static void assertEmptyArrayCoercesTo(int version, boolean expected) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(version);

Scriptable scope = cx.initStandardObjects(null);
Object result = cx.evaluateString(scope, "!![]", "test", 1, null);
assertEquals(expected, result);
return null;
});
}
}