-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
* Array#at is close to a "array get at index" primitive (except the to_int conversion which we don't support yet).
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
- 9.0.5.0
- 9.0.4.0
- 9.0.3.0
- 9.0.1.0
- 9.0.0.0
- 9.0.0.0.rc2
- 9.0.0.0.rc1
- 9.0.0.0.pre2
- 9.0.0.0.pre1
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,21 @@ | |
import org.jruby.truffle.nodes.methods.locals.ReadLevelVariableNodeFactory; | ||
import org.jruby.truffle.nodes.yield.YieldDispatchHeadNode; | ||
import org.jruby.truffle.runtime.*; | ||
import org.jruby.truffle.nodes.core.ArrayNodesFactory.AtNodeFactory; | ||
import org.jruby.truffle.runtime.RubyArguments; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
import org.jruby.truffle.runtime.UndefinedPlaceholder; | ||
import org.jruby.truffle.runtime.control.BreakException; | ||
import org.jruby.truffle.runtime.control.NextException; | ||
import org.jruby.truffle.runtime.control.RaiseException; | ||
import org.jruby.truffle.runtime.control.RedoException; | ||
import org.jruby.truffle.runtime.core.*; | ||
import org.jruby.truffle.runtime.core.RubyArray; | ||
import org.jruby.truffle.runtime.core.RubyModule; | ||
import org.jruby.truffle.runtime.core.RubyNilClass; | ||
import org.jruby.truffle.runtime.core.RubyProc; | ||
import org.jruby.truffle.runtime.core.RubyRange; | ||
import org.jruby.truffle.runtime.core.RubyString; | ||
import org.jruby.truffle.runtime.core.RubySymbol; | ||
import org.jruby.truffle.runtime.methods.MethodLike; | ||
import org.jruby.truffle.runtime.methods.SharedMethodInfo; | ||
import org.jruby.truffle.runtime.util.ArrayUtils; | ||
|
@@ -529,24 +539,26 @@ public boolean equal(VirtualFrame frame, RubyArray a, Object b) { | |
|
||
} | ||
|
||
@CoreMethod(names = {"[]", "at"}, required = 1, optional = 1, lowerFixnumParameters = {0, 1}) | ||
public abstract static class IndexNode extends ArrayCoreMethodNode { | ||
@CoreMethod(names = "at", required = 1, lowerFixnumParameters = 0) | ||
public abstract static class AtNode extends ArrayCoreMethodNode { | ||
|
||
public IndexNode(RubyContext context, SourceSection sourceSection) { | ||
public AtNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public IndexNode(IndexNode prev) { | ||
public AtNode(AtNode prev) { | ||
super(prev); | ||
} | ||
|
||
public abstract Object executeAt(RubyArray array, int index); | ||
|
||
@Specialization(guards = "isNull") | ||
public RubyNilClass getNull(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
public RubyNilClass getNull(RubyArray array, int index) { | ||
return getContext().getCoreLibrary().getNilObject(); | ||
} | ||
|
||
@Specialization(guards = "isIntegerFixnum", rewriteOn=UnexpectedResultException.class) | ||
public int getIntegerFixnumInBounds(RubyArray array, int index, UndefinedPlaceholder undefined) throws UnexpectedResultException { | ||
@Specialization(guards = "isIntegerFixnum", rewriteOn = UnexpectedResultException.class) | ||
public int getIntegerFixnumInBounds(RubyArray array, int index) throws UnexpectedResultException { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -557,7 +569,7 @@ public int getIntegerFixnumInBounds(RubyArray array, int index, UndefinedPlaceho | |
} | ||
|
||
@Specialization(contains = "getIntegerFixnumInBounds", guards = "isIntegerFixnum") | ||
public Object getIntegerFixnum(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
public Object getIntegerFixnum(RubyArray array, int index) { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -567,25 +579,8 @@ public Object getIntegerFixnum(RubyArray array, int index, UndefinedPlaceholder | |
} | ||
} | ||
|
||
@Specialization(guards = "isIntegerFixnum") | ||
public Object getIntegerFixnum(RubyArray array, int index, int length) { | ||
notDesignedForCompilation(); | ||
|
||
final int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) { | ||
return getContext().getCoreLibrary().getNilObject(); | ||
} else if (normalisedIndex == array.getSize()) { | ||
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0); | ||
} else { | ||
final int end = Math.min(array.getSize(), normalisedIndex + length); | ||
|
||
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((int[]) array.getStore(), normalisedIndex, end), end - normalisedIndex); | ||
} | ||
} | ||
|
||
@Specialization(guards = "isLongFixnum", rewriteOn=UnexpectedResultException.class) | ||
public long getLongFixnumInBounds(RubyArray array, int index, UndefinedPlaceholder undefined) throws UnexpectedResultException { | ||
@Specialization(guards = "isLongFixnum", rewriteOn = UnexpectedResultException.class) | ||
public long getLongFixnumInBounds(RubyArray array, int index) throws UnexpectedResultException { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -596,8 +591,7 @@ public long getLongFixnumInBounds(RubyArray array, int index, UndefinedPlacehold | |
} | ||
|
||
@Specialization(contains = "getLongFixnumInBounds", guards = "isLongFixnum") | ||
public Object getLongFixnum(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
|
||
public Object getLongFixnum(RubyArray array, int index) { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -607,8 +601,8 @@ public Object getLongFixnum(RubyArray array, int index, UndefinedPlaceholder und | |
} | ||
} | ||
|
||
@Specialization(guards = "isFloat", rewriteOn=UnexpectedResultException.class) | ||
public double getFloatInBounds(RubyArray array, int index, UndefinedPlaceholder undefined) throws UnexpectedResultException { | ||
@Specialization(guards = "isFloat", rewriteOn = UnexpectedResultException.class) | ||
public double getFloatInBounds(RubyArray array, int index) throws UnexpectedResultException { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -619,7 +613,7 @@ public double getFloatInBounds(RubyArray array, int index, UndefinedPlaceholder | |
} | ||
|
||
@Specialization(contains = "getFloatInBounds", guards = "isFloat") | ||
public Object getFloat(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
public Object getFloat(RubyArray array, int index) { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -630,7 +624,7 @@ public Object getFloat(RubyArray array, int index, UndefinedPlaceholder undefine | |
} | ||
|
||
@Specialization(guards = "isObject") | ||
public Object getObject(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
public Object getObject(RubyArray array, int index) { | ||
int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex >= array.getSize()) { | ||
|
@@ -640,6 +634,45 @@ public Object getObject(RubyArray array, int index, UndefinedPlaceholder undefin | |
} | ||
} | ||
|
||
} | ||
|
||
@CoreMethod(names = "[]", required = 1, optional = 1, lowerFixnumParameters = { 0, 1 }) | ||
public abstract static class IndexNode extends ArrayCoreMethodNode { | ||
|
||
@Child protected AtNode atNode; | ||
|
||
public IndexNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
atNode = AtNodeFactory.create(context, sourceSection, new RubyNode[] { null, null }); | ||
} | ||
|
||
public IndexNode(IndexNode prev) { | ||
super(prev); | ||
atNode = prev.atNode; | ||
} | ||
|
||
@Specialization | ||
public Object get(RubyArray array, int index, UndefinedPlaceholder undefined) { | ||
return atNode.executeAt(array, index); | ||
} | ||
|
||
@Specialization(guards = "isIntegerFixnum") | ||
public Object getIntegerFixnum(RubyArray array, int index, int length) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
eregon
Author
Member
|
||
notDesignedForCompilation(); | ||
|
||
final int normalisedIndex = array.normaliseIndex(index); | ||
|
||
if (normalisedIndex < 0 || normalisedIndex > array.getSize() || length < 0) { | ||
return getContext().getCoreLibrary().getNilObject(); | ||
} else if (normalisedIndex == array.getSize()) { | ||
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0); | ||
} else { | ||
final int end = Math.min(array.getSize(), normalisedIndex + length); | ||
|
||
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOfRange((int[]) array.getStore(), normalisedIndex, end), end - normalisedIndex); | ||
} | ||
} | ||
|
||
@Specialization(guards = "isObject") | ||
public Object getObject(RubyArray array, int index, int length) { | ||
notDesignedForCompilation(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
fails:Array#at tries to convert the passed argument to an Integer using #to_int | ||
fails:Array#at raises an ArgumentError when 2 or more arguments is passed |
This could do with a better name. I was looking at it thinking it was the normal
[n]
case when in fact it's the[n, m]
case isn't it?