forked from jruby/jruby
/
RubyKernel.java
2119 lines (1777 loc) · 96 KB
/
RubyKernel.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
/*
***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2001 Chad Fowler <chadfowler@chadfowler.com>
* Copyright (C) 2001 Alan Moore <alan_moore@gmx.net>
* Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
* Copyright (C) 2001-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2002-2006 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
* Copyright (C) 2005 Kiel Hodges <jruby-devel@selfsosoft.com>
* Copyright (C) 2006 Evan Buswell <evan@heron.sytes.net>
* Copyright (C) 2006 Ola Bini <ola@ologix.com>
* Copyright (C) 2006 Michael Studman <codehaus@michaelstudman.com>
* Copyright (C) 2006 Miguel Covarrubias <mlcovarrubias@gmail.com>
* Copyright (C) 2007 Nick Sieger <nicksieger@gmail.com>
* Copyright (C) 2008 Joseph LaFata <joe@quibb.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby;
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JRubyModule;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.evaluator.ASTInterpreter;
import org.jruby.exceptions.MainExitException;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.methods.CallConfiguration;
import org.jruby.internal.runtime.methods.JavaMethod.JavaMethodNBlock;
import org.jruby.javasupport.util.RuntimeHelpers;
import org.jruby.platform.Platform;
import org.jruby.runtime.Binding;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallType;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.IAutoloadMethod;
import org.jruby.util.ByteList;
import org.jruby.util.ConvertBytes;
import org.jruby.util.IdUtil;
import org.jruby.util.ShellLauncher;
import org.jruby.util.TypeConverter;
import org.jruby.util.cli.Options;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Map;
import static org.jruby.CompatVersion.RUBY1_8;
import static org.jruby.CompatVersion.RUBY1_9;
import static org.jruby.RubyEnumerator.enumeratorize;
import static org.jruby.anno.FrameField.BACKREF;
import static org.jruby.anno.FrameField.LASTLINE;
import static org.jruby.anno.FrameField.METHODNAME;
import static org.jruby.runtime.Visibility.PRIVATE;
import static org.jruby.runtime.Visibility.PROTECTED;
import static org.jruby.runtime.Visibility.PUBLIC;
/**
* Note: For CVS history, see KernelModule.java.
*/
@JRubyModule(name="Kernel")
public class RubyKernel {
public final static Class<?> IRUBY_OBJECT = IRubyObject.class;
public static abstract class MethodMissingMethod extends JavaMethodNBlock {
public MethodMissingMethod(RubyModule implementationClass) {
super(implementationClass, Visibility.PRIVATE, CallConfiguration.FrameFullScopeNone);
}
@Override
public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return methodMissing(context, self, clazz, name, args, block);
}
public abstract IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block);
}
public static RubyModule createKernelModule(Ruby runtime) {
RubyModule module = runtime.defineModule("Kernel");
runtime.setKernel(module);
module.defineAnnotatedMethods(RubyKernel.class);
module.setFlag(RubyObject.USER7_F, false); //Kernel is the only Module that doesn't need an implementor
runtime.setPrivateMethodMissing(new MethodMissingMethod(module) {
@Override
public IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return RubyKernel.methodMissing(context, self, name, PRIVATE, CallType.NORMAL, args, block);
}
});
runtime.setProtectedMethodMissing(new MethodMissingMethod(module) {
@Override
public IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return RubyKernel.methodMissing(context, self, name, PROTECTED, CallType.NORMAL, args, block);
}
});
runtime.setVariableMethodMissing(new MethodMissingMethod(module) {
@Override
public IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return RubyKernel.methodMissing(context, self, name, PUBLIC, CallType.VARIABLE, args, block);
}
});
runtime.setSuperMethodMissing(new MethodMissingMethod(module) {
@Override
public IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return RubyKernel.methodMissing(context, self, name, PUBLIC, CallType.SUPER, args, block);
}
});
runtime.setNormalMethodMissing(new MethodMissingMethod(module) {
@Override
public IRubyObject methodMissing(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
return RubyKernel.methodMissing(context, self, name, PUBLIC, CallType.NORMAL, args, block);
}
});
recacheBuiltinMethods(runtime);
return module;
}
/**
* Cache built-in versions of several core methods, to improve performance by using identity comparison (==) rather
* than going ahead with dynamic dispatch.
*
* @param runtime
*/
static void recacheBuiltinMethods(Ruby runtime) {
RubyModule module = runtime.getKernel();
runtime.setRespondToMethod(module.searchMethod("respond_to?"));
if (!runtime.is1_9()) { // method_missing is in BasicObject in 1.9
runtime.setDefaultMethodMissing(module.searchMethod("method_missing"));
}
}
@JRubyMethod(module = true, visibility = PRIVATE)
public static IRubyObject at_exit(ThreadContext context, IRubyObject recv, Block block) {
return context.runtime.pushExitBlock(context.runtime.newProc(Block.Type.PROC, block));
}
@JRubyMethod(name = "autoload?", required = 1, module = true, visibility = PRIVATE)
public static IRubyObject autoload_p(ThreadContext context, final IRubyObject recv, IRubyObject symbol) {
Ruby runtime = context.runtime;
final RubyModule module = getModuleForAutoload(runtime, recv);
String name = symbol.asJavaString();
String file = module.getAutoloadFile(name);
return (file == null) ? runtime.getNil() : runtime.newString(file);
}
@JRubyMethod(required = 2, module = true, visibility = PRIVATE)
public static IRubyObject autoload(final IRubyObject recv, IRubyObject symbol, final IRubyObject file) {
Ruby runtime = recv.getRuntime();
String nonInternedName = symbol.asJavaString();
if (!IdUtil.isValidConstantName(nonInternedName)) {
throw runtime.newNameError("autoload must be constant name", nonInternedName);
}
if (!runtime.is1_9() && !(file instanceof RubyString)) throw runtime.newTypeError(file, runtime.getString());
RubyString fileString = RubyFile.get_path(runtime.getCurrentContext(), file);
if (fileString.isEmpty()) throw runtime.newArgumentError("empty file name");
final String baseName = symbol.asJavaString().intern(); // interned, OK for "fast" methods
final RubyModule module = getModuleForAutoload(runtime, recv);
IRubyObject existingValue = module.fetchConstant(baseName);
if (existingValue != null && existingValue != RubyObject.UNDEF) return runtime.getNil();
module.defineAutoload(baseName, new IAutoloadMethod() {
public String file() {
return file.toString();
}
public void load(Ruby runtime) {
final ThreadContext context = runtime.getCurrentContext();
if (runtime.getKernel().callMethod(context, "require", file).isTrue()) {
// Do not finish autoloading by cyclic autoload
module.finishAutoload(baseName);
}
}
});
return runtime.getNil();
}
private static RubyModule getModuleForAutoload(Ruby runtime, IRubyObject recv) {
RubyModule module = recv instanceof RubyModule ? (RubyModule) recv : recv.getMetaClass().getRealClass();
if (module == runtime.getKernel()) {
// special behavior if calling Kernel.autoload directly
if (runtime.is1_9()) {
module = runtime.getObject().getSingletonClass();
} else {
module = runtime.getObject();
}
}
return module;
}
@JRubyMethod(rest = true, frame = true, visibility = PRIVATE, compat = RUBY1_8)
public static IRubyObject method_missing(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Visibility lastVis = context.getLastVisibility();
CallType lastCallType = context.getLastCallType();
if (args.length == 0 || !(args[0] instanceof RubySymbol)) {
throw context.runtime.newArgumentError("no id given");
}
return methodMissingDirect(context, recv, (RubySymbol)args[0], lastVis, lastCallType, args, block);
}
protected static IRubyObject methodMissingDirect(ThreadContext context, IRubyObject recv, RubySymbol symbol, Visibility lastVis, CallType lastCallType, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
// create a lightweight thunk
IRubyObject msg = new RubyNameError.RubyNameErrorMessage(runtime,
recv,
symbol,
lastVis,
lastCallType);
final IRubyObject[]exArgs;
final RubyClass exc;
if (lastCallType != CallType.VARIABLE) {
exc = runtime.getNoMethodError();
exArgs = new IRubyObject[]{msg, symbol, RubyArray.newArrayNoCopy(runtime, args, 1)};
} else {
exc = runtime.getNameError();
exArgs = new IRubyObject[]{msg, symbol};
}
throw new RaiseException((RubyException)exc.newInstance(context, exArgs, Block.NULL_BLOCK));
}
private static IRubyObject methodMissing(ThreadContext context, IRubyObject recv, String name, Visibility lastVis, CallType lastCallType, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
RubySymbol symbol = runtime.newSymbol(name);
// create a lightweight thunk
IRubyObject msg = new RubyNameError.RubyNameErrorMessage(runtime,
recv,
symbol,
lastVis,
lastCallType);
final IRubyObject[]exArgs;
final RubyClass exc;
if (lastCallType != CallType.VARIABLE) {
exc = runtime.getNoMethodError();
exArgs = new IRubyObject[]{msg, symbol, RubyArray.newArrayNoCopy(runtime, args)};
} else {
exc = runtime.getNameError();
exArgs = new IRubyObject[]{msg, symbol};
}
throw new RaiseException((RubyException)exc.newInstance(context, exArgs, Block.NULL_BLOCK));
}
private static IRubyObject[] popenArgs(Ruby runtime, String pipedArg, IRubyObject[] args) {
IRubyObject command = runtime.newString(pipedArg.substring(1));
if (args.length >= 2) return new IRubyObject[] { command, args[1] };
return new IRubyObject[] { command };
}
@JRubyMethod(required = 1, optional = 2, module = true, visibility = PRIVATE, compat = RUBY1_8)
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
String arg = args[0].convertToString().toString();
Ruby runtime = context.runtime;
// exec process, create IO with process
if (arg.startsWith("|")) return RubyIO.popen(context, runtime.getIO(), popenArgs(runtime, arg, args), block);
return RubyFile.open(context, runtime.getFile(), args, block);
}
@JRubyMethod(name = "open", required = 1, optional = 2, module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
if (args[0].respondsTo("to_open")) {
args[0] = args[0].callMethod(context, "to_open");
return RubyFile.open(context, runtime.getFile(), args, block);
}
String arg = args[0].convertToString().toString();
// exec process, create IO with process
if (arg.startsWith("|")) return RubyIO.popen19(context, runtime.getIO(), popenArgs(runtime, arg, args), block);
return RubyFile.open(context, runtime.getFile(), args, block);
}
@JRubyMethod(name = "getc", module = true, visibility = PRIVATE)
public static IRubyObject getc(ThreadContext context, IRubyObject recv) {
context.runtime.getWarnings().warn(ID.DEPRECATED_METHOD, "getc is obsolete; use STDIN.getc instead");
IRubyObject defin = context.runtime.getGlobalVariables().get("$stdin");
return defin.callMethod(context, "getc");
}
@JRubyMethod(name = "gets", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject gets(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
return RubyArgsFile.gets(context, context.runtime.getArgsFile(), args);
}
@JRubyMethod(name = "abort", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject abort(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
if(args.length == 1) {
runtime.getGlobalVariables().get("$stderr").callMethod(context,"puts",args[0].convertToString());
}
exit(runtime, new IRubyObject[] { runtime.getFalse() }, false);
return runtime.getNil(); // not reached
}
@JRubyMethod(name = "Array", required = 1, module = true, visibility = PRIVATE)
public static IRubyObject new_array(ThreadContext context, IRubyObject recv, IRubyObject object) {
return RuntimeHelpers.arrayValue(context, context.runtime, object);
}
@JRubyMethod(name = "Complex", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_complex(ThreadContext context, IRubyObject recv) {
return RuntimeHelpers.invoke(context, context.runtime.getComplex(), "convert");
}
@JRubyMethod(name = "Complex", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_complex(ThreadContext context, IRubyObject recv, IRubyObject arg) {
return RuntimeHelpers.invoke(context, context.runtime.getComplex(), "convert", arg);
}
@JRubyMethod(name = "Complex", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_complex(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
return RuntimeHelpers.invoke(context, context.runtime.getComplex(), "convert", arg0, arg1);
}
@JRubyMethod(name = "Rational", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_rational(ThreadContext context, IRubyObject recv) {
return RuntimeHelpers.invoke(context, context.runtime.getRational(), "convert");
}
@JRubyMethod(name = "Rational", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_rational(ThreadContext context, IRubyObject recv, IRubyObject arg) {
return RuntimeHelpers.invoke(context, context.runtime.getRational(), "convert", arg);
}
@JRubyMethod(name = "Rational", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_rational(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
return RuntimeHelpers.invoke(context, context.runtime.getRational(), "convert", arg0, arg1);
}
@JRubyMethod(name = "Float", module = true, visibility = PRIVATE, compat = RUBY1_8)
public static RubyFloat new_float(IRubyObject recv, IRubyObject object) {
if(object instanceof RubyFixnum){
return RubyFloat.newFloat(object.getRuntime(), ((RubyFixnum)object).getDoubleValue());
}else if(object instanceof RubyFloat){
return (RubyFloat)object;
}else if(object instanceof RubyBignum){
return RubyFloat.newFloat(object.getRuntime(), RubyBignum.big2dbl((RubyBignum)object));
}else if(object instanceof RubyString){
if(((RubyString) object).getByteList().getRealSize() == 0){ // rb_cstr_to_dbl case
throw recv.getRuntime().newArgumentError("invalid value for Float(): " + object.inspect());
}
return RubyNumeric.str2fnum(recv.getRuntime(),(RubyString)object,true);
}else if(object.isNil()){
throw recv.getRuntime().newTypeError("can't convert nil into Float");
} else {
RubyFloat rFloat = (RubyFloat)TypeConverter.convertToType(object, recv.getRuntime().getFloat(), "to_f");
if (Double.isNaN(rFloat.getDoubleValue())) throw recv.getRuntime().newArgumentError("invalid value for Float()");
return rFloat;
}
}
@JRubyMethod(name = "Float", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static RubyFloat new_float19(IRubyObject recv, IRubyObject object) {
Ruby runtime = recv.getRuntime();
if(object instanceof RubyFixnum){
return RubyFloat.newFloat(runtime, ((RubyFixnum)object).getDoubleValue());
} else if (object instanceof RubyFloat) {
return (RubyFloat)object;
} else if(object instanceof RubyBignum){
return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum)object));
} else if(object instanceof RubyString){
if(((RubyString) object).getByteList().getRealSize() == 0){ // rb_cstr_to_dbl case
throw runtime.newArgumentError("invalid value for Float(): " + object.inspect());
}
RubyString arg = (RubyString)object;
if (arg.toString().startsWith("0x")) {
return ConvertBytes.byteListToInum19(runtime, arg.getByteList(), 16, true).toFloat();
}
return RubyNumeric.str2fnum19(runtime, arg,true);
} else if(object.isNil()){
throw runtime.newTypeError("can't convert nil into Float");
} else {
return (RubyFloat)TypeConverter.convertToType19(object, runtime.getFloat(), "to_f");
}
}
@JRubyMethod(name = "Hash", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_hash(ThreadContext context, IRubyObject recv, IRubyObject arg) {
IRubyObject tmp;
Ruby runtime = recv.getRuntime();
if (arg.isNil()) return RubyHash.newHash(runtime);
tmp = TypeConverter.checkHashType(runtime, arg);
if (tmp.isNil()) {
if (arg instanceof RubyArray && ((RubyArray) arg).isEmpty()) {
return RubyHash.newHash(runtime);
}
throw runtime.newTypeError("can't convert " + arg.getMetaClass() + " into Hash");
}
return tmp;
}
@JRubyMethod(name = "Integer", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_8)
public static IRubyObject new_integer(ThreadContext context, IRubyObject recv, IRubyObject object) {
if (object instanceof RubyFloat) {
double val = ((RubyFloat)object).getDoubleValue();
if (val >= (double) RubyFixnum.MAX || val < (double) RubyFixnum.MIN) {
return RubyNumeric.dbl2num(context.runtime,((RubyFloat)object).getDoubleValue());
}
} else if (object instanceof RubyFixnum || object instanceof RubyBignum) {
return object;
} else if (object instanceof RubyString) {
return RubyNumeric.str2inum(context.runtime,(RubyString)object,0,true);
}
IRubyObject tmp = TypeConverter.convertToType(object, context.runtime.getInteger(), "to_int", false);
if (tmp.isNil()) return object.convertToInteger("to_i");
return tmp;
}
@JRubyMethod(name = "Integer", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_integer19(ThreadContext context, IRubyObject recv, IRubyObject object) {
if (object instanceof RubyFloat) {
double val = ((RubyFloat)object).getDoubleValue();
if (val > (double) RubyFixnum.MAX && val < (double) RubyFixnum.MIN) {
return RubyNumeric.dbl2num(context.runtime,((RubyFloat)object).getDoubleValue());
}
} else if (object instanceof RubyFixnum || object instanceof RubyBignum) {
return object;
} else if (object instanceof RubyString) {
return RubyNumeric.str2inum(context.runtime,(RubyString)object,0,true);
} else if(object instanceof RubyNil) {
throw context.runtime.newTypeError("can't convert nil into Integer");
}
IRubyObject tmp = TypeConverter.convertToType(object, context.runtime.getInteger(), "to_int", false);
if (tmp.isNil()) return object.convertToInteger("to_i");
return tmp;
}
@JRubyMethod(name = "Integer", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_integer19(ThreadContext context, IRubyObject recv, IRubyObject object, IRubyObject base) {
int bs = RubyNumeric.num2int(base);
if(object instanceof RubyString) {
return RubyNumeric.str2inum(context.runtime,(RubyString)object,bs,true);
} else {
IRubyObject tmp = object.checkStringType();
if(!tmp.isNil()) {
return RubyNumeric.str2inum(context.runtime,(RubyString)tmp,bs,true);
}
}
throw context.runtime.newArgumentError("base specified for non string value");
}
@JRubyMethod(name = "String", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_8)
public static IRubyObject new_string(ThreadContext context, IRubyObject recv, IRubyObject object) {
return TypeConverter.convertToType(object, context.runtime.getString(), "to_s");
}
@JRubyMethod(name = "String", required = 1, module = true, visibility = PRIVATE, compat = RUBY1_9)
public static IRubyObject new_string19(ThreadContext context, IRubyObject recv, IRubyObject object) {
return TypeConverter.convertToType19(object, context.runtime.getString(), "to_s");
}
@JRubyMethod(name = "p", rest = true, module = true, visibility = PRIVATE)
public static IRubyObject p(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
Ruby runtime = context.runtime;
IRubyObject defout = runtime.getGlobalVariables().get("$>");
for (int i = 0; i < args.length; i++) {
if (args[i] != null) {
defout.callMethod(context, "write", RubyObject.inspect(context, args[i]));
defout.callMethod(context, "write", runtime.newString("\n"));
}
}
IRubyObject result = runtime.getNil();
if (runtime.is1_9()) {
if (args.length == 1) {
result = args[0];
} else if (args.length > 1) {
result = runtime.newArray(args);
}
}
if (defout instanceof RubyFile) {
((RubyFile)defout).flush();
}
return result;
}
@JRubyMethod(name = "public_method",required = 1, module = true, compat = RUBY1_9)
public static IRubyObject public_method(ThreadContext context, IRubyObject recv, IRubyObject symbol) {
return recv.getMetaClass().newMethod(recv, symbol.asJavaString(), true, PUBLIC, true, false);
}
/** rb_f_putc
*/
@JRubyMethod(name = "putc", required = 1, module = true, visibility = PRIVATE)
public static IRubyObject putc(ThreadContext context, IRubyObject recv, IRubyObject ch) {
IRubyObject defout = context.runtime.getGlobalVariables().get("$>");
return RubyIO.putc(context, defout, ch);
}
@JRubyMethod(name = "puts", rest = true, module = true, visibility = PRIVATE)
public static IRubyObject puts(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
IRubyObject defout = context.runtime.getGlobalVariables().get("$>");
return RubyIO.puts(context, defout, args);
}
@JRubyMethod(name = "print", rest = true, module = true, visibility = PRIVATE)
public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
IRubyObject defout = context.runtime.getGlobalVariables().get("$>");
return RubyIO.print(context, defout, args);
}
@JRubyMethod(name = "printf", rest = true, module = true, visibility = PRIVATE)
public static IRubyObject printf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
if (args.length != 0) {
IRubyObject defout = context.runtime.getGlobalVariables().get("$>");
if (!(args[0] instanceof RubyString)) {
defout = args[0];
args = ArgsUtil.popArray(args);
}
defout.callMethod(context, "write", RubyKernel.sprintf(context, recv, args));
}
return context.runtime.getNil();
}
@JRubyMethod(name = "readline", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject readline(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
IRubyObject line = gets(context, recv, args);
if (line.isNil()) {
throw context.runtime.newEOFError();
}
return line;
}
@JRubyMethod(name = "readlines", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
return RubyArgsFile.readlines(context, context.runtime.getArgsFile(), args);
}
@JRubyMethod(name = "respond_to_missing?", module = true, compat = RUBY1_9)
public static IRubyObject respond_to_missing_p(ThreadContext context, IRubyObject recv, IRubyObject symbol) {
return context.runtime.getFalse();
}
@JRubyMethod(name = "respond_to_missing?", module = true, compat = RUBY1_9)
public static IRubyObject respond_to_missing_p(ThreadContext context, IRubyObject recv, IRubyObject symbol, IRubyObject isPrivate) {
return context.runtime.getFalse();
}
/** Returns value of $_.
*
* @throws TypeError if $_ is not a String or nil.
* @return value of $_ as String.
*/
private static RubyString getLastlineString(ThreadContext context, Ruby runtime) {
IRubyObject line = context.getCurrentScope().getLastLine(runtime);
if (line.isNil()) {
throw runtime.newTypeError("$_ value need to be String (nil given).");
} else if (!(line instanceof RubyString)) {
throw runtime.newTypeError("$_ value need to be String (" + line.getMetaClass().getName() + " given).");
} else {
return (RubyString) line;
}
}
@JRubyMethod(name = "sub!", module = true, visibility = PRIVATE, reads = LASTLINE, compat = RUBY1_8)
public static IRubyObject sub_bang(ThreadContext context, IRubyObject recv, IRubyObject arg0, Block block) {
return getLastlineString(context, context.runtime).sub_bang(context, arg0, block);
}
@JRubyMethod(name = "sub!", module = true, visibility = PRIVATE, reads = LASTLINE, compat = RUBY1_8)
public static IRubyObject sub_bang(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, Block block) {
return getLastlineString(context, context.runtime).sub_bang(context, arg0, arg1, block);
}
@JRubyMethod(name = "sub", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject sub(ThreadContext context, IRubyObject recv, IRubyObject arg0, Block block) {
RubyString str = (RubyString) getLastlineString(context, context.runtime).dup();
if (!str.sub_bang(context, arg0, block).isNil()) {
context.getCurrentScope().setLastLine(str);
}
return str;
}
@JRubyMethod(name = "sub", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject sub(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, Block block) {
RubyString str = (RubyString) getLastlineString(context, context.runtime).dup();
if (!str.sub_bang(context, arg0, arg1, block).isNil()) {
context.getCurrentScope().setLastLine(str);
}
return str;
}
@JRubyMethod(name = "gsub!", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject gsub_bang(ThreadContext context, IRubyObject recv, IRubyObject arg0, Block block) {
return getLastlineString(context, context.runtime).gsub_bang(context, arg0, block);
}
@JRubyMethod(name = "gsub!", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject gsub_bang(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, Block block) {
return getLastlineString(context, context.runtime).gsub_bang(context, arg0, arg1, block);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject gsub(ThreadContext context, IRubyObject recv, IRubyObject arg0, Block block) {
RubyString str = (RubyString) getLastlineString(context, context.runtime).dup();
if (!str.gsub_bang(context, arg0, block).isNil()) {
context.getCurrentScope().setLastLine(str);
}
return str;
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject gsub(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1, Block block) {
RubyString str = (RubyString) getLastlineString(context, context.runtime).dup();
if (!str.gsub_bang(context, arg0, arg1, block).isNil()) {
context.getCurrentScope().setLastLine(str);
}
return str;
}
@JRubyMethod(name = "chop!", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chop_bang(ThreadContext context, IRubyObject recv, Block block) {
return getLastlineString(context, context.runtime).chop_bang(context);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chop(ThreadContext context, IRubyObject recv, Block block) {
RubyString str = getLastlineString(context, context.runtime);
if (str.getByteList().getRealSize() > 0) {
str = (RubyString) str.dup();
str.chop_bang(context);
context.getCurrentScope().setLastLine(str);
}
return str;
}
@JRubyMethod(name = "chomp!", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chomp_bang(ThreadContext context, IRubyObject recv) {
return getLastlineString(context, context.runtime).chomp_bang(context);
}
@JRubyMethod(name = "chomp!", module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chomp_bang(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
return getLastlineString(context, context.runtime).chomp_bang(context, arg0);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chomp(ThreadContext context, IRubyObject recv) {
RubyString str = getLastlineString(context, context.runtime);
RubyString dup = (RubyString) str.dup();
if (dup.chomp_bang(context).isNil()) {
return str;
}
context.getCurrentScope().setLastLine(dup);
return dup;
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = LASTLINE, compat = RUBY1_8)
public static IRubyObject chomp(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
RubyString str = getLastlineString(context, context.runtime);
RubyString dup = (RubyString) str.dup();
if (dup.chomp_bang(context, arg0).isNil()) {
return str;
}
context.getCurrentScope().setLastLine(dup);
return dup;
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = {LASTLINE, BACKREF}, compat = RUBY1_8)
public static IRubyObject split(ThreadContext context, IRubyObject recv) {
return getLastlineString(context, context.runtime).split(context);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = {LASTLINE, BACKREF}, compat = RUBY1_8)
public static IRubyObject split(ThreadContext context, IRubyObject recv, IRubyObject arg0) {
return getLastlineString(context, context.runtime).split(context, arg0);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = LASTLINE, writes = {LASTLINE, BACKREF}, compat = RUBY1_8)
public static IRubyObject split(ThreadContext context, IRubyObject recv, IRubyObject arg0, IRubyObject arg1) {
return getLastlineString(context, context.runtime).split(context, arg0, arg1);
}
@JRubyMethod(module = true, visibility = PRIVATE, reads = {LASTLINE, BACKREF}, writes = {LASTLINE, BACKREF}, compat = RUBY1_8)
public static IRubyObject scan(ThreadContext context, IRubyObject recv, IRubyObject pattern, Block block) {
return getLastlineString(context, context.runtime).scan(context, pattern, block);
}
@JRubyMethod(required = 1, optional = 3, module = true, visibility = PRIVATE)
public static IRubyObject select(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
return RubyIO.select_static(context, context.runtime, args);
}
@JRubyMethod(optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject sleep(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
long milliseconds;
if (args.length == 0) {
// Zero sleeps forever
milliseconds = 0;
} else {
if (!(args[0] instanceof RubyNumeric)) {
throw context.runtime.newTypeError("can't convert " + args[0].getMetaClass().getName() + "into time interval");
}
milliseconds = (long) (args[0].convertToFloat().getDoubleValue() * 1000);
if (milliseconds < 0) {
throw context.runtime.newArgumentError("time interval must be positive");
} else if (milliseconds == 0) {
// Explicit zero in MRI returns immediately
return context.runtime.newFixnum(0);
}
}
long startTime = System.currentTimeMillis();
RubyThread rubyThread = context.getThread();
// Spurious wakeup-loop
do {
long loopStartTime = System.currentTimeMillis();
try {
// We break if we know this sleep was explicitly woken up/interrupted
if (!rubyThread.sleep(milliseconds)) break;
} catch (InterruptedException iExcptn) {
}
milliseconds -= (System.currentTimeMillis() - loopStartTime);
} while (milliseconds > 0);
return context.runtime.newFixnum(Math.round((System.currentTimeMillis() - startTime) / 1000.0));
}
// FIXME: Add at_exit and finalizers to exit, then make exit_bang not call those.
@JRubyMethod(name = "exit", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject exit(IRubyObject recv, IRubyObject[] args) {
exit(recv.getRuntime(), args, false);
return recv.getRuntime().getNil(); // not reached
}
@JRubyMethod(name = "exit!", optional = 1, module = true, visibility = PRIVATE)
public static IRubyObject exit_bang(IRubyObject recv, IRubyObject[] args) {
exit(recv.getRuntime(), args, true);
return recv.getRuntime().getNil(); // not reached
}
private static void exit(Ruby runtime, IRubyObject[] args, boolean hard) {
int status = hard ? 1 : 0;
if (args.length > 0) {
RubyObject argument = (RubyObject) args[0];
if (argument instanceof RubyBoolean) {
status = argument.isFalse() ? 1 : 0;
} else {
status = RubyNumeric.fix2int(argument);
}
}
if (hard) {
if (runtime.getInstanceConfig().isHardExit()) {
System.exit(status);
} else {
throw new MainExitException(status, true);
}
} else {
throw runtime.newSystemExit(status);
}
}
/** Returns an Array with the names of all global variables.
*
*/
@JRubyMethod(name = "global_variables", module = true, visibility = PRIVATE)
public static RubyArray global_variables(ThreadContext context, IRubyObject recv) {
Ruby runtime = context.runtime;
RubyArray globalVariables = runtime.newArray();
for (String globalVariableName : runtime.getGlobalVariables().getNames()) {
globalVariables.append(runtime.newString(globalVariableName));
}
return globalVariables;
}
// In 1.9, return symbols
@JRubyMethod(name = "global_variables", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static RubyArray global_variables19(ThreadContext context, IRubyObject recv) {
Ruby runtime = context.runtime;
RubyArray globalVariables = runtime.newArray();
for (String globalVariableName : runtime.getGlobalVariables().getNames()) {
globalVariables.append(runtime.newSymbol(globalVariableName));
}
return globalVariables;
}
/** Returns an Array with the names of all local variables.
*
*/
@JRubyMethod(name = "local_variables", module = true, visibility = PRIVATE)
public static RubyArray local_variables(ThreadContext context, IRubyObject recv) {
final Ruby runtime = context.runtime;
RubyArray localVariables = runtime.newArray();
for (String name: context.getCurrentScope().getAllNamesInScope()) {
if (IdUtil.isLocal(name)) localVariables.append(runtime.newString(name));
}
return localVariables;
}
// In 1.9, return symbols
@JRubyMethod(name = "local_variables", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static RubyArray local_variables19(ThreadContext context, IRubyObject recv) {
final Ruby runtime = context.runtime;
RubyArray localVariables = runtime.newArray();
for (String name: context.getCurrentScope().getAllNamesInScope()) {
if (IdUtil.isLocal(name)) localVariables.append(runtime.newSymbol(name));
}
return localVariables;
}
@JRubyMethod(module = true, visibility = PRIVATE)
public static RubyBinding binding(ThreadContext context, IRubyObject recv, Block block) {
return RubyBinding.newBinding(context.runtime, context.currentBinding(recv));
}
@JRubyMethod(name = "binding", module = true, visibility = PRIVATE, compat = RUBY1_9)
public static RubyBinding binding19(ThreadContext context, IRubyObject recv, Block block) {
return RubyBinding.newBinding(context.runtime, context.currentBinding());
}
@JRubyMethod(name = {"block_given?", "iterator?"}, module = true, visibility = PRIVATE)
public static RubyBoolean block_given_p(ThreadContext context, IRubyObject recv) {
return context.runtime.newBoolean(context.getCurrentFrame().getBlock().isGiven());
}
@Deprecated
public static IRubyObject sprintf(IRubyObject recv, IRubyObject[] args) {
return sprintf(recv.getRuntime().getCurrentContext(), recv, args);
}
@JRubyMethod(name = {"sprintf", "format"}, required = 1, rest = true, module = true, visibility = PRIVATE)
public static IRubyObject sprintf(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
if (args.length == 0) {
throw context.runtime.newArgumentError("sprintf must have at least one argument");
}
RubyString str = RubyString.stringValue(args[0]);
IRubyObject arg;
if (context.runtime.is1_9() && args.length == 2 && args[1] instanceof RubyHash) {
arg = args[1];
} else {
RubyArray newArgs = context.runtime.newArrayNoCopy(args);
newArgs.shift(context);
arg = newArgs;
}
return str.op_format(context, arg);
}
@JRubyMethod(name = {"raise", "fail"}, optional = 3, module = true, visibility = PRIVATE, omit = true)
public static IRubyObject raise(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
// FIXME: Pass block down?
Ruby runtime = context.runtime;
RaiseException raise;
switch (args.length) {
case 0:
IRubyObject lastException = runtime.getGlobalVariables().get("$!");
if (lastException.isNil()) {
raise = new RaiseException(runtime, runtime.getRuntimeError(), "", false);
} else {
// non RubyException value is allowed to be assigned as $!.
raise = new RaiseException((RubyException) lastException);
}
break;
case 1:
if (args[0] instanceof RubyString) {
raise = new RaiseException((RubyException) runtime.getRuntimeError().newInstance(context, args, block));
} else {
raise = new RaiseException(convertToException(runtime, args[0], null));
}
break;
case 2:
raise = new RaiseException(convertToException(runtime, args[0], args[1]));
break;
default:
raise = new RaiseException(convertToException(runtime, args[0], args[1]), args[2]);
break;
}
if (runtime.getDebug().isTrue()) {
printExceptionSummary(context, runtime, raise.getException());
}
throw raise;
}
private static RubyException convertToException(Ruby runtime, IRubyObject obj, IRubyObject optionalMessage) {
if (!obj.respondsTo("exception")) {
throw runtime.newTypeError("exception class/object expected");
}
IRubyObject exception;
if (optionalMessage == null) {
exception = obj.callMethod(runtime.getCurrentContext(), "exception");
} else {
exception = obj.callMethod(runtime.getCurrentContext(), "exception", optionalMessage);
}
try {
return (RubyException) exception;
} catch (ClassCastException cce) {
throw runtime.newTypeError("exception object expected");
}
}
private static void printExceptionSummary(ThreadContext context, Ruby runtime, RubyException rEx) {
RubyStackTraceElement[] elements = rEx.getBacktraceElements();
RubyStackTraceElement firstElement = elements.length > 0 ? elements[0] : new RubyStackTraceElement("", "", "(empty)", 0, false);
String msg = String.format("Exception `%s' at %s:%s - %s\n",
rEx.getMetaClass(),
firstElement.getFileName(), firstElement.getLineNumber(),
runtime.is1_9() ? TypeConverter.convertToType(rEx, runtime.getString(), "to_s") : rEx.convertToString().toString());
runtime.getErrorStream().print(msg);
}