Skip to content

Commit f005e48

Browse files
committed
Resolve ambiguous methods by arguments count.
1 parent 2805229 commit f005e48

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

Jurassic/Compiler/Binders/BinderMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private Type ParamArrayElementType
187187
/// Gets an array of method parameters.
188188
/// </summary>
189189
/// <returns> An array of ParameterInfo instances describing the method parameters. </returns>
190-
protected virtual ParameterInfo[] GetParameters()
190+
internal virtual ParameterInfo[] GetParameters()
191191
{
192192
return this.Method.GetParameters();
193193
}

Jurassic/Compiler/Binders/BinderUtilities.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public static int ResolveOverloads(RuntimeMethodHandle[] methodHandles, ScriptEn
3333
const int disqualification = 65536;
3434
for (int i = 0; i < methods.Length; i++)
3535
{
36-
foreach (var argument in methods[i].GetArguments(arguments.Length))
36+
IEnumerable<BinderArgument> binderArguments = methods[i].GetArguments(arguments.Length);
37+
foreach (var argument in binderArguments)
3738
{
3839
// Get the input parameter.
3940
object input;
@@ -147,6 +148,21 @@ public static int ResolveOverloads(RuntimeMethodHandle[] methodHandles, ScriptEn
147148
lowestIndices = _LowestIndices(methods, demeritPoints, out lowestScore);
148149
}
149150

151+
// Try to get the method with most close arguments count
152+
if (lowestIndices.Count > 1)
153+
{
154+
for (int i = 0; i < demeritPoints.Length; i++)
155+
{
156+
demeritPoints[i] = disqualification;
157+
}
158+
for (int i = 0; i < lowestIndices.Count; i++)
159+
{
160+
int index = lowestIndices[i];
161+
demeritPoints[index] = _CalcArgumentsPoint(methods[index], arguments.Length);
162+
}
163+
lowestIndices = _LowestIndices(methods, demeritPoints, out lowestScore);
164+
}
165+
150166
// Throw an error if the match is ambiguous.
151167
if (lowestIndices.Count > 1)
152168
{
@@ -215,6 +231,13 @@ private static int _CalcMethodDistance(Type thisType, Type declaringType)
215231

216232
return result;
217233
}
234+
235+
236+
private static int _CalcArgumentsPoint(BinderMethod method, int argumentsCount)
237+
{
238+
int points = Math.Max(method.GetParameters().Length - argumentsCount, 0);
239+
return points;
240+
}
218241
}
219242

220243
}

Jurassic/Compiler/Binders/JSBinderMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public int MaxParameterCount
163163
/// Gets an array of method parameters.
164164
/// </summary>
165165
/// <returns> An array of ParameterInfo instances describing the method parameters. </returns>
166-
protected override ParameterInfo[] GetParameters()
166+
internal override ParameterInfo[] GetParameters()
167167
{
168168
// Pull out the first and/or second parameters.
169169
var result = base.GetParameters();

0 commit comments

Comments
 (0)