From b2a12f87b83a14347674bdba4276baa667298baa Mon Sep 17 00:00:00 2001 From: Elad Zelingher Date: Thu, 19 Jul 2018 14:18:20 -0400 Subject: [PATCH] Making Xamarin.iOS use the PCL code generator Adding tuple support to it --- NuGet/WampSharp.nuspec | 1 - src/Xamarin.iOS10/WampSharp/WampSharp.csproj | 6 +- .../CalleeProxyCodeGenerator.cs | 14 +++-- .../CodeGeneration/FormatTypeExtensions.cs | 57 +++++++++++++++++-- .../PCL/CodeGeneration/IProxyMethodWriter.cs | 6 +- .../CodeGeneration/OutRefProxyMethodWriter.cs | 6 +- .../CodeGeneration/SimpleProxyMethodWriter.cs | 6 +- .../V2/PCL/CodeGeneration/TemplateHelper.cs | 4 +- 8 files changed, 69 insertions(+), 31 deletions(-) diff --git a/NuGet/WampSharp.nuspec b/NuGet/WampSharp.nuspec index ca1925b25..4e8a9f48a 100644 --- a/NuGet/WampSharp.nuspec +++ b/NuGet/WampSharp.nuspec @@ -53,7 +53,6 @@ - diff --git a/src/Xamarin.iOS10/WampSharp/WampSharp.csproj b/src/Xamarin.iOS10/WampSharp/WampSharp.csproj index 5617632e0..00d994821 100644 --- a/src/Xamarin.iOS10/WampSharp/WampSharp.csproj +++ b/src/Xamarin.iOS10/WampSharp/WampSharp.csproj @@ -23,7 +23,7 @@ - $(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;DISPATCH_PROXY;MANUAL_PROXY; + $(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;MANUAL_PROXY; @@ -35,8 +35,4 @@ - - - - \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/CalleeProxyCodeGenerator.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/CalleeProxyCodeGenerator.cs index 0627c151d..c8713bbcb 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/CalleeProxyCodeGenerator.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/CalleeProxyCodeGenerator.cs @@ -1,5 +1,4 @@ -#if !CASTLE && !DISPATCH_PROXY -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -119,9 +118,13 @@ private IProxyMethodWriter GetWriter(MethodInfo method) private static void ValidateMethod(MethodInfo method) { - if (typeof (Task).IsAssignableFrom(method.ReturnType)) + if (!typeof(Task).IsAssignableFrom(method.ReturnType)) { - if (method.IsDefined(typeof (WampProgressiveResultProcedureAttribute))) + MethodInfoValidation.ValidateSyncMethod(method); + } + else + { + if (method.IsDefined(typeof(WampProgressiveResultProcedureAttribute))) { MethodInfoValidation.ValidateProgressiveMethod(method); } @@ -158,5 +161,4 @@ private string GetInterfaceName(Type interfaceType) } } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/FormatTypeExtensions.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/FormatTypeExtensions.cs index 33b0fb01d..11964fa41 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/FormatTypeExtensions.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/FormatTypeExtensions.cs @@ -1,7 +1,11 @@ -#if !CASTLE && !DISPATCH_PROXY -using System; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using WampSharp.Core.Utilities.ValueTuple; +using TaskExtensions = WampSharp.Core.Utilities.TaskExtensions; namespace WampSharp.CodeGeneration { @@ -96,6 +100,51 @@ private static string Prettify(string fullName) return result; } + + public static string GetFormattedReturnType(MethodInfo method) + { + if (!method.ReturnsTuple()) + { + return FormatType(method.ReturnType); + } + else + { + Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType); + + IEnumerable tupleElementTypes = + returnType.GetValueTupleElementTypes(); + + TupleElementNamesAttribute tupleElementNamesAttribute = + method.ReturnParameter.GetCustomAttribute(); + + IEnumerable tupleElementsIdentifiers; + + IEnumerable tupleElementTypesIdentifiers = tupleElementTypes + .Select(x => FormatType(x)); + + if (tupleElementNamesAttribute == null) + { + tupleElementsIdentifiers = tupleElementTypesIdentifiers; + } + else + { + tupleElementsIdentifiers = + tupleElementTypesIdentifiers + .Zip(tupleElementNamesAttribute.TransformNames, + (type, name) => $"{type} {name}"); + } + + string tupleIdentifierContent = string.Join(", ", tupleElementsIdentifiers); + + if (typeof(Task).IsAssignableFrom(method.ReturnType)) + { + return $"Task<({tupleIdentifierContent})>"; + } + else + { + return $"({tupleIdentifierContent})"; + } + } + } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/IProxyMethodWriter.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/IProxyMethodWriter.cs index 87128b544..f24b88370 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/IProxyMethodWriter.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/IProxyMethodWriter.cs @@ -1,5 +1,4 @@ -#if !CASTLE && !DISPATCH_PROXY -using System.Reflection; +using System.Reflection; namespace WampSharp.CodeGeneration { @@ -8,5 +7,4 @@ internal interface IProxyMethodWriter string WriteField(int methodIndex, MethodInfo method); string WriteMethod(int methodIndex, MethodInfo method); } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/OutRefProxyMethodWriter.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/OutRefProxyMethodWriter.cs index 869fad92c..5f944fe45 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/OutRefProxyMethodWriter.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/OutRefProxyMethodWriter.cs @@ -1,4 +1,3 @@ -#if !CASTLE && !DISPATCH_PROXY using System; using System.Collections.Generic; using System.Linq; @@ -110,7 +109,7 @@ public string WriteMethod(int methodIndex, MethodInfo method) new[] {"this", "___array"}); Type returnType = method.ReturnType; - dictionary["returnType"] = FormatTypeExtensions.FormatType(returnType); + dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method); if (returnType != typeof(void)) { @@ -165,5 +164,4 @@ private string GetCallerParameter(ParameterInfo parameter) } } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/SimpleProxyMethodWriter.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/SimpleProxyMethodWriter.cs index b466ec696..60e47e3e9 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/SimpleProxyMethodWriter.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/SimpleProxyMethodWriter.cs @@ -1,4 +1,3 @@ -#if !CASTLE && !DISPATCH_PROXY using System; using System.Collections.Generic; using System.Linq; @@ -87,7 +86,7 @@ public string WriteMethod(int methodIndex, MethodInfo method) new[] {"this"}.Concat(specialParameters).Concat(methodCallParameters .Select(x => x.Name))); - dictionary["returnType"] = FormatTypeExtensions.FormatType(method.ReturnType); + dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method); @@ -137,5 +136,4 @@ public string WriteField(int methodIndex, MethodInfo method) return CodeGenerationHelper.ProcessTemplate(mFieldTemplate, dictionary); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/TemplateHelper.cs b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/TemplateHelper.cs index 09cacebcb..e27d95fae 100644 --- a/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/TemplateHelper.cs +++ b/src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/TemplateHelper.cs @@ -1,4 +1,3 @@ -#if !CASTLE && !DISPATCH_PROXY using System; using System.Collections.Generic; using System.Reflection; @@ -30,5 +29,4 @@ public static string ProcessTemplate(string template, IDictionary