Skip to content

Commit

Permalink
Invoke methods of a generic class or of a class with generic base class
Browse files Browse the repository at this point in the history
  • Loading branch information
objmalloc committed Apr 8, 2019
1 parent 412df62 commit 1c96802
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion Jurassic/Compiler/Binders/BinderUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,48 @@ public static class BinderUtilities
/// <returns> The index of the selected method. </returns>
public static int ResolveOverloads(RuntimeMethodHandle[] methodHandles, ScriptEngine engine, object thisValue, object[] arguments)
{
object thisUnwrapped = thisValue;
if (thisUnwrapped is Library.ClrInstanceWrapper)
{
thisUnwrapped = (thisUnwrapped as Library.ClrInstanceWrapper).WrappedInstance;
}
Type unwrappedType;
if (thisUnwrapped is Library.ClrStaticTypeWrapper)
{
unwrappedType = (thisUnwrapped as Library.ClrStaticTypeWrapper).WrappedType;
}
else if (thisUnwrapped is Library.ClrInstanceTypeWrapper)
{
unwrappedType = (thisUnwrapped as Library.ClrInstanceTypeWrapper).WrappedType;
}
else
{
unwrappedType = thisUnwrapped.GetType();
}

// Get methods from the handles.
var methods = new BinderMethod[methodHandles.Length];
for (int i = 0; i < methodHandles.Length; i++)
methods[i] = new BinderMethod(MethodBase.GetMethodFromHandle(methodHandles[i]));
{
if (unwrappedType.IsGenericType)
{
// Generic methods. Works for classes which are generic.
methods[i] = new BinderMethod(
MethodBase.GetMethodFromHandle(methodHandles[i], unwrappedType.TypeHandle));
}
else if (unwrappedType.BaseType.IsGenericType)
{
// Generic methods. Works for classes which inherit generic type.
// For example: class AlternateViewCollection : Collection<AlternateView>
methods[i] = new BinderMethod(
MethodBase.GetMethodFromHandle(methodHandles[i],
unwrappedType.BaseType.TypeHandle));
}
else
{
methods[i] = new BinderMethod(MethodBase.GetMethodFromHandle(methodHandles[i]));
}
}

// Keep a score for each method. Add one point if a type conversion is required, or
// a million points if a type conversion cannot be performed.
Expand Down

0 comments on commit 1c96802

Please sign in to comment.