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

Non deterministic behaviour with vararg methods #1433

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/org/mozilla/javascript/JavaMembers.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -308,7 +309,8 @@ private Object getExplicitFunction(
*/
private Method[] discoverAccessibleMethods(
Class<?> clazz, boolean includeProtected, boolean includePrivate) {
Map<MethodSignature, Method> map = new HashMap<>();
Map<MethodSignature, Method> map =
new LinkedHashMap<>(); // use linked hash map for deterministic discovery
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To discuss: Should we make this change? It would help to add determinism (as long as the JVM's getDeclaredMethods returns them in the same order as in source code)

discoverAccessibleMethods(clazz, map, includeProtected, includePrivate);
return map.values().toArray(new Method[0]);
}
Expand Down
9 changes: 9 additions & 0 deletions src/org/mozilla/javascript/NativeJavaMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,15 @@ private static int preferSignature(
break;
}
}
if (totalPreference == PREFERENCE_EQUAL && vararg1 != vararg2) {
// It could happen that we have found two methods, that may fit
// In this case, we will take the no-vararg one, if possible
if (vararg1) {
totalPreference = PREFERENCE_SECOND_ARG;
} else {
totalPreference = PREFERENCE_FIRST_ARG;
}
}
return totalPreference;
}

Expand Down
72 changes: 72 additions & 0 deletions testsrc/org/mozilla/javascript/tests/OverloadTestVarArgs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* 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 OverloadTestVarArgs {

public String args(String arg1) {
return "arg1";
}

public String args(String arg1, String arg2) {
return "arg1 + arg2";
}

public String args(String arg1, String... args) {
return "arg1 + args";
}

public String args2(String arg1, String... args) {
return "arg1 + args";
}

public String args2(String arg1, String arg2) {
return "arg1 + arg2";
}

public String args2(String arg1) {
return "arg1";
}

@Test
public void argsTestJavaReference() {
// this is java reference
assertEquals("arg1", this.args("foo"));
assertEquals("arg1 + arg2", this.args("foo", "bar"));
assertEquals("arg1 + args", this.args("foo", "bar", "baz"));
}

@Test
public void argsTestJs() {
assertEvaluates("arg1", "self.args('foo');");
assertEvaluates("arg1 + arg2", "self.args('foo', 'bar');");
assertEvaluates("arg1 + args", "self.args('foo', 'bar', 'baz');");
}

@Test
public void args2TestJs() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

same test, but different order. If using HashMap instead of LinkedHashMap, I cannot provide stable failing tests

assertEvaluates("arg1", "self.args2('foo');");
assertEvaluates("arg1 + arg2", "self.args2('foo', 'bar');");
assertEvaluates("arg1 + args", "self.args2('foo', 'bar', 'baz');");
}

private void assertEvaluates(final Object expected, final String source) {
Utils.runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
scope.put("self", scope, this);
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, Context.jsToJava(rep, String.class));
return null;
});
}
}