Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] Implemented more of String#[].
  • Loading branch information
nirvdrum committed Feb 28, 2015
1 parent 9710b08 commit 0fbfcfc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
11 changes: 0 additions & 11 deletions spec/truffle/tags/core/string/slice_tags.txt
Expand Up @@ -11,20 +11,10 @@ fails:String#slice with Regexp returns subclass instances
fails:String#slice with Regexp, index returns the capture for the given index
fails:String#slice with Regexp, index always taints resulting strings when self or regexp is tainted
fails:String#slice with Regexp, index returns an untrusted string if the regexp is untrusted
fails:String#slice with Regexp, index returns nil if there is no match
fails:String#slice with Regexp, index returns nil if there is no capture for the given index
fails:String#slice with Regexp, index calls to_int on the given index
fails:String#slice with Regexp, index returns subclass instances
fails:String#slice with Regexp, index sets $~ to MatchData when there is a match and nil when there's none
fails:String#slice with Regexp, group returns the capture for the given name
fails:String#slice with Regexp, group returns the last capture for duplicate names
fails:String#slice with Regexp, group returns the innermost capture for nested duplicate names
fails:String#slice with Regexp, group always taints resulting strings when self or regexp is tainted
fails:String#slice with Regexp, group returns nil if there is no match
fails:String#slice with Regexp, group raises an IndexError if there is no capture for the given name
fails:String#slice with Regexp, group raises an IndexError when given the empty String as a group name
fails:String#slice with Regexp, group returns subclass instances
fails:String#slice with Regexp, group sets $~ to MatchData when there is a match and nil when there's none
fails:String#slice with String returns other_str if it occurs in self
fails:String#slice with String taints resulting strings when other is tainted
fails:String#slice with String doesn't set $~
Expand Down Expand Up @@ -66,7 +56,6 @@ fails:String#slice! with Regexp, index returns nil if there is no capture for id
fails:String#slice! with Regexp, index calls to_int on idx
fails:String#slice! with Regexp, index returns subclass instances
fails:String#slice! with Regexp, index returns the encoding aware capture for the given index
fails:String#slice! with Regexp, index sets $~ to MatchData when there is a match and nil when there's none
fails:String#slice! with Regexp, index raises a RuntimeError if self is frozen
fails:String#slice! with String removes and returns the first occurrence of other_str from self
fails:String#slice! with String taints resulting strings when other is tainted
Expand Down
Expand Up @@ -284,6 +284,7 @@ private RubyString formatSlow(RubyString format, Object[] args) {
public abstract static class GetIndexNode extends CoreMethodNode {

@Child private ToIntNode toIntNode;
@Child private MatchDataNodes.GetIndexNode getMatchDataIndexNode;

private final BranchProfile outOfBounds = BranchProfile.create();

Expand Down Expand Up @@ -406,6 +407,66 @@ public Object slice(RubyString string, RubyRegexp regexp, UndefinedPlaceholder c
return ((RubyMatchData) matchData).getValues()[0];
}

@Specialization
public Object slice(RubyString string, RubyRegexp regexp, int capture) {
notDesignedForCompilation();

final Object matchData = regexp.matchCommon(string, false, false);

if (matchData == getContext().getCoreLibrary().getNilObject()) {
return matchData;
}

if (getMatchDataIndexNode == null) {
CompilerDirectives.transferToInterpreter();
getMatchDataIndexNode = insert(MatchDataNodesFactory.GetIndexNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{}));
}

return getMatchDataIndexNode.getIndex((RubyMatchData) matchData, capture);
}

@Specialization
public Object slice(VirtualFrame frame, RubyString string, RubyRegexp regexp, RubyString capture) {
notDesignedForCompilation();

final Object matchData = regexp.matchCommon(string, false, false);

if (matchData == getContext().getCoreLibrary().getNilObject()) {
return matchData;
}

if (getMatchDataIndexNode == null) {
CompilerDirectives.transferToInterpreter();
getMatchDataIndexNode = insert(MatchDataNodesFactory.GetIndexNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{}));
}

return getMatchDataIndexNode.getIndex((RubyMatchData) matchData, capture);
}

@Specialization(guards = { "!isUndefinedPlaceholder(arguments[2])", "!isRubyString(arguments[2])" })
public Object slice(VirtualFrame frame, RubyString string, RubyRegexp regexp, Object capture) {
notDesignedForCompilation();

final Object matchData = regexp.matchCommon(string, false, false);

if (matchData == getContext().getCoreLibrary().getNilObject()) {
return matchData;
}

if (toIntNode == null) {
CompilerDirectives.transferToInterpreter();
toIntNode = insert(ToIntNodeFactory.create(getContext(), getSourceSection(), null));
}

if (getMatchDataIndexNode == null) {
CompilerDirectives.transferToInterpreter();
getMatchDataIndexNode = insert(MatchDataNodesFactory.GetIndexNodeFactory.create(getContext(), getSourceSection(), new RubyNode[]{}));
}

final int index = toIntNode.executeIntegerFixnum(frame, capture);

return getMatchDataIndexNode.getIndex((RubyMatchData) matchData, index);
}
}

@CoreMethod(names = "[]=", required = 2, lowerFixnumParameters = 0)
Expand Down

0 comments on commit 0fbfcfc

Please sign in to comment.