-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathMarshalMemberBuilder.cs
545 lines (469 loc) · 20.5 KB
/
MarshalMemberBuilder.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Java.Interop.Expressions;
namespace Java.Interop {
[RequiresDynamicCode (ExpressionRequiresUnreferencedCode)]
[RequiresUnreferencedCode (ExpressionRequiresUnreferencedCode)]
public class MarshalMemberBuilder : JniRuntime.JniMarshalMemberBuilder
{
internal const string ExpressionRequiresUnreferencedCode = "System.Linq.Expression usage may trim away required code.";
public MarshalMemberBuilder ()
{
}
public MarshalMemberBuilder (JniRuntime runtime)
{
if (runtime == null)
throw new ArgumentNullException (nameof (runtime));
OnSetRuntime (runtime);
}
public override LambdaExpression CreateMarshalToManagedExpression (MethodInfo method)
{
if (method == null)
throw new ArgumentNullException (nameof (method));
return CreateMarshalToManagedExpression (method, null, method.DeclaringType);
}
public override IEnumerable<JniNativeMethodRegistration> GetExportedMemberRegistrations (Type declaringType)
{
if (declaringType == null)
throw new ArgumentNullException ("declaringType");
return CreateExportedMemberRegistrationIterator (declaringType);
}
IEnumerable<JniNativeMethodRegistration> CreateExportedMemberRegistrationIterator (Type declaringType)
{
foreach (var method in declaringType.GetTypeInfo ().DeclaredMethods) {
var exports = (JavaCallableAttribute[]) method.GetCustomAttributes (typeof(JavaCallableAttribute), inherit:false);
if (exports == null || exports.Length == 0)
continue;
var export = exports [0];
yield return CreateMarshalToManagedMethodRegistration (export, method, declaringType);
}
}
public JniNativeMethodRegistration CreateMarshalToManagedMethodRegistration (JavaCallableAttribute export, MethodInfo method, Type? type = null)
{
if (export == null)
throw new ArgumentNullException ("export");
if (method == null)
throw new ArgumentNullException ("method");
string signature = GetJniMethodSignature (export, method);
return new JniNativeMethodRegistration () {
Name = GetJniMethodName (export, method),
Signature = signature,
Marshaler = CreateJniMethodMarshaler (method, export, type),
};
}
string GetJniMethodName (JavaCallableAttribute export, MethodInfo method)
{
return "n_" + method.Name;
}
public string GetJniMethodSignature (JavaCallableAttribute export, MethodInfo method)
{
if (export == null)
throw new ArgumentNullException ("export");
if (method == null)
throw new ArgumentNullException ("method");
if (export.Signature != null)
return export.Signature;
return export.Signature = GetJniMethodSignature (method);
}
Delegate CreateJniMethodMarshaler (MethodInfo method, JavaCallableAttribute? export, Type? type)
{
var e = CreateMarshalToManagedExpression (method, export, type);
return e.Compile ();
}
public LambdaExpression CreateMarshalToManagedExpression (MethodInfo method, JavaCallableAttribute? callable, Type? type = null)
{
if (method == null)
throw new ArgumentNullException ("method");
type = type ?? method.DeclaringType;
var methodParameters = method.GetParameters ();
CheckMarshalTypesMatch (method, callable?.Signature, methodParameters);
bool direct = IsDirectMethod (methodParameters);
var jnienv = Expression.Parameter (typeof (IntPtr), direct ? methodParameters [0].Name : "__jnienv");
var context = Expression.Parameter (typeof (IntPtr), direct ? methodParameters [1].Name : (method.IsStatic ? "__class" : "__this"));
var envp = Expression.Variable (typeof (JniTransition), "__envp");
var jvm = Expression.Variable (typeof (JniRuntime), "__jvm");
var vm = Expression.Variable (typeof (JniRuntime.JniValueManager), "__vm");
var envpVars = new List<ParameterExpression> () {
envp,
jvm,
};
int peerableParametersCount = 0;
for (int i = 0; i < methodParameters.Length; ++i) {
var marshaler = GetParameterMarshaler (methodParameters [i]);
if (typeof (IJavaPeerable).GetTypeInfo ().IsAssignableFrom (methodParameters [i].ParameterType.GetTypeInfo ()))
peerableParametersCount ++;
}
bool useVmVariable = (!method.IsStatic || peerableParametersCount > 0) && !direct;
if (useVmVariable)
envpVars.Add (vm);
var envpBody = new List<Expression> () {
Expression.Assign (envp, CreateJniTransition (jnienv)),
};
var waitForGCBridge = typeof(JniRuntime.JniValueManager)
.GetRuntimeMethod (nameof (JniRuntime.JniValueManager.WaitForGCBridgeProcessing), new Type [0]) ??
throw new NotSupportedException ("Could not find JniRuntime.JniValueManager.WaitForGCBridgeProcessing()");
var marshalBody = new List<Expression> () {
Expression.Assign (jvm, GetRuntime ()),
};
if (useVmVariable) {
marshalBody.Add (Expression.Assign (vm, Expression.Property (jvm, "ValueManager")));
marshalBody.Add (Expression.Call (vm, waitForGCBridge));
} else
marshalBody.Add (Expression.Call (Expression.Property (jvm, "ValueManager"), waitForGCBridge));
Expression? self = null;
var marshalerContext = new JniValueMarshalerContext (jvm, useVmVariable ? vm : null);
if (!method.IsStatic) {
var selfMarshaler = Runtime.ValueManager.GetValueMarshaler (type!);
self = selfMarshaler.CreateParameterToManagedExpression (marshalerContext, context, 0, type);
}
var marshalParameters = new List<ParameterExpression> (methodParameters.Length);
var invokeParameters = new List<Expression> (methodParameters.Length);
for (int i = 0; i < methodParameters.Length; ++i) {
var marshaler = GetParameterMarshaler (methodParameters [i]);
ParameterExpression np;
if (i > 1 || !direct)
np = Expression.Parameter (marshaler.MarshalType, methodParameters [i].Name);
else {
if (i == 0)
np = jnienv;
else if (i == 1)
np = context;
else
throw new InvalidOperationException ("Should not be reached.");
}
var p = marshaler.CreateParameterToManagedExpression (marshalerContext, np, methodParameters [i].Attributes, methodParameters [i].ParameterType);
marshalParameters.Add (np);
invokeParameters.Add (p);
}
marshalBody.AddRange (marshalerContext.CreationStatements);
Expression invoke = method.IsStatic
? Expression.Call (method, invokeParameters)
: Expression.Call (self, method, invokeParameters);
Expression? ret = null;
if (method.ReturnType == typeof (void)) {
envpVars.AddRange (marshalerContext.LocalVariables);
marshalBody.Add (invoke);
envpBody.Add (
Expression.TryCatchFinally (
Expression.Block (marshalBody),
CreateDisposeJniEnvironment (envp, marshalerContext.CleanupStatements),
CreateMarshalException (envp, jvm, null)));
} else {
var rmarshaler = GetParameterMarshaler (method.ReturnParameter);
var jniRType = rmarshaler.MarshalType;
var exit = Expression.Label (jniRType, "__exit");
var mret = Expression.Variable (method.ReturnType, "__mret");
envpVars.Add (mret);
marshalBody.Add (Expression.Assign (mret, invoke));
marshalerContext.CreationStatements.Clear ();
ret = rmarshaler.CreateReturnValueFromManagedExpression (marshalerContext, mret);
marshalBody.AddRange (marshalerContext.CreationStatements);
marshalBody.Add (Expression.Return (exit, ret));
envpVars.AddRange (marshalerContext.LocalVariables);
envpBody.Add (
Expression.TryCatchFinally (
Expression.Block (marshalBody),
CreateDisposeJniEnvironment (envp, marshalerContext.CleanupStatements),
CreateMarshalException (envp, jvm, exit)));
envpBody.Add (Expression.Label (exit, Expression.Default (jniRType)));
}
var funcTypeParams = new List<Type> ();
var bodyParams = new List<ParameterExpression> ();
if (!direct) {
funcTypeParams.Add (typeof (IntPtr));
funcTypeParams.Add (typeof (IntPtr));
bodyParams.Add (jnienv);
bodyParams.Add (context);
}
foreach (var p in marshalParameters)
funcTypeParams.Add (p.Type);
var marshalerType = GetMarshalerType (ret?.Type, funcTypeParams, method.DeclaringType);
bodyParams.AddRange (marshalParameters);
var body = Expression.Block (envpVars, envpBody);
return marshalerType == null
? Expression.Lambda (body, bodyParams)
: Expression.Lambda (marshalerType, body, bodyParams);
}
// Keep in sync with ExpressionAssemblyBuilder.GetMarshalMethodDelegateType()
static Type? GetMarshalerType (Type? returnType, List<Type> funcTypeParams, Type? declaringType)
{
// Too many parameters; does a `_JniMarshal_*` type exist in the type's declaring assembly?
funcTypeParams.RemoveRange (0, 2);
var marshalDelegateName = new StringBuilder ();
marshalDelegateName.Append ("_JniMarshal_PP");
foreach (var paramType in funcTypeParams) {
marshalDelegateName.Append (GetJniMarshalDelegateParameterIdentifier (paramType));
}
marshalDelegateName.Append ("_");
if (returnType == null) {
marshalDelegateName.Append ("V");
} else {
marshalDelegateName.Append (GetJniMarshalDelegateParameterIdentifier (returnType));
}
Type? marshalDelegateType = declaringType?.Assembly.GetType (marshalDelegateName.ToString (), throwOnError: false);
if (marshalDelegateType != null) {
return marshalDelegateType;
}
#if !NET
// Punt?; System.Linq.Expressions will automagically produce the needed delegate type.
// Unfortunately, this won't work with jnimarshalmethod-gen.exe.
return marshalDelegateType;
#else // NET
return CreateMarshalDelegateType (marshalDelegateName.ToString (), returnType, funcTypeParams);
#endif // NET
}
#if NET
static object ab_lock = new object ();
static AssemblyBuilder? assemblyBuilder;
static ModuleBuilder? moduleBuilder;
static Type[]? DelegateCtorSignature;
static Dictionary<string, Type>? marshalDelegateTypes;
static Type? CreateMarshalDelegateType (string name, Type? returnType, List<Type> funcTypeParams)
{
lock (ab_lock) {
if (assemblyBuilder == null) {
var aname = new AssemblyName ("jni-marshal-method-delegates");
assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
moduleBuilder = assemblyBuilder.DefineDynamicModule (aname.Name!);
DelegateCtorSignature = new Type[] {
typeof (object),
typeof (IntPtr)
};
marshalDelegateTypes = new (StringComparer.Ordinal);
}
if (marshalDelegateTypes!.TryGetValue (name, out var type)) {
return type;
}
funcTypeParams.Insert (0, typeof (IntPtr));
funcTypeParams.Insert (0, typeof (IntPtr));
var typeBuilder = moduleBuilder!.DefineType (
name,
TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
typeof (MulticastDelegate)
);
const MethodAttributes CtorAttributes = MethodAttributes.RTSpecialName | MethodAttributes.HideBySig | MethodAttributes.Public;
const MethodImplAttributes ImplAttributes = MethodImplAttributes.Runtime | MethodImplAttributes.Managed;
const MethodAttributes InvokeAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual;
typeBuilder.DefineConstructor (CtorAttributes, CallingConventions.Standard, DelegateCtorSignature)
.SetImplementationFlags (ImplAttributes);
typeBuilder.DefineMethod ("Invoke", InvokeAttributes, returnType, funcTypeParams.ToArray ())
.SetImplementationFlags (ImplAttributes);
var marshalDelType = typeBuilder.CreateTypeInfo ();
marshalDelegateTypes.Add (name, marshalDelType);
return marshalDelType;
}
}
#endif // NET
static char GetJniMarshalDelegateParameterIdentifier (Type type)
{
if (type == typeof (bool)) return 'Z';
if (type == typeof (byte)) return 'B';
if (type == typeof (sbyte)) return 'B';
if (type == typeof (char)) return 'C';
if (type == typeof (short)) return 'S';
if (type == typeof (ushort)) return 's';
if (type == typeof (int)) return 'I';
if (type == typeof (uint)) return 'i';
if (type == typeof (long)) return 'J';
if (type == typeof (ulong)) return 'j';
if (type == typeof (float)) return 'F';
if (type == typeof (double)) return 'D';
if (type == typeof (void)) return 'V';
return 'L';
}
void CheckMarshalTypesMatch (MethodInfo method, string? signature, ParameterInfo[] methodParameters)
{
if (signature == null)
return;
var mptypes = JniSignature.GetMarshalParameterTypes (signature).ToList ();
int rpcount = methodParameters.Length;
int len = Math.Min (methodParameters.Length, mptypes.Count);
int start = 0;
if (IsDirectMethod (methodParameters)) {
start += 2;
rpcount -= 2;
}
for (int i = start; i < len; ++i) {
var vm = GetParameterMarshaler (methodParameters [i]);
var jni = vm.MarshalType;
if (mptypes [i] != jni)
throw new ArgumentException (
$"JNI parameter type mismatch. Type `{jni}` != `{mptypes [i]}` at index {i} in `{signature}`.",
"signature");
}
if (mptypes.Count != rpcount)
throw new ArgumentException (
string.Format ("JNI parametr count mismatch: signature contains {0} parameters, method contains {1}.",
mptypes.Count, methodParameters.Length),
nameof (signature));
var jrinfo = JniSignature.GetMarshalReturnType (signature);
var mrvm = GetParameterMarshaler (method.ReturnParameter);
var mrinfo = mrvm.MarshalType;
if (mrinfo != jrinfo)
throw new ArgumentException (
string.Format ("JNI return type mismatch. Type '{0}' != '{1}'.", jrinfo, mrinfo),
nameof (signature));
}
static ConstructorInfo JniTransitionConstructor =
(from c in typeof (JniTransition).GetTypeInfo ().DeclaredConstructors
let p = c.GetParameters ()
where p.Length == 1 && p [0].ParameterType == typeof (IntPtr)
select c)
.First ();
static Expression CreateJniTransition (ParameterExpression jnienv)
{
return Expression.New (
JniTransitionConstructor,
jnienv);
}
static readonly MethodInfo JniRuntime_ExceptionShouldTransitionToJni =
typeof(JniRuntime).GetRuntimeMethod ("ExceptionShouldTransitionToJni", new[] { typeof (Exception) }) ??
throw new NotSupportedException ("Could not find `JniRuntime.ExceptionShouldTransitionToJni()`");
static readonly MethodInfo JniTransition_SetPendingException =
((Action<Exception>) (new JniTransition ().SetPendingException)).Method;
static CatchBlock CreateMarshalException (ParameterExpression envp, ParameterExpression jvm, LabelTarget? exit)
{
var ex = Expression.Variable (typeof (Exception), "__e");
var body = new List<Expression> () {
Expression.Call (envp, JniTransition_SetPendingException, ex),
};
if (exit != null) {
body.Add (Expression.Return (exit, Expression.Default (exit.Type)));
}
var filter = Expression.Call (jvm, JniRuntime_ExceptionShouldTransitionToJni, ex);
return Expression.Catch (ex, Expression.Block (body), filter);
}
static readonly MethodInfo JniTransition_Dispose = ((Action) (new JniTransition ().Dispose)).Method;
static Expression CreateDisposeJniEnvironment (ParameterExpression envp, IList<Expression> cleanup)
{
var disposeTransition = Expression.Call (envp, JniTransition_Dispose);
return Expression.Block (
cleanup.Reverse ().Concat (new[]{ disposeTransition }));;
}
static Expression GetRuntime ()
{
return Expression.Property (null, typeof (JniEnvironment), "Runtime");
}
static MethodInfo FormatterServices_GetUninitializedObject =
#if NETCOREAPP
((Func<Type, object>) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject)
#else // !NETCOREAPP
((Func<Type, object>) System.Runtime.Serialization.FormatterServices.GetUninitializedObject)
#endif // NETCOREAPP
.Method;
static MethodInfo IJavaPeerable_SetPeerReference =
typeof (IJavaPeerable).GetRuntimeMethod ("SetPeerReference", new[]{typeof (JniObjectReference)}) ??
throw new NotSupportedException ("Could not find IJavaPeerable.SetPeerReference()!");
public override Expression<Func<ConstructorInfo, JniObjectReference, object?[]?, object>> CreateConstructActivationPeerExpression (ConstructorInfo constructor)
{
if (constructor == null)
throw new ArgumentNullException (nameof (constructor));
Func<object?, object?[]?, object?> mbi = constructor.Invoke;
var c = Expression.Parameter (typeof (ConstructorInfo), "constructor");
var r = Expression.Parameter (typeof (JniObjectReference), "reference");
var p = Expression.Parameter (typeof (object[]), "parameters");
var t = Expression.Variable (typeof (Type), "type");
var s = Expression.Variable (typeof (object), "self");
var b = Expression.Block (
new []{t, s},
Expression.Assign (t, Expression.Property (c, "DeclaringType")),
Expression.Assign (s, Expression.Call (FormatterServices_GetUninitializedObject, t)),
Expression.Call (Expression.Convert (s, typeof (IJavaPeerable)), IJavaPeerable_SetPeerReference, r),
Expression.Call (c, mbi.GetMethodInfo (), s, p),
s);
return Expression.Lambda<Func<ConstructorInfo, JniObjectReference, object?[]?, object>> (b, new []{c, r, p});
}
public static string GetMarshalMethodName (string name, string signature)
{
if (name == null)
throw new ArgumentNullException (nameof (name));
if (signature == null)
throw new ArgumentNullException (nameof (signature));
var idx1 = signature.IndexOf ('(');
var idx2 = signature.IndexOf (')');
var arguments = signature;
if (idx1 >= 0 && idx2 >= idx1)
arguments = arguments.Substring (idx1 + 1, idx2 - idx1 - 1);
return $"n_{name}{(string.IsNullOrEmpty (arguments) ? "" : "_")}{arguments?.Replace ('/', '_')?.Replace (';', '_')}";
}
}
static class JniSignature {
public static Type? GetMarshalReturnType (string signature)
{
int idx = signature.LastIndexOf (')') + 1;
return ExtractMarshalTypeFromSignature (signature, ref idx);
}
public static IEnumerable<Type> GetMarshalParameterTypes (string signature)
{
if (signature.StartsWith ("(", StringComparison.Ordinal)) {
int e = signature.IndexOf (")", StringComparison.Ordinal);
signature = signature.Substring (1, e >= 0 ? e-1 : signature.Length-1);
}
int i = 0;
Type? t;
while ((t = ExtractMarshalTypeFromSignature (signature, ref i)) != null)
yield return t;
}
// as per: http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/types.html
static Type? ExtractMarshalTypeFromSignature (string signature, ref int index)
{
#if false
if (index >= signature.Length)
return null;
var i = index++;
switch (signature [i]) {
case 'B': return typeof (sbyte);
case 'C': return typeof (char);
case 'D': return typeof (double);
case 'F': return typeof (float);
case 'I': return typeof (int);
case 'J': return typeof (long);
case 'S': return typeof (short);
case 'V': return typeof (void);
case 'Z': return typeof (bool);
case '[':
case 'L': return typeof (IntPtr);
default:
throw new ArgumentException ("Unknown JNI Type '" + signature [i] + "' within: " + signature, "signature");
}
#else
if (index >= signature.Length)
return null;
var i = index++;
switch (signature [i]) {
case 'B': return typeof (sbyte);
case 'C': return typeof (char);
case 'D': return typeof (double);
case 'F': return typeof (float);
case 'I': return typeof (int);
case 'J': return typeof (long);
case 'S': return typeof (short);
case 'V': return typeof (void);
case 'Z': return typeof (bool);
case '[':
++i;
if (i >= signature.Length)
throw new ArgumentException ("Missing array type after '[' at index " + i + " in: " + signature, "signature");
ExtractMarshalTypeFromSignature (signature, ref index);
return typeof (IntPtr);
case 'L': {
var e = signature.IndexOf (";", index, StringComparison.Ordinal);
if (e <= 0)
throw new ArgumentException ("Missing reference type after 'L' at index " + i + "in: " + signature, "signature");
index = e + 1;
return typeof (IntPtr);
}
default:
throw new ArgumentException ("Unknown JNI Type '" + signature [i] + "' within: " + signature, "signature");
}
#endif
}
}
}