Skip to content

Commit 9dfbd58

Browse files
committed
Copying the associated pointers with a String should be done in String#initialize_copy
1 parent 6d0b2d8 commit 9dfbd58

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,6 @@ protected Property[] getCopiedProperties(Shape shape) {
411411
}
412412
}
413413

414-
final Property associatedProperty = shape.getProperty(Layouts.ASSOCIATED_IDENTIFIER);
415-
416-
if (associatedProperty != null) {
417-
copiedProperties.add(associatedProperty);
418-
}
419-
420414
return copiedProperties.toArray(new Property[copiedProperties.size()]);
421415
}
422416

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@
162162
import org.truffleruby.language.objects.ReadObjectFieldNode;
163163
import org.truffleruby.language.objects.ReadObjectFieldNodeGen;
164164
import org.truffleruby.language.objects.TaintNode;
165+
import org.truffleruby.language.objects.WriteObjectFieldNode;
166+
import org.truffleruby.language.objects.WriteObjectFieldNodeGen;
165167
import org.truffleruby.language.yield.YieldNode;
166168

167169
import com.oracle.truffle.api.CompilerDirectives;
@@ -1320,6 +1322,9 @@ public Rope getRope(DynamicObject str) {
13201322
@CoreMethod(names = "initialize_copy", required = 1)
13211323
public abstract static class InitializeCopyNode extends CoreMethodArrayArgumentsNode {
13221324

1325+
@Child private ReadObjectFieldNode readAssociatedNode = ReadObjectFieldNodeGen.create(Layouts.ASSOCIATED_IDENTIFIER, null);
1326+
@Child private WriteObjectFieldNode writeAssociatedNode;
1327+
13231328
@Specialization(guards = "self == from")
13241329
public Object initializeCopySelfIsSameAsFrom(DynamicObject self, DynamicObject from) {
13251330
return self;
@@ -1329,17 +1334,29 @@ public Object initializeCopySelfIsSameAsFrom(DynamicObject self, DynamicObject f
13291334
@Specialization(guards = { "self != from", "isRubyString(from)", "!isNativeRope(from)" })
13301335
public Object initializeCopy(DynamicObject self, DynamicObject from) {
13311336
StringOperations.setRope(self, rope(from));
1332-
1337+
copyAssociated(self, from);
13331338
return self;
13341339
}
13351340

13361341
@Specialization(guards = { "self != from", "isRubyString(from)", "isNativeRope(from)" })
13371342
public Object initializeCopyFromNative(DynamicObject self, DynamicObject from) {
13381343
StringOperations.setRope(self, ((NativeRope) rope(from)).makeCopy(getContext().getFinalizationService()));
1339-
1344+
copyAssociated(self, from);
13401345
return self;
13411346
}
13421347

1348+
private void copyAssociated(DynamicObject self, DynamicObject from) {
1349+
final Object associated = readAssociatedNode.execute(from);
1350+
if (associated != null) {
1351+
if (writeAssociatedNode == null) {
1352+
CompilerDirectives.transferToInterpreterAndInvalidate();
1353+
writeAssociatedNode = insert(WriteObjectFieldNodeGen.create(Layouts.ASSOCIATED_IDENTIFIER));
1354+
}
1355+
1356+
writeAssociatedNode.write(self, associated);
1357+
}
1358+
}
1359+
13431360
protected boolean isNativeRope(DynamicObject other) {
13441361
return rope(other) instanceof NativeRope;
13451362
}

0 commit comments

Comments
 (0)