forked from iskiselev/JSIL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackedStructArray.cs
More file actions
188 lines (151 loc) · 8.32 KB
/
Copy pathPackedStructArray.cs
File metadata and controls
188 lines (151 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using JSIL.Ast;
using Mono.Cecil;
namespace JSIL.Internal {
public static class PackedArrayUtil {
public static bool IsPackedArrayType (TypeReference type) {
if (type.Namespace != "JSIL.Runtime")
return false;
return type.Name == "IPackedArray`1";
}
public static TypeReference GetElementType (TypeReference arrayType) {
var git = (GenericInstanceType)arrayType;
return git.GenericArguments[0];
}
public static TypeReference MakePackedArrayType (TypeReference arrayType, TypeReference attributeType) {
var at = arrayType as ArrayType;
if (at == null)
throw new InvalidOperationException("Cannot apply JSPackedArray to a non-array type");
var elementType = at.ElementType;
if (!TypeUtil.ExpandPositionalGenericParameters(elementType, out elementType))
elementType = at.ElementType;
if (!TypeUtil.IsStruct(elementType))
throw new InvalidOperationException("Cannot apply JSPackedArray to a non-struct array");
var attributeTypeDefinition = attributeType.Resolve();
var linkedTypeAttribute = attributeTypeDefinition.CustomAttributes.First(
(ca) => ca.AttributeType.FullName == "JSIL.Runtime.LinkedTypeAttribute"
);
var openTypeReference = (TypeReference)linkedTypeAttribute.ConstructorArguments[0].Value;
var closedTypeReference = new GenericInstanceType(openTypeReference);
closedTypeReference.GenericArguments.Add(at.ElementType);
return closedTypeReference;
}
public static void CheckInvocationSafety (MethodInfo method, JSExpression[] argumentValues, TypeSystem typeSystem) {
if (method.Metadata.HasAttribute("JSIL.Meta.JSAllowPackedArrayArgumentsAttribute"))
return;
TypeReference temp;
string[] argumentNames = GetPackedArrayArgumentNames(method, out temp);
for (var i = 0; i < method.Parameters.Length; i++) {
if (i >= argumentValues.Length)
continue;
var valueType = argumentValues[i].GetActualType(typeSystem);
if (!IsPackedArrayType(valueType)) {
if ((argumentNames != null) && argumentNames.Contains(method.Parameters[i].Name))
throw new ArgumentException(
"Invalid attempt to pass a normal array as parameter '" + method.Parameters[i].Name + "' to method '" + method.Name + "'. " +
"This parameter must be a packed array."
);
} else {
if ((argumentNames == null) || !argumentNames.Contains(method.Parameters[i].Name))
throw new ArgumentException(
"Invalid attempt to pass a packed array as parameter '" + method.Parameters[i].Name + "' to method '" + method.Name + "'. " +
"If this is intentional, annotate the method with the JSPackedArrayArguments attribute."
);
}
}
}
public static string[] GetPackedArrayArgumentNames (MethodInfo method, out TypeReference packedArrayAttributeType) {
packedArrayAttributeType = null;
AttributeGroup packedArrayAttribute;
if (
(method != null) &&
((packedArrayAttribute = method.Metadata.GetAttribute("JSIL.Meta.JSPackedArrayArgumentsAttribute")) != null)
) {
var result = new List<string>();
foreach (var entry in packedArrayAttribute.Entries) {
packedArrayAttributeType = entry.Type;
var argumentNames = entry.Arguments[0].Value as IList<CustomAttributeArgument>;
if (argumentNames == null)
throw new ArgumentException("Arguments to JSPackedArrayArguments must be strings");
foreach (var attributeArgument in argumentNames) {
var argumentName = attributeArgument.Value as string;
if (argumentName == null)
throw new ArgumentException("Arguments to JSPackedArrayArguments must be strings");
result.Add(argumentName);
}
}
return result.ToArray();
}
return null;
}
public static JSExpression FilterInvocationResult (
MethodReference methodReference, MethodInfo method,
JSExpression result,
ITypeInfoSource typeInfo, TypeSystem typeSystem
) {
if (method == null)
return result;
var resultType = result.GetActualType(typeSystem);
var resultIsPackedArray = PackedArrayUtil.IsPackedArrayType(resultType);
var returnValueAttribute = method.Metadata.GetAttribute("JSIL.Meta.JSPackedArrayReturnValueAttribute");
if (returnValueAttribute != null) {
if (TypeUtil.IsOpenType(resultType)) {
// FIXME: We need to restrict substitution to when the result type is a generic parameter owned by the invocation...
resultType = JSExpression.SubstituteTypeArgs(typeInfo, resultType, methodReference);
}
if (!resultIsPackedArray)
return JSChangeTypeExpression.New(result, PackedArrayUtil.MakePackedArrayType(resultType, returnValueAttribute.Entries.First().Type), typeSystem);
}
return result;
}
public static void CheckReturnValue (MethodInfo method, JSExpression returnValue, TypeSystem typeSystem) {
if (method == null)
return;
var resultType = returnValue.GetActualType(typeSystem);
var resultIsPackedArray = PackedArrayUtil.IsPackedArrayType(resultType);
var returnValueAttribute = method.Metadata.GetAttribute("JSIL.Meta.JSPackedArrayReturnValueAttribute");
if (returnValueAttribute != null) {
if (!resultIsPackedArray)
throw new ArgumentException(
"Return value of method '" + method.Name + "' must be a packed array."
);
} else {
if (resultIsPackedArray)
throw new ArgumentException(
"Return value of method '" + method.Name + "' is a packed array. " +
"For this to be valid you must attach a JSPackedArrayReturnValueAttribute to the method."
);
}
}
public static JSExpression GetItem (TypeReference targetType, TypeInfo targetTypeInfo, JSExpression target, JSExpression index, MethodTypeFactory methodTypes, bool proxy = false) {
var targetGit = (GenericInstanceType)targetType;
var getMethodName = "get_Item";
var getMethod = (JSIL.Internal.MethodInfo)targetTypeInfo.Members.First(
(kvp) => kvp.Key.Name == getMethodName
).Value;
if (proxy)
return new JSNewPackedArrayElementProxy(
target, index, targetGit.GenericArguments[0]
);
// We have to construct a custom reference to the method in order for ILSpy's
// SubstituteTypeArgs method not to explode later on
var getMethodReference = CecilUtil.RebindMethod(getMethod.Member, targetGit, targetGit.GenericArguments[0]);
var result = JSInvocationExpression.InvokeMethod(
new JSType(targetType),
new JSMethod(getMethodReference, getMethod, methodTypes),
target,
new JSExpression[] { index },
constantIfArgumentsAre: proxy
);
return result;
}
public static bool IsElementProxy (JSExpression expression) {
return expression.SelfAndChildrenRecursive.OfType<JSNewPackedArrayElementProxy>().Any() ||
expression.SelfAndChildrenRecursive.OfType<JSRetargetPackedArrayElementProxy>().Any();
}
}
}