Skip to content

Commit

Permalink
[Truffle] Last of the top-level language Regexp specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Dec 6, 2014
1 parent 6127af9 commit 3cbe259
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 20 deletions.
Expand Up @@ -36,7 +36,7 @@ public StringToRegexpNode(StringToRegexpNode prev) {
public RubyRegexp doString(RubyString string) {
notDesignedForCompilation();

return new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), string.toString(), Option.DEFAULT);
return new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), string.getBytes(), Option.DEFAULT);
}

}
24 changes: 21 additions & 3 deletions core/src/main/java/org/jruby/truffle/nodes/core/RegexpNodes.java
Expand Up @@ -149,7 +149,7 @@ public InitializeNode(InitializeNode prev) {
public RubyRegexp initialize(RubyRegexp regexp, RubyString string) {
notDesignedForCompilation();

regexp.initialize(this, string.toString());
regexp.initialize(this, string.getBytes());
return regexp;
}

Expand Down Expand Up @@ -219,8 +219,26 @@ public SourceNode(SourceNode prev) {
}

@Specialization
public Object source(RubyRegexp regexp) {
return getContext().makeString(regexp.getSource());
public RubyString source(RubyRegexp regexp) {
return getContext().makeString(regexp.getSource().toString());
}

}

@CoreMethod(names = "to_s")
public abstract static class ToSNode extends CoreMethodNode {

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

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

@Specialization
public RubyString to_s(RubyRegexp regexp) {
return getContext().makeString(org.jruby.RubyRegexp.newRegexp(getContext().getRuntime(), regexp.getSource()).to_s().toString());
}

}
Expand Down
Expand Up @@ -499,7 +499,7 @@ public GsubNode(GsubNode prev) {
public RubyString gsub(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).toString(), Option.DEFAULT);
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).getBytes(), Option.DEFAULT);
return gsub(string, regexp, replacement);
}

Expand Down Expand Up @@ -640,7 +640,7 @@ public MatchNode(MatchNode prev) {
public Object match(RubyString string, RubyString regexpString) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), regexpString.toString(), Option.DEFAULT);
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), regexpString.getBytes(), Option.DEFAULT);
return regexp.matchCommon(string.getBytes(), false);
}

Expand Down Expand Up @@ -718,7 +718,7 @@ public ScanNode(ScanNode prev) {
public RubyArray scan(RubyString string, RubyString regexpString) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), regexpString.toString(), Option.DEFAULT);
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), regexpString.getBytes(), Option.DEFAULT);
return scan(string, regexp);
}

Expand Down Expand Up @@ -835,7 +835,7 @@ public SubNode(SubNode prev) {
public RubyString sub(VirtualFrame frame, RubyString string, RubyString regexpString, RubyString replacement) {
notDesignedForCompilation();

final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).toString(), Option.DEFAULT);
final RubyRegexp regexp = new RubyRegexp(this, getContext().getCoreLibrary().getRegexpClass(), escape(frame, regexpString).getBytes(), Option.DEFAULT);
return sub(string, regexp, replacement);
}

Expand Down
18 changes: 8 additions & 10 deletions core/src/main/java/org/jruby/truffle/runtime/core/RubyRegexp.java
Expand Up @@ -52,28 +52,28 @@ public RubyBasicObject newInstance(RubyNode currentNode) {
}

@CompilationFinal private Regex regex;
@CompilationFinal private String source;
@CompilationFinal private ByteList source;

public RubyRegexp(RubyClass regexpClass) {
super(regexpClass);
}

public RubyRegexp(RubyNode currentNode, RubyClass regexpClass, String regex, int options) {
public RubyRegexp(RubyNode currentNode, RubyClass regexpClass, ByteList regex, int options) {
this(regexpClass);
initialize(compile(currentNode, getContext(), regex, options), regex);
}

public RubyRegexp(RubyClass regexpClass, Regex regex, String source) {
public RubyRegexp(RubyClass regexpClass, Regex regex, ByteList source) {
this(regexpClass);
initialize(regex, source);
}

public void initialize(RubyNode currentNode, String setSource) {
public void initialize(RubyNode currentNode, ByteList setSource) {
regex = compile(currentNode, getContext(), setSource, Option.DEFAULT);
source = setSource;
}

public void initialize(Regex setRegex, String setSource) {
public void initialize(Regex setRegex, ByteList setSource) {
regex = setRegex;
source = setSource;
}
Expand All @@ -82,7 +82,7 @@ public Regex getRegex() {
return regex;
}

public String getSource() {
public ByteList getSource() {
return source;
}

Expand Down Expand Up @@ -315,11 +315,9 @@ public boolean equals(Object obj) {
return true;
}

public static Regex compile(RubyNode currentNode, RubyContext context, String pattern, int options) {
public static Regex compile(RubyNode currentNode, RubyContext context, ByteList bytes, int options) {
RubyNode.notDesignedForCompilation();

final byte[] bytes = pattern.getBytes(StandardCharsets.UTF_8);
return compile(currentNode, context, bytes, UTF8Encoding.INSTANCE, options);
return compile(currentNode, context, bytes.bytes(), UTF8Encoding.INSTANCE, options);
}

public static Regex compile(RubyNode currentNode, RubyContext context, byte[] bytes, Encoding encoding, int options) {
Expand Down
Expand Up @@ -1884,7 +1884,7 @@ public RubyNode visitRedoNode(org.jruby.ast.RedoNode node) {
public RubyNode visitRegexpNode(org.jruby.ast.RegexpNode node) {
Regex regex = RubyRegexp.compile(currentNode, context, node.getValue().bytes(), node.getEncoding(), node.getOptions().toOptions());

final RubyRegexp regexp = new RubyRegexp(context.getCoreLibrary().getRegexpClass(), regex, node.getValue().toString());
final RubyRegexp regexp = new RubyRegexp(context.getCoreLibrary().getRegexpClass(), regex, node.getValue());
final ObjectLiteralNode literalNode = new ObjectLiteralNode(context, translate(node.getPosition()), regexp);
return literalNode;
}
Expand Down
26 changes: 26 additions & 0 deletions spec/truffle/tags/core/regexp/new_tags.txt
Expand Up @@ -42,3 +42,29 @@ fails:Regexp.new given a Regexp sets the encoding to EUC-JP if the Regexp litera
fails:Regexp.new given a Regexp sets the encoding to Windows-31J if the Regexp literal has the 's' option
fails:Regexp.new given a Regexp sets the encoding to US-ASCII if the Regexp literal has the 'n' option and the source String is ASCII only
fails:Regexp.new given a Regexp sets the encoding to source String's encoding if the Regexp literal has the 'n' option and the source String is not ASCII only
fails:Regexp.new given a String with escaped characters accepts a three-digit octal value
fails:Regexp.new given a String with escaped characters interprets a digit following a three-digit octal value as a character
fails:Regexp.new given a String with escaped characters accepts '\M-\n'
fails:Regexp.new given a String with escaped characters accepts '\M-\t'
fails:Regexp.new given a String with escaped characters accepts '\M-\r'
fails:Regexp.new given a String with escaped characters accepts '\M-\f'
fails:Regexp.new given a String with escaped characters accepts '\M-\v'
fails:Regexp.new given a String with escaped characters accepts '\M-\a'
fails:Regexp.new given a String with escaped characters accepts '\M-\e'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\n'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\t'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\r'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\f'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\v'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\a'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\e'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\n'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\t'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\r'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\f'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\v'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\a'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\e'
fails:Regexp.new given a String with escaped characters accepts '\M-\c\n'
fails:Regexp.new given a String with escaped characters accepts '\M-\C-\n'
fails:Regexp.new given a String with escaped characters accepts '\M-\n'
1 change: 0 additions & 1 deletion spec/truffle/tags/language/regexp_tags.txt

This file was deleted.

1 change: 1 addition & 0 deletions spec/truffle/truffle.mspec
Expand Up @@ -93,6 +93,7 @@ class MSpecScript
"^spec/ruby/core/regexp/source_spec.rb",
"^spec/ruby/core/regexp/fixed_encoding_spec.rb",
"^spec/ruby/core/regexp/encoding_spec.rb",
"^spec/ruby/core/regexp/new_spec.rb",
"^spec/ruby/core/signal/list_spec.rb",
"^spec/ruby/core/string/chomp_spec.rb",
"^spec/ruby/core/string/crypt_spec.rb",
Expand Down

0 comments on commit 3cbe259

Please sign in to comment.