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 1 commit
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
4 changes: 4 additions & 0 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ 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_2 && length == 0) return Boolean.FALSE;
}
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
62 changes: 62 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Issue1402Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* 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 emptyArraysCoerceToFalseInLegacyMode() {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_1_0);

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

@Test
public void emptyArraysCoerceToFalseInNormalVersions() {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_1_3); // And any above

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