Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Truffle] Fixed bad implementation of MatchData#captures.
  • Loading branch information
nirvdrum committed Jan 8, 2015
1 parent fb4b3b8 commit 1a36da4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Expand Up @@ -15,6 +15,7 @@
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyMatchData;
import org.jruby.truffle.runtime.core.RubyString;
import org.jruby.truffle.runtime.util.ArrayUtils;

@CoreClass(name = "MatchData")
public abstract class MatchDataNodes {
Expand Down Expand Up @@ -43,6 +44,29 @@ public Object getIndex(RubyMatchData matchData, int index) {

}

@CoreMethod(names = "captures")
public abstract static class CapturesNode extends CoreMethodNode {

public CapturesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public CapturesNode(CapturesNode prev) {
super(prev);
}

@Specialization
public RubyArray toA(RubyMatchData matchData) {
notDesignedForCompilation();

Object[] values = matchData.getValues();

// There should always be at least one value because the entire matched string will be in the array. Thus,
// there is no risk of an ArrayIndexOutOfBoundsException here.
return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), ArrayUtils.extractRange(values, 1, values.length));
}
}

@CoreMethod(names = "pre_match")
public abstract static class PreMatchNode extends CoreMethodNode {

Expand Down Expand Up @@ -79,7 +103,7 @@ public RubyString postMatch(RubyMatchData matchData) {

}

@CoreMethod(names = {"to_a", "captures"})
@CoreMethod(names = "to_a")
public abstract static class ToANode extends CoreMethodNode {

public ToANode(RubyContext context, SourceSection sourceSection) {
Expand All @@ -96,7 +120,6 @@ public RubyArray toA(RubyMatchData matchData) {

return RubyArray.fromObjects(getContext().getCoreLibrary().getArrayClass(), matchData.getValues());
}

}

@CoreMethod(names = "values_at", argumentsAsArray = true)
Expand Down
1 change: 0 additions & 1 deletion spec/truffle/tags/core/matchdata/captures_tags.txt

This file was deleted.

2 comments on commit 1a36da4

@nirvdrum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisseaton I could have implemented this is Ruby as to_a[1..-1]. That looked like it'd be doing a lot more and the Java here was fairly compact. But I can change it to pure Ruby if you'd like.

@chrisseaton
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Please sign in to comment.