32
32
import org .jruby .truffle .nodes .methods .locals .ReadLevelVariableNodeFactory ;
33
33
import org .jruby .truffle .nodes .yield .YieldDispatchHeadNode ;
34
34
import org .jruby .truffle .runtime .*;
35
+ import org .jruby .truffle .nodes .core .ArrayNodesFactory .AtNodeFactory ;
36
+ import org .jruby .truffle .runtime .RubyArguments ;
37
+ import org .jruby .truffle .runtime .RubyContext ;
38
+ import org .jruby .truffle .runtime .UndefinedPlaceholder ;
35
39
import org .jruby .truffle .runtime .control .BreakException ;
36
40
import org .jruby .truffle .runtime .control .NextException ;
37
41
import org .jruby .truffle .runtime .control .RaiseException ;
38
42
import org .jruby .truffle .runtime .control .RedoException ;
39
- import org .jruby .truffle .runtime .core .*;
43
+ import org .jruby .truffle .runtime .core .RubyArray ;
44
+ import org .jruby .truffle .runtime .core .RubyModule ;
45
+ import org .jruby .truffle .runtime .core .RubyNilClass ;
46
+ import org .jruby .truffle .runtime .core .RubyProc ;
47
+ import org .jruby .truffle .runtime .core .RubyRange ;
48
+ import org .jruby .truffle .runtime .core .RubyString ;
49
+ import org .jruby .truffle .runtime .core .RubySymbol ;
40
50
import org .jruby .truffle .runtime .methods .MethodLike ;
41
51
import org .jruby .truffle .runtime .methods .SharedMethodInfo ;
42
52
import org .jruby .truffle .runtime .util .ArrayUtils ;
@@ -529,24 +539,26 @@ public boolean equal(VirtualFrame frame, RubyArray a, Object b) {
529
539
530
540
}
531
541
532
- @ CoreMethod (names = { "[]" , " at"} , required = 1 , optional = 1 , lowerFixnumParameters = { 0 , 1 } )
533
- public abstract static class IndexNode extends ArrayCoreMethodNode {
542
+ @ CoreMethod (names = " at" , required = 1 , lowerFixnumParameters = 0 )
543
+ public abstract static class AtNode extends ArrayCoreMethodNode {
534
544
535
- public IndexNode (RubyContext context , SourceSection sourceSection ) {
545
+ public AtNode (RubyContext context , SourceSection sourceSection ) {
536
546
super (context , sourceSection );
537
547
}
538
548
539
- public IndexNode ( IndexNode prev ) {
549
+ public AtNode ( AtNode prev ) {
540
550
super (prev );
541
551
}
542
552
553
+ public abstract Object executeAt (RubyArray array , int index );
554
+
543
555
@ Specialization (guards = "isNull" )
544
- public RubyNilClass getNull (RubyArray array , int index , UndefinedPlaceholder undefined ) {
556
+ public RubyNilClass getNull (RubyArray array , int index ) {
545
557
return getContext ().getCoreLibrary ().getNilObject ();
546
558
}
547
559
548
- @ Specialization (guards = "isIntegerFixnum" , rewriteOn = UnexpectedResultException .class )
549
- public int getIntegerFixnumInBounds (RubyArray array , int index , UndefinedPlaceholder undefined ) throws UnexpectedResultException {
560
+ @ Specialization (guards = "isIntegerFixnum" , rewriteOn = UnexpectedResultException .class )
561
+ public int getIntegerFixnumInBounds (RubyArray array , int index ) throws UnexpectedResultException {
550
562
int normalisedIndex = array .normaliseIndex (index );
551
563
552
564
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -557,7 +569,7 @@ public int getIntegerFixnumInBounds(RubyArray array, int index, UndefinedPlaceho
557
569
}
558
570
559
571
@ Specialization (contains = "getIntegerFixnumInBounds" , guards = "isIntegerFixnum" )
560
- public Object getIntegerFixnum (RubyArray array , int index , UndefinedPlaceholder undefined ) {
572
+ public Object getIntegerFixnum (RubyArray array , int index ) {
561
573
int normalisedIndex = array .normaliseIndex (index );
562
574
563
575
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -567,25 +579,8 @@ public Object getIntegerFixnum(RubyArray array, int index, UndefinedPlaceholder
567
579
}
568
580
}
569
581
570
- @ Specialization (guards = "isIntegerFixnum" )
571
- public Object getIntegerFixnum (RubyArray array , int index , int length ) {
572
- notDesignedForCompilation ();
573
-
574
- final int normalisedIndex = array .normaliseIndex (index );
575
-
576
- if (normalisedIndex < 0 || normalisedIndex > array .getSize () || length < 0 ) {
577
- return getContext ().getCoreLibrary ().getNilObject ();
578
- } else if (normalisedIndex == array .getSize ()) {
579
- return new RubyArray (getContext ().getCoreLibrary ().getArrayClass (), null , 0 );
580
- } else {
581
- final int end = Math .min (array .getSize (), normalisedIndex + length );
582
-
583
- return new RubyArray (getContext ().getCoreLibrary ().getArrayClass (), Arrays .copyOfRange ((int []) array .getStore (), normalisedIndex , end ), end - normalisedIndex );
584
- }
585
- }
586
-
587
- @ Specialization (guards = "isLongFixnum" , rewriteOn =UnexpectedResultException .class )
588
- public long getLongFixnumInBounds (RubyArray array , int index , UndefinedPlaceholder undefined ) throws UnexpectedResultException {
582
+ @ Specialization (guards = "isLongFixnum" , rewriteOn = UnexpectedResultException .class )
583
+ public long getLongFixnumInBounds (RubyArray array , int index ) throws UnexpectedResultException {
589
584
int normalisedIndex = array .normaliseIndex (index );
590
585
591
586
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -596,8 +591,7 @@ public long getLongFixnumInBounds(RubyArray array, int index, UndefinedPlacehold
596
591
}
597
592
598
593
@ Specialization (contains = "getLongFixnumInBounds" , guards = "isLongFixnum" )
599
- public Object getLongFixnum (RubyArray array , int index , UndefinedPlaceholder undefined ) {
600
-
594
+ public Object getLongFixnum (RubyArray array , int index ) {
601
595
int normalisedIndex = array .normaliseIndex (index );
602
596
603
597
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -607,8 +601,8 @@ public Object getLongFixnum(RubyArray array, int index, UndefinedPlaceholder und
607
601
}
608
602
}
609
603
610
- @ Specialization (guards = "isFloat" , rewriteOn = UnexpectedResultException .class )
611
- public double getFloatInBounds (RubyArray array , int index , UndefinedPlaceholder undefined ) throws UnexpectedResultException {
604
+ @ Specialization (guards = "isFloat" , rewriteOn = UnexpectedResultException .class )
605
+ public double getFloatInBounds (RubyArray array , int index ) throws UnexpectedResultException {
612
606
int normalisedIndex = array .normaliseIndex (index );
613
607
614
608
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -619,7 +613,7 @@ public double getFloatInBounds(RubyArray array, int index, UndefinedPlaceholder
619
613
}
620
614
621
615
@ Specialization (contains = "getFloatInBounds" , guards = "isFloat" )
622
- public Object getFloat (RubyArray array , int index , UndefinedPlaceholder undefined ) {
616
+ public Object getFloat (RubyArray array , int index ) {
623
617
int normalisedIndex = array .normaliseIndex (index );
624
618
625
619
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -630,7 +624,7 @@ public Object getFloat(RubyArray array, int index, UndefinedPlaceholder undefine
630
624
}
631
625
632
626
@ Specialization (guards = "isObject" )
633
- public Object getObject (RubyArray array , int index , UndefinedPlaceholder undefined ) {
627
+ public Object getObject (RubyArray array , int index ) {
634
628
int normalisedIndex = array .normaliseIndex (index );
635
629
636
630
if (normalisedIndex < 0 || normalisedIndex >= array .getSize ()) {
@@ -640,6 +634,45 @@ public Object getObject(RubyArray array, int index, UndefinedPlaceholder undefin
640
634
}
641
635
}
642
636
637
+ }
638
+
639
+ @ CoreMethod (names = "[]" , required = 1 , optional = 1 , lowerFixnumParameters = { 0 , 1 })
640
+ public abstract static class IndexNode extends ArrayCoreMethodNode {
641
+
642
+ @ Child protected AtNode atNode ;
643
+
644
+ public IndexNode (RubyContext context , SourceSection sourceSection ) {
645
+ super (context , sourceSection );
646
+ atNode = AtNodeFactory .create (context , sourceSection , new RubyNode [] { null , null });
647
+ }
648
+
649
+ public IndexNode (IndexNode prev ) {
650
+ super (prev );
651
+ atNode = prev .atNode ;
652
+ }
653
+
654
+ @ Specialization
655
+ public Object get (RubyArray array , int index , UndefinedPlaceholder undefined ) {
656
+ return atNode .executeAt (array , index );
657
+ }
658
+
659
+ @ Specialization (guards = "isIntegerFixnum" )
660
+ public Object getIntegerFixnum (RubyArray array , int index , int length ) {
661
+ notDesignedForCompilation ();
662
+
663
+ final int normalisedIndex = array .normaliseIndex (index );
664
+
665
+ if (normalisedIndex < 0 || normalisedIndex > array .getSize () || length < 0 ) {
666
+ return getContext ().getCoreLibrary ().getNilObject ();
667
+ } else if (normalisedIndex == array .getSize ()) {
668
+ return new RubyArray (getContext ().getCoreLibrary ().getArrayClass (), null , 0 );
669
+ } else {
670
+ final int end = Math .min (array .getSize (), normalisedIndex + length );
671
+
672
+ return new RubyArray (getContext ().getCoreLibrary ().getArrayClass (), Arrays .copyOfRange ((int []) array .getStore (), normalisedIndex , end ), end - normalisedIndex );
673
+ }
674
+ }
675
+
643
676
@ Specialization (guards = "isObject" )
644
677
public Object getObject (RubyArray array , int index , int length ) {
645
678
notDesignedForCompilation ();
0 commit comments