IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MethodHandleUtil.root.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2011-2014 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25
26#if IMPORTER
28
29using Type = IKVM.Reflection.Type;
30#else
31using System.Reflection;
32using System.Reflection.Emit;
33#endif
34
35namespace IKVM.Runtime
36{
37
38 partial class MethodHandleUtil
39 {
40
41 internal const int MaxArity = 8;
42
43 readonly RuntimeContext context;
44
45 readonly Type typeofMHA;
46 readonly Type[] typeofMHV;
47 readonly Type[] typeofMH;
48
53 {
54 this.context = context ?? throw new ArgumentNullException(nameof(context));
55
56 typeofMHA = context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHA`8").AsReflection();
57 typeofMHV = new Type[] {
58 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV").AsReflection(),
59 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`1").AsReflection(),
60 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`2").AsReflection(),
61 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`3").AsReflection(),
62 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`4").AsReflection(),
63 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`5").AsReflection(),
64 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`6").AsReflection(),
65 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`7").AsReflection(),
66 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MHV`8").AsReflection(),
67 };
68 typeofMH = new Type[] {
69 null,
70 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`1").AsReflection(),
71 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`2").AsReflection(),
72 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`3").AsReflection(),
73 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`4").AsReflection(),
74 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`5").AsReflection(),
75 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`6").AsReflection(),
76 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`7").AsReflection(),
77 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`8").AsReflection(),
78 context.Resolver.ResolveRuntimeType("IKVM.Runtime.MH`9").AsReflection(),
79 };
80 }
81
82 internal bool IsPackedArgsContainer(Type type)
83 {
84 return type.IsGenericType && type.GetGenericTypeDefinition() == typeofMHA;
85 }
86
87 internal Type CreateMethodHandleDelegateType(RuntimeJavaType[] args, RuntimeJavaType ret)
88 {
89 Type[] typeArgs = new Type[args.Length];
90 for (int i = 0; i < args.Length; i++)
91 {
92 typeArgs[i] = args[i].TypeAsSignatureType;
93 }
94 return CreateDelegateType(typeArgs, ret.TypeAsSignatureType);
95 }
96
97 internal Type CreateMemberWrapperDelegateType(RuntimeJavaType[] args, RuntimeJavaType ret)
98 {
99 Type[] typeArgs = new Type[args.Length];
100 for (int i = 0; i < args.Length; i++)
101 {
102 typeArgs[i] = AsBasicType(args[i]);
103 }
104 return CreateDelegateType(typeArgs, AsBasicType(ret));
105 }
106
107 Type CreateDelegateType(Type[] types, Type retType)
108 {
109 if (types.Length == 0 && retType == context.Types.Void)
110 {
111 return typeofMHV[0];
112 }
113 else if (types.Length > MaxArity)
114 {
115 int arity = types.Length;
116 int remainder = (arity - 8) % 7;
117 int count = (arity - 8) / 7;
118 if (remainder == 0)
119 {
120 remainder = 7;
121 count--;
122 }
123
124 var last = typeofMHA.MakeGenericType(SubArray(types, types.Length - 8, 8));
125 for (int i = 0; i < count; i++)
126 {
127 var temp = SubArray(types, types.Length - 8 - 7 * (i + 1), 8);
128 temp[7] = last;
129 last = typeofMHA.MakeGenericType(temp);
130 }
131
132 types = SubArray(types, 0, remainder + 1);
133 types[remainder] = last;
134 }
135
136 if (retType == context.Types.Void)
137 {
138 return typeofMHV[types.Length].MakeGenericType(types);
139 }
140 else
141 {
142 types = ArrayUtil.Concat(types, retType);
143 return typeofMH[types.Length].MakeGenericType(types);
144 }
145 }
146
147 Type[] SubArray(Type[] inArray, int start, int length)
148 {
149 var outArray = new Type[length];
150 Array.Copy(inArray, start, outArray, 0, length);
151 return outArray;
152 }
153
154 internal Type AsBasicType(RuntimeJavaType tw)
155 {
156 if (tw == context.PrimitiveJavaTypeFactory.BOOLEAN || tw == context.PrimitiveJavaTypeFactory.BYTE || tw == context.PrimitiveJavaTypeFactory.CHAR || tw == context.PrimitiveJavaTypeFactory.SHORT || tw == context.PrimitiveJavaTypeFactory.INT)
157 return context.Types.Int32;
158 else if (tw == context.PrimitiveJavaTypeFactory.LONG || tw == context.PrimitiveJavaTypeFactory.FLOAT || tw == context.PrimitiveJavaTypeFactory.DOUBLE || tw == context.PrimitiveJavaTypeFactory.VOID)
159 return tw.TypeAsSignatureType;
160 else
161 return context.Types.Object;
162 }
163
164 internal bool HasOnlyBasicTypes(RuntimeJavaType[] args, RuntimeJavaType ret)
165 {
166 foreach (var tw in args)
167 if (!IsBasicType(tw))
168 return false;
169
170 return IsBasicType(ret);
171 }
172
173 bool IsBasicType(RuntimeJavaType tw)
174 {
175 return tw == context.PrimitiveJavaTypeFactory.INT
176 || tw == context.PrimitiveJavaTypeFactory.LONG
177 || tw == context.PrimitiveJavaTypeFactory.FLOAT
178 || tw == context.PrimitiveJavaTypeFactory.DOUBLE
179 || tw == context.PrimitiveJavaTypeFactory.VOID
180 || tw == context.JavaBase.TypeOfJavaLangObject;
181 }
182
183 internal int SlotCount(RuntimeJavaType[] parameters)
184 {
185 int count = 0;
186 for (int i = 0; i < parameters.Length; i++)
187 {
188 count += parameters[i].IsWidePrimitive ? 2 : 1;
189 }
190 return count;
191 }
192
193 }
194
195}
IKVM.Reflection.Type Type
MethodHandleUtil(RuntimeContext context)
Initializes a new instance.
Maintains services relevant to an instane of the IKVM runtime.
ISymbolResolver Resolver
Gets the ISymbolResolver associated with this instance of the runtime.