Skip to content

Commit

Permalink
[Truffle] Support String coercion in String#[]=.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Feb 6, 2015
1 parent 5cb8ba2 commit f24ae99
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/element_set_tags.txt
@@ -1,7 +1,6 @@
fails:String#[]= with Fixnum index taints self if other_str is tainted
fails:String#[]= with Fixnum index raises IndexError if the string index doesn't match a position in the string
fails:String#[]= with Fixnum index calls to_int on index
fails:String#[]= with Fixnum index calls #to_str to convert other to a String
fails:String#[]= with Fixnum index calls #to_int to convert the index
fails:String#[]= with Fixnum index raises a TypeError if #to_int does not return an Fixnum
fails:String#[]= with Fixnum index raises an IndexError if #to_int returns a value out of range
Expand Down
Expand Up @@ -317,16 +317,20 @@ public Object slice(RubyString string, int start, int length) {
@CoreMethod(names = "[]=", required = 2, lowerFixnumParameters = 0)
public abstract static class ElementSetNode extends CoreMethodNode {

@Child private ToStrNode toStrNode;

public ElementSetNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
toStrNode = ToStrNodeFactory.create(context, sourceSection, null);
}

public ElementSetNode(ElementSetNode prev) {
super(prev);
toStrNode = prev.toStrNode;
}

@Specialization
public RubyString elementSet(RubyString string, int index, RubyString replacement) {
public RubyString elementSet(VirtualFrame frame, RubyString string, int index, Object replacement) {
notDesignedForCompilation();

if (string.isFrozen()) {
Expand All @@ -350,13 +354,14 @@ public RubyString elementSet(RubyString string, int index, RubyString replacemen
throw new RaiseException(getContext().getCoreLibrary().indexError(String.format("index %d out of string", index), this));
}

StringSupport.replaceInternal19(index, 1, string, replacement);
final RubyString coerced = toStrNode.executeRubyString(frame, replacement);
StringSupport.replaceInternal19(index, 1, string, coerced);

return replacement;
return coerced;
}

@Specialization
public RubyString elementSet(RubyString string, RubyRange.IntegerFixnumRange range, RubyString replacement) {
public RubyString elementSet(VirtualFrame frame, RubyString string, RubyRange.IntegerFixnumRange range, Object replacement) {
notDesignedForCompilation();

if (string.isFrozen()) {
Expand Down Expand Up @@ -400,9 +405,10 @@ public RubyString elementSet(RubyString string, RubyRange.IntegerFixnumRange ran
length = 0;
}

StringSupport.replaceInternal19(begin, length, string, replacement);
final RubyString coerced = toStrNode.executeRubyString(frame, replacement);
StringSupport.replaceInternal19(begin, length, string, coerced);

return replacement;
return coerced;
}
}

Expand Down

0 comments on commit f24ae99

Please sign in to comment.