Skip to content

Commit b2a12f8

Browse files
committed
Making Xamarin.iOS use the PCL code generator
Adding tuple support to it
1 parent 6b96deb commit b2a12f8

File tree

8 files changed

+69
-31
lines changed

8 files changed

+69
-31
lines changed

NuGet/WampSharp.nuspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
<dependency id="NETStandard.Library" version="[2.0.0, )" />
5454
<dependency id="System.Collections.Immutable" version="[1.3.1, )" />
5555
<dependency id="System.Reactive" version="[4.0.0, )" />
56-
<dependency id="System.Reflection.DispatchProxy" version="[4.4.0, )" />
5756
<dependency id="System.Threading.Tasks.Dataflow" version="[4.7.0, )" />
5857
<dependency id="System.ValueTuple" version="[4.4.0, )" />
5958
</group>

src/Xamarin.iOS10/WampSharp/WampSharp.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</ItemGroup>
2424

2525
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
26-
<DefineConstants>$(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;DISPATCH_PROXY;MANUAL_PROXY;</DefineConstants>
26+
<DefineConstants>$(DefineConstants);NET45;NETCORE;PCL;LIBLOG_PUBLIC;LIBLOG_PORTABLE;TPL;ASYNC_LOCAL;ASYNC;WAMPCRA;MANUAL_PROXY;</DefineConstants>
2727
</PropertyGroup>
2828

2929
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
@@ -35,8 +35,4 @@
3535
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
3636
</ItemGroup>
3737

38-
<ItemGroup>
39-
<PackageReference Include="System.Reflection.DispatchProxy" Version="4.4.0" />
40-
</ItemGroup>
41-
4238
</Project>

src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/CalleeProxyCodeGenerator.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if !CASTLE && !DISPATCH_PROXY
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Reflection;
@@ -119,9 +118,13 @@ private IProxyMethodWriter GetWriter(MethodInfo method)
119118

120119
private static void ValidateMethod(MethodInfo method)
121120
{
122-
if (typeof (Task).IsAssignableFrom(method.ReturnType))
121+
if (!typeof(Task).IsAssignableFrom(method.ReturnType))
123122
{
124-
if (method.IsDefined(typeof (WampProgressiveResultProcedureAttribute)))
123+
MethodInfoValidation.ValidateSyncMethod(method);
124+
}
125+
else
126+
{
127+
if (method.IsDefined(typeof(WampProgressiveResultProcedureAttribute)))
125128
{
126129
MethodInfoValidation.ValidateProgressiveMethod(method);
127130
}
@@ -158,5 +161,4 @@ private string GetInterfaceName(Type interfaceType)
158161
}
159162
}
160163
}
161-
}
162-
#endif
164+
}

src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/FormatTypeExtensions.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#if !CASTLE && !DISPATCH_PROXY
2-
using System;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
5+
using System.Runtime.CompilerServices;
6+
using System.Threading.Tasks;
7+
using WampSharp.Core.Utilities.ValueTuple;
8+
using TaskExtensions = WampSharp.Core.Utilities.TaskExtensions;
59

610
namespace WampSharp.CodeGeneration
711
{
@@ -96,6 +100,51 @@ private static string Prettify(string fullName)
96100

97101
return result;
98102
}
103+
104+
public static string GetFormattedReturnType(MethodInfo method)
105+
{
106+
if (!method.ReturnsTuple())
107+
{
108+
return FormatType(method.ReturnType);
109+
}
110+
else
111+
{
112+
Type returnType = TaskExtensions.UnwrapReturnType(method.ReturnType);
113+
114+
IEnumerable<Type> tupleElementTypes =
115+
returnType.GetValueTupleElementTypes();
116+
117+
TupleElementNamesAttribute tupleElementNamesAttribute =
118+
method.ReturnParameter.GetCustomAttribute<TupleElementNamesAttribute>();
119+
120+
IEnumerable<string> tupleElementsIdentifiers;
121+
122+
IEnumerable<string> tupleElementTypesIdentifiers = tupleElementTypes
123+
.Select(x => FormatType(x));
124+
125+
if (tupleElementNamesAttribute == null)
126+
{
127+
tupleElementsIdentifiers = tupleElementTypesIdentifiers;
128+
}
129+
else
130+
{
131+
tupleElementsIdentifiers =
132+
tupleElementTypesIdentifiers
133+
.Zip(tupleElementNamesAttribute.TransformNames,
134+
(type, name) => $"{type} {name}");
135+
}
136+
137+
string tupleIdentifierContent = string.Join(", ", tupleElementsIdentifiers);
138+
139+
if (typeof(Task).IsAssignableFrom(method.ReturnType))
140+
{
141+
return $"Task<({tupleIdentifierContent})>";
142+
}
143+
else
144+
{
145+
return $"({tupleIdentifierContent})";
146+
}
147+
}
148+
}
99149
}
100-
}
101-
#endif
150+
}
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if !CASTLE && !DISPATCH_PROXY
2-
using System.Reflection;
1+
using System.Reflection;
32

43
namespace WampSharp.CodeGeneration
54
{
@@ -8,5 +7,4 @@ internal interface IProxyMethodWriter
87
string WriteField(int methodIndex, MethodInfo method);
98
string WriteMethod(int methodIndex, MethodInfo method);
109
}
11-
}
12-
#endif
10+
}

src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/OutRefProxyMethodWriter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if !CASTLE && !DISPATCH_PROXY
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;
@@ -110,7 +109,7 @@ public string WriteMethod(int methodIndex, MethodInfo method)
110109
new[] {"this", "___array"});
111110

112111
Type returnType = method.ReturnType;
113-
dictionary["returnType"] = FormatTypeExtensions.FormatType(returnType);
112+
dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method);
114113

115114
if (returnType != typeof(void))
116115
{
@@ -165,5 +164,4 @@ private string GetCallerParameter(ParameterInfo parameter)
165164
}
166165
}
167166
}
168-
}
169-
#endif
167+
}

src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/SimpleProxyMethodWriter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if !CASTLE && !DISPATCH_PROXY
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;
@@ -87,7 +86,7 @@ public string WriteMethod(int methodIndex, MethodInfo method)
8786
new[] {"this"}.Concat(specialParameters).Concat(methodCallParameters
8887
.Select(x => x.Name)));
8988

90-
dictionary["returnType"] = FormatTypeExtensions.FormatType(method.ReturnType);
89+
dictionary["returnType"] = FormatTypeExtensions.GetFormattedReturnType(method);
9190

9291

9392

@@ -137,5 +136,4 @@ public string WriteField(int methodIndex, MethodInfo method)
137136
return CodeGenerationHelper.ProcessTemplate(mFieldTemplate, dictionary);
138137
}
139138
}
140-
}
141-
#endif
139+
}

src/net45/WampSharp/WAMP2/V2/PCL/CodeGeneration/TemplateHelper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if !CASTLE && !DISPATCH_PROXY
21
using System;
32
using System.Collections.Generic;
43
using System.Reflection;
@@ -30,5 +29,4 @@ public static string ProcessTemplate(string template, IDictionary<string, string
3029
return result;
3130
}
3231
}
33-
}
34-
#endif
32+
}

0 commit comments

Comments
 (0)