Skip to content

Commit

Permalink
Cleanup Codec/Argument Reversing Passing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gertdreyer authored and lostmsu committed Feb 26, 2024
1 parent 1542cc9 commit 71ca063
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public void Dispose()
}

// Mock Integer class to test math ops on non-native dotnet types
public struct OwnInt
public readonly struct OwnInt
{
private int _value;
private readonly int _value;

public int Num => _value;

Expand Down
31 changes: 13 additions & 18 deletions src/runtime/MethodBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ internal class MethodBinder

public bool argsReversed = false;

internal MethodBinder(bool argsReversed = false)
internal MethodBinder()
{
list = new List<MaybeMethodBase>();
this.argsReversed = argsReversed;
}

internal MethodBinder(MethodInfo mi, bool argsReversed = false)
internal MethodBinder(MethodInfo mi)
{
list = new List<MaybeMethodBase> { new MaybeMethodBase(mi) };
this.argsReversed = argsReversed;
}

public int Count
Expand Down Expand Up @@ -276,11 +274,10 @@ internal static int ArgPrecedence(Type t)
/// <param name="inst">The Python target of the method invocation.</param>
/// <param name="args">The Python arguments.</param>
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
{
return Bind(inst, args, kw, null, null, argsReversed);
return Bind(inst, args, kw, null, null);
}

/// <summary>
Expand All @@ -293,11 +290,10 @@ internal static int ArgPrecedence(Type t)
/// <param name="args">The Python arguments.</param>
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="info">If not null, only bind to that method.</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info)
{
return Bind(inst, args, kw, info, null, argsReversed);
return Bind(inst, args, kw, info, null);
}

private readonly struct MatchedMethod
Expand Down Expand Up @@ -341,9 +337,8 @@ public MismatchedMethod(Exception exception, MethodBase mb)
/// <param name="kw">The Python keyword arguments.</param>
/// <param name="info">If not null, only bind to that method.</param>
/// <param name="methodinfo">If not null, additionally attempt to bind to the generic methods in this array by inferring generic type parameters.</param>
/// <param name="argsReversed">Reverse arguments of methods. Used for methods such as __radd__, __rsub__, __rmod__ etc</param>
/// <returns>A Binding if successful. Otherwise null.</returns>
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool argsReversed = false)
internal Binding? Bind(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo)
{
// loop to find match, return invoker w/ or w/o error
var kwargDict = new Dictionary<string, PyObject>();
Expand Down Expand Up @@ -819,14 +814,14 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
return match;
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
{
return Invoke(inst, args, kw, null, null, argsReversed);
return Invoke(inst, args, kw, null, null);
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info)
{
return Invoke(inst, args, kw, info, null, argsReversed = false);
return Invoke(inst, args, kw, info, null);
}

protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference args)
Expand Down Expand Up @@ -862,7 +857,7 @@ protected static void AppendArgumentTypes(StringBuilder to, BorrowedReference ar
to.Append(')');
}

internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo, bool argsReversed = false)
internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info, MethodBase[]? methodinfo)
{
// No valid methods, nothing to bind.
if (GetMethods().Length == 0)
Expand All @@ -875,7 +870,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
return Exceptions.RaiseTypeError(msg.ToString());
}

Binding? binding = Bind(inst, args, kw, info, methodinfo, argsReversed);
Binding? binding = Bind(inst, args, kw, info, methodinfo);
object result;
IntPtr ts = IntPtr.Zero;

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/Types/MethodBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static NewReference tp_call(BorrowedReference ob, BorrowedReference args,
}
}

return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue, self.m.binder.argsReversed);
return self.m.Invoke(target is null ? BorrowedReference.Null : target, args, kw, self.info.UnsafeValue);
}
finally
{
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/Types/MethodObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public MethodObject(MaybeType type, string name, MethodBase[] info, bool allow_t
this.type = type;
this.name = name;
this.infoList = new List<MaybeMethodInfo>();
binder = new MethodBinder(argsReversed);
binder = new MethodBinder() { argsReversed = argsReversed };
foreach (MethodBase item in info)
{
this.infoList.Add(item);
Expand Down Expand Up @@ -67,14 +67,14 @@ internal MethodBase[] info
}
}

public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, bool argsReversed = false)
public virtual NewReference Invoke(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
{
return Invoke(inst, args, kw, null, argsReversed);
return Invoke(inst, args, kw, null);
}

public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info, bool argsReversed = false)
public virtual NewReference Invoke(BorrowedReference target, BorrowedReference args, BorrowedReference kw, MethodBase? info)
{
return binder.Invoke(target, args, kw, info, this.info, argsReversed);
return binder.Invoke(target, args, kw, info, this.info);
}

/// <summary>
Expand Down

0 comments on commit 71ca063

Please sign in to comment.