From a14ef82cbb94ef422713079823c11cfe2b63079e Mon Sep 17 00:00:00 2001 From: Victor Uriarte Date: Thu, 9 Feb 2017 01:02:41 -0700 Subject: [PATCH] Refactor converter.cs & methodbind*.cs --- src/runtime/converter.cs | 85 ++++++++++++++----------------- src/runtime/methodbinder.cs | 99 ++++++++++++++++-------------------- src/runtime/methodbinding.cs | 30 +++++------ 3 files changed, 97 insertions(+), 117 deletions(-) diff --git a/src/runtime/converter.cs b/src/runtime/converter.cs index 3a3fa309d..aeaf2d871 100644 --- a/src/runtime/converter.cs +++ b/src/runtime/converter.cs @@ -52,62 +52,53 @@ static Converter() /// internal static Type GetTypeByAlias(IntPtr op) { - if (op == Runtime.PyStringType || - op == Runtime.PyUnicodeType) - { + if (op == Runtime.PyStringType) return stringType; - } - else if (op == Runtime.PyIntType) - { + + if (op == Runtime.PyUnicodeType) + return stringType; + + if (op == Runtime.PyIntType) return int32Type; - } - else if (op == Runtime.PyLongType) - { + + if (op == Runtime.PyLongType) return int64Type; - } - else if (op == Runtime.PyFloatType) - { + + if (op == Runtime.PyFloatType) return doubleType; - } - else if (op == Runtime.PyBoolType) - { + + if (op == Runtime.PyBoolType) return boolType; - } + return null; } internal static IntPtr GetPythonTypeByAlias(Type op) { if (op == stringType) - { return Runtime.PyUnicodeType; - } - else if (Runtime.IsPython3 && (op == int16Type || - op == int32Type || - op == int64Type)) - { + if (op == int16Type) return Runtime.PyIntType; - } - else if (op == int16Type || - op == int32Type) - { + if (op == int32Type) return Runtime.PyIntType; - } - else if (op == int64Type) - { + + if (op == int64Type && Runtime.IsPython2) return Runtime.PyLongType; - } - else if (op == doubleType || - op == singleType) - { + + if (op == int64Type) + return Runtime.PyIntType; + + if (op == doubleType) return Runtime.PyFloatType; - } - else if (op == boolType) - { + + if (op == singleType) + return Runtime.PyFloatType; + + if (op == boolType) return Runtime.PyBoolType; - } + return IntPtr.Zero; } @@ -329,27 +320,27 @@ internal static bool ToManagedValue(IntPtr value, Type obType, return ToPrimitive(value, stringType, out result, setError); } - else if (Runtime.PyBool_Check(value)) + if (Runtime.PyBool_Check(value)) { return ToPrimitive(value, boolType, out result, setError); } - else if (Runtime.PyInt_Check(value)) + if (Runtime.PyInt_Check(value)) { return ToPrimitive(value, int32Type, out result, setError); } - else if (Runtime.PyLong_Check(value)) + if (Runtime.PyLong_Check(value)) { return ToPrimitive(value, int64Type, out result, setError); } - else if (Runtime.PyFloat_Check(value)) + if (Runtime.PyFloat_Check(value)) { return ToPrimitive(value, doubleType, out result, setError); } - else if (Runtime.PySequence_Check(value)) + if (Runtime.PySequence_Check(value)) { return ToArray(value, typeof(object[]), out result, setError); } @@ -371,31 +362,31 @@ internal static bool ToManagedValue(IntPtr value, Type obType, return true; } - else if (value == Runtime.PyBoolType) + if (value == Runtime.PyBoolType) { result = boolType; return true; } - else if (value == Runtime.PyIntType) + if (value == Runtime.PyIntType) { result = int32Type; return true; } - else if (value == Runtime.PyLongType) + if (value == Runtime.PyLongType) { result = int64Type; return true; } - else if (value == Runtime.PyFloatType) + if (value == Runtime.PyFloatType) { result = doubleType; return true; } - else if (value == Runtime.PyListType || value == Runtime.PyTupleType) + if (value == Runtime.PyListType || value == Runtime.PyTupleType) { result = typeof(object[]); return true; diff --git a/src/runtime/methodbinder.cs b/src/runtime/methodbinder.cs index 86a76dd82..1417c21c5 100644 --- a/src/runtime/methodbinder.cs +++ b/src/runtime/methodbinder.cs @@ -170,6 +170,9 @@ internal MethodBase[] GetMethods() /// Precedence algorithm largely lifted from jython - the concerns are /// generally the same so we'll start w/this and tweak as necessary. /// + /// + /// TODO: Add link to specific Jython Section/Code/File + /// internal static int GetPrecedence(MethodBase mi) { ParameterInfo[] pi = mi.GetParameters(); @@ -198,61 +201,49 @@ internal static int ArgPrecedence(Type t) TypeCode tc = Type.GetTypeCode(t); // TODO: Clean up - if (tc == TypeCode.Object) + switch (tc) { - return 1; - } - if (tc == TypeCode.UInt64) - { - return 10; - } - if (tc == TypeCode.UInt32) - { - return 11; - } - if (tc == TypeCode.UInt16) - { - return 12; - } - if (tc == TypeCode.Int64) - { - return 13; - } - if (tc == TypeCode.Int32) - { - return 14; - } - if (tc == TypeCode.Int16) - { - return 15; - } - if (tc == TypeCode.Char) - { - return 16; - } - if (tc == TypeCode.SByte) - { - return 17; - } - if (tc == TypeCode.Byte) - { - return 18; - } - if (tc == TypeCode.Single) - { - return 20; - } - if (tc == TypeCode.Double) - { - return 21; - } - if (tc == TypeCode.String) - { - return 30; - } - if (tc == TypeCode.Boolean) - { - return 40; + case TypeCode.Object: + return 1; + + case TypeCode.UInt64: + return 10; + + case TypeCode.UInt32: + return 11; + + case TypeCode.UInt16: + return 12; + + case TypeCode.Int64: + return 13; + + case TypeCode.Int32: + return 14; + + case TypeCode.Int16: + return 15; + + case TypeCode.Char: + return 16; + + case TypeCode.SByte: + return 17; + + case TypeCode.Byte: + return 18; + + case TypeCode.Single: + return 20; + + case TypeCode.Double: + return 21; + + case TypeCode.String: + return 30; + + case TypeCode.Boolean: + return 40; } if (t.IsArray) diff --git a/src/runtime/methodbinding.cs b/src/runtime/methodbinding.cs index df6632068..07090a92c 100644 --- a/src/runtime/methodbinding.cs +++ b/src/runtime/methodbinding.cs @@ -55,8 +55,7 @@ public static IntPtr mp_subscript(IntPtr tp, IntPtr idx) return Exceptions.RaiseTypeError("No match found for given type params"); } - var mb = new MethodBinding(self.m, self.target); - mb.info = mi; + var mb = new MethodBinding(self.m, self.target) { info = mi }; Runtime.XIncref(mb.pyHandle); return mb.pyHandle; } @@ -76,22 +75,21 @@ public static IntPtr tp_getattro(IntPtr ob, IntPtr key) } string name = Runtime.GetManagedString(key); - if (name == "__doc__") + switch (name) { - IntPtr doc = self.m.GetDocString(); - Runtime.XIncref(doc); - return doc; + case "__doc__": + IntPtr doc = self.m.GetDocString(); + Runtime.XIncref(doc); + return doc; + // FIXME: deprecate __overloads__ soon... + case "__overloads__": + case "Overloads": + var om = new OverloadMapper(self.m, self.target); + Runtime.XIncref(om.pyHandle); + return om.pyHandle; + default: + return Runtime.PyObject_GenericGetAttr(ob, key); } - - // FIXME: deprecate __overloads__ soon... - if (name == "__overloads__" || name == "Overloads") - { - var om = new OverloadMapper(self.m, self.target); - Runtime.XIncref(om.pyHandle); - return om.pyHandle; - } - - return Runtime.PyObject_GenericGetAttr(ob, key); }