Skip to content

Commit f24ae99

Browse files
committed
[Truffle] Support String coercion in String#[]=.
1 parent 5cb8ba2 commit f24ae99

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

spec/truffle/tags/core/string/element_set_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
fails:String#[]= with Fixnum index taints self if other_str is tainted
22
fails:String#[]= with Fixnum index raises IndexError if the string index doesn't match a position in the string
33
fails:String#[]= with Fixnum index calls to_int on index
4-
fails:String#[]= with Fixnum index calls #to_str to convert other to a String
54
fails:String#[]= with Fixnum index calls #to_int to convert the index
65
fails:String#[]= with Fixnum index raises a TypeError if #to_int does not return an Fixnum
76
fails:String#[]= with Fixnum index raises an IndexError if #to_int returns a value out of range

truffle/src/main/java/org/jruby/truffle/nodes/core/StringNodes.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,20 @@ public Object slice(RubyString string, int start, int length) {
317317
@CoreMethod(names = "[]=", required = 2, lowerFixnumParameters = 0)
318318
public abstract static class ElementSetNode extends CoreMethodNode {
319319

320+
@Child private ToStrNode toStrNode;
321+
320322
public ElementSetNode(RubyContext context, SourceSection sourceSection) {
321323
super(context, sourceSection);
324+
toStrNode = ToStrNodeFactory.create(context, sourceSection, null);
322325
}
323326

324327
public ElementSetNode(ElementSetNode prev) {
325328
super(prev);
329+
toStrNode = prev.toStrNode;
326330
}
327331

328332
@Specialization
329-
public RubyString elementSet(RubyString string, int index, RubyString replacement) {
333+
public RubyString elementSet(VirtualFrame frame, RubyString string, int index, Object replacement) {
330334
notDesignedForCompilation();
331335

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

353-
StringSupport.replaceInternal19(index, 1, string, replacement);
357+
final RubyString coerced = toStrNode.executeRubyString(frame, replacement);
358+
StringSupport.replaceInternal19(index, 1, string, coerced);
354359

355-
return replacement;
360+
return coerced;
356361
}
357362

358363
@Specialization
359-
public RubyString elementSet(RubyString string, RubyRange.IntegerFixnumRange range, RubyString replacement) {
364+
public RubyString elementSet(VirtualFrame frame, RubyString string, RubyRange.IntegerFixnumRange range, Object replacement) {
360365
notDesignedForCompilation();
361366

362367
if (string.isFrozen()) {
@@ -400,9 +405,10 @@ public RubyString elementSet(RubyString string, RubyRange.IntegerFixnumRange ran
400405
length = 0;
401406
}
402407

403-
StringSupport.replaceInternal19(begin, length, string, replacement);
408+
final RubyString coerced = toStrNode.executeRubyString(frame, replacement);
409+
StringSupport.replaceInternal19(begin, length, string, coerced);
404410

405-
return replacement;
411+
return coerced;
406412
}
407413
}
408414

0 commit comments

Comments
 (0)