-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGenerator.cs
More file actions
1408 lines (1244 loc) · 45.1 KB
/
Generator.cs
File metadata and controls
1408 lines (1244 loc) · 45.1 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Xamarin.Java.Interop
{
static class StringCoda {
public static string FixupType (this string t)
{
return t.Replace ("*", "Ptr").Replace ("[]", "Array").Replace (" ", "");
}
}
partial class Generator
{
static string jnienv_g_c;
static string jnienv_g_h;
static string jnienv_g_cs;
public static int Main (string [] args)
{
jnienv_g_c = "JniEnvironment.g.c";
jnienv_g_h = "JniEnvironment.g.h";
jnienv_g_cs = "JniEnvironment.g.cs";
if (args.Length > 0)
jnienv_g_cs = args [0];
if (args.Length > 1) {
jnienv_g_c = args [1];
if (jnienv_g_c != "-") {
jnienv_g_h = Path.Combine (Path.GetDirectoryName (jnienv_g_c), $"{Path.GetFileNameWithoutExtension(jnienv_g_c)}-api.h");
} else {
jnienv_g_h = "-";
}
}
if (args.Length > 2) {
jnienv_g_h = args [2];
}
try {
using (TextWriter w = new StringWriter ()) {
w.NewLine = "\n";
GenerateFile (w);
string content = w.ToString ();
if (jnienv_g_cs == "-")
Console.WriteLine (content);
else
File.WriteAllText (jnienv_g_cs, content);
}
using (TextWriter sw = new StringWriter ()) {
using (TextWriter hw = new StringWriter ()) {
sw.NewLine = "\n";
GenerateNativeLibSource (sw, hw, jnienv_g_h);
string sourceContent = sw.ToString ();
string headerContent = hw.ToString ();
if (jnienv_g_c == "-" || jnienv_g_cs == "-") {
Console.WriteLine (headerContent);
Console.WriteLine ();
Console.WriteLine (sourceContent);
} else {
File.WriteAllText (jnienv_g_h, headerContent);
File.WriteAllText (jnienv_g_c, sourceContent);
}
}}
return 0;
} catch (Exception ex) {
Console.WriteLine (ex);
return 1;
}
}
static string Escape (string value)
{
switch (value) {
case "object":
case "string":
case "ref":
return "@" + value;
default: return value;
}
}
static void GenerateFile (TextWriter o)
{
o.WriteLine ("// Generated file; DO NOT EDIT!");
o.WriteLine ("//");
o.WriteLine ("// To make changes, edit monodroid/tools/jnienv-gen-interop and rerun");
o.WriteLine ();
o.WriteLine ("#if !FEATURE_JNIENVIRONMENT_SAFEHANDLES && !FEATURE_JNIENVIRONMENT_JI_INTPTRS && !FEATURE_JNIENVIRONMENT_JI_PINVOKES && !FEATURE_JNIENVIRONMENT_XA_INTPTRS && !FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS");
o.WriteLine ("#define FEATURE_JNIENVIRONMENT_JI_PINVOKES");
o.WriteLine ("#endif // !FEATURE_JNIENVIRONMENT_SAFEHANDLES && !FEATURE_JNIENVIRONMENT_JI_INTPTRS && !FEATURE_JNIENVIRONMENT_JI_PINVOKES && !FEATURE_JNIENVIRONMENT_XA_INTPTRS");
o.WriteLine ();
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_JI_INTPTRS");
o.WriteLine ("#define _NAMESPACE_PER_HANDLE");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_JI_INTPTRS");
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_JI_PINVOKES");
o.WriteLine ("#define _NAMESPACE_PER_HANDLE");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_JI_PINVOKES");
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_XA_INTPTRS");
o.WriteLine ("#define _NAMESPACE_PER_HANDLE");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES && FEATURE_JNIENVIRONMENT_XA_INTPTRS");
o.WriteLine ();
o.WriteLine ("using System;");
o.WriteLine ("using System.Linq;");
o.WriteLine ("using System.Runtime.ExceptionServices;");
o.WriteLine ("using System.Runtime.InteropServices;");
o.WriteLine ("using System.Threading;");
o.WriteLine ();
o.WriteLine ("using Java.Interop;");
o.WriteLine ();
o.WriteLine ("using JNIEnvPtr = System.IntPtr;");
o.WriteLine ();
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS");
o.WriteLine ("\tusing jinstanceFieldID = System.IntPtr;");
o.WriteLine ("\tusing jstaticFieldID = System.IntPtr;");
o.WriteLine ("\tusing jinstanceMethodID = System.IntPtr;");
o.WriteLine ("\tusing jstaticMethodID = System.IntPtr;");
o.WriteLine ("\tusing jobject = System.IntPtr;");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS");
o.WriteLine ();
o.WriteLine ("namespace Java.Interop {");
GenerateJniNativeInterface (o);
o.WriteLine ("}");
WriteSection (o, HandleStyle.SafeHandle, "FEATURE_JNIENVIRONMENT_SAFEHANDLES", "Java.Interop.SafeHandles");
WriteSection (o, HandleStyle.JIIntPtr, "FEATURE_JNIENVIRONMENT_JI_INTPTRS", "Java.Interop.JIIntPtrs");
WriteSection (o, HandleStyle.JIIntPtrPinvokeWithErrors, "FEATURE_JNIENVIRONMENT_JI_PINVOKES", "Java.Interop.JIPinvokes");
WriteSection (o, HandleStyle.XAIntPtr, "FEATURE_JNIENVIRONMENT_XA_INTPTRS", "Java.Interop.XAIntPtrs");
WriteSection (o, HandleStyle.JIFunctionPtrWithErrors, "FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS", "Java.Interop.JIFunctionPointers");
}
static void WriteSection (TextWriter o, HandleStyle style, string define, string specificNamespace)
{
o.WriteLine ("#if {0}", define);
o.WriteLine ("namespace");
o.WriteLine ("#if _NAMESPACE_PER_HANDLE");
o.WriteLine ("\t{0}", specificNamespace);
o.WriteLine ("#else");
o.WriteLine ("\tJava.Interop");
o.WriteLine ("#endif");
o.WriteLine ("{");
o.WriteLine ();
if (style != HandleStyle.JIIntPtrPinvokeWithErrors && style != HandleStyle.JIFunctionPtrWithErrors) {
GenerateDelegates (o, style);
o.WriteLine ();
}
GenerateTypes (o, style);
o.WriteLine ();
switch (style) {
case HandleStyle.JIIntPtr:
case HandleStyle.SafeHandle:
case HandleStyle.XAIntPtr:
GenerateJniNativeInterfaceInvoker (o, style);
break;
}
o.WriteLine ("}");
o.WriteLine ("#endif // {0}", define);
}
static void GenerateDelegates (TextWriter o, HandleStyle style)
{
created_delegates = new HashSet<string> ();
foreach (var e in JNIEnvEntries) {
CreateDelegate (o, e, style);
}
}
static void GenerateJniNativeInterface (TextWriter o)
{
o.WriteLine ("#pragma warning disable 0649 // Field is assigned to, and will always have its default value `null`; ignore as it'll be set in native code.");
o.WriteLine ("#pragma warning disable 0169 // Field never used; ignore since these fields make the structure have the right layout.");
o.WriteLine ();
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS");
o.WriteLine ("\t[StructLayout (LayoutKind.Sequential)]");
o.WriteLine ("\tpartial struct JniNativeInterfaceStruct {");
o.WriteLine ();
int maxName = JNIEnvEntries.Max (e => e.Name.Length);
for (int i = 0; i < 4; i++)
o.WriteLine ("\t\tprivate IntPtr reserved{0}; // void*", i);
foreach (var e in JNIEnvEntries) {
o.WriteLine ("\t\tpublic IntPtr {0};{1} // {2}", e.Name, new string (' ', maxName - e.Name.Length), e.Prototype);
}
o.WriteLine ("\t}");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS");
o.WriteLine ();
o.WriteLine ("#if FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS");
o.WriteLine ("\t[StructLayout (LayoutKind.Sequential)]");
o.WriteLine ("\tunsafe partial struct JNIEnv {");
for (int i = 0; i < 4; i++)
o.WriteLine ("\t\tprivate IntPtr reserved{0}; // void*", i);
foreach (var e in JNIEnvEntries) {
if (e.Parameters.Length > 0 &&
"va_list" == e.Parameters [e.Parameters.Length-1].Type.GetManagedType (HandleStyle.JIFunctionPtrWithErrors, isReturn: false, isPinvoke: true)) {
o.WriteLine ("\t\tpublic IntPtr {0};{1} // {2}", e.Name, new string (' ', maxName - e.Name.Length), e.Prototype);
continue;
}
o.Write ("\t\tpublic delegate* unmanaged <IntPtr /* env */");
foreach (var p in e.Parameters) {
o.Write (", ");
o.Write (p.Type.GetMarshalType (HandleStyle.JIFunctionPtrWithErrors, isReturn: false, isPinvoke: true));
o.Write ($" /* {p.Name} */");
}
o.Write (", ");
o.Write (e.ReturnType.GetMarshalType (HandleStyle.JIFunctionPtrWithErrors, isReturn: true, isPinvoke: true));
o.WriteLine ($"> {e.Name};");
}
o.WriteLine ("\t}");
o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS");
o.WriteLine ();
o.WriteLine ("#pragma warning restore 0169");
o.WriteLine ("#pragma warning restore 0649");
}
static string Initialize (JniFunction e, string prefix, string delegateType)
{
return string.Format ("{0}{1} = ({2}) Marshal.GetDelegateForFunctionPointer (env.{1}, typeof ({2}));",
prefix, e.Name, delegateType);
}
static void GenerateJniNativeInterfaceInvoker (TextWriter o, HandleStyle style)
{
o.WriteLine ("\tpartial class JniEnvironmentInvoker {");
o.WriteLine ();
o.WriteLine ("\t\tinternal JniNativeInterfaceStruct env;");
o.WriteLine ();
o.WriteLine ("\t\tpublic unsafe JniEnvironmentInvoker (JniNativeInterfaceStruct* p)");
o.WriteLine ("\t\t{");
o.WriteLine ("\t\t\tenv = *p;");
foreach (var e in JNIEnvEntries) {
if (!e.Prebind)
continue;
var d = e.GetDelegateTypeName (style);
if (e.GetDelegateTypeName (style) == null)
continue;
o.WriteLine ("\t\t\t{0}", Initialize (e, "", d));
}
o.WriteLine ("\t\t}");
o.WriteLine ();
foreach (var e in JNIEnvEntries) {
var d = e.GetDelegateTypeName (style);
if (d == null)
continue;
o.WriteLine ();
if (e.Prebind)
o.WriteLine ("\t\tpublic readonly {0} {1};\n", d, e.Name);
else {
o.WriteLine ("\t\t{0} _{1};", d, e.Name);
o.WriteLine ("\t\tpublic {0} {1} {{", d, e.Name);
o.WriteLine ("\t\t\tget {");
o.WriteLine ("\t\t\t\tif (_{0} == null)\n\t\t\t\t\t{1}", e.Name, Initialize (e, "_", d));
o.WriteLine ("\t\t\t\treturn _{0};\n\t\t\t}}", e.Name);
o.WriteLine ("\t\t}");
}
}
o.WriteLine ("\t}");
}
static HashSet<string> created_delegates = new HashSet<string> ();
static void CreateDelegate (TextWriter o, JniFunction entry, HandleStyle style)
{
StringBuilder builder = new StringBuilder ();
bool has_char_array = false;
string name = entry.GetDelegateTypeName (style);
if (name == null)
return;
builder.AppendFormat ("\tunsafe delegate {0} {1} ({2} env", entry.GetMarshalReturnType (style), name, GetJniEnvironmentPointerType (style));
for (int i = 0; i < entry.Parameters.Length; i++) {
if (i >= 0) {
builder.Append (", ");
builder.AppendFormat ("{0} {1}",
entry.Parameters [i].Type.GetMarshalType (style, isReturn: false, isPinvoke: true),
Escape (entry.Parameters [i].Name));
}
var ptype = entry.Parameters [i].Type.GetManagedType (style, isReturn: false, isPinvoke: true);
if (ptype == "va_list")
return;
if (ptype == "char[]")
has_char_array = true;
}
builder.Append (");");
if (created_delegates.Contains (name))
return;
created_delegates.Add (name);
if (entry.Name == "NewString" || has_char_array)
o.WriteLine ("\t[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl, CharSet=CharSet.Unicode)]");
o.WriteLine (builder.ToString ());
}
static string GetJniEnvironmentPointerType (HandleStyle style)
{
return "JNIEnvPtr";
}
static void GenerateTypes (TextWriter o, HandleStyle style)
{
if (style == HandleStyle.JIIntPtrPinvokeWithErrors) {
GenerateNativeMethods (o, style);
}
if (style == HandleStyle.JIFunctionPtrWithErrors) {
GenerateJniNativeMethods (o, style);
}
var visibilities = new Dictionary<string, string> {
{ ArrayOperationsCategory, "public" },
{ ClassesCategory, "public" },
{ ExceptionsCategory, "public" },
{ InstanceFieldsCategory, "public" },
{ InstanceMethodsCategory, "public" },
{ MonitorOperationsCategory, "public" },
{ NIOSupportCategory, "public" },
{ ObjectOperationsCategory, "public" },
{ ReferencesCatgeory, "public" },
{ StaticFieldsCategory, "public" },
{ StaticMethodsCategory, "public" },
{ StringOperationsCategory, "public" },
};
o.WriteLine ("\tpartial class JniEnvironment {");
foreach (var t in JNIEnvEntries
.Select (e => e.DeclaringType ?? "JniEnvironment")
.Distinct ()
.OrderBy (t => t)) {
string visibility;
if (!visibilities.TryGetValue (t, out visibility))
visibility = "internal";
GenerateJniEnv (o, t, visibility, style);
}
o.WriteLine ("\t}");
}
static void GenerateNativeMethods (TextWriter o, HandleStyle style)
{
o.WriteLine ("\tstatic partial class NativeMethods {");
o.WriteLine ();
o.WriteLine ("\t\tconst string JavaInteropLib = \"java-interop\";");
foreach (var entry in JNIEnvEntries) {
if (entry.Parameters == null)
continue;
if (entry.IsPrivate || entry.CustomWrapper)
continue;
o.WriteLine ();
o.WriteLine ("\t\t[DllImport (JavaInteropLib, CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]");
o.WriteLine ("\t\tinternal static extern unsafe {0} {1} (IntPtr jnienv{2}{3}{4});",
entry.ReturnType.GetMarshalType (style, isReturn: true, isPinvoke: true),
GetPinvokeName (entry.Name),
entry.Throws ? ", out IntPtr thrown" : "",
entry.Parameters.Length != 0 ? ", " : "",
string.Join (", ", entry.Parameters.Select (p => string.Format ("{0} {1}", p.Type.GetMarshalType (style, isReturn: false, isPinvoke: true), Escape (p.Name)))));
}
o.WriteLine ("\t}");
o.WriteLine ();
}
static void GenerateJniNativeMethods (TextWriter o, HandleStyle style)
{
o.WriteLine ("\tstatic partial class JniNativeMethods {");
o.WriteLine ();
foreach (var entry in JNIEnvEntries) {
if (entry.Parameters == null)
continue;
if (entry.IsPrivate || entry.CustomWrapper)
continue;
var returnType = entry.ReturnType.GetMarshalType (HandleStyle.JIFunctionPtrWithErrors, isReturn: true, isPinvoke: true);
o.WriteLine ();
o.WriteLine ("\t\t[System.Runtime.CompilerServices.MethodImpl (System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]");
o.Write ("\t\tinternal static unsafe ");
o.Write (entry.ReturnType.GetMarshalType (HandleStyle.JIFunctionPtrWithErrors, isReturn: true, isPinvoke: true));
o.Write ($" {entry.Name} (IntPtr env");
foreach (var p in entry.Parameters) {
o.Write (", ");
o.Write (p.Type.GetMarshalType (HandleStyle.JIFunctionPtrWithErrors, isReturn: false, isPinvoke: true));
o.Write ($" {Escape (p.Name)}");
}
o.WriteLine (")");
o.WriteLine ("\t\t{");
o.Write ("\t\t\t");
if (returnType != "void") {
o.Write ("return ");
}
o.Write ($"(*((JNIEnv**)env))->{entry.Name} (env");
foreach (var p in entry.Parameters) {
o.Write (", ");
o.Write (Escape (p.Name));
}
o.WriteLine (");");
o.WriteLine ("\t\t}");
}
o.WriteLine ("\t}");
o.WriteLine ();
}
static string GetPinvokeName (string name)
{
var sb = new StringBuilder ("java_interop_jnienv_".Length + name.Length * 2);
sb.Append ("java_interop_jnienv");
var uc = false;
foreach (var c in name) {
if (!uc && char.IsUpper (c)) {
sb.Append ("_");
uc = true;
} else {
uc = false;
}
sb.Append (char.ToLower (c));
}
return sb.ToString ();
}
static void GenerateJniEnv (TextWriter o, string type, string visibility, HandleStyle style)
{
o.WriteLine ();
o.WriteLine ("\t{0} static partial class {1} {{", visibility, type);
foreach (JniFunction entry in JNIEnvEntries) {
if ((entry.DeclaringType ?? "JniEnvironment") != type)
continue;
if (entry.Parameters == null)
continue;
if (entry.IsPrivate || entry.CustomWrapper)
continue;
o.WriteLine ();
o.Write ("\t\t{2} static unsafe {0} {1} (", entry.GetManagedReturnType (style), entry.ApiName, entry.Visibility);
switch (entry.ApiName) {
default:
bool is_void = entry.ReturnType.JniType == "void";
for (int i = 0; i < entry.Parameters.Length; i++) {
if (i > 0)
o.Write (", ");
o.Write ("{0} {1}", entry.Parameters [i].Type.GetManagedType (style, isReturn: false), Escape (entry.Parameters [i].Name));
}
o.WriteLine (")");
o.WriteLine ("\t\t{");
NullCheckParameters (o, entry.Parameters, style);
PrepareParameters (o, entry.Parameters, style);
if (style == HandleStyle.JIIntPtrPinvokeWithErrors) {
if (entry.Throws)
o.WriteLine ("\t\t\tIntPtr thrown;");
} else if (style == HandleStyle.JIFunctionPtrWithErrors) {
o.WriteLine ($"\t\t\tIntPtr __env = JniEnvironment.EnvironmentPointer;");
} else {
o.WriteLine ("\t\t\tvar __info = JniEnvironment.CurrentInfo;");
}
o.Write ("\t\t\t");
if (!is_void)
o.Write ("var tmp = ");
if (style == HandleStyle.JIIntPtrPinvokeWithErrors) {
o.Write ("NativeMethods.{0} (JniEnvironment.EnvironmentPointer{1}",
GetPinvokeName (entry.Name),
entry.Throws ? ", out thrown" : "");
} else if (style == HandleStyle.JIFunctionPtrWithErrors) {
o.Write ($"JniNativeMethods.{entry.Name} (__env");
} else {
o.Write ("__info.Invoker.{0} (__info.EnvironmentPointer", entry.Name);
}
for (int i = 0; i < entry.Parameters.Length; i++) {
var p = entry.Parameters [i];
o.Write (", ");
var needOut = p.Type.GetManagedType (style, isReturn: false).StartsWith ("out ", StringComparison.Ordinal);
if (needOut && style == HandleStyle.JIFunctionPtrWithErrors) {
o.Write ("&");
} else if (needOut) {
o.Write ("out ");
}
o.Write (p.Type.GetManagedToMarshalExpression (style, Escape (entry.Parameters [i].Name)));
}
o.WriteLine (");");
if (style == HandleStyle.JIFunctionPtrWithErrors && entry.Throws) {
o.WriteLine ("\t\t\tIntPtr thrown = JniNativeMethods.ExceptionOccurred (__env);");
}
CleanupParameters (o, entry.Parameters, style);
RaiseException (o, entry, style);
if (is_void) {
} else {
foreach (var line in entry.ReturnType.GetHandleCreationLogStatements (style, entry.Name, "tmp"))
o.WriteLine ("\t\t\t{0}", line);
foreach (var line in entry.ReturnType.GetMarshalToManagedStatements (style, "tmp", entry))
o.WriteLine ("\t\t\t{0}", line);
}
break;
}
o.WriteLine ("\t\t}");
}
o.WriteLine ("\t}");
}
static void NullCheckParameters (TextWriter o, ParamInfo[] ps, HandleStyle style)
{
bool haveChecks = false;
for (int i = 0; i < ps.Length; i++) {
var p = ps [i];
if (p.CanBeNull)
continue;
var pn = Escape (p.Name);
foreach (var line in p.Type.VerifyParameter (style, pn)) {
haveChecks = true;
o.WriteLine ("\t\t\t{0}", line);
}
}
if (haveChecks)
o.WriteLine ();
}
static void PrepareParameters (TextWriter o, ParamInfo[] ps, HandleStyle style)
{
bool haveChecks = false;
foreach (var e in ps) {
foreach (var s in e.Type.GetManagedToMarshalPrepareStatements (style, Escape (e.Name))) {
haveChecks = true;
o.WriteLine ($"\t\t\t{s}");
}
}
if (haveChecks)
o.WriteLine ();
}
static void CleanupParameters (TextWriter o, ParamInfo[] ps, HandleStyle style)
{
foreach (var e in ps) {
foreach (var s in e.Type.GetManagedToMarshalCleanupStatements (style, Escape (e.Name))) {
o.WriteLine ($"\t\t\t{s}");
}
}
}
static void RaiseException (TextWriter o, JniFunction entry, HandleStyle style)
{
if (!entry.Throws)
return;
o.WriteLine ();
o.WriteLine ("\t\t\tException __e = JniEnvironment.GetExceptionForLastThrowable ({0});",
(style == HandleStyle.JIIntPtrPinvokeWithErrors || style == HandleStyle.JIFunctionPtrWithErrors)
? "thrown"
: "");
o.WriteLine ("\t\t\tif (__e != null)");
o.WriteLine ("\t\t\t\tExceptionDispatchInfo.Capture (__e).Throw ();");
o.WriteLine ();
}
static void WriteNativeFileHeader (TextWriter o)
{
o.WriteLine ("/*");
o.WriteLine (" * Generated file; DO NOT EDIT!");
o.WriteLine (" *");
o.WriteLine (" * To make changes, edit Java.Interop/build-tools/jnienv-gen and rerun");
o.WriteLine (" */");
o.WriteLine ();
}
static void GenerateNativeLibSource (TextWriter source, TextWriter header, string headerName)
{
WriteNativeFileHeader (source);
WriteNativeFileHeader (header);
header.WriteLine ("#if !defined (__JAVA_INTEROP_NATIVE_H)");
header.WriteLine ("#define __JAVA_INTEROP_NATIVE_H");
header.WriteLine ();
header.WriteLine ("#include <jni.h>");
header.WriteLine ();
header.WriteLine ("typedef jmethodID jstaticmethodID;");
header.WriteLine ("typedef jfieldID jstaticfieldID;");
header.WriteLine ("typedef jobject jglobal;");
header.WriteLine ();
header.WriteLine ("#if !defined(JI_NO_VISIBILITY)");
header.WriteLine ("\t/* VS 2010 and later have stdint.h */");
header.WriteLine ("\t#if defined(_MSC_VER)");
header.WriteLine ();
header.WriteLine ("\t\t#define JI_API_EXPORT __declspec(dllexport)");
header.WriteLine ("\t\t#define JI_API_IMPORT __declspec(dllimport)");
header.WriteLine ();
header.WriteLine ("\t#else /* defined(_MSC_VER */");
header.WriteLine ();
header.WriteLine ("\t\t#define JI_API_EXPORT __attribute__ ((visibility (\"default\")))");
header.WriteLine ("\t\t#define JI_API_IMPORT");
header.WriteLine ();
header.WriteLine ("\t#endif /* !defined(_MSC_VER) */");
header.WriteLine ();
header.WriteLine ("\t#if defined(JI_DLL_EXPORT)");
header.WriteLine ("\t\t#define JI_API JI_API_EXPORT");
header.WriteLine ("\t#elif defined(JI_DLL_IMPORT)");
header.WriteLine ("\t\t#define JI_API JI_API_IMPORT");
header.WriteLine ("\t#else /* !defined(JI_DLL_IMPORT) && !defined(JI_API_IMPORT) */");
header.WriteLine ("\t\t#define JI_API");
header.WriteLine ("\t#endif /* JI_DLL_EXPORT... */");
header.WriteLine ("#else // JI_NO_VISIBILITY");
header.WriteLine ("\t#define JI_API");
header.WriteLine ("#endif // JI_NO_VISIBILITY");
header.WriteLine ();
if (headerName != "-") {
source.WriteLine ($"#include \"{Path.GetFileName(headerName)}\"");
}
foreach (JniFunction entry in JNIEnvEntries) {
if (entry.IsPrivate || entry.CustomWrapper)
continue;
header.WriteLine (
"JI_API {0} {1} (JNIEnv *env{2}{3}{4});",
entry.ReturnType.JniType,
GetPinvokeName (entry.Name),
entry.Throws ? ", jthrowable *_thrown" : "",
entry.Parameters.Length != 0 ? ", " : "",
string.Join (", ", entry.Parameters.Select (p => string.Format ("{0} {1}", p.Type.JniType, p.Name)))
);
source.WriteLine ();
source.WriteLine ("JI_API {0}", entry.ReturnType.JniType);
source.WriteLine ("{0} (JNIEnv *env{1}{2}{3})",
GetPinvokeName (entry.Name),
entry.Throws ? ", jthrowable *_thrown" : "",
entry.Parameters.Length != 0 ? ", " : "",
string.Join (", ", entry.Parameters.Select (p => string.Format ("{0} {1}", p.Type.JniType, p.Name))));
source.WriteLine ("{");
bool isVoid = entry.ReturnType.JniType == "void";
if (entry.Throws)
source.WriteLine ("\t*_thrown = 0;");
source.Write ("\t");
if (!isVoid)
source.Write ("{0} _r_ = ", entry.ReturnType.JniType);
source.WriteLine ("(*env)->{0} (env{1}{2});",
entry.Name,
entry.Parameters.Length != 0 ? ", " : "",
string.Join (", ", entry.Parameters.Select (p => p.Name)));
if (entry.Throws)
source.WriteLine ("\t*_thrown = (*env)->ExceptionOccurred (env);");
if (!isVoid)
source.WriteLine ("\treturn _r_;");
source.WriteLine ("}");
}
header.WriteLine ();
header.WriteLine ("#endif // __JAVA_INTEROP_NATIVE_H");
}
}
class JniFunction {
public string DeclaringType;
// The java name
public string Name;
// The name of the property/method we will generate. Defaults to the (java) name.
private string api_name;
public string ApiName
{
get { return api_name ?? Name; }
set { api_name = value; }
}
// The C prototype that we are binding (for diagnostic purposes)
public string Prototype;
public TypeInfo ReturnType;
public ParamInfo [] Parameters;
// If true, then we initialize the binding on the static ctor, we dont lazy-define it
public bool Prebind = false;
// If there is a custom wrapper in JNIEnv (so an automatic one shouldn't be generated)
public bool CustomWrapper = false;
// If the JNI function can throw an exception (ExceptionOccurred needs to be invoked)
public bool Throws;
private string visibility;
public string Visibility {
get {
if (visibility == null)
return "public";
return visibility;
}
set {
visibility = value;
}
}
public bool IsPublic { get { return Visibility == "public"; } }
public bool IsPrivate { get { return visibility == "private"; } }
public string GetManagedReturnType (HandleStyle style)
{
if (ReturnType == null)
return "void";
return ReturnType.GetManagedType (style, isReturn:true);
}
public string GetMarshalReturnType (HandleStyle style)
{
if (ReturnType == null)
return "void";
return ReturnType.GetMarshalType (style, isReturn:true);
}
public string GetDelegateTypeName (HandleStyle style)
{
StringBuilder name = new StringBuilder ();
if (ReturnType == null || ReturnType.JniType == "void")
name.Append ("JniAction_");
else
name.Append ("JniFunc_");
name.Append ("JNIEnvPtr");
for (int i = 0; i < Parameters.Length; i++) {
var pt = Parameters [i].Type.GetMarshalType (style, isReturn: false);
if (pt == "va_list")
return null;
name.AppendFormat ("_").Append (pt.FixupType ());
}
string rt = GetMarshalReturnType (style);
if (rt != "void")
name.Append ("_").Append (rt.FixupType ());
return name.ToString ();
}
}
abstract class TypeInfo
{
static readonly Dictionary<string, TypeInfo> types = new Dictionary<string, TypeInfo> () {
{ "jvalue*", new BuiltinTypeInfo ("jvalue*", "JniArgumentValue*") },
{ "jbyte", new BuiltinTypeInfo ("jbyte", "sbyte") },
{ "jchar", new BuiltinTypeInfo ("jchar", "char") },
{ "jchar*", new BuiltinTypeInfo ("jchar*", "char*") },
{ "jshort", new BuiltinTypeInfo ("jshort", "short") },
{ "jsize", new BuiltinTypeInfo ("jsize", "int") },
{ "jint", new BuiltinTypeInfo ("jint", "int") },
{ "jlong", new BuiltinTypeInfo ("jlong", "long") },
{ "jfloat", new BuiltinTypeInfo ("jfloat", "float") },
{ "jdouble", new BuiltinTypeInfo ("jdouble", "double") },
{ "jboolean", new BooleanTypeInfo ("jboolean") },
{ "void*", new BuiltinTypeInfo ("void*", "IntPtr") },
{ "const jchar*", new StringTypeInfo ("const jchar*") },
{ "const char*", new StringTypeInfo ("const char*") },
{ "const JNINativeMethod*", new BuiltinTypeInfo ("const JNINativeMethod*", "IntPtr") },
{ "jobjectRefType", new BuiltinTypeInfo ("jobjectRefType", "JniObjectReferenceType") },
{ "jfieldID", new InstanceFieldTypeInfo ("jfieldID") },
{ "jstaticfieldID", new StaticFieldTypeInfo ("jstaticfieldID") },
{ "jmethodID", new InstanceMethodTypeInfo ("jmethodID") },
{ "jstaticmethodID", new StaticMethodTypeInfo ("jstaticmethodID") },
{ "jstring", new LocalReferenceTypeInfo ("jstring") },
{ "jarray", new LocalReferenceTypeInfo ("jarray") },
{ "jobject", new LocalReferenceTypeInfo ("jobject") },
{ "jthrowable", new LocalReferenceTypeInfo ("jthrowable") },
{ "jclass", new LocalReferenceTypeInfo ("jclass") },
{ "jweak", new WeakGlobalReferenceTypeInfo ("jweak") },
{ "jglobal", new GlobalReferenceTypeInfo ("jglobal") },
{ "JavaVM**", new JavaVMPointerTypeInfo ("JavaVM**") },
{ "JniReleaseArrayElementsMode", new JniReleaseArrayElementsModeTypeInfo () },
};
static readonly Dictionary<string, string> pointerMapping = new Dictionary<string, string> {
{ "jboolean*", "bool*" },
{ "jbyte*", "sbyte*" },
{ "jchar*", "char*" },
{ "jdouble*", "double*" },
{ "jfloat*", "float*" },
{ "jint*", "int*" },
{ "jlong*", "long*" },
{ "jshort*", "short*" },
};
public static TypeInfo Create (string type, string managedType = null)
{
if (managedType != null)
return new BuiltinTypeInfo (type, managedType);
TypeInfo t;
if (types.TryGetValue (type, out t))
return t;
if (type.EndsWith ("Array", StringComparison.Ordinal))
return new ArrayTypeInfo (type);
string p;
if (pointerMapping.TryGetValue (type, out p))
return new BuiltinTypeInfo (type, p);
if (type.EndsWith ("*", StringComparison.Ordinal))
return new BuiltinTypeInfo (type, "IntPtr");
return new BuiltinTypeInfo (type, type);
}
public static implicit operator TypeInfo (string jniType)
{
return Create (jniType);
}
public readonly string JniType;
protected TypeInfo (string jniType)
{
JniType = jniType;
}
public abstract string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke = false);
public abstract string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke = false);
public virtual string[] GetHandleCreationLogStatements (HandleStyle style, string method, string variable)
{
return new string [0];
}
public virtual string GetManagedToMarshalExpression (HandleStyle style, string variable)
{
return variable;
}
public virtual string[] GetMarshalToManagedStatements (HandleStyle style, string variable, JniFunction entry)
{
return new[] {
string.Format ("return {0};", variable),
};
}
public virtual string[] VerifyParameter (HandleStyle style, string variable)
{
return new string [0];
}
public virtual string[] GetManagedToMarshalPrepareStatements (HandleStyle style, string variable) => Array.Empty<string> ();
public virtual string[] GetManagedToMarshalCleanupStatements (HandleStyle style, string variable) => Array.Empty<string> ();
}
class BuiltinTypeInfo : TypeInfo {
/// <summary>
/// NOTE: .NET framework can't marshal this
/// </summary>
const string JniArgumentValue = "JniArgumentValue*";
string managed;
public BuiltinTypeInfo (string jni, string managed)
: base (jni)
{
this.managed = managed;
}
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
if (isPinvoke && managed == JniArgumentValue) {
return "IntPtr";
}
return managed;
}
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
if (isPinvoke && managed == JniArgumentValue) {
return "IntPtr";
}
return managed;
}
public override string GetManagedToMarshalExpression (HandleStyle style, string variable)
{
var value = base.GetManagedToMarshalExpression (style, variable);
if (managed == JniArgumentValue) {
value = "(IntPtr) " + value;
}
return value;
}
public override string[] VerifyParameter (HandleStyle style, string variable)
{
if (managed != "IntPtr")
return new string [0];
var variableName = variable.StartsWith ("@", StringComparison.Ordinal)
? variable.Substring (1)
: variable;
return new[] {
string.Format ("if ({0} == IntPtr.Zero)", variable),
string.Format ("\tthrow new ArgumentException (\"'{0}' must not be IntPtr.Zero.\", \"{0}\");", variableName),
};
}
}
class BooleanTypeInfo : TypeInfo {
public BooleanTypeInfo (string jni)
: base (jni)
{
}
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "byte";
}
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "bool";
}
public override string[] GetMarshalToManagedStatements (HandleStyle style, string variable, JniFunction entry)
{
return new string[] {
string.Format ("return ({0} != 0) ? true : false;", variable),
};
}
public override string GetManagedToMarshalExpression (HandleStyle style, string variable)
{
return string.Format ("({0} ? (byte) 1 : (byte) 0)", variable);
}
}
class StringTypeInfo : TypeInfo {
public StringTypeInfo (string jni)
: base (jni)
{
}
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
if (style == HandleStyle.JIFunctionPtrWithErrors && isPinvoke) {
return "IntPtr";
}
return "string";
}
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "string";
}
public override string GetManagedToMarshalExpression (HandleStyle style, string variable)
{
switch (style) {
case HandleStyle.JIFunctionPtrWithErrors:
return $"_{variable}_ptr";
default:
return variable;
}
}
public override string[] GetMarshalToManagedStatements (HandleStyle style, string variable, JniFunction entry)
{
switch (style) {
case HandleStyle.SafeHandle:
case HandleStyle.XAIntPtr:
return new [] {
string.Format ("JniEnvironment.LogCreateLocalRef ({0});", variable),
string.Format ("return {0};", variable),
};
case HandleStyle.JIIntPtr:
case HandleStyle.JIIntPtrPinvokeWithErrors:
case HandleStyle.JIFunctionPtrWithErrors:
return new [] {
string.Format ("JniEnvironment.LogCreateLocalRef ({0});", variable),
string.Format ("return new JniObjectReference ({0}, JniObjectReferenceType.Local);", variable),
};
}
return null;
}
public override string[] VerifyParameter (HandleStyle style, string variable)
{
var variableName = variable.StartsWith ("@", StringComparison.Ordinal)
? variable.Substring (1)
: variable;
return new[] {
string.Format ("if ({0} == null)", variable),
string.Format ("\tthrow new ArgumentNullException (\"{0}\");", variableName),
};
}
public override string[] GetManagedToMarshalPrepareStatements (HandleStyle style, string variable)
{
switch (style) {
case HandleStyle.JIFunctionPtrWithErrors:
return new[]{
$"var _{variable}_ptr = Marshal.StringToCoTaskMemUTF8 ({variable});",
};
default:
return base.GetManagedToMarshalPrepareStatements (style, variable);
}