-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathJavaPrimitiveArrays.tt
More file actions
298 lines (254 loc) · 10.8 KB
/
JavaPrimitiveArrays.tt
File metadata and controls
298 lines (254 loc) · 10.8 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Java.Interop.Expressions;
using System.Linq.Expressions;
namespace Java.Interop {
<#
var arrayTypeInfo = new[]{
new { JniType = "Z", JniMarshalType = "Boolean", ManagedType = "Boolean",TypeModifier = "Boolean" },
new { JniType = "B", JniMarshalType = "Byte", ManagedType = "SByte", TypeModifier = "SByte" },
new { JniType = "C", JniMarshalType = "Char", ManagedType = "Char", TypeModifier = "Char" },
new { JniType = "S", JniMarshalType = "Short", ManagedType = "Int16", TypeModifier = "Int16" },
new { JniType = "I", JniMarshalType = "Int", ManagedType = "Int32", TypeModifier = "Int32" },
new { JniType = "J", JniMarshalType = "Long", ManagedType = "Int64", TypeModifier = "Int64" },
new { JniType = "F", JniMarshalType = "Float", ManagedType = "Single", TypeModifier = "Single" },
new { JniType = "D", JniMarshalType = "Double", ManagedType = "Double", TypeModifier = "Double" },
};
#>
partial class JniRuntime {
partial class ReflectionJniTypeManager {
readonly struct JniPrimitiveArrayInfo {
public readonly JniTypeSignature JniTypeSignature;
public readonly Type PrimitiveType;
public readonly Type[] ArrayTypes;
public JniPrimitiveArrayInfo (string jniSimpleReference, Type primitiveType, params Type[] arrayTypes)
{
JniTypeSignature = new JniTypeSignature (jniSimpleReference, arrayRank: 1, keyword: true);
PrimitiveType = primitiveType;
ArrayTypes = arrayTypes;
}
}
static readonly JniPrimitiveArrayInfo[] JniPrimitiveArrayTypes = new JniPrimitiveArrayInfo[]{
<#
foreach (var type in arrayTypeInfo) {
#>
new ("<#= type.JniType #>", typeof (<#= type.ManagedType #>), typeof (<#= type.ManagedType #>[]), typeof (JavaArray<<#= type.ManagedType #>>), typeof (JavaPrimitiveArray<<#= type.ManagedType #>>), typeof (Java<#= type.ManagedType #>Array)),
<#
}
#>
};
static bool GetBuiltInTypeArraySignature (Type type, ref JniTypeSignature signature)
{
foreach (var e in JniPrimitiveArrayTypes) {
if (Array.IndexOf (e.ArrayTypes, type) < 0)
continue;
signature = e.JniTypeSignature;
return true;
}
signature = default;
return false;
}
}
static readonly Lazy<KeyValuePair<Type, JniValueMarshaler>[]> JniPrimitiveArrayMarshalers = new Lazy<KeyValuePair<Type, JniValueMarshaler>[]> (InitJniPrimitiveArrayMarshalers);
static KeyValuePair<Type, JniValueMarshaler>[] InitJniPrimitiveArrayMarshalers ()
{
return new [] {
<#
foreach (var info in arrayTypeInfo) {
#>
new KeyValuePair<Type, JniValueMarshaler>(typeof (<#= info.ManagedType #>[]), Java<#= info.TypeModifier #>Array.ArrayMarshaler),
new KeyValuePair<Type, JniValueMarshaler>(typeof (JavaArray<<#= info.ManagedType #>>), Java<#= info.TypeModifier #>Array.ArrayMarshaler),
new KeyValuePair<Type, JniValueMarshaler>(typeof (JavaPrimitiveArray<<#= info.ManagedType #>>), Java<#= info.TypeModifier #>Array.ArrayMarshaler),
new KeyValuePair<Type, JniValueMarshaler>(typeof (Java<#= info.TypeModifier #>Array), Java<#= info.TypeModifier #>Array.ArrayMarshaler),
<#
}
#>
};
}
}
<#
foreach (var info in arrayTypeInfo) {
#>
partial class JniEnvironment {
partial class Arrays {
public static Java<#= info.TypeModifier #>Array? CreateMarshal<#= info.TypeModifier #>Array (System.Collections.Generic.IEnumerable<<#= info.ManagedType #>>? value)
{
if (value == null) {
return null;
}
if (value is Java<#= info.TypeModifier #>Array c) {
return c;
}
return new Java<#= info.TypeModifier #>Array (value) {
forMarshalCollection = true,
};
}
}
}
public sealed class Jni<#= info.TypeModifier #>ArrayElements : JniArrayElements {
JniObjectReference arrayHandle;
internal unsafe Jni<#= info.TypeModifier #>ArrayElements (JniObjectReference arrayHandle, <#= info.ManagedType #>* elements, int size)
: base ((IntPtr) elements, size)
{
this.arrayHandle = arrayHandle;
}
public new unsafe <#= info.ManagedType #>* Elements {
get {return (<#= info.ManagedType #>*) base.Elements;}
}
public ref <#= info.ManagedType #> this [int index] {
get {
if (IsDisposed)
throw new ObjectDisposedException (GetType ().FullName);
unsafe {
return ref Elements [index];
}
}
}
protected override unsafe void Synchronize (JniReleaseArrayElementsMode releaseMode)
{
JniEnvironment.Arrays.Release<#= info.JniMarshalType #>ArrayElements (arrayHandle, Elements, releaseMode);
}
}
[JniTypeSignature ("<#= info.JniType #>", ArrayRank=1, IsKeyword=true, GenerateJavaPeer=false)]
public sealed class Java<#= info.TypeModifier #>Array : JavaPrimitiveArray<<#= info.ManagedType #>> {
internal static readonly ValueMarshaler ArrayMarshaler = new ValueMarshaler ();
public Java<#= info.TypeModifier #>Array (ref JniObjectReference handle, JniObjectReferenceOptions options)
: base (ref handle, options)
{
}
public unsafe Java<#= info.TypeModifier #>Array (int length)
: base (ref *InvalidJniObjectReference, JniObjectReferenceOptions.None)
{
var peer = JniEnvironment.Arrays.New<#= info.JniMarshalType #>Array (CheckLength (length));
Construct (ref peer, JniObjectReferenceOptions.CopyAndDispose);
}
public Java<#= info.TypeModifier #>Array (System.Collections.Generic.IList<<#= info.ManagedType #>> value)
: this (CheckLength (value))
{
CopyFrom (ToArray (value), 0, 0, value.Count);
}
public Java<#= info.TypeModifier #>Array (System.Collections.Generic.IEnumerable<<#= info.ManagedType #>> value)
: this (ToArray (value))
{
}
protected override JniArrayElements CreateElements ()
{
return GetElements ();
}
public new unsafe Jni<#= info.TypeModifier #>ArrayElements GetElements ()
{
if (!PeerReference.IsValid)
throw JniEnvironment.CreateObjectDisposedException (this);
var elements = JniEnvironment.Arrays.Get<#= info.JniMarshalType #>ArrayElements (PeerReference, null);
if (elements == null)
throw new InvalidOperationException ("`JniEnvironment.Arrays.Get<#= info.JniMarshalType #>ArrayElements()` returned NULL!");
return new Jni<#= info.TypeModifier #>ArrayElements (PeerReference, elements, Length*sizeof (<#= info.ManagedType #>));
}
public override unsafe int IndexOf (<#= info.ManagedType #> item)
{
int len = Length;
if (len == 0)
return -1;
using (var e = GetElements ()) {
Debug.Assert (e != null, "Java.<#= info.TypeModifier #>.Array.GetElements() returned null! OOM?");
if (e == null)
return -1; // IList<T>.IndexOf() documents no exceptions. :-/
for (int i = 0; i < len; ++i) {
if (e [i] == item)
return i;
}
}
return -1;
}
public override unsafe void Clear ()
{
int len = Length;
using (var e = GetElements ()) {
for (int i = 0; i < len; ++i) {
e [i] = default (<#= info.ManagedType #>);
}
}
}
public override unsafe void CopyTo (int sourceIndex, <#= info.ManagedType #>[] destinationArray, int destinationIndex, int length)
{
if (destinationArray == null)
throw new ArgumentNullException (nameof (destinationArray));
CheckArrayCopy (sourceIndex, Length, destinationIndex, destinationArray.Length, length);
if (destinationArray.Length == 0)
return;
fixed (<#= info.ManagedType #>* b = destinationArray)
JniEnvironment.Arrays.Get<#= info.JniMarshalType #>ArrayRegion (PeerReference, sourceIndex, length, (b+destinationIndex));
}
public override unsafe void CopyFrom (<#= info.ManagedType #>[] sourceArray, int sourceIndex, int destinationIndex, int length)
{
if (sourceArray == null)
throw new ArgumentNullException (nameof (sourceArray));
CheckArrayCopy (sourceIndex, sourceArray.Length, destinationIndex, Length, length);
if (sourceArray.Length == 0)
return;
fixed (<#= info.ManagedType #>* b = sourceArray)
JniEnvironment.Arrays.Set<#= info.JniMarshalType #>ArrayRegion (PeerReference, destinationIndex, length, (b+sourceIndex));
}
internal override bool TargetTypeIsCurrentType (Type? targetType)
{
return base.TargetTypeIsCurrentType (targetType) ||
typeof (JavaPrimitiveArray<<#= info.ManagedType #>>) == targetType ||
typeof (Java<#= info.TypeModifier #>Array) == targetType;
}
public static object? CreateMarshaledValue (
IntPtr handle,
[DynamicallyAccessedMembers (ConstructorsAndInterfaces)]
Type? targetType)
{
return ArrayMarshaler.CreateValue (handle, targetType);
}
internal sealed class ValueMarshaler : JniValueMarshaler<IList<<#= info.TypeModifier #>>> {
public override IList<<#= info.TypeModifier #>> CreateGenericValue (
ref JniObjectReference reference,
JniObjectReferenceOptions options,
[DynamicallyAccessedMembers (ConstructorsAndInterfaces)]
Type? targetType)
{
return JavaArray<<#= info.TypeModifier #>>.CreateValue (
ref reference,
options,
targetType,
(ref JniObjectReference h, JniObjectReferenceOptions o) => new Java<#= info.TypeModifier #>Array (ref h, o));
}
public override JniValueMarshalerState CreateGenericObjectReferenceArgumentState ([MaybeNull] IList<<#= info.TypeModifier #>> value, ParameterAttributes synchronize)
{
return JavaArray<<#= info.TypeModifier #>>.CreateArgumentState (value, synchronize, (list, copy) => {
var a = copy
? new Java<#= info.TypeModifier #>Array (list)
: new Java<#= info.TypeModifier #>Array (list.Count);
a.forMarshalCollection = true;
return a;
});
}
public override void DestroyGenericArgumentState ([AllowNull] IList<<#= info.TypeModifier #>> value, ref JniValueMarshalerState state, ParameterAttributes synchronize)
{
JavaArray<<#= info.ManagedType #>>.DestroyArgumentState<Java<#= info.TypeModifier #>Array> (value, ref state, synchronize);
}
[RequiresDynamicCode (ExpressionRequiresUnreferencedCode)]
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
public override Expression CreateParameterToManagedExpression (JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize = 0, Type? targetType = null)
{
Func<IntPtr, Type?, object?> m = Java<#= info.TypeModifier #>Array.CreateMarshaledValue;
var call = Expression.Call (m.GetMethodInfo (), sourceValue, Expression.Constant (targetType, typeof (Type)));
return targetType == null
? (Expression) call
: Expression.Convert (call, targetType);
}
}
}
<# } #>
}