-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRuntimeUtils.java
1104 lines (995 loc) · 45.7 KB
/
RuntimeUtils.java
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
package ca.weblite.objc;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.ByReference;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.DoubleByReference;
import com.sun.jna.ptr.FloatByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.ptr.ShortByReference;
import ca.weblite.nativeutils.NativeUtils;
/**
* A Java class with static methods that interact with the Objective-C runtime.
* These methods wrap the *very* low level methods provided by the Runtime class
* to provide a slightly higher level of abstraction.
*
* <p>It is helpful to perform a static import of the methods in this class if you
* will be using the objc runtime library, as it contains many utility methods
* to deal with Objective-C primitives. E.g. the cls() method can return the
* Pointer to a class given its name. The clsName() returns the name of the
* class specified by a Pointer(). </p>
*
* <p>There are also many variants of the msg() method for sendng messages to
* objects in the Objective-C runtime.</p>
*
* @author shannah
* @see <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html">Objective-C Runtime Reference</a>
* @version $Id: $Id
* @since 1.1
*/
public class RuntimeUtils {
/**
* Short reference to the runtime instance for interacting with Objective-C
*/
private static final Runtime rt = Runtime.INSTANCE;
private RuntimeUtils() {
}
static {
String libraryPath = "/libjcocoa.dylib";
try {
NativeUtils.loadLibraryFromJar(libraryPath);
init();
} catch (IOException ioException) {
throw new UncheckedIOException("Failed loading library " + libraryPath, ioException);
}
}
/**
* Returns a pointer to the class for specific class name.
* <pre>
* {@code
* Pointer nsObject = cls("NSObject");
* }</pre>
*
* @param name The name of the class to retrieve.
* @return The pointer to the class structure.
*/
public static Pointer cls(String name){
return rt.objc_lookUpClass(name);
}
/**
* Returns a pointer to a class given a Peerable object which wraps
* the pointer. Effectively this just calls peer.getPeer() and
* returns its value. A convenience method.
*
* @param peer The peer object that is wrapping the Objective-C class
* object.
* @return A Pointer to the Objective-C class.
*/
public static Pointer cls(Peerable peer){
return peer.getPeer();
}
/**
* Returns the name of an objective C class specified by the given class pointer.
*
* @param cls The pointer to the class whose name we wish to retrieve.
* @return The name of the class.
* @see Runtime#class_getName(Pointer)
*/
public static String clsName(Pointer cls){
return rt.class_getName(cls);
}
/**
* A wrapper for the clsName() method given a Peerable object that wraps
* the class pointer. This will return the class name.
*
* @param peer a {@link ca.weblite.objc.Peerable} object.
* @see Runtime#class_getName(Pointer)
* @return a {@link java.lang.String} object.
*/
public static String clsName(Peerable peer){
return clsName(peer.getPeer());
}
/**
* Returns a pointer to the selector specified by the given selector name.
*
* @param name a {@link java.lang.String} object.
* @return Pointer to an Objective-C message selector.
* @see <a href="http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/Chapters/ocSelectors.html">Objective-C Selectors Reference</a>
*/
public static Pointer sel(String name){
return rt.sel_getUid(name);
}
/**
* Returns a pointer to the selector that is wrapped by a Peerable object.
*
* @param peer a {@link ca.weblite.objc.Peerable} object.
* @return Pointer to a specified selector.
*/
public static Pointer sel(Peerable peer){
return peer.getPeer();
}
/**
* Returns the name of a selector.
*
* @param sel Pointer to a selector.
* @return The name of the selector.
*/
public static String selName(Pointer sel){
return rt.sel_getName(sel);
}
/**
* Returns the name of a selector.
*
* @return The name of the selector.
* @param peer a {@link ca.weblite.objc.Peerable} object.
*/
public static String selName(Peerable peer){
return selName(peer.getPeer());
}
/**
* Sends a message to a specified class using the given selector.
*
* @param cls The name of the class.
* @param msg The name of the selector.
* @param args The arguments passed to the method. These are passed raw
* and will not be coerced.
* @return The result of the message. This may be a pointer, if the message
* returns a pointer, or a value, if the method returns a BOOL, long, int,
* short, byte, or char. Note that you cannot use this method for
* messages that return float and double values. For these, you <em>must</em>
* use msgDouble(). This is because doubles and floats are handled using
* a different underlying runtime function (objc_msgSend_fpret). Alternatively
* you can use the variation of msg() that takes coerceInput and coerceOutput
* boolean values, as this will automatically choose the correct underlying
* message function depending on the return type reported by the message
* signature.
* @see #msgDouble(String, String, Object...)
*/
public static long msg(String cls, String msg, Object... args){
return msg(cls(cls), msg, args);
}
/**
* Sends a message to a specified class using the given selector.
*
* @param cls The name of the class.
* @param msg The name of the selector.
* @param args The arguments passed to the method. These are passed raw
* and will not be coerced.
* @return The result of the message. This may be a pointer, if the message
* returns a pointer, or a value, if the method returns a BOOL, long, int,
* short, byte, or char. Note that you cannot use this method for
* messages that return float and double values. For these, you <em>must</em>
* use msgDouble(). This is because doubles and floats are handled using
* a different underlying runtime function (objc_msgSend_fpret). Alternatively
* you can use the variation of msg() that takes coerceInput and coerceOutput
* boolean values, as this will automatically choose the correct underlying
* message function depending on the return type reported by the message
* signature.
* @see #msgDouble(String, Pointer, Object...)
*/
public static long msg(String cls, Pointer msg, Object... args){
return msg(cls(cls), msg, args);
}
/**
* Sends a message to a specified class using the given selector.
*
* @param msg The name of the selector.
* @param args The arguments passed to the method. These are passed raw
* and will not be coerced.
* @return The result of the message. This may be a pointer, if the message
* returns a pointer, or a value, if the method returns a BOOL, long, int,
* short, byte, or char. Note that you cannot use this method for
* messages that return float and double values. For these, you <em>must</em>
* use msgDouble(). This is because doubles and floats are handled using
* a different underlying runtime function (objc_msgSend_fpret). Alternatively
* you can use the variation of msg() that takes coerceInput and coerceOutput
* boolean values, as this will automatically choose the correct underlying
* message function depending on the return type reported by the message
* signature.
* @see #msgDouble(Pointer, String, Object...)
* @param receiver a {@link com.sun.jna.Pointer} object.
*/
public static long msg(Pointer receiver, String msg, Object... args){
return objc_msgSend(receiver, sel(msg), args);
}
/**
* Generate the Runtime class name suffix in RuntimeMappings that defines
* the appropriate version of objc_msgSend for the given arguments.
* @param args The arguments to check.
* @return The suffix. This will be a binary string in which a 1 in the i'th
* index corresponds with a Structure.ByValue parameter.
*/
private static String getArgsSuffix(Object... args) {
StringBuilder sb = new StringBuilder();
boolean foundStructByValue = false;
for (Object o : args) {
if (o instanceof Structure.ByValue) {
foundStructByValue = true;
sb.append("1");
} else {
sb.append("0");
}
}
if (foundStructByValue) {
return sb.toString();
}
return "";
}
/**
* Gets the parameter types a call to objc_msgSend should use, including the first two
* Pointer parameers.
* @param args The input arguments.
* @return The corresponding class types. The output array will be longer than the input array by two, because
* of the two Pointer parameters at the beginning.
*/
private static Class<?>[] getArgsParamTypes( Object... args) {
Class<?>[] out = new Class<?>[args.length+2];
out[0] = Pointer.class;
out[1] = Pointer.class;
for (int i=0; i<args.length; i++) {
out[i+2] = (args[i] instanceof Structure.ByValue) ? Structure.ByValue.class: Object.class;
}
return out;
}
/**
* Merges the parameters into a single array, for use in the {@link #objc_msgSend(Pointer, Pointer, Object...)}
* method.
* @param receiver The receiver of the message.
* @param selector THe selector to call.
* @param args The arguments.
* @return The full parameter array.
*/
private static Object[] merge(Pointer receiver, Pointer selector, Object... args) {
Object[] out = new Object[args.length+2];
out[0] = receiver;
out[1] = selector;
for (int i=0; i<args.length; i++) {
out[i+2] = args[i];
}
return out;
}
/**
* A wrapper around the obj_msgSend() method to do preprocessing, and call the correct
* variant. If any of the parameters are {@link Structure.ByValue}, then the dispatch will
* use reflection to find the correct JNA mapping.
* @param receiver The receiver
* @param selector The selector
* @param args The arguments
* @return The output
*/
private static long objc_msgSend(Pointer receiver, Pointer selector, Object... args) {
String argSuffix = getArgsSuffix(args);
if (args.length <= 7 && argSuffix.isEmpty()) {
switch (args.length) {
case 0:
return rt.objc_msgSend(receiver, selector);
case 1:
return rt.objc_msgSend(receiver, selector, args[0]);
case 2:
return rt.objc_msgSend(receiver, selector, args[0], args[1]);
case 3:
return rt.objc_msgSend(receiver, selector, args[0], args[1], args[2]);
case 4:
return rt.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3]);
case 5:
return rt.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4]);
case 6:
return rt.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5]);
case 7:
return rt.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
default:
throw new IllegalArgumentException("msg currently supports max 4 args");
}
} else {
try {
Class runtimeClass = RuntimeUtils.class.getClassLoader().loadClass("ca.weblite.objc.RuntimeMappings$Runtime"+argSuffix);
Field runtimeInstanceField = runtimeClass.getField("INSTANCE");
Object runtimeInstance = runtimeInstanceField.get(null);
return (long)runtimeClass.getMethod("objc_msgSend", getArgsParamTypes(args)).invoke(runtimeInstance, merge(receiver, selector, args));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
/**
* Sends a message to a specified class using the given selector.
*
* @param args The arguments passed to the method. These are passed raw
* and will not be coerced.
* @return The result of the message. This may be a pointer, if the message
* returns a pointer, or a value, if the method returns a BOOL, long, int,
* short, byte, or char. Note that you cannot use this method for
* messages that return float and double values. For these, you <em>must</em>
* use msgDouble(). This is because doubles and floats are handled using
* a different underlying runtime function (objc_msgSend_fpret). Alternatively
* you can use the variation of msg() that takes coerceInput and coerceOutput
* boolean values, as this will automatically choose the correct underlying
* message function depending on the return type reported by the message
* signature.
* @see #msgDouble(Pointer, Pointer, Object...)
* @param receiver a {@link com.sun.jna.Pointer} object.
* @param selector a {@link com.sun.jna.Pointer} object.
*/
public static long msg(Pointer receiver, Pointer selector, Object... args){
long out = objc_msgSend(receiver, selector, args);
return out;
}
/**
* Wrapper around msg() that returns a Pointer. This should only be used for
* sending messages that return Pointers (or Objects).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return A Pointer return value of the message.
*/
public static Pointer msgPointer(Pointer receiver, Pointer selector, Object... args){
long res = msg(receiver, selector, args);
return new Pointer(res);
}
/**
* Wrapper around msg() that returns a Pointer. This should only be used for
* sending messages that return Pointers (or Objects).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return A Pointer return value of the message.
*/
public static Pointer msgPointer(Pointer receiver, String selector, Object... args){
return msgPointer(receiver, sel(selector), args);
}
/**
* Wrapper around msg() that returns a Pointer. This should only be used for
* sending messages that return Pointers (or Objects).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return A Pointer return value of the message.
*/
public static Pointer msgPointer(String receiver, Pointer selector, Object... args){
return msgPointer(cls(receiver), selector, args);
}
/**
* Wrapper around msg() that returns a Pointer. This should only be used for
* sending messages that return Pointers (or Objects).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return A Pointer return value of the message.
*/
public static Pointer msgPointer(String receiver, String selector, Object... args){
return msgPointer(cls(receiver), sel(selector), args);
}
/**
* Wrapper around msg() that returns an int. This should only be used for
* sending messages that return int-compatible numeric values. E.g.
* byte, bool, long, int, short. Do not use this if the message will return
* something else (like a float, double, string, or pointer). Narrowing
* primitive conversion will be applied; this will cause information loss
* if the {@code long} result does not fit in the {@code int} value range.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return An int value returned by the message.
*/
public static int msgInt(Pointer receiver, Pointer selector, Object... args){
long res = msg(receiver, selector, args);
return (int) res;
}
/**
* Wrapper around msg() that returns an int. This should only be used for
* sending messages that return int-compatible numeric values. E.g.
* byte, bool, long, int, short. Do not use this if the message will return
* something else (like a float, double, string, or pointer).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return An int value returned by the message.
*/
public static int msgInt(String receiver, Pointer selector, Object... args){
return msgInt(cls(receiver), selector, args);
}
/**
* Wrapper around msg() that returns an int. This should only be used for
* sending messages that return int-compatible numeric values. E.g.
* byte, bool, long, int, short. Do not use this if the message will return
* something else (like a float, double, string, or pointer).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return An int value returned by the message.
*/
public static int msgInt(String receiver, String selector, Object... args){
return msgInt(cls(receiver), sel(selector), args);
}
/**
* Wrapper around msg() that returns an int. This should only be used for
* sending messages that return int-compatible numeric values. E.g.
* byte, bool, long, int, short. Do not use this if the message will return
* something else (like a float, double, string, or pointer).
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return An int value returned by the message.
*/
public static int msgInt(Pointer receiver, String selector, Object... args){
return msgInt(receiver, sel(selector), args);
}
/**
* Wrapper around msg() that returns a boolean value. This should only be used
* for sending messages that return boolean-compatible numeric values. Essentially
* any non-zero value is interpreted (and returned) as true. Zero values are
* interpreted as false.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return Boolean return value of the message.
*/
public static boolean msgBoolean(Pointer receiver, Pointer selector, Object... args){
long res = msg(receiver, selector, args);
return res > 0L;
}
/**
* Wrapper around msg() that returns a boolean value. This should only be used
* for sending messages that return boolean-compatible numeric values. Essentially
* any non-zero value is interpreted (and returned) as true. Zero values are
* interpreted as false.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return Boolean return value of the message.
*/
public static boolean msgBoolean(String receiver, Pointer selector, Object... args){
return msgBoolean(cls(receiver), selector, args);
}
/**
* Wrapper around msg() that returns a boolean value. This should only be used
* for sending messages that return boolean-compatible numeric values. Essentially
* any non-zero value is interpreted (and returned) as true. Zero values are
* interpreted as false.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return Boolean return value of the message.
*/
public static boolean msgBoolean(String receiver, String selector, Object... args){
return msgBoolean(cls(receiver), sel(selector), args);
}
/**
* Wrapper around msg() that returns a boolean value. This should only be used
* for sending messages that return boolean-compatible numeric values. Essentially
* any non-zero value is interpreted (and returned) as true. Zero values are
* interpreted as false.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return Boolean return value of the message.
*/
public static boolean msgBoolean(Pointer receiver, String selector, Object... args){
return msgBoolean(receiver, sel(selector), args);
}
/**
* Wrapper around msg() that returns a string value. This should only be used
* for messages that return a CString. Do not use this for messages that
* return numeric values or Pointers to objects (e.g. NSString). Use the
* msg() variant that takes coerceInputs and coerceOutputs parameters if you
* want NSStrings to be automatically converted to java Strings.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return The string return value of the message.
*/
public static String msgString(Pointer receiver, Pointer selector, Object... args){
long res = msg(receiver, selector, args);
return new Pointer(res).getString(0);
}
/**
* Wrapper around msg() that returns a string value. This should only be used
* for messages that return a CString. Do not use this for messages that
* return numeric values or Pointers to objects (e.g. NSString). Use the
* msg() variant that takes coerceInputs and coerceOutputs parameters if you
* want NSStrings to be automatically converted to java Strings.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return The string return value of the message.
*/
public static String msgString(String receiver, Pointer selector, Object... args){
return msgString(cls(receiver), selector, args);
}
/**
* Wrapper around msg() that returns a string value. This should only be used
* for messages that return a CString. Do not use this for messages that
* return numeric values or Pointers to objects (e.g. NSString). Use the
* msg() variant that takes coerceInputs and coerceOutputs parameters if you
* want NSStrings to be automatically converted to java Strings.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return The string return value of the message.
*/
public static String msgString(String receiver, String selector, Object... args){
return msgString(cls(receiver), sel(selector), args);
}
/**
* Wrapper around msg() that returns a string value. This should only be used
* for messages that return a CString. Do not use this for messages that
* return numeric values or Pointers to objects (e.g. NSString). Use the
* msg() variant that takes coerceInputs and coerceOutputs parameters if you
* want NSStrings to be automatically converted to java Strings.
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args Arguments passed to the message.
* @return The string return value of the message.
*/
public static String msgString(Pointer receiver, String selector, Object... args){
return msgString(receiver, sel(selector), args);
}
/**
* Sends a message that returns a double value. This variant must be used
* if the method returns a float or a double. It doesn't actually wrap the
* primitive msg() method.. Rather it uses a different core Objective-C method,
* objc_msgSend_fpret().
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return The double return value of the message
*/
public static double msgDouble(Pointer receiver, Pointer selector, Object... args){
return objc_msgSend_fpret(receiver, selector, args);
}
/**
* Flag to check if this is apple silicon. (arm64)
*/
private static boolean isArm64 = System.getProperty("os.arch").equals("aarch64");
/**
* A wrapper for the objc_msgSend_fpret method. Since this method is not available
* in arm64, this will use a JNA mapping of objc_msgSend which returns double on that platform.
* On Intel, it will dispatch to the correct objc_msgSend_fpret mapping, according to parameter.
* @param receiver The receiver.
* @param selector The selector.
* @param args Arguments
* @return The result.
*/
private static double objc_msgSend_fpret(Pointer receiver, Pointer selector, Object... args) {
if (isArm64) {
switch (args.length) {
case 0:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector);
case 1:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0]);
case 2:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1]);
case 3:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1], args[2]);
case 4:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3]);
case 5:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4]);
case 6:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5]);
case 7:
return RuntmeArm64Extensions.INSTANCE.objc_msgSend(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
default:
throw new IllegalArgumentException("objc_msgSend does not support "+args.length+" arguments yet");
}
}
switch (args.length) {
case 0:
return rt.objc_msgSend_fpret(receiver, selector);
case 1:
return rt.objc_msgSend_fpret(receiver, selector, args[0]);
case 2:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1]);
case 3:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1], args[2]);
case 4:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1], args[2], args[3]);
case 5:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1], args[2], args[3], args[4]);
case 6:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5]);
case 7:
return rt.objc_msgSend_fpret(receiver, selector, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
default:
throw new IllegalArgumentException("objc_msgSend_fpret does not support "+args.length+" arguments yet");
}
}
/**
* Sends a message that returns a double value. This variant must be used
* if the method returns a float or a double. It doesn't actually wrap the
* primitive msg() method.. Rather it uses a different core Objective-C method,
* objc_msgSend_fpret().
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return The double return value of the message
*/
public static double msgDouble(String receiver, Pointer selector, Object... args){
return msgDouble(cls(receiver), selector, args);
}
/**
* Sends a message that returns a double value. This variant must be used
* if the method returns a float or a double. It doesn't actually wrap the
* primitive msg() method.. Rather it uses a different core Objective-C method,
* objc_msgSend_fpret().
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return The double return value of the message
*/
public static double msgDouble(String receiver, String selector, Object... args){
return msgDouble(cls(receiver), sel(selector), args);
}
/**
* Sends a message that returns a double value. This variant must be used
* if the method returns a float or a double. It doesn't actually wrap the
* primitive msg() method.. Rather it uses a different core Objective-C method,
* objc_msgSend_fpret().
*
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments passed to the message.
* @return The double return value of the message
*/
public static double msgDouble(Pointer receiver, String selector, Object... args){
return msgDouble(receiver, sel(selector), args);
}
private static void sleep50() {
try {
Thread.sleep(500);
} catch (Exception ex){}
}
/**
* Sends a message with the option of coercing the inputs and outputs. This variant
* uses a higher level of abstraction than the standard msg() and msgXXX() methods.
* If coerceReturn and coerceArgs are set to true, then this method will
* convert the inputs from Java inputs to the corresponding Objective-C inputs.
* Further it will return values that are more appropriate for use in Java.
*
* <p>Furthermore, this variant is smart about which variable of msg() to call
* behind the scenes. E.g. if the message signature specifies that this message
* returns a double, it will automatically use the double variant of msg().</p>
*
* @param coerceReturn If true, then the return value will be mapped to an appropriate
* Java value using the TypeMapper class.
* @param coerceArgs If true, then the inputs will be mapped from Java to appropriate
* C values using the TypeMapper class.
* @param receiver The target of the message.
* @param selector The selector for the message.
* @param args The arguments to be passed in the message.
* @return The return value of the message. This may be a Pointer or a value depending
* on the return type of the message.
*/
public static Object msg(boolean coerceReturn, boolean coerceArgs, Pointer receiver, Pointer selector, Object... args){
Pointer methodSignature = msgPointer(receiver, "methodSignatureForSelector:", selector);
if (Pointer.nativeValue(methodSignature) == 0L) {
throw new RuntimeException(new NoSuchMethodException("Method cannot be found for signature "+Pointer.nativeValue(selector)));
}
int numArgs = (int)msg(methodSignature, "numberOfArguments");
if ( numArgs >=2 && numArgs != args.length+2 ){
throw new RuntimeException("Wrong argument count. The selector "+selName(selector)+" requires "+(numArgs-2)+" arguments, but received "+args.length);
}
long returnTypePtr = msg(methodSignature, "methodReturnType");
String returnTypeSignature = new Pointer(returnTypePtr).getString(0);
if ( numArgs == 0 && returnTypeSignature == null ){
return msg(receiver, selector, args);
}
if ( coerceArgs && args.length > 0 ){
for ( int i=0; i<args.length; i++ ){
long out2 = msg(methodSignature, "getArgumentTypeAtIndex:", i+2);
String argumentTypeSignature = new Pointer(out2).getString(0);
args[i] = TypeMapper.getInstance().jToC(args[i], argumentTypeSignature, TypeMapper.getInstance());
}
}
String prefixes = "rnNoORV";
int offset = 0;
while ( prefixes.indexOf(returnTypeSignature.charAt(offset)) != -1 ){
offset++;
if ( offset > returnTypeSignature.length()-1 ){
break;
}
}
if ( offset > 0 ){
returnTypeSignature = returnTypeSignature.substring(offset);
}
char returnTypeFirstChar = returnTypeSignature.charAt(0);
if ( "[{(".indexOf(returnTypeFirstChar) ==-1 ){
// We are not returning a structure so we'll just
// do the message.
// We need to handle doubles and floats separately
if ( "df".indexOf(returnTypeFirstChar) != -1 ){
Object res = msgDouble(receiver, selector, args);
for ( int i=0; i<args.length; i++){
Proxy.release(args[i]);
}
return res;
} else {
long result = msg(receiver, selector, args);
if ( coerceReturn ){
Object res2 = TypeMapper.getInstance().cToJ(result, returnTypeSignature, TypeMapper.getInstance());
for ( int i=0; i<args.length; i++){
Proxy.release(args[i]);
}
return res2;
} else {
for ( int i=0; i<args.length; i++){
Proxy.release(args[i]);
}
return result;
}
}
}
Object output = msg(receiver, selector, args);
for ( int i=0; i<args.length; i++){
Proxy.release(args[i]);
}
return output;
}
/**
* Returns the size of an array that is specified in a signature.
*
* @param signature The signature for a parameter using <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C type encodings</a>.
* @return The size of the array.
* @see <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C Type Encodings</a>
*/
public static int arraySize(String signature){
int typeIndex = 2;
String digits = "0123456789";
while ( digits.indexOf(signature.charAt(typeIndex++)) != -1 ){}
return Integer.parseInt(signature.substring(1, typeIndex));
}
/**
* Returns the pointer to the Native peer for a Peerable object.
*
* @param peer a {@link ca.weblite.objc.Peerable} object.
* @return The Pointer to a native peer.
*/
public static Pointer addr(Peerable peer){
return peer.getPeer();
}
/**
* Sends a batch of messages in sequence.
*
* @param messages a {@link ca.weblite.objc.Message} object.
* @return a {@link java.lang.Object} object.
*/
public static Object msg(Message... messages){
for ( int i=0; i<messages.length; i++){
if ( i>0 ){
messages[i].previous = messages[i-1];
}
if ( i< messages.length-1 ){
messages[i].next = messages[i+1];
}
}
for ( int i=0; i<messages.length; i++){
Message m = messages[i];
if ( m.receiver == Pointer.NULL ){
m.receiver = (Pointer)m.previous.result;
}
m.beforeRequest();
if ( m.status == Message.STATUS_SKIPPED ){
continue;
} else if ( m.status == Message.STATUS_CANCELLED){
break;
} else {
boolean coerceInput = false;
boolean coerceOutput = false;
if ( i == messages.length -1 ){
coerceInput = m.coerceInput;
coerceOutput = m.coerceOutput;
}
try {
m.result = msg(coerceOutput, coerceInput, m.receiver, m.selector, m.args.toArray());
} catch (Exception ex){
m.error = ex;
}
m.status = Message.STATUS_COMPLETED;
}
}
if ( messages.length > 0 ){
return messages[messages.length-1].result;
} else {
throw new RuntimeException("Message queue was empty");
}
}
/**
* Converts a Java string to an NSString object.
*
* @param str The Java string to convert.
* @return Pointer to the NSString that corresponds to this string.
*/
public static Pointer str(String str){
return msgPointer("NSString", "stringWithUTF8String:", str);
}
/**
* Converts A native NSString object to a Java string.
*
* @param str a {@link com.sun.jna.Pointer} object.
* @return A Java string.
*/
public static String str(Pointer str){
long ptr = msg(str, "UTF8String");
return new Pointer(ptr).getString(0);
}
/**
* Wraps the given value in the appropriate ByReference subclass according
* to the provided signature. This is a useful method in cases where
* you need to pass a parameter to a C-function by reference, but you don't
* necessarily know what type of data it is.
*
* @param val The value to be wrapped in a byReference.
* @param signature The signature (using <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C type encodings</a>) of the value.
* @return A pointer to the reference that can be passed in a C function.
* @see <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C Type Encodings</a>
*/
public static Pointer getAsReference(Object val, String signature){
return getAsReferenceWrapper(val, signature).getPointer();
}
/**
* Wraps the given value in the appropriate ByReference subclass according
* to the provided signature. This is a useful method in cases where
* you need to pass a parameter to a C-function by reference, but you don't
* necessarily know what type of data it is.
*
* @param val The value to be wrapped in a byReference.
* @param signature The signature (using <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C type encodings</a>) of the value.
* @return The ByReference object that contains the pointer that can be passed
* to a c function.
* @see <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html">Objective-C Type Encodings</a>
*/
public static ByReference getAsReferenceWrapper(Object val, String signature){
String prefixes = "rnNoORV";
int offset = 0;
while ( prefixes.indexOf(signature.charAt(offset)) != -1 ){
offset++;
if ( offset > signature.length()-1 ){
break;
}
}
if ( offset > 0 ){
signature = signature.substring(offset);
}
switch ( signature.charAt(0)){
case 'i':
case 'I':
int intVal;
if (val instanceof Number) {
intVal = ((Number) val).intValue();
} else if (val instanceof String) {
intVal = Integer.parseInt((String) val);
} else {
throw new RuntimeException("Attempt to pass ineligible value to int: "+val);
}
return new IntByReference(intVal);
case 's':
case 'S':
short shortVal;
if (val instanceof Number) {
shortVal = ((Number) val).shortValue();
} else if (val instanceof String) {
shortVal = Short.parseShort((String) val);
} else {
throw new RuntimeException("Attempt to pass ineligible value to short: "+val);