/
CoreLibrary.java
789 lines (635 loc) · 34.9 KB
/
CoreLibrary.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
/*
* Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.core;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.Source;
import org.jcodings.specific.BIG5Encoding;
import org.jcodings.specific.EUCJPEncoding;
import org.jcodings.specific.SJISEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jruby.runtime.Constants;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.core.ArrayNodes;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.RubyCallStack;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.rubinius.RubiniusLibrary;
import org.jruby.truffle.translator.TranslatorDriver;
import org.jruby.util.cli.Options;
import org.jruby.util.cli.OutputStrings;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.LinkedHashMap;
import java.util.Map;
public class CoreLibrary {
private final RubyContext context;
@CompilerDirectives.CompilationFinal private RubyClass argumentErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass arrayClass;
@CompilerDirectives.CompilationFinal private RubyClass basicObjectClass;
@CompilerDirectives.CompilationFinal private RubyClass bignumClass;
@CompilerDirectives.CompilationFinal private RubyClass bindingClass;
@CompilerDirectives.CompilationFinal private RubyClass classClass;
@CompilerDirectives.CompilationFinal private RubyClass continuationClass;
@CompilerDirectives.CompilationFinal private RubyClass dirClass;
@CompilerDirectives.CompilationFinal private RubyClass encodingClass;
@CompilerDirectives.CompilationFinal private RubyClass exceptionClass;
@CompilerDirectives.CompilationFinal private RubyClass falseClass;
@CompilerDirectives.CompilationFinal private RubyClass fiberClass;
@CompilerDirectives.CompilationFinal private RubyClass fileClass;
@CompilerDirectives.CompilationFinal private RubyClass fixnumClass;
@CompilerDirectives.CompilationFinal private RubyClass floatClass;
@CompilerDirectives.CompilationFinal private RubyClass hashClass;
@CompilerDirectives.CompilationFinal private RubyClass integerClass;
@CompilerDirectives.CompilationFinal private RubyClass indexErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass ioClass;
@CompilerDirectives.CompilationFinal private RubyClass loadErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass localJumpErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass matchDataClass;
@CompilerDirectives.CompilationFinal private RubyClass moduleClass;
@CompilerDirectives.CompilationFinal private RubyClass nameErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass nilClass;
@CompilerDirectives.CompilationFinal private RubyClass noMethodErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass numericClass;
@CompilerDirectives.CompilationFinal private RubyClass objectClass;
@CompilerDirectives.CompilationFinal private RubyClass procClass;
@CompilerDirectives.CompilationFinal private RubyClass processClass;
@CompilerDirectives.CompilationFinal private RubyClass rangeClass;
@CompilerDirectives.CompilationFinal private RubyClass rangeErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass regexpClass;
@CompilerDirectives.CompilationFinal private RubyClass rubyTruffleErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass runtimeErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass standardErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass stringClass;
@CompilerDirectives.CompilationFinal private RubyClass symbolClass;
@CompilerDirectives.CompilationFinal private RubyClass syntaxErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass systemCallErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass systemExitClass;
@CompilerDirectives.CompilationFinal private RubyClass threadClass;
@CompilerDirectives.CompilationFinal private RubyClass timeClass;
@CompilerDirectives.CompilationFinal private RubyClass trueClass;
@CompilerDirectives.CompilationFinal private RubyClass typeErrorClass;
@CompilerDirectives.CompilationFinal private RubyClass zeroDivisionErrorClass;
@CompilerDirectives.CompilationFinal private RubyModule comparableModule;
@CompilerDirectives.CompilationFinal private RubyModule configModule;
@CompilerDirectives.CompilationFinal private RubyModule enumerableModule;
@CompilerDirectives.CompilationFinal private RubyModule errnoModule;
@CompilerDirectives.CompilationFinal private RubyModule gcModule;
@CompilerDirectives.CompilationFinal private RubyModule kernelModule;
@CompilerDirectives.CompilationFinal private RubyModule mathModule;
@CompilerDirectives.CompilationFinal private RubyModule objectSpaceModule;
@CompilerDirectives.CompilationFinal private RubyModule signalModule;
@CompilerDirectives.CompilationFinal private RubyModule truffleModule;
@CompilerDirectives.CompilationFinal private RubyModule truffleDebugModule;
@CompilerDirectives.CompilationFinal private RubyClass edomClass;
@CompilerDirectives.CompilationFinal private RubyClass encodingConverterClass;
@CompilerDirectives.CompilationFinal private RubyArray argv;
@CompilerDirectives.CompilationFinal private RubyBasicObject globalVariablesObject;
@CompilerDirectives.CompilationFinal private RubyBasicObject mainObject;
@CompilerDirectives.CompilationFinal private RubyNilClass nilObject;
@CompilerDirectives.CompilationFinal private RubyHash envHash;
private ArrayNodes.MinBlock arrayMinBlock;
private ArrayNodes.MaxBlock arrayMaxBlock;
@CompilerDirectives.CompilationFinal private RubiniusLibrary rubiniusLibrary;
public CoreLibrary(RubyContext context) {
this.context = context;
}
/**
* Convert a value to a {@code Float}, without doing any lookup.
*/
public static double toDouble(Object value) {
RubyNode.notDesignedForCompilation();
assert value != null;
if (value instanceof RubyNilClass || value instanceof RubyNilClass) {
return 0;
}
if (value instanceof Integer) {
return (int) value;
}
if (value instanceof RubyBignum) {
return ((RubyBignum) value).doubleValue();
}
if (value instanceof Double) {
return (double) value;
}
CompilerDirectives.transferToInterpreter();
throw new UnsupportedOperationException();
}
public static boolean fitsIntoInteger(long value) {
return value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE;
}
public void initialize() {
// Create the cyclic classes and modules
classClass = new RubyClass.RubyClassClass(context);
basicObjectClass = RubyClass.createBootClass(context, "BasicObject");
objectClass = RubyClass.createBootClass(context, "Object");
moduleClass = new RubyModule.RubyModuleClass(context);
// Close the cycles
classClass.unsafeSetLogicalClass(classClass);
objectClass.unsafeSetSuperclass(basicObjectClass);
moduleClass.unsafeSetSuperclass(objectClass);
classClass.unsafeSetSuperclass(moduleClass);
classClass.getAdoptedByLexicalParent(objectClass, null);
basicObjectClass.getAdoptedByLexicalParent(objectClass, null);
objectClass.getAdoptedByLexicalParent(objectClass, null);
moduleClass.getAdoptedByLexicalParent(objectClass, null);
// BasicObject knows itself
basicObjectClass.setConstant(null, "BasicObject", basicObjectClass);
// Create all other classes and modules
numericClass = new RubyClass(context, objectClass, objectClass, "Numeric");
integerClass = new RubyClass(context, objectClass, numericClass, "Integer");
exceptionClass = new RubyException.RubyExceptionClass(context, objectClass, objectClass, "Exception");
standardErrorClass = new RubyException.RubyExceptionClass(context, objectClass, exceptionClass, "StandardError");
ioClass = new RubyClass(context, objectClass, objectClass, "IO");
argumentErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "ArgumentError");
arrayClass = new RubyArray.RubyArrayClass(context, objectClass);
bignumClass = new RubyBignum.RubyBignumClass(context, objectClass, integerClass, "Bignum");
bindingClass = new RubyClass(context, objectClass, objectClass, "Binding");
comparableModule = new RubyModule(context, objectClass, "Comparable");
configModule = new RubyModule(context, objectClass, "Config");
continuationClass = new RubyClass(context, objectClass, objectClass, "Continuation");
dirClass = new RubyClass(context, objectClass, objectClass, "Dir");
encodingClass = new RubyEncoding.RubyEncodingClass(context, objectClass);
errnoModule = new RubyModule(context, objectClass, "Errno");
enumerableModule = new RubyModule(context, objectClass, "Enumerable");
falseClass = new RubyClass(context, objectClass, objectClass, "FalseClass");
fiberClass = new RubyFiber.RubyFiberClass(context, objectClass);
fileClass = new RubyClass(context, objectClass, ioClass, "File");
fixnumClass = new RubyClass(context, objectClass, integerClass, "Fixnum");
floatClass = new RubyClass(context, objectClass, numericClass, "Float");
gcModule = new RubyModule(context, objectClass, "GC");
hashClass = new RubyHash.RubyHashClass(context, objectClass);
indexErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "IndexError");
kernelModule = new RubyModule(context, objectClass, "Kernel");
loadErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "LoadError");
localJumpErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "LocalJumpError");
matchDataClass = new RubyClass(context, objectClass, objectClass, "MatchData");
mathModule = new RubyModule(context, objectClass, "Math");
nameErrorClass = new RubyClass(context, objectClass, standardErrorClass, "NameError");
nilClass = new RubyClass(context, objectClass, objectClass, "NilClass");
noMethodErrorClass = new RubyException.RubyExceptionClass(context, objectClass, nameErrorClass, "NoMethodError");
objectSpaceModule = new RubyModule(context, objectClass, "ObjectSpace");
procClass = new RubyProc.RubyProcClass(context, objectClass);
processClass = new RubyClass(context, objectClass, objectClass, "Process");
rangeClass = new RubyClass(context, objectClass, objectClass, "Range");
rangeErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "RangeError");
regexpClass = new RubyRegexp.RubyRegexpClass(context, objectClass);
rubyTruffleErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "RubyTruffleError");
runtimeErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "RuntimeError");
signalModule = new RubyModule(context, objectClass, "Signal");
stringClass = new RubyString.RubyStringClass(context, objectClass, objectClass, "String");
symbolClass = new RubyClass(context, objectClass, objectClass, "Symbol");
syntaxErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "SyntaxError");
systemCallErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "SystemCallError");
systemExitClass = new RubyException.RubyExceptionClass(context, objectClass, exceptionClass, "SystemExit");
threadClass = new RubyThread.RubyThreadClass(context, objectClass);
timeClass = new RubyTime.RubyTimeClass(context, objectClass);
trueClass = new RubyClass(context, objectClass, objectClass, "TrueClass");
truffleModule = new RubyModule(context, objectClass, "Truffle");
truffleDebugModule = new RubyModule(context, truffleModule, "Debug");
typeErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "TypeError");
zeroDivisionErrorClass = new RubyException.RubyExceptionClass(context, objectClass, standardErrorClass, "ZeroDivisionError");
encodingConverterClass = new RubyEncodingConverter.RubyEncodingConverterClass(context, encodingClass, objectClass);
// Includes
objectClass.include(null, kernelModule);
numericClass.include(null, comparableModule);
// Set constants
objectClass.setConstant(null, "RUBY_VERSION", RubyString.fromJavaString(stringClass, "2.1.0"));
objectClass.setConstant(null, "RUBY_PATCHLEVEL", 0);
objectClass.setConstant(null, "RUBY_ENGINE", RubyString.fromJavaString(stringClass, "jrubytruffle"));
objectClass.setConstant(null, "RUBY_PLATFORM", RubyString.fromJavaString(stringClass, "jvm"));
final LinkedHashMap<Object, Object> configHashMap = new LinkedHashMap<>();
configHashMap.put(RubyString.fromJavaString(stringClass, "ruby_install_name"), RubyString.fromJavaString(stringClass, "rubytruffle"));
configHashMap.put(RubyString.fromJavaString(stringClass, "RUBY_INSTALL_NAME"), RubyString.fromJavaString(stringClass, "rubytruffle"));
configHashMap.put(RubyString.fromJavaString(stringClass, "host_os"), RubyString.fromJavaString(stringClass, "unknown"));
configHashMap.put(RubyString.fromJavaString(stringClass, "exeext"), RubyString.fromJavaString(stringClass, ""));
configHashMap.put(RubyString.fromJavaString(stringClass, "EXEEXT"), RubyString.fromJavaString(stringClass, "rubytruffle"));
edomClass = new RubyException.RubyExceptionClass(context, errnoModule, systemCallErrorClass, "EDOM");
new RubyClass(context, errnoModule, systemCallErrorClass, "ENOENT");
new RubyClass(context, errnoModule, systemCallErrorClass, "EPERM");
new RubyClass(context, errnoModule, systemCallErrorClass, "ENOTEMPTY");
new RubyClass(context, errnoModule, systemCallErrorClass, "EEXIST");
new RubyClass(context, errnoModule, systemCallErrorClass, "EXDEV");
new RubyClass(context, errnoModule, systemCallErrorClass, "EACCES");
// TODO(cs): this should be a separate exception
mathModule.setConstant(null, "DomainError", edomClass);
// TODO(cs): the alias should be the other way round, Config is legacy (and should warn).
objectClass.setConstant(null, "RbConfig", configModule);
// Create some key objects
mainObject = new RubyBasicObject(objectClass);
nilObject = new RubyNilClass(nilClass);
// Create the globals object
globalVariablesObject = new RubyBasicObject(objectClass);
globalVariablesObject.switchToPrivateLayout();
globalVariablesObject.setInstanceVariable("$LOAD_PATH", new RubyArray(arrayClass));
globalVariablesObject.setInstanceVariable("$LOADED_FEATURES", new RubyArray(arrayClass));
globalVariablesObject.setInstanceVariable("$:", globalVariablesObject.getInstanceVariable("$LOAD_PATH"));
globalVariablesObject.setInstanceVariable("$\"", globalVariablesObject.getInstanceVariable("$LOADED_FEATURES"));
globalVariablesObject.setInstanceVariable("$,", nilObject);
initializeEncodingConstants();
arrayMinBlock = new ArrayNodes.MinBlock(context);
arrayMaxBlock = new ArrayNodes.MaxBlock(context);
argv = new RubyArray(arrayClass);
envHash = getSystemEnv();
objectClass.setConstant(null, "ARGV", argv);
objectClass.setConstant(null, "ENV", envHash);
objectClass.setConstant(null, "TRUE", true);
objectClass.setConstant(null, "FALSE", false);
objectClass.setConstant(null, "NIL", nilObject);
final RubyHash configHash = new RubyHash(hashClass, null, null, configHashMap, 0);
configModule.setConstant(null, "CONFIG", configHash);
floatClass.setConstant(null, "EPSILON", org.jruby.RubyFloat.EPSILON);
floatClass.setConstant(null, "INFINITY", org.jruby.RubyFloat.INFINITY);
floatClass.setConstant(null, "NAN", org.jruby.RubyFloat.NAN);
mathModule.setConstant(null, "PI", Math.PI);
mathModule.setConstant(null, "E", Math.E);
fileClass.setConstant(null, "SEPARATOR", RubyString.fromJavaString(stringClass, File.separator));
fileClass.setConstant(null, "Separator", RubyString.fromJavaString(stringClass, File.separator));
fileClass.setConstant(null, "ALT_SEPARATOR", nilObject);
fileClass.setConstant(null, "PATH_SEPARATOR", RubyString.fromJavaString(stringClass, File.pathSeparator));
fileClass.setConstant(null, "FNM_SYSCASE", 0);
globalVariablesObject.setInstanceVariable("$DEBUG", context.getRuntime().isDebug());
globalVariablesObject.setInstanceVariable("$VERBOSE", context.getRuntime().warningsEnabled() ? context.getRuntime().isVerbose() : nilObject);
}
public void initializeAfterMethodsAdded() {
// Just create a dummy object for $stdout - we can use Kernel#print and a special method TruffleDebug.flush_stdout
final RubyBasicObject stdout = new RubyBasicObject(objectClass);
stdout.getSingletonClass(null).addMethod(null, ModuleOperations.lookupMethod(stdout.getMetaClass(), "print").withVisibility(Visibility.PUBLIC));
stdout.getSingletonClass(null).addMethod(null, ModuleOperations.lookupMethod(truffleDebugModule.getSingletonClass(null), "flush_stdout").withNewName("flush"));
globalVariablesObject.setInstanceVariable("$stdout", stdout);
objectClass.setConstant(null, "STDIN", new RubyBasicObject(objectClass));
objectClass.setConstant(null, "STDOUT", stdout);
objectClass.setConstant(null, "STDERR", stdout);
objectClass.setConstant(null, "RUBY_RELEASE_DATE", context.makeString(Constants.COMPILE_DATE));
objectClass.setConstant(null, "RUBY_DESCRIPTION", context.makeString(OutputStrings.getVersionString()));
if (Options.TRUFFLE_LOAD_CORE.load()) {
final String[] files = new String[]{
"jruby/truffle/core/kernel.rb"
};
for (String file : files) {
loadRubyCore(file);
}
}
rubiniusLibrary = new RubiniusLibrary(this);
}
public void loadRubyCore(String fileName) {
final Source source;
try {
source = Source.fromReader(new InputStreamReader(context.getRuntime().getLoadService().getClassPathResource(context.getRuntime().getJRubyClassLoader(), fileName).getInputStream()), fileName);
} catch (IOException e) {
throw new RuntimeException(e);
}
context.execute(context, source, TranslatorDriver.ParserContext.TOP_LEVEL, mainObject, null, null);
}
public void initializeEncodingConstants() {
encodingClass.setConstant(null, "US_ASCII", RubyEncoding.getEncoding(context, USASCIIEncoding.INSTANCE));
encodingClass.setConstant(null, "ASCII_8BIT", RubyEncoding.getEncoding(context, USASCIIEncoding.INSTANCE));
encodingClass.setConstant(null, "UTF_8", RubyEncoding.getEncoding(context, USASCIIEncoding.INSTANCE));
encodingClass.setConstant(null, "EUC_JP", RubyEncoding.getEncoding(context, EUCJPEncoding.INSTANCE));
encodingClass.setConstant(null, "Windows_31J", RubyEncoding.getEncoding(context, SJISEncoding.INSTANCE));
encodingClass.setConstant(null, "Big5", RubyEncoding.getEncoding(context, BIG5Encoding.INSTANCE));
encodingClass.setConstant(null, "IBM437", RubyEncoding.getEncoding(context, USASCIIEncoding.INSTANCE));
}
public RubyClass getMetaClass(Object object) {
RubyNode.notDesignedForCompilation();
if (object instanceof RubyBasicObject) {
return ((RubyBasicObject) object).getMetaClass();
} else if (object instanceof Boolean) {
if ((boolean) object) {
return trueClass;
} else {
return falseClass;
}
} else if (object instanceof Integer) {
return fixnumClass;
} else if (object instanceof Long) {
return fixnumClass;
} else if (object instanceof Double) {
return floatClass;
} else if (object == null) {
throw new RuntimeException();
} else {
throw new UnsupportedOperationException(String.format("Don't know how to get the metaclass for %s", object.getClass()));
}
}
public RubyClass getLogicalClass(Object object) {
RubyNode.notDesignedForCompilation();
if (object instanceof RubyBasicObject) {
return ((RubyBasicObject) object).getLogicalClass();
} else if (object instanceof Boolean) {
if ((boolean) object) {
return trueClass;
} else {
return falseClass;
}
} else if (object instanceof Integer) {
return fixnumClass;
} else if (object instanceof Long) {
return fixnumClass;
} else if (object instanceof Double) {
return floatClass;
} else if (object == null) {
throw new RuntimeException();
} else {
throw new UnsupportedOperationException(String.format("Don't know how to get the logical class for %s", object.getClass()));
}
}
public RubyException runtimeError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(runtimeErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException frozenError(String className, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return runtimeError(String.format("FrozenError: can't modify frozen %s", className), currentNode);
}
public RubyException argumentError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(argumentErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException argumentError(int passed, int required, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return argumentError(String.format("wrong number of arguments (%d for %d)", passed, required), currentNode);
}
public RubyException argumentError(int passed, int required, int optional, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return argumentError(String.format("wrong number of arguments (%d for %d..%d)", passed, required, required + optional), currentNode);
}
public RubyException indexError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(indexErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException indexTooSmallError(String type, int index, int length, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return indexError(String.format("index %d too small for %s; minimum: -%d", index, type, length), currentNode);
}
public RubyException indexNegativeLength(int length, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return indexError(String.format("negative length (%d)", length), currentNode);
}
public RubyException localJumpError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(localJumpErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException unexpectedReturn(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return localJumpError("unexpected return", currentNode);
}
public RubyException noBlockToYieldTo(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return localJumpError("no block given (yield)", currentNode);
}
public RubyException typeError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(typeErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException typeErrorCantDefineSingleton(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError("can't define singleton", currentNode);
}
public RubyException typeErrorNoClassToMakeAlias(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError("no class to make alias", currentNode);
}
public RubyException typeErrorShouldReturn(String object, String method, String expectedType, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError(String.format("%s#%s should return %s", object, method, expectedType), currentNode);
}
public RubyException typeErrorCantConvertTo(String from, String to, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError(String.format("can't convert %s to %s", from, to), currentNode);
}
public RubyException typeErrorCantConvertInto(String from, String to, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError(String.format("can't convert %s into %s", from, to), currentNode);
}
public RubyException typeErrorCantConvertInto(Object from, RubyClass to, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeErrorCantConvertInto(getLogicalClass(from).getName(), to.getName(), currentNode);
}
public RubyException typeErrorIsNotA(String value, String expectedType, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return typeError(String.format("%s is not a %s", value, expectedType), currentNode);
}
public RubyException nameError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(nameErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException nameErrorUninitializedConstant(RubyModule module, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("uninitialized constant %s::%s", module.getName(), name), currentNode);
}
public RubyException nameErrorPrivateConstant(RubyModule module, String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("private constant %s::%s referenced", module.getName(), name), currentNode);
}
public RubyException nameErrorInstanceNameNotAllowable(String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("`%s' is not allowable as an instance variable name", name), currentNode);
}
public RubyException nameErrorReadOnly(String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return nameError(String.format("%s is a read-only variable", name), currentNode);
}
public RubyException nameErrorUndefinedLocalVariableOrMethod(String name, String object, Node currentNode) {
return nameError(String.format("undefined local variable or method `%s' for %s", name, object), currentNode);
}
public RubyException noMethodError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(context.getCoreLibrary().getNoMethodErrorClass(), context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException noMethodError(String name, String object, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return noMethodError(String.format("undefined method `%s' for %s", name, object), currentNode);
}
public RubyException privateMethodError(String name, String object, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return noMethodError(String.format("private method `%s' called for %s", name, object), currentNode);
}
public RubyException loadError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(context.getCoreLibrary().getLoadErrorClass(), context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException loadErrorCannotLoad(String name, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return loadError(String.format("cannot load such file -- %s", name), currentNode);
}
public RubyException zeroDivisionError(Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(context.getCoreLibrary().getZeroDivisionErrorClass(), context.makeString("divided by 0"), RubyCallStack.getBacktrace(currentNode));
}
public RubyException syntaxError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(syntaxErrorClass, context.makeString(message), RubyCallStack.getBacktrace(currentNode));
}
public RubyException mathDomainError(String method, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(edomClass, context.makeString(String.format("Numerical argument is out of domain - \"%s\"", method)), RubyCallStack.getBacktrace(currentNode));
}
public RubyException rangeError(String type, String value, String range, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(rangeErrorClass, context.makeString(String.format("%s %s out of range of %s", type, value, range)), RubyCallStack.getBacktrace(currentNode));
}
public RubyException internalError(String message, Node currentNode) {
CompilerAsserts.neverPartOfCompilation();
return new RubyException(context.getCoreLibrary().getRubyTruffleErrorClass(), context.makeString("internal implementation error - " + message), RubyCallStack.getBacktrace(currentNode));
}
public RubyContext getContext() {
return context;
}
public RubyClass getArrayClass() {
return arrayClass;
}
public RubyClass getBignumClass() {
return bignumClass;
}
public RubyClass getBindingClass() {
return bindingClass;
}
public RubyClass getClassClass() {
return classClass;
}
public RubyClass getContinuationClass() {
return continuationClass;
}
public RubyClass getExceptionClass() { return exceptionClass; }
public RubyClass getFalseClass() {
return falseClass;
}
public RubyClass getFiberClass() {
return fiberClass;
}
public RubyClass getFileClass() {
return fileClass;
}
public RubyClass getFixnumClass() {
return fixnumClass;
}
public RubyClass getFloatClass() {
return floatClass;
}
public RubyClass getHashClass() {
return hashClass;
}
public RubyClass getLoadErrorClass() {
return loadErrorClass;
}
public RubyClass getMatchDataClass() {
return matchDataClass;
}
public RubyClass getModuleClass() {
return moduleClass;
}
public RubyClass getNameErrorClass() {
return nameErrorClass;
}
public RubyClass getNilClass() {
return nilClass;
}
public RubyClass getNoMethodErrorClass() {
return noMethodErrorClass;
}
public RubyClass getObjectClass() {
return objectClass;
}
public RubyClass getProcClass() {
return procClass;
}
public RubyClass getRangeClass() {
return rangeClass;
}
public RubyClass getRegexpClass() {
return regexpClass;
}
public RubyClass getRubyTruffleErrorClass() {
return rubyTruffleErrorClass;
}
public RubyClass getRuntimeErrorClass() {
return runtimeErrorClass;
}
public RubyClass getStringClass() {
return stringClass;
}
public RubyClass getEncodingClass(){ return encodingClass; }
public RubyClass getSymbolClass() {
return symbolClass;
}
public RubyClass getSyntaxErrorClass() {
return syntaxErrorClass;
}
public RubyClass getThreadClass() {
return threadClass;
}
public RubyClass getTimeClass() {
return timeClass;
}
public RubyClass getTrueClass() {
return trueClass;
}
public RubyClass getZeroDivisionErrorClass() {
return zeroDivisionErrorClass;
}
public RubyModule getKernelModule() {
return kernelModule;
}
public RubyArray getArgv() {
return argv;
}
public RubyBasicObject getGlobalVariablesObject() {
return globalVariablesObject;
}
public RubyArray getLoadPath() {
return (RubyArray) globalVariablesObject.getInstanceVariable("$LOAD_PATH");
}
public RubyArray getLoadedFeatures() {
return (RubyArray) globalVariablesObject.getInstanceVariable("$LOADED_FEATURES");
}
public RubyBasicObject getMainObject() {
return mainObject;
}
public RubyNilClass getNilObject() {
return nilObject;
}
public RubyHash getENV() {
return envHash;
}
public RubyEncoding getDefaultEncoding() { return RubyEncoding.getEncoding(context, "US-ASCII"); }
private RubyHash getSystemEnv() {
final LinkedHashMap<Object, Object> storage = new LinkedHashMap<>();
for (Map.Entry<String, String> variable : System.getenv().entrySet()) {
storage.put(context.makeString(variable.getKey()), context.makeString(variable.getValue()));
}
return new RubyHash(context.getCoreLibrary().getHashClass(), null, null, storage, 0);
}
public ArrayNodes.MinBlock getArrayMinBlock() {
return arrayMinBlock;
}
public ArrayNodes.MaxBlock getArrayMaxBlock() {
return arrayMaxBlock;
}
public RubyClass getNumericClass() {
return numericClass;
}
public RubyClass getIntegerClass() {
return integerClass;
}
public RubiniusLibrary getRubiniusLibrary() {
return rubiniusLibrary;
}
public RubyClass getArgumentErrorClass() {
return argumentErrorClass;
}
public RubyClass getEncodingConverterClass() {
return encodingConverterClass;
}
}