Improve partial-evaluation when amending null parents (#1836)

GraalVM's partial evaulator can't handle recursive calls into the same
node.

The current implementation around evaluating `VmNull` as a parent causes
the partial evaluator to bail out, leaving Pkl stuck in interpreter
mode.

This rewrites the various object literals to add individual
specializations for each type of default value we can see from a VmNull
parent.

Also:
* Fix `isTypeObjectClass` impl
* Fix bug when amending with generator object literal node with object params
This commit is contained in:
Daniel Chao authored and GitHub committed 2026-09-02 18:18:55 +00:00
1 parent 2c2f50113d
commit 0035199176
29 files changed
+1038 -41

No files matched your search

@@ -17,6 +17,7 @@ package org.pkl.core.ast.expression.generator;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Idempotent;
@@ -34,7 +35,7 @@ import org.pkl.core.ast.type.UnresolvedTypeNode;
import org.pkl.core.runtime.*;
/** An object literal node that contains at least one for- or when-expression. */
@ImportStatic(BaseModule.class)
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
@Children private final GeneratorMemberNode[] memberNodes;
@@ -80,6 +81,20 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
return data.storeGeneratorFrames(result);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isDynamicClass()", "checkObjectCannotHaveParameters()"})
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
var parentDynamic = (VmDynamic) defaultValue;
var data = executeChildren(frame, parentDynamic, parentDynamic.getLength());
if (data.hasNoMembers()) {
return parentDynamic;
}
var result = new VmDynamic(frame.materialize(), parentDynamic, data.members(), data.length());
return data.storeGeneratorFrames(result);
}
@Specialization(guards = "checkObjectCannotHaveParameters()")
protected VmTyped evalTyped(VirtualFrame frame, VmTyped parent) {
VmUtils.checkIsInstantiable(parent.getVmClass(), getParentNode());
@@ -91,6 +106,14 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
return new VmTyped(frame.materialize(), parent, parent.getVmClass(), data.members());
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isTypedObjectClass(getClass(defaultValue))", "checkObjectCannotHaveParameters()"})
protected Object evalNullWithTypedDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalTyped(frame, (VmTyped) defaultValue);
}
@Specialization(guards = "checkListingCannotHaveParameters()")
protected VmListing evalListing(VirtualFrame frame, VmListing parent) {
var data = executeChildren(frame, parent, parent.getLength());
@@ -101,6 +124,14 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
return data.storeGeneratorFrames(result);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkListingCannotHaveParameters()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalListing(frame, (VmListing) defaultValue);
}
@Specialization(guards = "checkMappingCannotHaveParameters()")
protected VmMapping evalMapping(VirtualFrame frame, VmMapping parent) {
var data = executeChildren(frame, parent, 0);
@@ -111,10 +142,12 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
return data.storeGeneratorFrames(result);
}
@Specialization(guards = "checkObjectCannotHaveParameters()")
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isMappingClass()", "checkMappingCannotHaveParameters()"})
protected Object evalNullWithMappingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalMapping(frame, (VmMapping) defaultValue);
}
@Specialization(guards = "checkIsValidFunctionAmendment(parent)")
@@ -127,6 +160,18 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(guards = {"parent == getDynamicClass()", "checkObjectCannotHaveParameters()"})
protected VmDynamic evalDynamicClass(VirtualFrame frame, VmClass parent) {
var data = executeChildren(frame, parent, 0);
@@ -173,8 +218,13 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
@Fallback
@TruffleBoundary
protected void fallback(Object parent) {
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
VmUtils.checkIsInstantiable(
parent instanceof VmClass vmClass ? vmClass : VmUtils.getClass(parent), getParentNode());
value instanceof VmClass vmClass ? vmClass : VmUtils.getClass(value), getParentNode());
throw exceptionBuilder().unreachableCode().build();
}
@@ -186,7 +236,7 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder()
.evalError("objectAmendmentCannotHaveParameters")
.withLocation(parameterTypes[0])
.withLocation(getParentNode())
.build();
}
@@ -197,7 +247,7 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder()
.evalError("listingAmendmentCannotHaveParameters")
.withLocation(parameterTypes[0])
.withLocation(getParentNode())
.build();
}
@@ -208,7 +258,7 @@ public abstract class GeneratorObjectLiteralNode extends ObjectLiteralNode {
CompilerDirectives.transferToInterpreter();
throw exceptionBuilder()
.evalError("mappingAmendmentCannotHaveParameters")
.withLocation(parameterTypes[0])
.withLocation(getParentNode())
.build();
}
@@ -33,7 +33,7 @@ import org.pkl.core.runtime.*;
* all entry keys are constants. Example: `new foo { ["one"] = 1 }`
*/
// IDEA: don't materialize frames if all members have constant values
@ImportStatic(BaseModule.class)
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class ConstantEntriesLiteralNode extends SpecializedObjectLiteralNode {
public ConstantEntriesLiteralNode(
SourceSection sourceSection,
@@ -73,21 +73,38 @@ public abstract class ConstantEntriesLiteralNode extends SpecializedObjectLitera
return new VmMapping(frame.materialize(), parent, members);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isMappingClass()", "checkIsValidMappingAmendment()"})
protected Object evalNullWithMappingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalMapping(frame, (VmMapping) defaultValue);
}
@Specialization
protected VmDynamic evalDynamic(VirtualFrame frame, VmDynamic parent) {
return new VmDynamic(frame.materialize(), parent, members, parent.getLength());
}
@SuppressWarnings("unused")
@Specialization(guards = "getClass(defaultValue).isDynamicClass()")
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalDynamic(frame, (VmDynamic) defaultValue);
}
@Specialization(guards = "checkIsValidListingAmendment()")
protected VmListing evalListing(VirtualFrame frame, VmListing parent) {
checkMaxListingMemberIndex(parent.getLength());
return new VmListing(frame.materialize(), parent, members, parent.getLength());
}
@Specialization
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkIsValidListingAmendment()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalListing(frame, (VmListing) defaultValue);
}
@Specialization(guards = "checkIsValidFunctionAmendment(parent)")
@@ -100,6 +117,18 @@ public abstract class ConstantEntriesLiteralNode extends SpecializedObjectLitera
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(guards = {"parent == getMappingClass()", "checkIsValidMappingAmendment()"})
protected VmMapping evalMappingClass(
VirtualFrame frame, @SuppressWarnings("unused") VmClass parent) {
@@ -127,6 +156,11 @@ public abstract class ConstantEntriesLiteralNode extends SpecializedObjectLitera
@Fallback
@TruffleBoundary
protected void fallback(Object parent) {
elementsEntriesFallback(parent, findFirstNonProperty(members), false);
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
elementsEntriesFallback(value, findFirstNonProperty(members), false);
}
}
@@ -16,6 +16,7 @@
package org.pkl.core.ast.expression.literal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -36,7 +37,7 @@ import org.pkl.core.util.EconomicMaps;
* Object literal that contains both elements and entries (and possibly properties). Example: `new
* foo { "pigeon", [3] = "barn owl" }`
*/
@ImportStatic(BaseModule.class)
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class ElementsEntriesLiteralNode extends SpecializedObjectLiteralNode {
private final ObjectMember[] elements;
@Children private final ExpressionNode[] keyNodes;
@@ -95,6 +96,14 @@ public abstract class ElementsEntriesLiteralNode extends SpecializedObjectLitera
parent.getLength() + elements.length);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkIsValidListingAmendment()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalListing(frame, (VmListing) defaultValue);
}
@Specialization
protected VmDynamic evalDynamic(VirtualFrame frame, VmDynamic parent) {
return new VmDynamic(
@@ -104,10 +113,11 @@ public abstract class ElementsEntriesLiteralNode extends SpecializedObjectLitera
parent.getLength() + elements.length);
}
@Specialization
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(guards = "getClass(defaultValue).isDynamicClass()")
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalDynamic(frame, (VmDynamic) defaultValue);
}
@Specialization(guards = "checkIsValidFunctionAmendment(parent)")
@@ -120,6 +130,18 @@ public abstract class ElementsEntriesLiteralNode extends SpecializedObjectLitera
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(guards = {"parent == getListingClass()", "checkIsValidListingAmendment()"})
protected VmListing evalListingClass(
VirtualFrame frame, @SuppressWarnings("unused") VmClass parent) {
@@ -145,7 +167,12 @@ public abstract class ElementsEntriesLiteralNode extends SpecializedObjectLitera
@Fallback
@TruffleBoundary
protected void fallback(Object parent) {
elementsEntriesFallback(parent, elements[0], true);
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
elementsEntriesFallback(value, elements[0], true);
}
@ExplodeLoop
@@ -32,7 +32,7 @@ import org.pkl.core.util.EconomicMaps;
* Object literal that contains elements (and possibly properties) but not entries. Example: `new
* foo { "pigeon" }`
*/
@ImportStatic(BaseModule.class)
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class ElementsLiteralNode extends SpecializedObjectLiteralNode {
private final ObjectMember[] elements;
@@ -94,10 +94,11 @@ public abstract class ElementsLiteralNode extends SpecializedObjectLiteralNode {
parent.getLength() + elements.length);
}
@Specialization
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(guards = "getClass(defaultValue).isDynamicClass()")
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalDynamicUncached(frame, (VmDynamic) defaultValue);
}
@Specialization(guards = "checkIsValidFunctionAmendment(parent)")
@@ -110,6 +111,18 @@ public abstract class ElementsLiteralNode extends SpecializedObjectLiteralNode {
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(
guards = {
"parent == getListingClass()",
@@ -162,10 +175,23 @@ public abstract class ElementsLiteralNode extends SpecializedObjectLiteralNode {
parent.getLength() + elements.length);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkIsValidListingAmendment()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalListingUncached(frame, (VmListing) defaultValue);
}
@Fallback
@TruffleBoundary
protected void fallback(Object parent) {
elementsEntriesFallback(parent, elements[0], true);
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
elementsEntriesFallback(value, elements[0], true);
}
// offset element keys according to parentLength
@@ -38,7 +38,7 @@ import org.pkl.core.util.EconomicMaps;
* used.) Example: `foo { ["on" + "e"] = 1 }`
*/
// IDEA: don't materialize frames if all members have constant values
@ImportStatic(BaseModule.class)
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class EntriesLiteralNode extends SpecializedObjectLiteralNode {
@Children private final ExpressionNode[] keyNodes;
private final ObjectMember[] values;
@@ -92,11 +92,26 @@ public abstract class EntriesLiteralNode extends SpecializedObjectLiteralNode {
return new VmMapping(frame.materialize(), parent, createMapMembers(frame));
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isMappingClass()", "checkIsValidMappingAmendment()"})
protected Object evalNullWithMappingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalMapping(frame, (VmMapping) defaultValue);
}
@Specialization
protected VmDynamic evalDynamic(VirtualFrame frame, VmDynamic parent) {
return new VmDynamic(frame.materialize(), parent, createMapMembers(frame), parent.getLength());
}
@SuppressWarnings("unused")
@Specialization(guards = {"getClass(defaultValue).isDynamicClass()"})
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalDynamic(frame, (VmDynamic) defaultValue);
}
@Specialization(guards = "checkIsValidListingAmendment()")
protected VmListing evalListing(VirtualFrame frame, VmListing parent) {
return new VmListing(
@@ -107,10 +122,12 @@ public abstract class EntriesLiteralNode extends SpecializedObjectLiteralNode {
parent.getLength());
}
@Specialization
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkIsValidListingAmendment()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalListing(frame, (VmListing) defaultValue);
}
@Specialization(guards = "checkIsValidFunctionAmendment(parent)")
@@ -123,6 +140,18 @@ public abstract class EntriesLiteralNode extends SpecializedObjectLiteralNode {
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(guards = {"parent == getMappingClass()", "checkIsValidMappingAmendment()"})
protected VmMapping evalMappingClass(
VirtualFrame frame, @SuppressWarnings("unused") VmClass parent) {
@@ -152,7 +181,12 @@ public abstract class EntriesLiteralNode extends SpecializedObjectLiteralNode {
@Fallback
@TruffleBoundary
protected Object fallback(Object parent) {
return elementsEntriesFallback(parent, values[0], false);
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
return elementsEntriesFallback(value, values[0], false);
}
@ExplodeLoop
@@ -20,14 +20,17 @@ import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.LoopNode;
import com.oracle.truffle.api.source.SourceSection;
import org.jspecify.annotations.Nullable;
import org.pkl.core.ast.ExpressionNode;
import org.pkl.core.ast.type.TypeNode;
import org.pkl.core.ast.type.UnresolvedTypeNode;
import org.pkl.core.runtime.BaseModule;
import org.pkl.core.runtime.VmClass;
import org.pkl.core.runtime.VmFunction;
import org.pkl.core.runtime.VmLanguage;
import org.pkl.core.runtime.VmNull;
import org.pkl.core.runtime.VmUtils;
// IDEA: don't materialize frames when all members are constants
@@ -57,8 +60,6 @@ public abstract class ObjectLiteralNode extends ExpressionNode {
protected abstract ExpressionNode getParentNode();
protected abstract Object executeWithParent(VirtualFrame frame, Object parent);
protected abstract ObjectLiteralNode copy(ExpressionNode newParentNode);
protected final AmendFunctionNode createAmendFunctionNode(VirtualFrame frame) {
@@ -71,7 +72,14 @@ public abstract class ObjectLiteralNode extends ExpressionNode {
@Idempotent
protected static boolean isTypedObjectClass(VmClass clazz) {
return !(clazz.isListingClass() || clazz.isMappingClass() || clazz.isDynamicClass());
if (clazz.isListingClass()
|| clazz.isMappingClass()
|| clazz.isDynamicClass()
|| clazz.isFunctionClass()
|| clazz.isFunctionNClass()) {
return false;
}
return BaseModule.getTypedClass().isSuperclassOf(clazz);
}
protected final boolean checkIsValidFunctionAmendment(VmFunction parent) {
@@ -85,4 +93,25 @@ public abstract class ObjectLiteralNode extends ExpressionNode {
}
return true;
}
@Idempotent
protected final boolean checkIsValidFunctionAmendment(Object parent) {
return checkIsValidFunctionAmendment((VmFunction) parent);
}
@Idempotent
protected final boolean isFunction(Object value) {
return value instanceof VmFunction;
}
protected final Object getNullDefaultValue(VmNull parent) {
var value = parent.getDefaultValue();
var count = 0;
while (value instanceof VmNull n) {
value = n.getDefaultValue();
count++;
}
LoopNode.reportLoopCount(this, count);
return value;
}
}
@@ -16,7 +16,9 @@
package org.pkl.core.ast.expression.literal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -31,6 +33,7 @@ import org.pkl.core.runtime.*;
/** Object literal that contains properties but not elements or entries. */
// IDEA: don't materialize frame when all members are constants
@ImportStatic({BaseModule.class, VmUtils.class})
public abstract class PropertiesLiteralNode extends SpecializedObjectLiteralNode {
public PropertiesLiteralNode(
SourceSection sourceSection,
@@ -82,26 +85,77 @@ public abstract class PropertiesLiteralNode extends SpecializedObjectLiteralNode
return new VmTyped(frame.materialize(), parent, parent.getVmClass(), members);
}
@SuppressWarnings("unused")
@Specialization(
guards = {
"parentClass == getClass(defaultValue)",
"isTypedObjectClass(parentClass)",
"checkIsValidTypedAmendment(parentClass)"
})
protected Object evalNullWithTypedObjectDefaultCached(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached("getClass(defaultValue)") VmClass parentClass) {
var parentTyped = (VmTyped) defaultValue;
return new VmTyped(frame.materialize(), parentTyped, parentTyped.getVmClass(), members);
}
@SuppressWarnings("unused")
@Specialization(
guards = {
"isTypedObjectClass(getClass(defaultValue))",
"checkIsValidTypedAmendment(defaultValue)"
})
protected Object evalNullWithTypedObjectDefaultUncached(
VirtualFrame frame,
VmNull parent,
@Bind(value = "getNullDefaultValue(parent)") Object defaultValue) {
var parentTyped = (VmTyped) defaultValue;
return new VmTyped(frame.materialize(), parentTyped, parentTyped.getVmClass(), members);
}
@Specialization
protected Object evalDynamic(VirtualFrame frame, VmDynamic parent) {
return new VmDynamic(frame.materialize(), parent, members, parent.getLength());
}
@SuppressWarnings("unused")
@Specialization(guards = "getClass(defaultValue).isDynamicClass()")
protected Object evalNullWithDynamicDefault(
VirtualFrame frame, VmNull parent, @Bind("getNullDefaultValue(parent)") Object defaultValue) {
return evalDynamic(frame, (VmDynamic) defaultValue);
}
@Specialization(guards = "checkIsValidListingAmendment()")
protected Object evalListing(VirtualFrame frame, VmListing parent) {
return new VmListing(frame.materialize(), parent, members, parent.getLength());
}
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isListingClass()", "checkIsValidListingAmendment()"})
protected Object evalNullWithListingDefault(
VirtualFrame frame,
VmNull parent,
@Bind(value = "getNullDefaultValue(parent)") Object defaultValue) {
return evalListing(frame, (VmListing) defaultValue);
}
@ExplodeLoop
@Specialization(guards = "checkIsValidMappingAmendment()")
protected Object evalMapping(VirtualFrame frame, VmMapping parent) {
return new VmMapping(frame.materialize(), parent, members);
}
@Specialization
protected Object evalNull(VirtualFrame frame, VmNull parent) {
// assumes that Graal PE can handle recursive call to same node
return executeWithParent(frame, parent.getDefaultValue());
@SuppressWarnings("unused")
@Specialization(
guards = {"getClass(defaultValue).isMappingClass()", "checkIsValidMappingAmendment()"})
protected Object evalNullWithMappingDefault(
VirtualFrame frame,
VmNull parent,
@Bind(value = "getNullDefaultValue(parent)") Object defaultValue) {
return evalMapping(frame, (VmMapping) defaultValue);
}
// Ultimately, this lambda or a lambda returned from it will call one of the other
@@ -121,6 +175,18 @@ public abstract class PropertiesLiteralNode extends SpecializedObjectLiteralNode
return amendFunctionNode.execute(frame, parent);
}
@SuppressWarnings("unused")
@Specialization(
guards = {"isFunction(defaultValue)", "checkIsValidFunctionAmendment(defaultValue)"})
protected Object evalNullWithFunctionDefault(
VirtualFrame frame,
VmNull parent,
@Bind("getNullDefaultValue(parent)") Object defaultValue,
@Cached(value = "createAmendFunctionNode(frame)", neverDefault = true)
AmendFunctionNode amendFunctionNode) {
return evalFunction(frame, (VmFunction) defaultValue, amendFunctionNode);
}
@Specialization(
guards = {
"parent == cachedParent",
@@ -199,8 +265,13 @@ public abstract class PropertiesLiteralNode extends SpecializedObjectLiteralNode
@Specialization
@TruffleBoundary
protected void fallback(Object parent) {
var value = parent;
// blame the non-null type (e.g. blame `Duration` instead of `Null` in the case of `Duration?`)
if (value instanceof VmNull vmNull) {
value = getNullDefaultValue(vmNull);
}
// should always throw
checkIsValidTypedAmendment(parent);
checkIsValidTypedAmendment(value);
throw exceptionBuilder().unreachableCode().build();
}
@@ -19,6 +19,7 @@ import static org.pkl.core.PClassInfo.pklBaseUri;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Idempotent;
public final class BaseModule extends StdLibModule {
static final VmTyped instance = VmUtils.createEmptyModule();
@@ -91,6 +92,7 @@ public final class BaseModule extends StdLibModule {
return SetClass.instance;
}
@Idempotent
public static VmClass getListingClass() {
return ListingClass.instance;
}
@@ -99,10 +101,12 @@ public final class BaseModule extends StdLibModule {
return MapClass.instance;
}
@Idempotent
public static VmClass getMappingClass() {
return MappingClass.instance;
}
@Idempotent
public static VmClass getDynamicClass() {
return DynamicClass.instance;
}
@@ -135,6 +139,7 @@ public final class BaseModule extends StdLibModule {
return RegexMatchClass.instance;
}
@Idempotent
public static VmClass getFunctionClass() {
return FunctionClass.instance;
}
@@ -21,6 +21,7 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleStackTrace;
import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.api.nodes.*;
import com.oracle.truffle.api.source.Source;
@@ -528,6 +529,7 @@ public final class VmUtils {
}
// implements same behavior as AnyNodes#getClass
@Idempotent
public static VmClass getClass(Object value) {
if (value instanceof VmValue vmValue) {
return vmValue.getVmClass();