Skip to content

Commit

Permalink
Processing integers without decimal place is put under feature flag, …
Browse files Browse the repository at this point in the history
…test for this behaviour is added.
  • Loading branch information
KITSOleksandrMaksymenko authored and gbrail committed Mar 12, 2018
1 parent 8574ca4 commit 1ff7bc0
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/org/mozilla/javascript/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ public class Context
*/
public static final int FEATURE_THREAD_SAFE_OBJECTS = 17;

/**
* If set, then all integer numbers will be returned without decimal place. For instance
* assume there is a function like this:
* <code>function foo() {return 5;}</code>
* 5 will be returned if feature is set, 5.0 otherwise.
*/
public static final int FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE = 18;

public static final String languageVersionProperty = "language version";
public static final String errorReporterProperty = "error reporter";

Expand Down
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/ContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ protected boolean hasFeature(Context cx, int featureIndex)

case Context.FEATURE_THREAD_SAFE_OBJECTS:
return false;

case Context.FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE:
return false;
}
// It is a bug to call the method with unknown featureIndex
throw new IllegalArgumentException(String.valueOf(featureIndex));
Expand Down
11 changes: 7 additions & 4 deletions src/org/mozilla/javascript/NativeJavaObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,13 @@ else if (type == ScriptRuntime.StringClass) {
return ScriptRuntime.toString(value);
}
else if (type == ScriptRuntime.ObjectClass) {
//to process numbers like 2.0 as 2 without decimal place
long roundedValue = Math.round(toDouble(value));
if(roundedValue == toDouble(value)) {
return coerceToNumber(Long.TYPE, value);
Context context = Context.getCurrentContext();
if(context.hasFeature(Context.FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE)) {
//to process numbers like 2.0 as 2 without decimal place
long roundedValue = Math.round(toDouble(value));
if(roundedValue == toDouble(value)) {
return coerceToNumber(Long.TYPE, value);
}
}
return coerceToNumber(Double.TYPE, value);
}
Expand Down
2 changes: 1 addition & 1 deletion testsrc/doctests/413838.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ true
js> map.put("c",1)
null
js> map.put("c",1)
1
1.0
js> map.put("d",1)
null
js> map.get("c") == map.get("d")
Expand Down
8 changes: 8 additions & 0 deletions testsrc/doctests/feature18enabled.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
js> m = new java.util.LinkedHashMap(); m.put("a",1); m.put("b",2); m
{a=1, b=2}
js> for (i in Iterator(m.values())) print(i)
1
2
js> for (i in Iterator(m.values().iterator())) print(i)
1
2
10 changes: 5 additions & 5 deletions testsrc/doctests/iterable.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
js> m = new java.util.LinkedHashMap()
{}
js> m.put("a",1); m.put("b",2); m
{a=1, b=2}
{a=1.0, b=2.0}
js> for (i in Iterator(m.values())) print(i)
1
2
1.0
2.0
js> for (i in Iterator(m.values().iterator())) print(i)
1
2
1.0
2.0
14 changes: 7 additions & 7 deletions testsrc/doctests/javaadapter.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ null
js> x.get("foo")
2bar
js> x.get("bar")
2
2.0
js> x = new JavaAdapter({}); x.class.superclass
class java.lang.Object
js> x.class.interfaces.length
Expand Down Expand Up @@ -71,17 +71,17 @@ js> // test extending abstract base class
js> x = new java.util.AbstractList({get: function(i) { return i + 1; }})
[]
js> x.get(0)
1
1.0
js> x.get(1)
2
2.0
js> x.get(2)
3
3.0
js> // test implementing an interface
js> x = new java.util.List({get: function(i) { return i + 1; }, toString: function() { return "[]"; }})
[]
js> x.get(0)
1
1.0
js> x.get(1)
2
2.0
js> x.get(2)
3
3.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.mozilla.javascript.tests;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.tools.shell.Global;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public class DoctestFeature18EnabledTest extends DoctestsTest {
public DoctestFeature18EnabledTest(String name, String source, int optimizationLevel) {
super(name, source, optimizationLevel);
}

@Parameters(name = "{0}")
public static Collection<Object[]> singleDoctest() throws IOException {
List<Object[]> result = new ArrayList<Object[]>();
File f = new File(DoctestsTest.baseDirectory, "feature18enabled.doctest");
String contents = DoctestsTest.loadFile(f);
result.add(new Object[]{f.getName(), contents, -1});
return result;
}

@Test
public void runDoctest() {
ContextFactory contextFactory = new ContextFactory() {
@Override
protected boolean hasFeature(Context cx, int featureIndex) {
if (featureIndex == Context.FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE) {
return true;
}
return super.hasFeature(cx, featureIndex);
}
};
Context context = contextFactory.enterContext();
try {
context.setOptimizationLevel(optimizationLevel);
Global global = new Global(context);
int testsPassed = global.runDoctest(context, global, source, name, 1);
assertTrue(testsPassed > 0);
} finally {
Context.exit();
}
}
}
3 changes: 2 additions & 1 deletion testsrc/org/mozilla/javascript/tests/DoctestsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public static File[] getDoctestFiles() {
return TestUtils.recursiveListFiles(new File(baseDirectory),
new FileFilter() {
public boolean accept(File f) {
return f.getName().endsWith(doctestsExtension);
String name = f.getName();
return !name.contains("feature18enabled") && name.endsWith(doctestsExtension);
}
});
}
Expand Down

0 comments on commit 1ff7bc0

Please sign in to comment.