/
ModuleNodes.java
1472 lines (1108 loc) · 53 KB
/
ModuleNodes.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
/*
* 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.nodes.core;
import com.oracle.truffle.api.*;
import com.oracle.truffle.api.source.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.*;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.nodes.RubyRootNode;
import org.jruby.truffle.nodes.dispatch.Dispatch;
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
import org.jruby.truffle.nodes.cast.BooleanCastNode;
import org.jruby.truffle.nodes.cast.BooleanCastNodeFactory;
import org.jruby.truffle.nodes.control.SequenceNode;
import org.jruby.truffle.nodes.methods.arguments.CheckArityNode;
import org.jruby.truffle.nodes.methods.arguments.MissingArgumentBehaviour;
import org.jruby.truffle.nodes.methods.arguments.ReadPreArgumentNode;
import org.jruby.truffle.nodes.objects.ReadInstanceVariableNode;
import org.jruby.truffle.nodes.objects.SelfNode;
import org.jruby.truffle.nodes.objects.WriteInstanceVariableNode;
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode;
import org.jruby.truffle.runtime.*;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.*;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.methods.*;
import org.jruby.truffle.translator.TranslatorDriver;
import org.jruby.util.IdUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@CoreClass(name = "Module")
public abstract class ModuleNodes {
@CoreMethod(names = "===", required = 1)
public abstract static class ContainsInstanceNode extends CoreMethodNode {
public ContainsInstanceNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ContainsInstanceNode(ContainsInstanceNode prev) {
super(prev);
}
@Specialization
public boolean containsInstance(RubyModule module, RubyBasicObject instance) {
notDesignedForCompilation();
return ModuleOperations.includesModule(instance.getMetaClass(), module);
}
@Specialization
public boolean containsInstance(RubyModule module, Object instance) {
notDesignedForCompilation();
return ModuleOperations.includesModule(getContext().getCoreLibrary().getMetaClass(instance), module);
}
}
@CoreMethod(names = "<=", required = 1)
public abstract static class IsSubclassOfNode extends CoreMethodNode {
public IsSubclassOfNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public IsSubclassOfNode(IsSubclassOfNode prev) {
super(prev);
}
public abstract Object executeIsSubclassOf(VirtualFrame frame, RubyModule self, RubyModule other);
@Specialization
public Object isSubclassOf(VirtualFrame frame, RubyModule self, RubyModule other) {
notDesignedForCompilation();
if (self == other || ModuleOperations.includesModule(self, other)) {
return true;
}
if (ModuleOperations.includesModule(other, self)) {
return false;
}
return getContext().getCoreLibrary().getNilObject();
}
@Specialization
public Object isSubclassOf(VirtualFrame frame, RubyModule self, RubyBasicObject other) {
notDesignedForCompilation();
throw new RaiseException(getContext().getCoreLibrary().typeError("compared with non class/module", this));
}
}
@CoreMethod(names = "<=>", required = 1)
public abstract static class CompareNode extends CoreMethodNode {
@Child protected IsSubclassOfNode subclassNode;
@Child protected BooleanCastNode booleanCastNode;
public CompareNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public CompareNode(CompareNode prev) {
super(prev);
}
private Object isSubclass(VirtualFrame frame, RubyModule self, RubyModule other) {
if (subclassNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
subclassNode = insert(ModuleNodesFactory.IsSubclassOfNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{null, null}));
}
return subclassNode.executeIsSubclassOf(frame, self, other);
}
private boolean booleanCast(VirtualFrame frame, Object value) {
if (booleanCastNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
booleanCastNode = insert(BooleanCastNodeFactory.create(getContext(), getSourceSection(), null));
}
return booleanCastNode.executeBoolean(frame, value);
}
@Specialization
public Object compare(VirtualFrame frame, RubyModule self, RubyModule other) {
notDesignedForCompilation();
if (self == other) {
return 0;
}
final Object isSubclass = isSubclass(frame, self, other);
if (isSubclass instanceof RubyNilClass) {
return getContext().getCoreLibrary().getNilObject();
} else if (booleanCast(frame, isSubclass)) {
return -1;
}
return 1;
}
@Specialization
public Object compare(VirtualFrame frame, RubyModule self, RubyBasicObject other) {
notDesignedForCompilation();
return getContext().getCoreLibrary().getNilObject();
}
}
@CoreMethod(names = "alias_method", required = 2)
public abstract static class AliasMethodNode extends CoreMethodNode {
public AliasMethodNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AliasMethodNode(AliasMethodNode prev) {
super(prev);
}
@Specialization
public RubyModule aliasMethod(RubyModule module, RubySymbol newName, RubySymbol oldName) {
notDesignedForCompilation();
module.alias(this, newName.toString(), oldName.toString());
return module;
}
}
@CoreMethod(names = "ancestors")
public abstract static class AncestorsNode extends CoreMethodNode {
public AncestorsNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AncestorsNode(AncestorsNode prev) {
super(prev);
}
@Specialization
public RubyArray ancestors(RubyModule self) {
notDesignedForCompilation();
final List<RubyModule> ancestors = new ArrayList<>();
for (RubyModule module : self.ancestors()) {
ancestors.add(module);
}
return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), ancestors.toArray(new Object[ancestors.size()]));
}
}
@CoreMethod(names = "append_features", required = 1)
public abstract static class AppendFeaturesNode extends CoreMethodNode {
public AppendFeaturesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AppendFeaturesNode(AppendFeaturesNode prev) {
super(prev);
}
@Specialization
public RubyNilClass appendFeatures(RubyModule module, RubyModule other) {
notDesignedForCompilation();
module.appendFeatures(this, other);
return getContext().getCoreLibrary().getNilObject();
}
}
@CoreMethod(names = "attr_reader", argumentsAsArray = true)
public abstract static class AttrReaderNode extends CoreMethodNode {
public AttrReaderNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AttrReaderNode(AttrReaderNode prev) {
super(prev);
}
@Specialization
public RubyNilClass attrReader(RubyModule module, Object[] args) {
notDesignedForCompilation();
final SourceSection sourceSection = Truffle.getRuntime().getCallerFrame().getCallNode().getEncapsulatingSourceSection();
for (Object arg : args) {
final String accessorName;
if (arg instanceof RubySymbol) {
accessorName = ((RubySymbol) arg).toString();
} else {
throw new UnsupportedOperationException();
}
attrReader(this, getContext(), sourceSection, module, accessorName);
}
return getContext().getCoreLibrary().getNilObject();
}
public static void attrReader(RubyNode currentNode, RubyContext context, SourceSection sourceSection, RubyModule module, String name) {
CompilerDirectives.transferToInterpreter();
final CheckArityNode checkArity = new CheckArityNode(context, sourceSection, new Arity(0, 0, false, false));
final SelfNode self = new SelfNode(context, sourceSection);
final ReadInstanceVariableNode readInstanceVariable = new ReadInstanceVariableNode(context, sourceSection, "@" + name, self, false);
final RubyNode block = SequenceNode.sequence(context, sourceSection, checkArity, readInstanceVariable);
final String indicativeName = name + "(attr_reader)";
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, null, indicativeName, false, null, false);
final RubyRootNode rootNode = new RubyRootNode(context, sourceSection, null, sharedMethodInfo, block);
final CallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);
final RubyMethod method = new RubyMethod(sharedMethodInfo, name, module, Visibility.PUBLIC, false, callTarget, null);
module.addMethod(currentNode, method);
}
}
@CoreMethod(names = "attr_writer", argumentsAsArray = true)
public abstract static class AttrWriterNode extends CoreMethodNode {
public AttrWriterNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AttrWriterNode(AttrWriterNode prev) {
super(prev);
}
@Specialization
public RubyNilClass attrWriter(RubyModule module, Object[] args) {
notDesignedForCompilation();
final SourceSection sourceSection = Truffle.getRuntime().getCallerFrame().getCallNode().getEncapsulatingSourceSection();
for (Object arg : args) {
final String accessorName;
if (arg instanceof RubySymbol) {
accessorName = ((RubySymbol) arg).toString();
} else {
throw new UnsupportedOperationException();
}
attrWriter(this, getContext(), sourceSection, module, accessorName);
}
return getContext().getCoreLibrary().getNilObject();
}
public static void attrWriter(RubyNode currentNode, RubyContext context, SourceSection sourceSection, RubyModule module, String name) {
CompilerDirectives.transferToInterpreter();
final CheckArityNode checkArity = new CheckArityNode(context, sourceSection, new Arity(1, 0, false, false));
final SelfNode self = new SelfNode(context, sourceSection);
final ReadPreArgumentNode readArgument = new ReadPreArgumentNode(context, sourceSection, 0, MissingArgumentBehaviour.RUNTIME_ERROR);
final WriteInstanceVariableNode writeInstanceVariable = new WriteInstanceVariableNode(context, sourceSection, "@" + name, self, readArgument, false);
final RubyNode block = SequenceNode.sequence(context, sourceSection, checkArity, writeInstanceVariable);
final String indicativeName = name + "(attr_writer)";
final SharedMethodInfo sharedMethodInfo = new SharedMethodInfo(sourceSection, null, indicativeName, false, null, false);
final RubyRootNode rootNode = new RubyRootNode(context, sourceSection, null, sharedMethodInfo, block);
final CallTarget callTarget = Truffle.getRuntime().createCallTarget(rootNode);
final RubyMethod method = new RubyMethod(sharedMethodInfo, name + "=", module, Visibility.PUBLIC, false, callTarget, null);
module.addMethod(currentNode, method);
}
}
@CoreMethod(names = {"attr_accessor", "attr"}, argumentsAsArray = true)
public abstract static class AttrAccessorNode extends CoreMethodNode {
public AttrAccessorNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public AttrAccessorNode(AttrAccessorNode prev) {
super(prev);
}
@Specialization
public RubyNilClass attrAccessor(RubyModule module, Object[] args) {
notDesignedForCompilation();
final SourceSection sourceSection = Truffle.getRuntime().getCallerFrame().getCallNode().getEncapsulatingSourceSection();
for (Object arg : args) {
final String accessorName;
if (arg instanceof RubySymbol) {
accessorName = ((RubySymbol) arg).toString();
} else {
throw new UnsupportedOperationException();
}
attrAccessor(this, getContext(), sourceSection, module, accessorName);
}
return getContext().getCoreLibrary().getNilObject();
}
public static void attrAccessor(RubyNode currentNode, RubyContext context, SourceSection sourceSection, RubyModule module, String name) {
CompilerDirectives.transferToInterpreter();
AttrReaderNode.attrReader(currentNode, context, sourceSection, module, name);
AttrWriterNode.attrWriter(currentNode, context, sourceSection, module, name);
}
}
@CoreMethod(names = {"class_eval","module_eval"}, optional = 3, needsBlock = true)
public abstract static class ClassEvalNode extends CoreMethodNode {
@Child protected YieldDispatchHeadNode yield;
public ClassEvalNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
yield = new YieldDispatchHeadNode(context);
}
public ClassEvalNode(ClassEvalNode prev) {
super(prev);
yield = prev.yield;
}
@Specialization
public Object classEval(VirtualFrame frame, RubyModule module, RubyString code, @SuppressWarnings("unused") UndefinedPlaceholder file, @SuppressWarnings("unused") UndefinedPlaceholder line, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();
final Source source = Source.fromText(code.getBytes(), "(eval)");
return getContext().execute(getContext(), source, code.getBytes().getEncoding(), TranslatorDriver.ParserContext.MODULE, module, frame.materialize(), this);
}
@Specialization
public Object classEval(VirtualFrame frame, RubyModule module, RubyString code, RubyString file, @SuppressWarnings("unused") UndefinedPlaceholder line, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();
final Source source = Source.asPseudoFile(code.getBytes(), file.toString());
return getContext().execute(getContext(), source, code.getBytes().getEncoding(), TranslatorDriver.ParserContext.MODULE, module, frame.materialize(), this);
}
@Specialization
public Object classEval(VirtualFrame frame, RubyModule module, RubyString code, RubyString file, @SuppressWarnings("unused") int line, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();
final Source source = Source.asPseudoFile(code.getBytes(), file.toString());
return getContext().execute(getContext(), source, code.getBytes().getEncoding(), TranslatorDriver.ParserContext.MODULE, module, frame.materialize(), this);
}
@Specialization
public Object classEval(VirtualFrame frame, RubyModule self, @SuppressWarnings("unused") UndefinedPlaceholder code, @SuppressWarnings("unused") UndefinedPlaceholder file, @SuppressWarnings("unused") UndefinedPlaceholder line, RubyProc block) {
notDesignedForCompilation();
return yield.dispatchWithModifiedSelf(frame, block, self);
}
@Specialization
public Object classEval(RubyModule self, UndefinedPlaceholder code, UndefinedPlaceholder file, UndefinedPlaceholder line, UndefinedPlaceholder block) {
notDesignedForCompilation();
throw new RaiseException(getContext().getCoreLibrary().argumentError(0, 1, 2, this));
}
}
@CoreMethod(names = {"class_exec","module_exec"}, argumentsAsArray = true, needsBlock = true)
public abstract static class ClassExecNode extends CoreMethodNode {
@Child protected YieldDispatchHeadNode yield;
public ClassExecNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
yield = new YieldDispatchHeadNode(context);
}
public ClassExecNode(ClassExecNode prev) {
super(prev);
yield = prev.yield;
}
public abstract Object executeClassEval(VirtualFrame frame, RubyModule self, Object[] args, RubyProc block);
@Specialization
public Object classEval(VirtualFrame frame, RubyModule self, Object[] args, RubyProc block) {
notDesignedForCompilation();
// TODO: deal with args
return yield.dispatchWithModifiedSelf(frame, block, self);
}
}
@CoreMethod(names = "class_variable_defined?", required = 1)
public abstract static class ClassVariableDefinedNode extends CoreMethodNode {
public ClassVariableDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ClassVariableDefinedNode(ClassVariableDefinedNode prev) {
super(prev);
}
@Specialization
public boolean isClassVariableDefined(RubyModule module, RubyString name) {
notDesignedForCompilation();
return module.getClassVariables().containsKey(name.toString());
}
@Specialization
public boolean isClassVariableDefined(RubyModule module, RubySymbol name) {
notDesignedForCompilation();
return module.getClassVariables().containsKey(name.toString());
}
}
@CoreMethod(names = "class_variable_get", required = 1)
public abstract static class ClassVariableGetNode extends CoreMethodNode {
public ClassVariableGetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ClassVariableGetNode(ClassVariableGetNode prev) {
super(prev);
}
@Specialization
public Object getClassVariable(RubyModule module, RubyString name) {
notDesignedForCompilation();
return ModuleOperations.lookupClassVariable(module, RubyContext.checkClassVariableName(getContext(), name.toString(), this));
}
@Specialization
public Object getClassVariable(RubyModule module, RubySymbol name) {
notDesignedForCompilation();
return ModuleOperations.lookupClassVariable(module, RubyContext.checkClassVariableName(getContext(), name.toString(), this));
}
}
@CoreMethod(names = "class_variables")
public abstract static class ClassVariablesNode extends CoreMethodNode {
public ClassVariablesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ClassVariablesNode(ClassVariablesNode prev) {
super(prev);
}
@Specialization
public RubyArray getClassVariables(RubyModule module) {
notDesignedForCompilation();
final RubyArray array = new RubyArray(module.getContext().getCoreLibrary().getArrayClass());
for (String variable : ModuleOperations.getAllClassVariables(module).keySet()) {
array.slowPush(RubySymbol.newSymbol(module.getContext(), variable));
}
return array;
}
}
@CoreMethod(names = "constants", optional = 1)
public abstract static class ConstantsNode extends CoreMethodNode {
public ConstantsNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ConstantsNode(ConstantsNode prev) {
super(prev);
}
@Specialization
public RubyArray constants(RubyModule module, UndefinedPlaceholder unused) {
return constants(module, true);
}
@Specialization
public RubyArray constants(RubyModule module, boolean inherit) {
notDesignedForCompilation();
final RubyArray array = new RubyArray(getContext().getCoreLibrary().getArrayClass());
// TODO(cs): handle inherit
for (String constant : module.getConstants().keySet()) {
array.slowPush(getContext().newSymbol(constant));
}
return array;
}
}
@CoreMethod(names = "const_defined?", required = 1, optional = 1)
public abstract static class ConstDefinedNode extends CoreMethodNode {
public ConstDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ConstDefinedNode(ConstDefinedNode prev) {
super(prev);
}
@Specialization
public boolean isConstDefined(RubyModule module, RubyString name, @SuppressWarnings("unused") UndefinedPlaceholder inherit) {
notDesignedForCompilation();
return ModuleOperations.lookupConstant(getContext(), LexicalScope.NONE, module, name.toString()) != null;
}
@Specialization
public boolean isConstDefined(RubyModule module, RubyString name, boolean inherit) {
notDesignedForCompilation();
if (inherit) {
return ModuleOperations.lookupConstant(getContext(), LexicalScope.NONE, module, name.toString()) != null;
} else {
return module.getConstants().containsKey(name.toString());
}
}
@Specialization
public boolean isConstDefined(RubyModule module, RubySymbol name, @SuppressWarnings("unused") UndefinedPlaceholder inherit) {
notDesignedForCompilation();
return ModuleOperations.lookupConstant(getContext(), LexicalScope.NONE, module, name.toString()) != null;
}
}
@CoreMethod(names = "const_get", required = 1)
public abstract static class ConstGetNode extends CoreMethodNode {
@Child protected DispatchHeadNode dispatch;
public ConstGetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
dispatch = new DispatchHeadNode(context, Dispatch.MissingBehavior.CALL_CONST_MISSING);
}
public ConstGetNode(ConstGetNode prev) {
super(prev);
dispatch = prev.dispatch;
}
@Specialization
public Object getConstant(VirtualFrame frame, RubyModule module, RubyString name) {
notDesignedForCompilation();
return dispatch.dispatch(
frame,
getContext().getCoreLibrary().getNilObject(),
null,
module,
name,
null,
new Object[]{},
Dispatch.DispatchAction.READ_CONSTANT);
}
@Specialization
public Object getConstant(VirtualFrame frame, RubyModule module, RubySymbol name) {
notDesignedForCompilation();
return dispatch.dispatch(
frame,
getContext().getCoreLibrary().getNilObject(),
null,
module,
name,
null,
new Object[]{},
Dispatch.DispatchAction.READ_CONSTANT);
}
}
@CoreMethod(names = "const_missing", required = 1)
public abstract static class ConstMissingNode extends CoreMethodNode {
public ConstMissingNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ConstMissingNode(ConstMissingNode prev) {
super(prev);
}
@Specialization
public Object methodMissing(RubyModule module, RubySymbol name) {
throw new RaiseException(getContext().getCoreLibrary().nameErrorUninitializedConstant(module, name.toString(), this));
}
}
@CoreMethod(names = "const_set", required = 2)
public abstract static class ConstSetNode extends CoreMethodNode {
public ConstSetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ConstSetNode(ConstSetNode prev) {
super(prev);
}
@Specialization
public RubyModule setConstant(RubyModule module, RubyString name, Object object) {
notDesignedForCompilation();
setConstant(module, name.toString(), object);
return module;
}
@Specialization
public RubyModule setConstant(RubyModule module, RubySymbol name, Object object) {
notDesignedForCompilation();
setConstant(module, name.toString(), object);
return module;
}
public void setConstant(RubyModule module, String name, Object object) {
if (!IdUtil.isConstant(name)) {
throw new RaiseException(getContext().getCoreLibrary().nameError(String.format("wrong constant name %s", name), this));
}
if (object instanceof RubyModule) {
final RubyModule setModule = (RubyModule) object;
if (setModule.getName() == null) {
setModule.setLexicalScope(new LexicalScope(null, module));
setModule.setName(name);
}
}
module.setConstant(this, name, object);
}
}
@CoreMethod(names = "define_method", needsBlock = true, required = 1, optional = 1)
public abstract static class DefineMethodNode extends CoreMethodNode {
public DefineMethodNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public DefineMethodNode(DefineMethodNode prev) {
super(prev);
}
@Specialization
public RubySymbol defineMethod(RubyModule module, RubyString name, @SuppressWarnings("unused") UndefinedPlaceholder proc, RubyProc block) {
notDesignedForCompilation();
return defineMethod(module, name, block, UndefinedPlaceholder.INSTANCE);
}
@Specialization
public RubySymbol defineMethod(RubyModule module, RubyString name, RubyProc proc, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();
final RubySymbol symbol = getContext().getSymbolTable().getSymbol(name.getBytes());
defineMethod(module, symbol, proc);
return symbol;
}
@Specialization
public RubySymbol defineMethod(RubyModule module, RubySymbol name, @SuppressWarnings("unused") UndefinedPlaceholder proc, RubyProc block) {
notDesignedForCompilation();
return defineMethod(module, name, block, UndefinedPlaceholder.INSTANCE);
}
@Specialization
public RubySymbol defineMethod(RubyModule module, RubySymbol name, RubyProc proc, @SuppressWarnings("unused") UndefinedPlaceholder block) {
notDesignedForCompilation();
defineMethod(module, name, proc);
return name;
}
private void defineMethod(RubyModule module, RubySymbol name, RubyProc proc) {
notDesignedForCompilation();
final CallTarget modifiedCallTarget = proc.getCallTargetForMethods();
final RubyMethod modifiedMethod = new RubyMethod(proc.getSharedMethodInfo(), name.toString(), module, Visibility.PUBLIC, false, modifiedCallTarget, proc.getDeclarationFrame());
module.addMethod(this, modifiedMethod);
}
}
@CoreMethod(names = "initialize", needsBlock = true)
public abstract static class InitializeNode extends CoreMethodNode {
@Child protected ModuleNodes.ClassExecNode classExecNode;
public InitializeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public InitializeNode(InitializeNode prev) {
super(prev);
}
public abstract RubyModule executeInitialize(VirtualFrame frame, RubyModule module, RubyProc block);
void classEval(VirtualFrame frame, RubyModule module, RubyProc block) {
if (classExecNode == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
classExecNode = insert(ModuleNodesFactory.ClassExecNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{null,null,null}));
}
classExecNode.executeClassEval(frame, module, new Object[]{}, block);
}
@Specialization
public RubyModule initialize(RubyModule module, UndefinedPlaceholder block) {
return module;
}
@Specialization
public RubyModule initialize(VirtualFrame frame, RubyModule module, RubyProc block) {
classEval(frame, module, block);
return module;
}
}
@CoreMethod(names = "initialize_copy", visibility = Visibility.PRIVATE, required = 1)
public abstract static class InitializeCopyNode extends CoreMethodNode {
public InitializeCopyNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public InitializeCopyNode(InitializeCopyNode prev) {
super(prev);
}
@Specialization
public Object initializeCopy(RubyModule self, RubyModule other) {
notDesignedForCompilation();
self.initCopy(other);
return getContext().getCoreLibrary().getNilObject();
}
}
@CoreMethod(names = "include", argumentsAsArray = true, required = 1)
public abstract static class IncludeNode extends CoreMethodNode {
@Child protected DispatchHeadNode appendFeaturesNode;
public IncludeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
appendFeaturesNode = new DispatchHeadNode(context);
}
public IncludeNode(IncludeNode prev) {
super(prev);
appendFeaturesNode = prev.appendFeaturesNode;
}
public abstract RubyNilClass executeInclude(VirtualFrame frame, RubyModule module, Object[] args);
@Specialization
public RubyNilClass include(VirtualFrame frame, RubyModule module, Object[] args) {
notDesignedForCompilation();
// Note that we traverse the arguments backwards
for (int n = args.length - 1; n >= 0; n--) {
if (args[n] instanceof RubyModule) {
final RubyModule included = (RubyModule) args[n];
appendFeaturesNode.call(frame, included, "append_features", null, module);
// TODO(cs): call included hook
}
}
return getContext().getCoreLibrary().getNilObject();
}
}
@CoreMethod(names = "include?", required = 1)
public abstract static class IncludePNode extends CoreMethodNode {
@Child protected DispatchHeadNode appendFeaturesNode;
public IncludePNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
appendFeaturesNode = new DispatchHeadNode(context);
}
public IncludePNode(IncludePNode prev) {
super(prev);
appendFeaturesNode = prev.appendFeaturesNode;
}
@Specialization
public boolean include(RubyModule module, RubyModule included) {
notDesignedForCompilation();
ModuleChain ancestor = module.getParentModule();
while (ancestor != null) {
if (ancestor.getActualModule() == included) {
return true;
}
ancestor = ancestor.getParentModule();
}
return false;
}
}
@CoreMethod(names = "method_defined?", required = 1, optional = 1)
public abstract static class MethodDefinedNode extends CoreMethodNode {
public MethodDefinedNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public MethodDefinedNode(MethodDefinedNode prev) {
super(prev);
}
@Specialization
public boolean isMethodDefined(RubyModule module, RubyString name, @SuppressWarnings("unused") UndefinedPlaceholder inherit) {
notDesignedForCompilation();
return ModuleOperations.lookupMethod(module, name.toString()) != null;
}
@Specialization
public boolean isMethodDefined(RubyModule module, RubyString name, boolean inherit) {
notDesignedForCompilation();
if (inherit) {
return ModuleOperations.lookupMethod(module, name.toString()) != null;
} else {
return module.getMethods().containsKey(name.toString());
}
}
@Specialization
public boolean isMethodDefined(RubyModule module, RubySymbol name, @SuppressWarnings("unused") UndefinedPlaceholder inherit) {
notDesignedForCompilation();
return ModuleOperations.lookupMethod(module, name.toString()) != null;
}
}
@CoreMethod(names = "module_function", argumentsAsArray = true)
public abstract static class ModuleFunctionNode extends CoreMethodNode {
public ModuleFunctionNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public ModuleFunctionNode(ModuleFunctionNode prev) {
super(prev);
}
@Specialization
public RubyNilClass moduleFunction(RubyModule module, Object... args) {
notDesignedForCompilation();
if (args.length == 0) {
final Frame unpacked = Truffle.getRuntime().getCallerFrame().getFrame(FrameInstance.FrameAccess.READ_WRITE, false);
final FrameSlot slot = unpacked.getFrameDescriptor().findFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);
/*
* setObject, even though it's a boolean, so we can getObject and either get the
* default Nil or the boolean value without triggering deoptimization.
*/
unpacked.setObject(slot, true);
} else {
for (Object argument : args) {
final String methodName;
if (argument instanceof RubySymbol) {
methodName = ((RubySymbol) argument).toString();
} else if (argument instanceof RubyString) {
methodName = ((RubyString) argument).toString();
} else {
throw new UnsupportedOperationException();
}
// TODO(cs): make this instance method private
final RubyMethod method = ModuleOperations.lookupMethod(module, methodName);
if (method == null) {
throw new UnsupportedOperationException();
}
module.getSingletonClass(this).addMethod(this, method.withVisibility(Visibility.PUBLIC));
}
}
return getContext().getCoreLibrary().getNilObject();
}
}
@CoreMethod(names = "name")
public abstract static class NameNode extends CoreMethodNode {
public NameNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
public NameNode(NameNode prev) {
super(prev);
}
@Specialization
public Object name(RubyModule module) {
notDesignedForCompilation();
if (module.getName() == null) {
return getContext().getCoreLibrary().getNilObject();
}
final StringBuilder builder = new StringBuilder();
builder.append(module.getName());