Make join call overriden toString() (#1872)

This changes the `join` method in `List`/`Set`/`Listing` to
call the overridden `toString()` method.

This is a breaking change, but is considered a bugfix.
This commit is contained in:
Daniel Chao authored and GitHub committed 2026-09-25 09:24:16 -07:00
1 parent 43e132a383
commit d1e043be4e
13 files changed
+176 -40

No files matched your search

@@ -34,6 +34,12 @@ public abstract class ToStringNode extends UnaryExpressionNode {
super(sourceSection);
}
/**
* When only using this execute method, pass `null` for `operandNode` to {@link
* ToStringNodeGen#create}.
*/
public abstract String executeWith(VirtualFrame frame, Object value);
@Specialization
protected String evalString(String value) {
return value;
@@ -74,7 +80,7 @@ public abstract class ToStringNode extends UnaryExpressionNode {
return (String) callNode.call(value, value.getVmClass().getPrototype());
}
@Specialization
@Specialization(guards = {"!value.isTyped()", "!value.isReference()"})
@TruffleBoundary
protected String evalVmValue(VmValue value) {
return value.toPklString();
@@ -96,4 +102,9 @@ public abstract class ToStringNode extends UnaryExpressionNode {
assert toStringMethod != null;
return DirectCallNode.create(toStringMethod.getCallTarget());
}
@Override
public final boolean isInstrumentable() {
return false;
}
}
@@ -160,23 +160,6 @@ public abstract class VmCollection extends VmValue implements Iterable<Object> {
return builder.build();
}
@TruffleBoundary
public final String join(String separator) {
if (isEmpty()) return "";
var iter = iterator();
var builder = new StringBuilder();
// TODO: use ToStringNode
builder.append(VmUtils.toPklString(iter.next()));
while (iter.hasNext()) {
builder.append(separator);
builder.append(VmUtils.toPklString(iter.next()));
}
return builder.toString();
}
@TruffleBoundary
public final String toPklString() {
return VmValueRenderer.multiLine(Integer.MAX_VALUE).render(this);
@@ -47,6 +47,10 @@ public abstract class VmValue {
return this instanceof VmTyped;
}
public boolean isReference() {
return this instanceof VmReference;
}
/**
* Tells if this value is a {@link VmCollection}, {@link VmListing}, or {@link VmDynamic} with
* {@link VmDynamic#hasElements() elements}.
@@ -20,10 +20,13 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.profiles.LoopConditionProfile;
import org.jspecify.annotations.Nullable;
import org.pkl.core.ast.expression.binary.*;
import org.pkl.core.ast.internal.IsInstanceOfNode;
import org.pkl.core.ast.internal.IsInstanceOfNodeGen;
import org.pkl.core.ast.internal.ToStringNode;
import org.pkl.core.ast.internal.ToStringNodeGen;
import org.pkl.core.ast.lambda.*;
import org.pkl.core.ast.type.TypeNode;
import org.pkl.core.ast.type.TypeNode.UInt8TypeAliasTypeNode;
@@ -1272,9 +1275,23 @@ public final class ListNodes {
}
public abstract static class join extends ExternalMethod1Node {
@Child ToStringNode toStringNode = ToStringNodeGen.create(sourceSection, null);
private final LoopConditionProfile loopConditionProfile = LoopConditionProfile.create();
@Specialization
protected String eval(VmList self, String separator) {
return self.join(separator);
protected String eval(VirtualFrame frame, VmList self, String separator) {
if (self.isEmpty()) return "";
var iter = self.iterator();
var builder = VmUtils.createBuilder();
VmUtils.appendToBuilder(builder, toStringNode.executeWith(frame, iter.next()));
while (loopConditionProfile.profile(iter.hasNext())) {
VmUtils.appendToBuilder(builder, separator);
VmUtils.appendToBuilder(builder, toStringNode.executeWith(frame, iter.next()));
}
return VmUtils.builderToString(builder);
}
}
@@ -18,9 +18,12 @@ package org.pkl.core.stdlib.base;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.IndirectCallNode;
import com.oracle.truffle.api.nodes.LoopNode;
import org.pkl.core.ast.PklNode;
import org.pkl.core.ast.internal.ToStringNode;
import org.pkl.core.ast.internal.ToStringNodeGen;
import org.pkl.core.ast.lambda.*;
import org.pkl.core.runtime.*;
import org.pkl.core.stdlib.ExternalMethod0Node;
@@ -282,23 +285,30 @@ public final class ListingNodes {
}
public abstract static class join extends ExternalMethod1Node {
@Child ToStringNode toStringNode = ToStringNodeGen.create(sourceSection, null);
@Specialization
protected Object eval(VmListing self, String separator) {
protected Object eval(VirtualFrame frame, VmListing self, String separator) {
if (self.isEmpty()) return "";
var builder = new StringBuilder();
var materializedFrame = frame.materialize();
self.forceAndIterateMemberValues(
(key, member, value) -> {
if (!key.equals(0L)) {
builder.append(separator);
}
// TODO: use ToStringNode
builder.append(VmUtils.toPklString(value));
builder.append(toStringNode.executeWith(materializedFrame, value));
return true;
});
LoopNode.reportLoopCount(this, self.getLength());
return builder.toString();
}
@Override
public final boolean isInstrumentable() {
return false;
}
}
public abstract static class toList extends ExternalMethod0Node {
@@ -20,12 +20,15 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.profiles.LoopConditionProfile;
import org.pkl.core.ast.expression.binary.GreaterThanNode;
import org.pkl.core.ast.expression.binary.GreaterThanNodeGen;
import org.pkl.core.ast.expression.binary.LessThanNode;
import org.pkl.core.ast.expression.binary.LessThanNodeGen;
import org.pkl.core.ast.internal.IsInstanceOfNode;
import org.pkl.core.ast.internal.IsInstanceOfNodeGen;
import org.pkl.core.ast.internal.ToStringNode;
import org.pkl.core.ast.internal.ToStringNodeGen;
import org.pkl.core.ast.lambda.*;
import org.pkl.core.runtime.*;
import org.pkl.core.stdlib.*;
@@ -1025,9 +1028,23 @@ public final class SetNodes {
}
public abstract static class join extends ExternalMethod1Node {
@Child ToStringNode toStringNode = ToStringNodeGen.create(sourceSection, null);
private final LoopConditionProfile loopConditionProfile = LoopConditionProfile.create();
@Specialization
protected String eval(VmSet self, String separator) {
return self.join(separator);
protected String eval(VirtualFrame frame, VmSet self, String separator) {
if (self.isEmpty()) return "";
var iter = self.iterator();
var builder = VmUtils.createBuilder();
VmUtils.appendToBuilder(builder, toStringNode.executeWith(frame, iter.next()));
while (loopConditionProfile.profile(iter.hasNext())) {
VmUtils.appendToBuilder(builder, separator);
VmUtils.appendToBuilder(builder, toStringNode.executeWith(frame, iter.next()));
}
return VmUtils.builderToString(builder);
}
}