-
Notifications
You must be signed in to change notification settings - Fork 914
/
Helpers.java
2871 lines (2383 loc) · 122 KB
/
Helpers.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 org.jruby.runtime;
import java.lang.reflect.Array;
import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import jnr.constants.platform.Errno;
import org.jruby.*;
import org.jruby.ast.ArgsNode;
import org.jruby.ast.ArgumentNode;
import org.jruby.ast.DAsgnNode;
import org.jruby.ast.LocalAsgnNode;
import org.jruby.ast.MultipleAsgnNode;
import org.jruby.ast.Node;
import org.jruby.ast.OptArgNode;
import org.jruby.ast.UnnamedRestArgNode;
import org.jruby.ast.types.INameNode;
import org.jruby.ast.RequiredKeywordArgumentValueNode;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.Unrescuable;
import org.jruby.internal.runtime.methods.*;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.Interp;
import org.jruby.ir.JIT;
import org.jruby.ir.operands.UndefinedValue;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.javasupport.JavaClass;
import org.jruby.javasupport.JavaUtil;
import org.jruby.javasupport.proxy.InternalJavaProxy;
import org.jruby.parser.StaticScope;
import org.jruby.platform.Platform;
import org.jruby.runtime.JavaSites.HelpersSites;
import org.jruby.runtime.backtrace.BacktraceData;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.invokedynamic.MethodNames;
import org.jruby.util.ArraySupport;
import org.jruby.util.ByteList;
import org.jruby.util.DefinedMessage;
import org.jruby.util.JavaNameMangler;
import org.jruby.util.MurmurHash;
import org.jruby.util.TypeConverter;
import org.jcodings.Encoding;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jcodings.unicode.UnicodeEncoding;
import static org.jruby.runtime.Visibility.PRIVATE;
import static org.jruby.runtime.Visibility.PROTECTED;
import static org.jruby.runtime.invokedynamic.MethodNames.EQL;
import static org.jruby.runtime.invokedynamic.MethodNames.OP_EQUAL;
import static org.jruby.util.CodegenUtils.sig;
import static org.jruby.util.StringSupport.EMPTY_STRING_ARRAY;
import org.jruby.util.io.EncodingUtils;
/**
* Helper methods which are called by the compiler. Note: These will show no consumers, but
* generated code does call these so don't remove them thinking they are dead code.
*
*/
public class Helpers {
public static CallSite selectAttrAsgnCallSite(IRubyObject receiver, IRubyObject self, CallSite normalSite, CallSite variableSite) {
if (receiver == self) return variableSite;
return normalSite;
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject value, ThreadContext context, IRubyObject caller) {
callSite.call(context, caller, receiver, value);
return value;
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject arg0, IRubyObject value, ThreadContext context, IRubyObject caller) {
callSite.call(context, caller, receiver, arg0, value);
return value;
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject arg0, IRubyObject arg1, IRubyObject value, ThreadContext context, IRubyObject caller) {
callSite.call(context, caller, receiver, arg0, arg1, value);
return value;
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, IRubyObject value, ThreadContext context, IRubyObject caller) {
callSite.call(context, caller, receiver, arg0, arg1, arg2, value);
return value;
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject[] args, ThreadContext context, IRubyObject caller) {
callSite.call(context, caller, receiver, args);
return args[args.length - 1];
}
public static IRubyObject doAttrAsgn(IRubyObject receiver, CallSite callSite, IRubyObject[] args, IRubyObject value, ThreadContext context, IRubyObject caller) {
IRubyObject[] newArgs = new IRubyObject[args.length + 1];
System.arraycopy(args, 0, newArgs, 0, args.length);
newArgs[args.length] = value;
callSite.call(context, caller, receiver, newArgs);
return value;
}
public static boolean invokeEqqForCaseWhen(CallSite callSite, ThreadContext context, IRubyObject caller, IRubyObject arg, IRubyObject[] receivers) {
for (int i = 0; i < receivers.length; i++) {
IRubyObject receiver = receivers[i];
if (invokeEqqForCaseWhen(callSite, context, caller, arg, receiver)) return true;
}
return false;
}
public static boolean invokeEqqForCaseWhen(CallSite callSite, ThreadContext context, IRubyObject caller, IRubyObject arg, IRubyObject receiver) {
IRubyObject result = callSite.call(context, caller, receiver, arg);
if (result.isTrue()) return true;
return false;
}
public static boolean invokeEqqForCaseWhen(CallSite callSite, ThreadContext context, IRubyObject caller, IRubyObject arg, IRubyObject receiver0, IRubyObject receiver1) {
IRubyObject result = callSite.call(context, caller, receiver0, arg);
if (result.isTrue()) return true;
return invokeEqqForCaseWhen(callSite, context, caller, arg, receiver1);
}
public static boolean invokeEqqForCaseWhen(CallSite callSite, ThreadContext context, IRubyObject caller, IRubyObject arg, IRubyObject receiver0, IRubyObject receiver1, IRubyObject receiver2) {
IRubyObject result = callSite.call(context, caller, receiver0, arg);
if (result.isTrue()) return true;
return invokeEqqForCaseWhen(callSite, context, caller, arg, receiver1, receiver2);
}
public static boolean invokeEqqForCaselessWhen(IRubyObject[] receivers) {
for (int i = 0; i < receivers.length; i++) {
if (receivers[i].isTrue()) return true;
}
return false;
}
public static boolean invokeEqqForCaselessWhen(IRubyObject receiver) {
return receiver.isTrue();
}
public static boolean invokeEqqForCaselessWhen(IRubyObject receiver0, IRubyObject receiver1) {
return receiver0.isTrue() || receiver1.isTrue();
}
public static boolean invokeEqqForCaselessWhen(IRubyObject receiver0, IRubyObject receiver1, IRubyObject receiver2) {
return receiver0.isTrue() || receiver1.isTrue() || receiver2.isTrue();
}
public static String[] parseBlockDescriptor(String descriptor) {
String[] mangled = descriptor.split(":");
mangled[0] = JavaNameMangler.demangleMethodName(mangled[0]);
mangled[4] = JavaNameMangler.demangleMethodName(mangled[4]);
return mangled;
}
public static RubyClass getSingletonClass(Ruby runtime, IRubyObject receiver) {
if (receiver instanceof RubyFixnum || receiver instanceof RubySymbol) {
throw runtime.newTypeError("can't define singleton");
} else {
return receiver.getSingletonClass();
}
}
@Deprecated // no longer used - only been used by interface implementation
public static IRubyObject invokeMethodMissing(IRubyObject receiver, String name, IRubyObject[] args) {
ThreadContext context = receiver.getRuntime().getCurrentContext();
// store call information so method_missing impl can use it
context.setLastCallStatusAndVisibility(CallType.FUNCTIONAL, Visibility.PUBLIC);
if (name.equals("method_missing")) {
return RubyKernel.method_missing(context, receiver, args, Block.NULL_BLOCK);
}
IRubyObject[] newArgs = prepareMethodMissingArgs(args, context, name);
return invoke(context, receiver, "method_missing", newArgs, Block.NULL_BLOCK);
}
public static IRubyObject callMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType, IRubyObject[] args, Block block) {
return selectMethodMissing(context, receiver, visibility, name, callType).call(context, receiver, receiver.getMetaClass(), name, args, block);
}
public static IRubyObject callMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType, IRubyObject arg0, Block block) {
return selectMethodMissing(context, receiver, visibility, name, callType).call(context, receiver, receiver.getMetaClass(), name, arg0, block);
}
public static IRubyObject callMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType, IRubyObject arg0, IRubyObject arg1, Block block) {
return selectMethodMissing(context, receiver, visibility, name, callType).call(context, receiver, receiver.getMetaClass(), name, arg0, arg1, block);
}
public static IRubyObject callMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
return selectMethodMissing(context, receiver, visibility, name, callType).call(context, receiver, receiver.getMetaClass(), name, arg0, arg1, arg2, block);
}
public static IRubyObject callMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType, Block block) {
return selectMethodMissing(context, receiver, visibility, name, callType).call(context, receiver, receiver.getMetaClass(), name, block);
}
public static DynamicMethod selectMethodMissing(ThreadContext context, IRubyObject receiver, Visibility visibility, String name, CallType callType) {
Ruby runtime = context.runtime;
if (name.equals("method_missing")) {
return selectInternalMM(runtime, visibility, callType);
}
DynamicMethod methodMissing = receiver.getMetaClass().searchMethod("method_missing");
if (methodMissing.isUndefined() || methodMissing.equals(runtime.getDefaultMethodMissing())) {
return selectInternalMM(runtime, visibility, callType);
}
return new MethodMissingMethod(methodMissing, visibility, callType);
}
public static DynamicMethod selectMethodMissing(ThreadContext context, RubyClass selfClass, Visibility visibility, String name, CallType callType) {
Ruby runtime = context.runtime;
if (name.equals("method_missing")) {
return selectInternalMM(runtime, visibility, callType);
}
DynamicMethod methodMissing = selfClass.searchMethod("method_missing");
if (methodMissing.isUndefined() || methodMissing.equals(runtime.getDefaultMethodMissing())) {
return selectInternalMM(runtime, visibility, callType);
}
return new MethodMissingMethod(methodMissing, visibility, callType);
}
public static DynamicMethod selectMethodMissing(RubyClass selfClass, Visibility visibility, String name, CallType callType) {
Ruby runtime = selfClass.getClassRuntime();
if (name.equals("method_missing")) {
return selectInternalMM(runtime, visibility, callType);
}
DynamicMethod methodMissing = selfClass.searchMethod("method_missing");
if (methodMissing.isUndefined() || methodMissing.equals(runtime.getDefaultMethodMissing())) {
return selectInternalMM(runtime, visibility, callType);
}
return new MethodMissingMethod(methodMissing, visibility, callType);
}
public static final Map<String, String> map(String... keyValues) {
HashMap<String, String> map = new HashMap<String, String>(keyValues.length / 2);
for (int i = 0; i < keyValues.length;) {
map.put(keyValues[i++], keyValues[i++]);
}
return map;
}
public static boolean additionOverflowed(long original, long other, long result) {
return (~(original ^ other) & (original ^ result) & RubyFixnum.SIGN_BIT) != 0;
}
public static boolean subtractionOverflowed(long original, long other, long result) {
return (~(original ^ ~other) & (original ^ result) & RubyFixnum.SIGN_BIT) != 0;
}
public static Errno errnoFromException(Throwable t) {
if (t instanceof ClosedChannelException) {
return Errno.EBADF;
}
final String errorMessage = t.getMessage();
// TODO: this is kinda gross
if (errorMessage != null) {
// All errors to sysread should be SystemCallErrors, but on a closed stream
// Ruby returns an IOError. Java throws same exception for all errors so
// we resort to this hack...
switch (errorMessage) {
case "Bad file descriptor" : return Errno.EBADF;
case "File not open" : return null;
case "An established connection was aborted by the software in your host machine" : return Errno.ECONNABORTED;
case "Broken pipe" : return Errno.EPIPE;
case "Connection reset by peer" : return Errno.ECONNRESET;
case "An existing connection was forcibly closed by the remote host" : return Errno.ECONNRESET;
case "No space left on device" : return Errno.ENOSPC;
case "Too many open files" : return Errno.EMFILE;
case "Message too large" : // Alpine Linux
case "Message too long" : return Errno.EMSGSIZE;
}
if (Platform.IS_WINDOWS && errorMessage.contains("connection was aborted")) return Errno.ECONNRESET;
}
return null;
}
public static RubyModule getNthScopeModule(StaticScope scope, int depth) {
int n = depth;
while (n > 0) {
scope = scope.getEnclosingScope();
if (scope.getScopeType() != null) {
n--;
}
}
return scope.getModule();
}
public static RubyArray viewArgsArray(ThreadContext context, RubyArray rubyArray, int preArgsCount, int postArgsCount) {
int n = rubyArray.getLength();
if (preArgsCount + postArgsCount >= n) {
return RubyArray.newEmptyArray(context.runtime);
} else {
return (RubyArray)rubyArray.subseq(context.runtime.getArray(), preArgsCount, n - preArgsCount - postArgsCount, true);
}
}
public static Class[] getStaticMethodParams(Class target, int args) {
switch (args) {
case 0:
return new Class[] {target, ThreadContext.class, IRubyObject.class, Block.class};
case 1:
return new Class[] {target, ThreadContext.class, IRubyObject.class, IRubyObject.class, Block.class};
case 2:
return new Class[] {target, ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, Block.class};
case 3:
return new Class[] {target, ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, Block.class};
case 4:
return new Class[] {target, ThreadContext.class, IRubyObject.class, IRubyObject[].class, Block.class};
default:
throw new RuntimeException("unsupported arity: " + args);
}
}
public static String getStaticMethodSignature(String classname, int args) {
switch (args) {
case 0:
return sig(IRubyObject.class, "L" + classname + ";", ThreadContext.class, IRubyObject.class, Block.class);
case 1:
return sig(IRubyObject.class, "L" + classname + ";", ThreadContext.class, IRubyObject.class, IRubyObject.class, Block.class);
case 2:
return sig(IRubyObject.class, "L" + classname + ";", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, Block.class);
case 3:
return sig(IRubyObject.class, "L" + classname + ";", ThreadContext.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, IRubyObject.class, Block.class);
case 4:
return sig(IRubyObject.class, "L" + classname + ";", ThreadContext.class, IRubyObject.class, IRubyObject[].class, Block.class);
default:
throw new RuntimeException("unsupported arity: " + args);
}
}
private static class MethodMissingMethod extends DynamicMethod {
private final DynamicMethod delegate;
private final CallType lastCallStatus;
private final Visibility lastVisibility;
public MethodMissingMethod(DynamicMethod delegate, Visibility lastVisibility, CallType lastCallStatus) {
this.delegate = delegate;
this.lastCallStatus = lastCallStatus;
this.lastVisibility = lastVisibility;
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
context.setLastCallStatusAndVisibility(lastCallStatus, lastVisibility);
return this.delegate.call(context, self, clazz, "method_missing", prepareMethodMissingArgs(args, context, name), block);
}
@Override
public DynamicMethod dup() {
return this;
}
}
private static DynamicMethod selectInternalMM(Ruby runtime, Visibility visibility, CallType callType) {
if (visibility == Visibility.PRIVATE) {
return runtime.getPrivateMethodMissing();
} else if (visibility == Visibility.PROTECTED) {
return runtime.getProtectedMethodMissing();
} else if (callType == CallType.VARIABLE) {
return runtime.getVariableMethodMissing();
} else if (callType == CallType.SUPER) {
return runtime.getSuperMethodMissing();
} else {
return runtime.getNormalMethodMissing();
}
}
private static IRubyObject[] prepareMethodMissingArgs(IRubyObject[] args, ThreadContext context, String name) {
return ArraySupport.newCopy(context.runtime.newSymbol(name), args);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, Block block) {
return self.getMetaClass().finvoke(context, self, name, block);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0, Block block) {
return self.getMetaClass().finvoke(context, self, name, arg0, block);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
return self.getMetaClass().finvoke(context, self, name, arg0, arg1, block);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
return self.getMetaClass().finvoke(context, self, name, arg0, arg1, arg2, block);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject[] args, Block block) {
return self.getMetaClass().finvoke(context, self, name, args, block);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name) {
return self.getMetaClass().finvoke(context, self, name);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0) {
return self.getMetaClass().finvoke(context, self, name, arg0);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1) {
return self.getMetaClass().finvoke(context, self, name, arg0, arg1);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2) {
return self.getMetaClass().finvoke(context, self, name, arg0, arg1, arg2);
}
public static IRubyObject invoke(ThreadContext context, IRubyObject self, String name, IRubyObject... args) {
return self.getMetaClass().finvoke(context, self, name, args);
}
public static IRubyObject invokeAs(ThreadContext context, RubyClass asClass, IRubyObject self, String name, IRubyObject[] args, Block block) {
return asClass.finvoke(context, self, name, args, block);
}
public static IRubyObject invokeAs(ThreadContext context, RubyClass asClass, IRubyObject self, String name, Block block) {
return asClass.finvoke(context, self, name, block);
}
public static IRubyObject invokeAs(ThreadContext context, RubyClass asClass, IRubyObject self, String name, IRubyObject arg0, Block block) {
return asClass.finvoke(context, self, name, arg0, block);
}
public static IRubyObject invokeAs(ThreadContext context, RubyClass asClass, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, Block block) {
return asClass.finvoke(context, self, name, arg0, arg1, block);
}
public static IRubyObject invokeAs(ThreadContext context, RubyClass asClass, IRubyObject self, String name, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
return asClass.finvoke(context, self, name, arg0, arg1, arg2, block);
}
/**
* Same behavior as invoke, but uses the given caller object to check visibility if callType demands it.
*/
public static IRubyObject invokeFrom(ThreadContext context, IRubyObject caller, IRubyObject self, String name, IRubyObject[] args, CallType callType, Block block) {
return self.getMetaClass().invokeFrom(context, callType, caller, self, name, args, block);
}
/**
* Same behavior as invoke, but uses the given caller object to check visibility if callType demands it.
*/
public static IRubyObject invokeFrom(ThreadContext context, IRubyObject caller, IRubyObject self, String name, IRubyObject arg, CallType callType, Block block) {
return self.getMetaClass().invokeFrom(context, callType, caller, self, name, arg, block);
}
/**
* Same behavior as invoke, but uses the given caller object to check visibility if callType demands it.
*/
public static IRubyObject invokeFrom(ThreadContext context, IRubyObject caller, IRubyObject self, String name, CallType callType) {
return self.getMetaClass().invokeFrom(context, callType, caller, self, name, IRubyObject.NULL_ARRAY, Block.NULL_BLOCK);
}
// MRI: rb_check_funcall
public static IRubyObject invokeChecked(ThreadContext context, IRubyObject self, String name) {
return self.getMetaClass().finvokeChecked(context, self, name);
}
// MRI: rb_check_funcall
public static IRubyObject invokeChecked(ThreadContext context, IRubyObject self, JavaSites.CheckedSites sites) {
return self.getMetaClass().finvokeChecked(context, self, sites);
}
// MRI: rb_check_funcall
public static IRubyObject invokeChecked(ThreadContext context, IRubyObject self, String name, IRubyObject... args) {
return self.getMetaClass().finvokeChecked(context, self, name, args);
}
// MRI: rb_check_funcall
public static IRubyObject invokeChecked(ThreadContext context, IRubyObject self, JavaSites.CheckedSites sites, IRubyObject... args) {
return self.getMetaClass().finvokeChecked(context, self, sites, args);
}
/**
* The protocol for super method invocation is a bit complicated
* in Ruby. In real terms it involves first finding the real
* implementation class (the super class), getting the name of the
* method to call from the frame, and then invoke that on the
* super class with the current self as the actual object
* invoking.
*/
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, IRubyObject[] args, Block block) {
return invokeSuper(context, self, context.getFrameKlazz(), context.getFrameName(), args, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, RubyModule klass, String name, IRubyObject[] args, Block block) {
checkSuperDisabledOrOutOfMethod(context, klass, name);
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klass).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, args, block);
}
return method.call(context, self, superClass, name, args, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, RubyModule klass, String name, IRubyObject arg0, Block block) {
checkSuperDisabledOrOutOfMethod(context, klass, name);
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klass).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, arg0, block);
}
return method.call(context, self, superClass, name, arg0, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, Block block) {
checkSuperDisabledOrOutOfMethod(context);
RubyModule klazz = context.getFrameKlazz();
String name = context.getFrameName();
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klazz).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, block);
}
return method.call(context, self, superClass, name, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, IRubyObject arg0, Block block) {
checkSuperDisabledOrOutOfMethod(context);
RubyModule klazz = context.getFrameKlazz();
String name = context.getFrameName();
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klazz).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, arg0, block);
}
return method.call(context, self, superClass, name, arg0, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, IRubyObject arg0, IRubyObject arg1, Block block) {
checkSuperDisabledOrOutOfMethod(context);
RubyModule klazz = context.getFrameKlazz();
String name = context.getFrameName();
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klazz).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, arg0, arg1, block);
}
return method.call(context, self, superClass, name, arg0, arg1, block);
}
public static IRubyObject invokeSuper(ThreadContext context, IRubyObject self, IRubyObject arg0, IRubyObject arg1, IRubyObject arg2, Block block) {
checkSuperDisabledOrOutOfMethod(context);
RubyModule klazz = context.getFrameKlazz();
String name = context.getFrameName();
RubyClass superClass = findImplementerIfNecessary(self.getMetaClass(), klazz).getSuperClass();
DynamicMethod method = superClass != null ? superClass.searchMethod(name) : UndefinedMethod.INSTANCE;
if (method.isUndefined()) {
return callMethodMissing(context, self, method.getVisibility(), name, CallType.SUPER, arg0, arg1, arg2, block);
}
return method.call(context, self, superClass, name, arg0, arg1, arg2, block);
}
public static RubyArray ensureRubyArray(IRubyObject value) {
return ensureRubyArray(value.getRuntime(), value);
}
public static RubyArray ensureRubyArray(Ruby runtime, IRubyObject value) {
return value instanceof RubyArray ? (RubyArray)value : RubyArray.newArray(runtime, value);
}
public static IRubyObject nullToNil(IRubyObject value, ThreadContext context) {
return value != null ? value : context.nil;
}
public static IRubyObject nullToNil(IRubyObject value, Ruby runtime) {
return value != null ? value : runtime.getNil();
}
public static IRubyObject nullToNil(IRubyObject value, IRubyObject nil) {
return value != null ? value : nil;
}
public static RubyClass prepareSuperClass(Ruby runtime, IRubyObject rubyClass) {
RubyClass.checkInheritable(rubyClass); // use the same logic as in EvaluationState
return (RubyClass)rubyClass;
}
public static RubyModule prepareClassNamespace(ThreadContext context, StaticScope scope, IRubyObject rubyModule) {
if (rubyModule == null || rubyModule.isNil()) {
rubyModule = scope.getModule();
if (rubyModule == null) {
throw context.runtime.newTypeError("no outer class/module");
}
}
if (rubyModule instanceof RubyModule) {
return (RubyModule)rubyModule;
} else {
throw context.runtime.newTypeError(rubyModule + " is not a class/module");
}
}
public static void handleArgumentSizes(ThreadContext context, Ruby runtime, int given, int required, int opt, int rest) {
if (opt == 0) {
if (rest < 0) {
// no opt, no rest, exact match
if (given != required) {
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
}
} else {
// only rest, must be at least required
if (given < required) {
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
}
}
} else {
if (rest < 0) {
// opt but no rest, must be at least required and no more than required + opt
if (given < required) {
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
} else if (given > (required + opt)) {
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + (required + opt) + ")");
}
} else {
// opt and rest, must be at least required
if (given < required) {
throw runtime.newArgumentError("wrong number of arguments (" + given + " for " + required + ")");
}
}
}
}
public static String getLocalJumpTypeOrRethrow(RaiseException re) {
RubyException exception = re.getException();
Ruby runtime = exception.getRuntime();
if (runtime.getLocalJumpError().isInstance(exception)) {
RubyLocalJumpError jumpError = (RubyLocalJumpError)re.getException();
IRubyObject reason = jumpError.reason();
return reason.asJavaString();
}
throw re;
}
public static IRubyObject unwrapLocalJumpErrorValue(RaiseException re) {
return ((RubyLocalJumpError)re.getException()).exit_value();
}
public static IRubyObject processBlockArgument(Ruby runtime, Block block) {
if (!block.isGiven()) {
return runtime.getNil();
}
return processGivenBlock(block, runtime);
}
private static IRubyObject processGivenBlock(Block block, Ruby runtime) {
RubyProc blockArg = block.getProcObject();
if (blockArg == null) {
blockArg = runtime.newBlockPassProc(Block.Type.PROC, block);
blockArg.getBlock().type = Block.Type.PROC;
}
return blockArg;
}
public static Block getBlockFromBlockPassBody(Ruby runtime, IRubyObject proc, Block currentBlock) {
// No block from a nil proc
if (proc.isNil()) return Block.NULL_BLOCK;
// If not already a proc then we should try and make it one.
if (!(proc instanceof RubyProc)) {
proc = coerceProc(proc, runtime);
}
return getBlockFromProc(currentBlock, proc);
}
private static IRubyObject coerceProc(IRubyObject maybeProc, Ruby runtime) throws RaiseException {
IRubyObject proc = TypeConverter.convertToType(maybeProc, runtime.getProc(), "to_proc", false);
if (!(proc instanceof RubyProc)) {
throw runtime.newTypeError("wrong argument type " + maybeProc.getMetaClass().getName() + " (expected Proc)");
}
return proc;
}
private static Block getBlockFromProc(Block currentBlock, IRubyObject proc) {
// TODO: Add safety check for taintedness
if (currentBlock != null && currentBlock.isGiven()) {
RubyProc procObject = currentBlock.getProcObject();
// The current block is already associated with proc. No need to create a new one
if (procObject != null && procObject == proc) {
return currentBlock;
}
}
return ((RubyProc) proc).getBlock();
}
public static Block getBlockFromBlockPassBody(IRubyObject proc, Block currentBlock) {
return getBlockFromBlockPassBody(proc.getRuntime(), proc, currentBlock);
}
public static IRubyObject backref(ThreadContext context) {
return RubyRegexp.getBackRef(context);
}
public static IRubyObject backrefLastMatch(ThreadContext context) {
IRubyObject backref = context.getBackRef();
return RubyRegexp.last_match(backref);
}
public static IRubyObject backrefMatchPre(ThreadContext context) {
IRubyObject backref = context.getBackRef();
return RubyRegexp.match_pre(backref);
}
public static IRubyObject backrefMatchPost(ThreadContext context) {
IRubyObject backref = context.getBackRef();
return RubyRegexp.match_post(backref);
}
public static IRubyObject backrefMatchLast(ThreadContext context) {
IRubyObject backref = context.getBackRef();
return RubyRegexp.match_last(backref);
}
public static IRubyObject[] appendToObjectArray(IRubyObject[] array, IRubyObject add) {
return ArraySupport.newCopy(array, add);
}
public static IRubyObject breakLocalJumpError(Ruby runtime, IRubyObject value) {
throw runtime.newLocalJumpError(RubyLocalJumpError.Reason.BREAK, value, "unexpected break");
}
public static IRubyObject[] concatObjectArrays(IRubyObject[] array, IRubyObject[] add) {
return toArray(array, add);
}
public static IRubyObject[] toArray(IRubyObject[] array, IRubyObject... rest) {
final int len = array.length;
IRubyObject[] newArray = new IRubyObject[len + rest.length];
ArraySupport.copy(array, newArray, 0, len);
ArraySupport.copy(rest, newArray, len, rest.length);
return newArray;
}
public static IRubyObject[] toArray(IRubyObject obj, IRubyObject... rest) {
return ArraySupport.newCopy(obj, rest);
}
public static IRubyObject[] toArray(IRubyObject obj0, IRubyObject obj1, IRubyObject... rest) {
IRubyObject[] newArray = new IRubyObject[2 + rest.length];
newArray[0] = obj0;
newArray[1] = obj1;
ArraySupport.copy(rest, newArray, 2, rest.length);
return newArray;
}
public static IRubyObject[] toArray(IRubyObject obj0, IRubyObject obj1, IRubyObject obj2, IRubyObject... rest) {
IRubyObject[] newArray = new IRubyObject[3 + rest.length];
newArray[0] = obj0;
newArray[1] = obj1;
newArray[2] = obj2;
ArraySupport.copy(rest, newArray, 3, rest.length);
return newArray;
}
public static IRubyObject isExceptionHandled(RubyException currentException, IRubyObject[] exceptions, ThreadContext context) {
for (int i = 0; i < exceptions.length; i++) {
IRubyObject result = isExceptionHandled(currentException, exceptions[i], context);
if (result.isTrue()) return result;
}
return context.runtime.getFalse();
}
public static IRubyObject isExceptionHandled(RubyException currentException, IRubyObject exception, ThreadContext context) {
return isExceptionHandled((IRubyObject) currentException, exception, context);
}
public static IRubyObject isExceptionHandled(IRubyObject currentException, IRubyObject exception, ThreadContext context) {
Ruby runtime = context.runtime;
if (!runtime.getModule().isInstance(exception)) {
throw runtime.newTypeError("class or module required for rescue clause");
}
IRubyObject result = invoke(context, exception, "===", currentException);
if (result.isTrue()) return result;
return runtime.getFalse();
}
public static IRubyObject isExceptionHandled(RubyException currentException, IRubyObject exception0, IRubyObject exception1, ThreadContext context) {
IRubyObject result = isExceptionHandled(currentException, exception0, context);
if (result.isTrue()) return result;
return isExceptionHandled(currentException, exception1, context);
}
public static IRubyObject isExceptionHandled(RubyException currentException, IRubyObject exception0, IRubyObject exception1, IRubyObject exception2, ThreadContext context) {
IRubyObject result = isExceptionHandled(currentException, exception0, context);
if (result.isTrue()) return result;
return isExceptionHandled(currentException, exception1, exception2, context);
}
public static boolean checkJavaException(final IRubyObject wrappedEx, final Throwable ex, IRubyObject catchable, ThreadContext context) {
final Ruby runtime = context.runtime;
if (
// rescue exception needs to catch Java exceptions
runtime.getException() == catchable ||
// rescue Object needs to catch Java exceptions
runtime.getObject() == catchable ||
// rescue StandardError needs to catch Java exceptions
runtime.getStandardError() == catchable) {
if (ex instanceof RaiseException) {
return isExceptionHandled(((RaiseException) ex).getException(), catchable, context).isTrue();
}
// let Ruby exceptions decide if they handle it
return isExceptionHandled(wrappedEx, catchable, context).isTrue();
}
if (runtime.getNativeException() == catchable) {
// NativeException catches Java exceptions, lazily creating the wrapper
return true;
}
if (catchable instanceof RubyClass && JavaClass.isProxyType(context, (RubyClass) catchable)) {
if ( ex instanceof InternalJavaProxy ) { // Ruby sub-class of a Java exception type
final IRubyObject target = ((InternalJavaProxy) ex).___getInvocationHandler().getOrig();
if ( target != null ) return ((RubyClass) catchable).isInstance(target);
}
return ((RubyClass) catchable).isInstance(wrappedEx);
}
if (catchable instanceof RubyModule) {
IRubyObject result = invoke(context, catchable, "===", wrappedEx);
return result.isTrue();
}
return false;
}
public static boolean checkJavaException(final Throwable ex, IRubyObject catchable, ThreadContext context) {
return checkJavaException(wrapJavaException(context.runtime, ex), ex, catchable, context);
}
// wrapJavaException(runtime, ex)
public static IRubyObject wrapJavaException(final Ruby runtime, final Throwable ex) {
return JavaUtil.convertJavaToUsableRubyObject(runtime, ex);
}
@Deprecated // due deprecated checkJavaException
public static IRubyObject isJavaExceptionHandled(Throwable currentThrowable, IRubyObject[] throwables, ThreadContext context) {
if (currentThrowable instanceof Unrescuable) {
throwException(currentThrowable);
}
if (currentThrowable instanceof RaiseException) {
return isExceptionHandled(((RaiseException) currentThrowable).getException(), throwables, context);
} else {
if (throwables.length == 0) {
// no rescue means StandardError, which rescues Java exceptions
return context.runtime.getTrue();
} else {
for (int i = 0; i < throwables.length; i++) {
if (checkJavaException(currentThrowable, throwables[i], context)) {
return context.runtime.getTrue();
}
}
}
return context.runtime.getFalse();
}
}
@Deprecated // due deprecated checkJavaException
public static IRubyObject isJavaExceptionHandled(Throwable currentThrowable, IRubyObject throwable, ThreadContext context) {
if (currentThrowable instanceof Unrescuable) {
throwException(currentThrowable);
}
if (currentThrowable instanceof RaiseException) {
return isExceptionHandled(((RaiseException) currentThrowable).getException(), throwable, context);
} else {
if (checkJavaException(currentThrowable, throwable, context)) {
return context.runtime.getTrue();
}
return context.runtime.getFalse();
}
}
@Deprecated // due deprecated checkJavaException
public static IRubyObject isJavaExceptionHandled(Throwable currentThrowable, IRubyObject throwable0, IRubyObject throwable1, ThreadContext context) {
if (currentThrowable instanceof Unrescuable) {
throwException(currentThrowable);
}
if (currentThrowable instanceof RaiseException) {
return isExceptionHandled(((RaiseException)currentThrowable).getException(), throwable0, throwable1, context);
} else {
if (checkJavaException(currentThrowable, throwable0, context)) {
return context.runtime.getTrue();
}
if (checkJavaException(currentThrowable, throwable1, context)) {
return context.runtime.getTrue();
}
return context.runtime.getFalse();
}
}
@Deprecated // due deprecated checkJavaException
public static IRubyObject isJavaExceptionHandled(Throwable currentThrowable, IRubyObject throwable0, IRubyObject throwable1, IRubyObject throwable2, ThreadContext context) {
if (currentThrowable instanceof Unrescuable) {
throwException(currentThrowable);
}
if (currentThrowable instanceof RaiseException) {
return isExceptionHandled(((RaiseException)currentThrowable).getException(), throwable0, throwable1, throwable2, context);
} else {
if (checkJavaException(currentThrowable, throwable0, context)) {
return context.runtime.getTrue();
}
if (checkJavaException(currentThrowable, throwable1, context)) {
return context.runtime.getTrue();
}
if (checkJavaException(currentThrowable, throwable2, context)) {
return context.runtime.getTrue();
}
return context.runtime.getFalse();
}
}
public static void storeExceptionInErrorInfo(Throwable currentThrowable, ThreadContext context) {
IRubyObject exception;
if (currentThrowable instanceof RaiseException) {
exception = ((RaiseException)currentThrowable).getException();
} else {
exception = JavaUtil.convertJavaToUsableRubyObject(context.runtime, currentThrowable);
}
context.setErrorInfo(exception);
}
public static void storeNativeExceptionInErrorInfo(Throwable currentThrowable, ThreadContext context) {
IRubyObject exception;
if (currentThrowable instanceof RaiseException) {
exception = ((RaiseException)currentThrowable).getException();
} else {
Ruby runtime = context.runtime;
// wrap Throwable in a NativeException object
exception = new NativeException(runtime, runtime.getNativeException(), currentThrowable);
((NativeException)exception).prepareIntegratedBacktrace(context, currentThrowable.getStackTrace());
}
context.setErrorInfo(exception);
}
public static void clearErrorInfo(ThreadContext context) {
context.setErrorInfo(context.runtime.getNil());
}
public static void checkSuperDisabledOrOutOfMethod(ThreadContext context) {
checkSuperDisabledOrOutOfMethod(context, context.getFrameKlazz(), context.getFrameName());
}
public static void checkSuperDisabledOrOutOfMethod(ThreadContext context, RubyModule klass, String name) {
if (klass == null) {
if (name != null) {
throw context.runtime.newNameError("superclass method '" + name + "' disabled", name);
}
}
if (name == null) {
throw context.runtime.newNoMethodError("super called outside of method", null, context.nil);
}
}
public static Block ensureSuperBlock(Block given, Block parent) {
if (!given.isGiven()) {
return parent;
}
return given;