From 263b96b9dbebc8ea183848b6ca0d09d995978640 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Mon, 2 Nov 2026 19:18:51 -0800 Subject: [PATCH] Isolate standard-library runtime state per Truffle language instance --- .../org/pkl/core/runtime/AnalyzeModule.java | 21 +- .../java/org/pkl/core/runtime/BaseModule.java | 307 +++--------------- .../org/pkl/core/runtime/BenchmarkModule.java | 15 +- .../org/pkl/core/runtime/CommandModule.java | 49 +-- .../org/pkl/core/runtime/JsonnetModule.java | 21 +- .../java/org/pkl/core/runtime/MathModule.java | 9 +- .../org/pkl/core/runtime/ModuleCache.java | 3 +- .../java/org/pkl/core/runtime/NetModule.java | 13 +- .../org/pkl/core/runtime/PklBinaryModule.java | 9 +- .../org/pkl/core/runtime/PlatformModule.java | 45 +-- .../org/pkl/core/runtime/ProjectModule.java | 9 +- .../java/org/pkl/core/runtime/RefModule.java | 19 +- .../org/pkl/core/runtime/ReflectModule.java | 114 +------ .../org/pkl/core/runtime/ReleaseModule.java | 33 +- .../org/pkl/core/runtime/SemVerModule.java | 15 +- .../org/pkl/core/runtime/SettingsModule.java | 9 +- .../org/pkl/core/runtime/StdLibModule.java | 151 +++++---- .../org/pkl/core/runtime/SyntaxModule.java | 25 +- .../java/org/pkl/core/runtime/TestModule.java | 9 +- .../java/org/pkl/core/runtime/VmClass.java | 4 +- .../java/org/pkl/core/runtime/VmContext.java | 70 +++- .../java/org/pkl/core/runtime/VmDynamic.java | 15 +- .../pkl/core/runtime/VmDynamicCursors.java | 5 +- .../java/org/pkl/core/runtime/VmLanguage.java | 2 + .../java/org/pkl/core/runtime/VmListing.java | 14 +- .../org/pkl/core/runtime/VmLocalContext.java | 75 +++-- .../java/org/pkl/core/runtime/VmMapping.java | 13 +- .../org/pkl/core/runtime/VmObjectCursor.java | 3 +- .../java/org/pkl/core/runtime/VmType.java | 9 +- .../java/org/pkl/core/runtime/VmUtils.java | 5 +- .../java/org/pkl/core/runtime/XmlModule.java | 27 +- .../org/pkl/core/stdlib/VmObjectFactory.java | 88 +++-- .../core/stdlib/protobuf/RendererNodes.java | 8 +- 33 files changed, 399 insertions(+), 815 deletions(-) diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/AnalyzeModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/AnalyzeModule.java index b8eb340e4c..12e1832548 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/AnalyzeModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/AnalyzeModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,30 +19,17 @@ import java.net.URI; public class AnalyzeModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:analyze"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:analyze")); } public static VmClass getImportGraphClass() { - return AnalyzeModule.ImportGraphClass.instance; + return loadClass("ImportGraph"); } public static VmClass getImportClass() { - return AnalyzeModule.ImportClass.instance; - } - - private static final class ImportGraphClass { - static final VmClass instance = loadClass("ImportGraph"); - } - - private static final class ImportClass { - static final VmClass instance = loadClass("Import"); + return loadClass("Import"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/BaseModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/BaseModule.java index a8951a53dc..0613d0a0cd 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/BaseModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/BaseModule.java @@ -22,101 +22,96 @@ import com.oracle.truffle.api.dsl.Idempotent; public final class BaseModule extends StdLibModule { - static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(pklBaseUri, instance); - } public static VmTyped getModule() { - return instance; + return getModule(pklBaseUri); } public static VmClass getAnyClass() { - return AnyClass.instance; + return loadClass("Any"); } public static VmClass getObjectClass() { - return ObjectClass.instance; + return loadClass("Object"); } public static VmClass getTypedClass() { - return TypedClass.instance; + return loadClass("Typed"); } public static VmClass getNullClass() { - return NullClass.instance; + return loadClass("Null"); } public static VmClass getNumberClass() { - return NumberClass.instance; + return loadClass("Number"); } public static VmClass getIntClass() { - return IntClass.instance; + return loadClass("Int"); } public static VmClass getFloatClass() { - return FloatClass.instance; + return loadClass("Float"); } public static VmClass getStringClass() { - return StringClass.instance; + return loadClass("String"); } public static VmClass getBooleanClass() { - return BooleanClass.instance; + return loadClass("Boolean"); } public static VmClass getDurationClass() { - return DurationClass.instance; + return loadClass("Duration"); } public static VmClass getDataSizeClass() { - return DataSizeClass.instance; + return loadClass("DataSize"); } public static VmClass getBytesClass() { - return BytesClass.instance; + return loadClass("Bytes"); } public static VmClass getIntSeqClass() { - return IntSeqClass.instance; + return loadClass("IntSeq"); } public static VmClass getCollectionClass() { - return CollectionClass.instance; + return loadClass("Collection"); } public static VmClass getListClass() { - return ListClass.instance; + return loadClass("List"); } public static VmClass getSetClass() { - return SetClass.instance; + return loadClass("Set"); } @Idempotent public static VmClass getListingClass() { - return ListingClass.instance; + return loadClass("Listing"); } public static VmClass getMapClass() { - return MapClass.instance; + return loadClass("Map"); } @Idempotent public static VmClass getMappingClass() { - return MappingClass.instance; + return loadClass("Mapping"); } @Idempotent public static VmClass getDynamicClass() { - return DynamicClass.instance; + return loadClass("Dynamic"); } public static VmClass getRenderDirectiveClass() { - return RenderDirectiveClass.instance; + return loadClass("RenderDirective"); } /** @@ -124,28 +119,28 @@ public static VmClass getRenderDirectiveClass() { * getModule().getVmClass()}. */ public static VmClass getModuleClass() { - return ModuleClass.instance; + return loadClass("Module"); } public static VmClass getClassClass() { - return ClassClass.instance; + return loadClass("Class"); } public static VmClass getTypeAliasClass() { - return TypeAliasClass.instance; + return loadClass("TypeAlias"); } public static VmClass getRegexClass() { - return RegexClass.instance; + return loadClass("Regex"); } public static VmClass getRegexMatchClass() { - return RegexMatchClass.instance; + return loadClass("RegexMatch"); } @Idempotent public static VmClass getFunctionClass() { - return FunctionClass.instance; + return loadClass("Function"); } public static VmClass getFunctionNClass(int paramCount) { @@ -165,295 +160,95 @@ public static VmClass getFunctionNClass(int paramCount) { } public static VmClass getFunction0Class() { - return Function0Class.instance; + return loadClass("Function0"); } public static VmClass getFunction1Class() { - return Function1Class.instance; + return loadClass("Function1"); } public static VmClass getFunction2Class() { - return Function2Class.instance; + return loadClass("Function2"); } public static VmClass getFunction3Class() { - return Function3Class.instance; + return loadClass("Function3"); } public static VmClass getFunction4Class() { - return Function4Class.instance; + return loadClass("Function4"); } public static VmClass getFunction5Class() { - return Function5Class.instance; + return loadClass("Function5"); } public static VmClass getPairClass() { - return PairClass.instance; + return loadClass("Pair"); } public static VmClass getVarArgsClass() { - return VarArgsClass.instance; + return loadClass("VarArgs"); } public static VmClass getModuleInfoClass() { - return ModuleInfoClass.instance; + return loadClass("ModuleInfo"); } public static VmClass getAnnotationClass() { - return AnnotationClass.instance; + return loadClass("Annotation"); } public static VmClass getConvertPropertyClass() { - return ConvertPropertyClass.instance; + return loadClass("ConvertProperty"); } public static VmClass getDeprecatedClass() { - return DeprecatedClass.instance; + return loadClass("Deprecated"); } public static VmClass getResourceClass() { - return ResourceClass.instance; + return loadClass("Resource"); } public static VmTypeAlias getNonNullTypeAlias() { - return NonNullTypeAlias.instance; + return loadTypeAlias("NonNull"); } public static VmTypeAlias getInt8TypeAlias() { - return Int8TypeAlias.instance; + return loadTypeAlias("Int8"); } public static VmTypeAlias getInt16TypeAlias() { - return Int16TypeAlias.instance; + return loadTypeAlias("Int16"); } public static VmTypeAlias getInt32TypeAlias() { - return Int32TypeAlias.instance; + return loadTypeAlias("Int32"); } public static VmTypeAlias getMixinTypeAlias() { - return MixinTypeAlias.instance; + return loadTypeAlias("Mixin"); } public static VmTypeAlias getUIntTypeAlias() { - return UIntTypeAlias.instance; + return loadTypeAlias("UInt"); } public static VmTypeAlias getUInt8TypeAlias() { - return UInt8TypeAlias.instance; + return loadTypeAlias("UInt8"); } public static VmTypeAlias getUInt16TypeAlias() { - return UInt16TypeAlias.instance; + return loadTypeAlias("UInt16"); } public static VmTypeAlias getUInt32TypeAlias() { - return UInt32TypeAlias.instance; + return loadTypeAlias("UInt32"); } public static VmTypeAlias getCharTypeAlias() { - return CharTypeAlias.instance; - } - - private static final class AnyClass { - static final VmClass instance = loadClass("Any"); - } - - private static final class ObjectClass { - static final VmClass instance = loadClass("Object"); - } - - private static final class TypedClass { - static final VmClass instance = loadClass("Typed"); - } - - private static final class NullClass { - static final VmClass instance = loadClass("Null"); - } - - private static final class NumberClass { - static final VmClass instance = loadClass("Number"); - } - - private static final class IntClass { - static final VmClass instance = loadClass("Int"); - } - - private static final class FloatClass { - static final VmClass instance = loadClass("Float"); - } - - private static final class StringClass { - static final VmClass instance = loadClass("String"); - } - - private static final class BooleanClass { - static final VmClass instance = loadClass("Boolean"); - } - - private static final class DurationClass { - static final VmClass instance = loadClass("Duration"); - } - - private static final class DataSizeClass { - static final VmClass instance = loadClass("DataSize"); - } - - private static final class BytesClass { - static final VmClass instance = loadClass("Bytes"); - } - - private static final class IntSeqClass { - static final VmClass instance = loadClass("IntSeq"); - } - - private static final class CollectionClass { - static final VmClass instance = loadClass("Collection"); - } - - private static final class ListClass { - static final VmClass instance = loadClass("List"); - } - - private static final class SetClass { - static final VmClass instance = loadClass("Set"); - } - - private static final class ListingClass { - static final VmClass instance = loadClass("Listing"); - } - - private static final class MapClass { - static final VmClass instance = loadClass("Map"); - } - - private static final class MappingClass { - static final VmClass instance = loadClass("Mapping"); - } - - private static final class DynamicClass { - static final VmClass instance = loadClass("Dynamic"); - } - - private static final class ModuleClass { - static final VmClass instance = loadClass("Module"); - } - - private static final class ClassClass { - static final VmClass instance = loadClass("Class"); - } - - private static final class TypeAliasClass { - static final VmClass instance = loadClass("TypeAlias"); - } - - private static final class RegexClass { - static final VmClass instance = loadClass("Regex"); - } - - private static final class RegexMatchClass { - static final VmClass instance = loadClass("RegexMatch"); - } - - private static final class RenderDirectiveClass { - static final VmClass instance = loadClass("RenderDirective"); - } - - private static final class PairClass { - static final VmClass instance = loadClass("Pair"); - } - - private static final class VarArgsClass { - static final VmClass instance = loadClass("VarArgs"); - } - - private static final class ModuleInfoClass { - static final VmClass instance = loadClass("ModuleInfo"); - } - - private static final class AnnotationClass { - static final VmClass instance = loadClass("Annotation"); - } - - private static final class ConvertPropertyClass { - static final VmClass instance = loadClass("ConvertProperty"); - } - - private static final class DeprecatedClass { - static final VmClass instance = loadClass("Deprecated"); - } - - private static final class ResourceClass { - static final VmClass instance = loadClass("Resource"); - } - - private static final class FunctionClass { - static final VmClass instance = loadClass("Function"); - } - - private static final class Function0Class { - static final VmClass instance = loadClass("Function0"); - } - - private static final class Function1Class { - static final VmClass instance = loadClass("Function1"); - } - - private static final class Function2Class { - static final VmClass instance = loadClass("Function2"); - } - - private static final class Function3Class { - static final VmClass instance = loadClass("Function3"); - } - - private static final class Function4Class { - static final VmClass instance = loadClass("Function4"); - } - - private static final class Function5Class { - static final VmClass instance = loadClass("Function5"); - } - - private static final class NonNullTypeAlias { - static final VmTypeAlias instance = loadTypeAlias("NonNull"); - } - - private static final class Int8TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("Int8"); - } - - private static final class Int16TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("Int16"); - } - - private static final class Int32TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("Int32"); - } - - private static final class UIntTypeAlias { - static final VmTypeAlias instance = loadTypeAlias("UInt"); - } - - private static final class UInt8TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("UInt8"); - } - - private static final class UInt16TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("UInt16"); - } - - private static final class UInt32TypeAlias { - static final VmTypeAlias instance = loadTypeAlias("UInt32"); - } - - private static final class CharTypeAlias { - static final VmTypeAlias instance = loadTypeAlias("Char"); - } - - private static final class MixinTypeAlias { - static final VmTypeAlias instance = loadTypeAlias("Mixin"); + return loadTypeAlias("Char"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/BenchmarkModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/BenchmarkModule.java index 0df802b950..46aeec115f 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/BenchmarkModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/BenchmarkModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,22 +19,13 @@ import java.net.URI; public final class BenchmarkModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:Benchmark"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:Benchmark")); } public static VmClass getBenchmarkResultClass() { - return BenchmarkResultClass.instance; - } - - private static final class BenchmarkResultClass { - static final VmClass instance = loadClass("BenchmarkResult"); + return loadClass("BenchmarkResult"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/CommandModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/CommandModule.java index 5f6491b4c1..ed56c663a4 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/CommandModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/CommandModule.java @@ -19,72 +19,39 @@ import java.net.URI; public final class CommandModule extends StdLibModule { - static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:Command"), instance); - } private CommandModule() {} public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:Command")); } public static VmClass getCommandInfoClass() { - return CommandInfoClass.instance; + return loadClass("CommandInfo"); } public static VmClass getBaseFlagClass() { - return BaseFlagClass.instance; + return loadClass("BaseFlag"); } public static VmClass getFlagClass() { - return FlagClass.instance; + return loadClass("Flag"); } public static VmClass getBooleanFlagClass() { - return BooleanFlagClass.instance; + return loadClass("BooleanFlag"); } public static VmClass getCountedFlagClass() { - return CountedFlagClass.instance; + return loadClass("CountedFlag"); } public static VmClass getArgumentClass() { - return ArgumentClass.instance; + return loadClass("Argument"); } public static VmClass getImportClass() { - return ImportClass.instance; - } - - private static final class CommandInfoClass { - static final VmClass instance = loadClass("CommandInfo"); - } - - private static final class BaseFlagClass { - static final VmClass instance = loadClass("BaseFlag"); - } - - private static final class FlagClass { - static final VmClass instance = loadClass("Flag"); - } - - private static final class BooleanFlagClass { - static final VmClass instance = loadClass("BooleanFlag"); - } - - private static final class CountedFlagClass { - static final VmClass instance = loadClass("CountedFlag"); - } - - private static final class ArgumentClass { - static final VmClass instance = loadClass("Argument"); - } - - private static final class ImportClass { - static final VmClass instance = loadClass("Import"); + return loadClass("Import"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/JsonnetModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/JsonnetModule.java index 4851f58896..1b19cabad4 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/JsonnetModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/JsonnetModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,30 +19,17 @@ import java.net.URI; public final class JsonnetModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:jsonnet"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:jsonnet")); } public static VmClass getImportStrClass() { - return ImportStrClass.instance; + return loadClass("ImportStr"); } public static VmClass getExtVarClass() { - return ExtVarClass.instance; - } - - private static final class ImportStrClass { - static final VmClass instance = loadClass("ImportStr"); - } - - private static final class ExtVarClass { - static final VmClass instance = loadClass("ExtVar"); + return loadClass("ExtVar"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/MathModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/MathModule.java index b6425c4d1c..6b30e84017 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/MathModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/MathModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,10 @@ import java.net.URI; public final class MathModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:math"), instance); - } private MathModule() {} public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:math")); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/ModuleCache.java b/pkl-core/src/main/java/org/pkl/core/runtime/ModuleCache.java index a88133adc8..c9b66635cc 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/ModuleCache.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/ModuleCache.java @@ -82,8 +82,7 @@ public synchronized VmTyped getOrLoad( if (ModuleKeys.isStdLibModule(moduleKey)) { var moduleName = moduleKey.getUri().getSchemeSpecificPart(); - // some standard library modules are cached as static singletons - // and hence aren't parsed/initialized anew for every evaluator + // Core standard-library modules are cached by the current language instance. switch (moduleName) { case "analyze": return AnalyzeModule.getModule(); diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/NetModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/NetModule.java index b677d46e25..0ee30b1cf3 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/NetModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/NetModule.java @@ -19,24 +19,15 @@ import java.net.URI; public final class NetModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:net"), instance); - } private NetModule() {} public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:net")); } public static VmClass getUrlClass() { - return UrlClass.instance; - } - - private static final class UrlClass { - static final VmClass instance = loadClass("Url"); + return loadClass("Url"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/PklBinaryModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/PklBinaryModule.java index 0063a15fcc..be58940ac6 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/PklBinaryModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/PklBinaryModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2025 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2025-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,13 +18,8 @@ import java.net.URI; public class PklBinaryModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:pklbinary"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:pklbinary")); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/PlatformModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/PlatformModule.java index 2a90227b9a..616390f8eb 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/PlatformModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/PlatformModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,62 +19,33 @@ import java.net.URI; public final class PlatformModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:platform"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:platform")); } public static VmClass getPlatformClass() { - return PlatformClass.instance; + return loadClass("Platform"); } public static VmClass getLanguageClass() { - return LanguageClass.instance; + return loadClass("Language"); } public static VmClass getRuntimeClass() { - return RuntimeClass.instance; + return loadClass("Runtime"); } public static VmClass getVirtualMachineClass() { - return VirtualMachineClass.instance; + return loadClass("VirtualMachine"); } public static VmClass getOperatingSystemClass() { - return OperatingSystemClass.instance; + return loadClass("OperatingSystem"); } public static VmClass getProcessorClass() { - return ProcessorClass.instance; - } - - private static final class PlatformClass { - static final VmClass instance = loadClass("Platform"); - } - - private static final class LanguageClass { - static final VmClass instance = loadClass("Language"); - } - - private static final class RuntimeClass { - static final VmClass instance = loadClass("Runtime"); - } - - private static final class VirtualMachineClass { - static final VmClass instance = loadClass("VirtualMachine"); - } - - private static final class OperatingSystemClass { - static final VmClass instance = loadClass("OperatingSystem"); - } - - private static final class ProcessorClass { - static final VmClass instance = loadClass("Processor"); + return loadClass("Processor"); } @CompilerDirectives.TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/ProjectModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/ProjectModule.java index 85a9a4788f..1328135538 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/ProjectModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/ProjectModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,10 @@ import static org.pkl.core.PClassInfo.pklProjectUri; public final class ProjectModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(pklProjectUri, instance); - } private ProjectModule() {} public static VmTyped getModule() { - return instance; + return getModule(pklProjectUri); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/RefModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/RefModule.java index 915b8fb123..a77980d289 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/RefModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/RefModule.java @@ -19,26 +19,13 @@ import java.net.URI; public class RefModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:ref"), instance); - } public static VmClass getReferenceClass() { - return ReferenceClass.instance; + return loadClass("Reference"); } public static VmClass getAccessClass() { - return AccessClass.instance; - } - - private static final class ReferenceClass { - static final VmClass instance = loadClass("Reference"); - } - - private static final class AccessClass { - static final VmClass instance = loadClass("Access"); + return loadClass("Access"); } @TruffleBoundary @@ -48,6 +35,6 @@ private static VmClass loadClass(String className) { } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:ref")); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/ReflectModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/ReflectModule.java index 2e01831ebc..a3f74e98fc 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/ReflectModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/ReflectModule.java @@ -19,157 +19,79 @@ import java.net.URI; public final class ReflectModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - // Forcing of `instance` inside `loadModule()` results in calls to methods such as - // `ReflectModule.getNullableTypeClass()` via `MirrorFactories`. - // By calling loadModule() in the outer class's static initializer (rather than having another - // nested class for this), - // initialization loops (e.g., during execution of native-image) are avoided. - loadModule(URI.create("pkl:reflect"), instance); - } private ReflectModule() {} public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:reflect")); } public static VmClass getModuleClass() { - return ModuleClass.instance; + return loadClass("Module"); } public static VmClass getClassClass() { - return ClassClass.instance; + return loadClass("Class"); } public static VmClass getTypeAliasClass() { - return TypeAliasClass.instance; + return loadClass("TypeAlias"); } public static VmClass getPropertyClass() { - return PropertyClass.instance; + return loadClass("Property"); } public static VmClass getMethodClass() { - return MethodClass.instance; + return loadClass("Method"); } public static VmClass getMethodParameterClass() { - return MethodParameterClass.instance; + return loadClass("MethodParameter"); } public static VmClass getTypeParameterClass() { - return TypeParameterClass.instance; + return loadClass("TypeParameter"); } public static VmClass getDeclaredTypeClass() { - return DeclaredTypeClass.instance; + return loadClass("DeclaredType"); } public static VmClass getStringLiteralTypeClass() { - return StringLiteralTypeClass.instance; + return loadClass("StringLiteralType"); } public static VmClass getUnionTypeClass() { - return UnionTypeClass.instance; + return loadClass("UnionType"); } public static VmClass getNullableTypeClass() { - return NullableTypeClass.instance; + return loadClass("NullableType"); } public static VmClass getModuleTypeClass() { - return ModuleTypeClass.instance; + return loadClass("ModuleType"); } public static VmClass getThisTypeClass() { - return ThisTypeClass.instance; + return loadClass("ThisType"); } public static VmClass getFunctionTypeClass() { - return FunctionTypeClass.instance; + return loadClass("FunctionType"); } public static VmClass getUnknownTypeClass() { - return UnknownTypeClass.instance; + return loadClass("UnknownType"); } public static VmClass getNothingTypeClass() { - return NothingTypeClass.instance; + return loadClass("NothingType"); } public static VmClass getTypeVariableClass() { - return TypeVariableClass.instance; - } - - private static final class ModuleClass { - static final VmClass instance = loadClass("Module"); - } - - private static final class ClassClass { - static final VmClass instance = loadClass("Class"); - } - - private static final class TypeAliasClass { - static final VmClass instance = loadClass("TypeAlias"); - } - - private static final class PropertyClass { - static final VmClass instance = loadClass("Property"); - } - - private static final class MethodClass { - static final VmClass instance = loadClass("Method"); - } - - private static final class MethodParameterClass { - static final VmClass instance = loadClass("MethodParameter"); - } - - private static final class TypeParameterClass { - static final VmClass instance = loadClass("TypeParameter"); - } - - private static final class DeclaredTypeClass { - static final VmClass instance = loadClass("DeclaredType"); - } - - private static final class StringLiteralTypeClass { - static final VmClass instance = loadClass("StringLiteralType"); - } - - private static final class UnionTypeClass { - static final VmClass instance = loadClass("UnionType"); - } - - private static final class NullableTypeClass { - static final VmClass instance = loadClass("NullableType"); - } - - private static final class ModuleTypeClass { - static final VmClass instance = loadClass("ModuleType"); - } - - private static final class ThisTypeClass { - static final VmClass instance = loadClass("ThisType"); - } - - private static final class FunctionTypeClass { - static final VmClass instance = loadClass("FunctionType"); - } - - private static final class UnknownTypeClass { - static final VmClass instance = loadClass("UnknownType"); - } - - private static final class NothingTypeClass { - static final VmClass instance = loadClass("NothingType"); - } - - private static final class TypeVariableClass { - static final VmClass instance = loadClass("TypeVariable"); + return loadClass("TypeVariable"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/ReleaseModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/ReleaseModule.java index fa40bf4358..e7d0a50929 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/ReleaseModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/ReleaseModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,46 +19,25 @@ import java.net.URI; public final class ReleaseModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:release"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:release")); } public static VmClass getReleaseClass() { - return ReleaseModule.ReleaseClass.instance; + return loadClass("Release"); } public static VmClass getSourceCodeClass() { - return ReleaseModule.SourceCodeClass.instance; + return loadClass("SourceCode"); } public static VmClass getDocumentationClass() { - return ReleaseModule.DocumentationClass.instance; + return loadClass("Documentation"); } public static VmClass getStandardLibraryClass() { - return ReleaseModule.StandardLibraryClass.instance; - } - - private static final class ReleaseClass { - static final VmClass instance = loadClass("Release"); - } - - private static final class SourceCodeClass { - static final VmClass instance = loadClass("SourceCode"); - } - - private static final class DocumentationClass { - static final VmClass instance = loadClass("Documentation"); - } - - private static final class StandardLibraryClass { - static final VmClass instance = loadClass("StandardLibrary"); + return loadClass("StandardLibrary"); } @CompilerDirectives.TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/SemVerModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/SemVerModule.java index 6438a37c8e..4a58225cb9 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/SemVerModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/SemVerModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,24 +20,15 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; public final class SemVerModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(pklSemverUri, instance); - } private SemVerModule() {} public static VmTyped getModule() { - return instance; + return getModule(pklSemverUri); } public static VmClass getVersionClass() { - return VersionClass.instance; - } - - private static final class VersionClass { - static final VmClass instance = loadClass("Version"); + return loadClass("Version"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/SettingsModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/SettingsModule.java index 048a9e5886..2babe4c2a0 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/SettingsModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/SettingsModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,10 @@ import static org.pkl.core.PClassInfo.pklSettingsUri; public final class SettingsModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(pklSettingsUri, instance); - } private SettingsModule() {} public static VmTyped getModule() { - return instance; + return getModule(pklSettingsUri); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/StdLibModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/StdLibModule.java index df8bdec3a7..407252f4ef 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/StdLibModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/StdLibModule.java @@ -15,80 +15,103 @@ */ package org.pkl.core.runtime; -import static org.pkl.core.PClassInfo.pklBaseUri; - import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; import java.net.URI; -import java.util.List; +import java.util.HashMap; import java.util.Map; -import org.pkl.core.Loggers; -import org.pkl.core.SecurityManagers; -import org.pkl.core.StackFrameTransformers; -import org.pkl.core.evaluatorSettings.TraceMode; -import org.pkl.core.http.HttpClient; -import org.pkl.core.module.ModuleKeyFactories; +import java.util.WeakHashMap; +import java.util.function.Supplier; +import org.graalvm.collections.UnmodifiableEconomicMap; +import org.graalvm.polyglot.Context; +import org.pkl.core.ast.member.ObjectMember; import org.pkl.core.module.ModuleKeys; import org.pkl.core.module.ResolvedModuleKey; -import org.pkl.core.resource.ResourceReader; -import org.pkl.core.resource.ResourceReaders; +/** Access to standard-library state owned by the current Truffle language instance. */ public abstract class StdLibModule { @TruffleBoundary - protected static void loadModule(URI uri, VmTyped instance) { - doLoad(uri, instance); + protected static VmTyped getModule(URI uri) { + return inContext(() -> VmLanguage.get(null).standardLibrary.getModule(uri)); } - private static void doLoad(URI uri, VmTyped instance) { - VmUtils.createContext( - () -> { - var vmContext = VmContext.get(null); - var isPklBaseModule = uri.equals(pklBaseUri); - var resourceReaders = - isPklBaseModule - // needed when initializing `pkl:base` because of - // `read("prop:pkl.outputFormat")` - ? List.of(ResourceReaders.externalProperty()) - : List.of(); - vmContext.initialize( - new VmContext.Holder( - StackFrameTransformers.defaultTransformer, - SecurityManagers.defaultManager, - HttpClient.dummyClient(), - new ModuleResolver(List.of(ModuleKeyFactories.standardLibrary)), - new ResourceManager(SecurityManagers.defaultManager, resourceReaders), - Loggers.noop(), - Map.of(), - Map.of(), - null, - null, - null, - null, - TraceMode.COMPACT, - false)); - var language = VmLanguage.get(null); - var moduleKey = ModuleKeys.standardLibrary(uri); - var source = VmUtils.loadSource((ResolvedModuleKey) moduleKey); - language.initializeModule( - moduleKey, - (ResolvedModuleKey) moduleKey, - vmContext.getModuleResolver(), - source, - instance, - null); - // evaluate eagerly to increase thread safety - // (stdlib module objects are statically shared singletons when running on JVM) - // and ensure compile-time evaluation in AOT mode - instance.force(false, true); + /** + * Runs internal factory work in the current context, or the legacy engine when none is entered. + */ + @TruffleBoundary + public static T inContext(Supplier action) { + boolean entered; + try { + Context.getCurrent(); // Checks without the assertion used by Truffle language references. + VmLanguage.get(null); + entered = true; + } catch (IllegalStateException e) { + entered = false; + } + if (entered) return action.get(); + try (var context = Context.newBuilder("pkl").engine(VmUtils.getEngine()).build()) { + context.initialize("pkl"); + context.enter(); + try { + return action.get(); + } finally { + context.leave(); + } + } + } + + /** Factory ASTs, including roots with no language, must not be shared across engines. */ + @TruffleBoundary + public static UnmodifiableEconomicMap factoryMembers( + Object factory, Supplier> builder) { + return inContext(() -> VmLanguage.get(null).standardLibrary.factoryMembers(factory, builder)); + } + + static final class Registry { + private final Map> factories = + new WeakHashMap<>(); + + synchronized UnmodifiableEconomicMap factoryMembers( + Object factory, Supplier> builder) { + return factories.computeIfAbsent(factory, ignored -> builder.get()); + } + + private final Map modules = new HashMap<>(); + private final Map failures = new HashMap<>(); - // seed base module's `output` members; `output` contains truffle nodes that - // need to be initialized statically (e.g. LetExprNode, TypeTestNode). - // additionally, its `cachedMembers` is not thread-safe and needs to be initialized - // statically. - if (isPklBaseModule) { - var output = VmUtils.readModuleOutput(instance); - output.force(false, true); - } - }) - .close(); + @TruffleBoundary + synchronized VmTyped getModule(URI uri) { + var failure = failures.get(uri); + if (failure instanceof RuntimeException exception) throw exception; + if (failure instanceof Error error) throw error; + var existing = modules.get(uri); + if (existing != null) return existing; + var module = VmUtils.createEmptyModule(); + // Recursive class/annotation initialization must see this same, partially built module. + // The registry lock prevents another thread from seeing it before initialization completes. + modules.put(uri, module); + try { + return VmContext.get(null) + .withStandardLibraryHolder( + () -> { + var moduleKey = ModuleKeys.standardLibrary(uri); + VmLanguage.get(null) + .initializeModule( + moduleKey, + (ResolvedModuleKey) moduleKey, + VmContext.get(null).getModuleResolver(), + VmUtils.loadSource((ResolvedModuleKey) moduleKey), + module, + null); + module.force(false, true); + if (uri.toString().equals("pkl:base")) { + VmUtils.readModuleOutput(module).force(false, true); + } + return module; + }); + } catch (RuntimeException | Error e) { + failures.put(uri, e); + throw e; + } + } } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/SyntaxModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/SyntaxModule.java index e4303770b9..78842be720 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/SyntaxModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/SyntaxModule.java @@ -19,38 +19,21 @@ import java.net.URI; public final class SyntaxModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:syntax"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:syntax")); } public static VmClass getNodeClass() { - return NodeClass.instance; + return loadClass("Node"); } public static VmClass getSpanClass() { - return SpanClass.instance; + return loadClass("Span"); } public static VmClass getSourceLocationClass() { - return SourceLocationClass.instance; - } - - private static final class NodeClass { - static final VmClass instance = loadClass("Node"); - } - - private static final class SpanClass { - static final VmClass instance = loadClass("Span"); - } - - private static final class SourceLocationClass { - static final VmClass instance = loadClass("SourceLocation"); + return loadClass("SourceLocation"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/TestModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/TestModule.java index 81672fa593..107276f485 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/TestModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/TestModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +18,10 @@ import java.net.URI; public final class TestModule extends StdLibModule { - static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:test"), instance); - } private TestModule() {} public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:test")); } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmClass.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmClass.java index 13094a4f78..3f667fb74b 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmClass.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmClass.java @@ -37,8 +37,8 @@ import org.pkl.core.util.EconomicMaps; import org.pkl.core.util.LateInit; -// Most stdlib modules and their members are initialized in static initializers -// and reused across Truffle contexts. +// Standard-library modules and their members are initialized per language instance +// and reused by contexts sharing that instance. // As a consequence, VmObject/VmClass instances of stdlib modules must be thread-safe. // The currently implemented (and likely insufficient) solution is to // * deeply force standard library modules at initialization time. diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmContext.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmContext.java index e267e5aeb9..d0824f41f3 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmContext.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmContext.java @@ -21,16 +21,23 @@ import com.oracle.truffle.api.nodes.Node; import java.nio.file.Path; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; import org.jspecify.annotations.Nullable; import org.pkl.core.Logger; +import org.pkl.core.Loggers; import org.pkl.core.SecurityManager; +import org.pkl.core.SecurityManagers; import org.pkl.core.StackFrameTransformer; +import org.pkl.core.StackFrameTransformers; import org.pkl.core.evaluatorSettings.TraceMode; import org.pkl.core.http.HttpClient; +import org.pkl.core.module.ModuleKeyFactories; import org.pkl.core.module.ProjectDependenciesManager; import org.pkl.core.packages.PackageResolver; -import org.pkl.core.util.LateInit; +import org.pkl.core.resource.ResourceReaders; public final class VmContext { private static final ContextReference REFERENCE = @@ -42,7 +49,7 @@ public VmContext(VmLanguage vmLanguage, Env env) { new VmValueTrackerFactory(env.lookup(Instrumenter.class), vmLanguage); } - @LateInit private Holder holder; + @Nullable private Holder holder; public static final class Holder { private static final String OUTPUT_FORMAT_KEY = "pkl.outputFormat"; @@ -112,60 +119,91 @@ public void initialize(Holder holder) { this.holder = holder; } + private Holder getHolder() { + return Objects.requireNonNull(holder, "Pkl context is not initialized"); + } + + /** Bootstrap with fixed inputs and isolated thread-local type-checking/instrumentation state. */ + T withStandardLibraryHolder(Supplier action) { + var previous = holder; + holder = + new Holder( + StackFrameTransformers.defaultTransformer, + SecurityManagers.defaultManager, + HttpClient.dummyClient(), + new ModuleResolver(List.of(ModuleKeyFactories.standardLibrary)), + new ResourceManager( + SecurityManagers.defaultManager, List.of(ResourceReaders.externalProperty())), + Loggers.noop(), + Map.of(), + Map.of(), + null, + null, + null, + null, + TraceMode.COMPACT, + false); + try { + return VmLanguage.get(null).localContext.get().withBootstrapState(action); + } finally { + holder = previous; + } + } + public ModuleCache getModuleCache() { - return holder.moduleCache; + return getHolder().moduleCache; } public @Nullable Path getModuleCacheDir() { - return holder.moduleCacheDir; + return getHolder().moduleCacheDir; } public StackFrameTransformer getFrameTransformer() { - return holder.frameTransformer; + return getHolder().frameTransformer; } public SecurityManager getSecurityManager() { - return holder.securityManager; + return getHolder().securityManager; } public HttpClient getHttpClient() { - return holder.httpClient; + return getHolder().httpClient; } public ModuleResolver getModuleResolver() { - return holder.moduleResolver; + return getHolder().moduleResolver; } public ResourceManager getResourceManager() { - return holder.resourceManager; + return getHolder().resourceManager; } public Logger getLogger() { - return holder.logger; + return getHolder().logger; } public Map getEnvironmentVariables() { - return holder.environmentVariables; + return getHolder().environmentVariables; } public Map getExternalProperties() { - return holder.externalProperties; + return getHolder().externalProperties; } public @Nullable PackageResolver getPackageResolver() { - return holder.packageResolver; + return getHolder().packageResolver; } public @Nullable ProjectDependenciesManager getProjectDependenciesManager() { - return holder.projectDependenciesManager; + return getHolder().projectDependenciesManager; } public TraceMode getTraceMode() { - return holder.traceMode; + return getHolder().traceMode; } public boolean getPowerAssertionsEnabled() { - return holder.powerAssertions; + return getHolder().powerAssertions; } public VmValueTrackerFactory getValueTrackerFactory() { diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java index 3f1c88a7f3..3e8677705c 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamic.java @@ -40,19 +40,14 @@ public final class VmDynamic extends VmObject { private int cachedRegularMemberCount = -1; - private static final class EmptyHolder { - private static final VmDynamic EMPTY = - new VmDynamic( - VmUtils.createEmptyMaterializedFrame(), - BaseModule.getDynamicClass().getPrototype(), - EconomicMaps.create(), - 0); - } - private final int length; public static VmDynamic empty() { - return EmptyHolder.EMPTY; + return new VmDynamic( + VmUtils.createEmptyMaterializedFrame(), + BaseModule.getDynamicClass().getPrototype(), + EconomicMaps.create(), + 0); } public VmDynamic( diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamicCursors.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamicCursors.java index 1dee6df2cb..156e92e66c 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamicCursors.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmDynamicCursors.java @@ -229,7 +229,7 @@ public AbstractMemberCursor(VmDynamic iteratee) { super(iteratee); var builder = ArrayBuilder.of(UnmodifiableMapCursor[]::new); for (VmObject object = iteratee; - object != null && object.parent != objectPrototype; + object != null && object.parent != BaseModule.getObjectClass().getPrototype(); object = object.parent) { builder.add(object.members.getEntries()); } @@ -265,7 +265,6 @@ public Object key() { } abstract static sealed class AbstractUnorderedMemberCursor extends VmObjectCursor { - private static final VmTyped dynamicPrototype = BaseModule.getDynamicClass().getPrototype(); protected final VmDynamic iteratee; private VmObject currentObject; @@ -293,7 +292,7 @@ public final boolean advance() { && shouldVisit(currentMember.getValue())) return true; } var parent = currentObject.parent; - if (parent == dynamicPrototype) return false; + if (parent == BaseModule.getDynamicClass().getPrototype()) return false; assert parent != null; currentObject = parent; currentMember = currentObject.members.getEntries(); diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmLanguage.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmLanguage.java index 2b34d8426d..b74431a979 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmLanguage.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmLanguage.java @@ -40,6 +40,8 @@ contextPolicy = ContextPolicy.SHARED) @ProvidedTags(PklTags.Expression.class) public final class VmLanguage extends TruffleLanguage { + final StdLibModule.Registry standardLibrary = new StdLibModule.Registry(); + public static final String MIME_TYPE = "application/x-pkl"; private static final LanguageReference REFERENCE = diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmListing.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmListing.java index 374d0cd134..9dd6113379 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmListing.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmListing.java @@ -32,19 +32,15 @@ import org.pkl.core.util.EconomicMaps; public final class VmListing extends VmListingOrMapping { - private static final class EmptyHolder { - private static final VmListing EMPTY = - new VmListing( - VmUtils.createEmptyMaterializedFrame(), - BaseModule.getListingClass().getPrototype(), - EconomicMaps.create(), - 0); - } private final int length; public static VmListing empty() { - return EmptyHolder.EMPTY; + return new VmListing( + VmUtils.createEmptyMaterializedFrame(), + BaseModule.getListingClass().getPrototype(), + EconomicMaps.create(), + 0); } public VmListing( diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmLocalContext.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmLocalContext.java index 36c2132773..9daea1dac6 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmLocalContext.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmLocalContext.java @@ -17,81 +17,96 @@ import java.util.ArrayDeque; import java.util.Deque; +import java.util.function.Supplier; /** A per-context thread-local value that can be used to influence execution. */ public class VmLocalContext { - private boolean shouldEagerTypecheck = false; + private State state = new State(); - /** Whether we are currently inside a type test ({@code is} check). */ - private boolean inTypeTest = false; + private static final class State { + private boolean shouldEagerTypecheck = false; - /** The number of classes currently being initialized. */ - private int classDepth = 0; + /** Whether we are currently inside a type test ({@code is} check). */ + private boolean inTypeTest = false; - /** The classes currently being initialized. */ - private final Deque pendingClasses = new ArrayDeque<>(); + /** The number of classes currently being initialized. */ + private int classDepth = 0; - /** - * Number of active {@link VmValueTracker} instances. Used to determine if instrumentation is - * already active. - */ - private int activeTrackerDepth = 0; + /** The classes currently being initialized. */ + private final Deque pendingClasses = new ArrayDeque<>(); - private boolean instrumentationEverUsed = false; + /** + * Number of active {@link VmValueTracker} instances. Used to determine if instrumentation is + * already active. + */ + private int activeTrackerDepth = 0; + + private boolean instrumentationEverUsed = false; + } + + T withBootstrapState(Supplier action) { + var previous = state; + state = new State(); + try { + return action.get(); + } finally { + state = previous; + } + } public VmLocalContext() {} public void shouldEagerTypecheck(boolean shouldEagerTypecheck) { - this.shouldEagerTypecheck = shouldEagerTypecheck; + state.shouldEagerTypecheck = shouldEagerTypecheck; } public boolean shouldEagerTypecheck() { - return this.shouldEagerTypecheck; + return state.shouldEagerTypecheck; } public void setInTypeTest(boolean inTypeTest) { - this.inTypeTest = inTypeTest; + state.inTypeTest = inTypeTest; } public boolean isInTypeTest() { - return inTypeTest; + return state.inTypeTest; } public void beginClassInit(VmClass vmClass) { - classDepth++; - pendingClasses.add(vmClass); + state.classDepth++; + state.pendingClasses.add(vmClass); } public void endClassInit() { - classDepth--; - if (classDepth > 0) { + state.classDepth--; + if (state.classDepth > 0) { return; } - while (!pendingClasses.isEmpty()) { - var clazz = pendingClasses.pop(); + while (!state.pendingClasses.isEmpty()) { + var clazz = state.pendingClasses.pop(); clazz.onFullyInitialized(); } } public void clearClassInitState() { - pendingClasses.clear(); - classDepth = 0; + state.pendingClasses.clear(); + state.classDepth = 0; } public void enterTracker() { - activeTrackerDepth++; - instrumentationEverUsed = true; + state.activeTrackerDepth++; + state.instrumentationEverUsed = true; } public void exitTracker() { - activeTrackerDepth--; + state.activeTrackerDepth--; } public boolean hasActiveTracker() { - return activeTrackerDepth > 0; + return state.activeTrackerDepth > 0; } public boolean isInstrumentationEverUsed() { - return instrumentationEverUsed; + return state.instrumentationEverUsed; } } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmMapping.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmMapping.java index 19e138dcd1..c6e5a2cab9 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmMapping.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmMapping.java @@ -38,14 +38,6 @@ public final class VmMapping extends VmListingOrMapping { private static final EnumSet isEmptyComputationCursor = EnumSet.of(CursorOption.ANY_ORDER, CursorOption.LAZY_REQUIRED); - private static final class EmptyHolder { - private static final VmMapping EMPTY = - new VmMapping( - VmUtils.createEmptyMaterializedFrame(), - BaseModule.getMappingClass().getPrototype(), - EconomicMaps.create()); - } - @CompilationFinal private long cachedLength = -1; @CompilationFinal private boolean isEmptyComputed; @CompilationFinal private boolean isEmpty; @@ -54,7 +46,10 @@ private static final class EmptyHolder { private @Nullable VmSet __allKeys; public static VmMapping empty() { - return EmptyHolder.EMPTY; + return new VmMapping( + VmUtils.createEmptyMaterializedFrame(), + BaseModule.getMappingClass().getPrototype(), + EconomicMaps.create()); } public VmMapping( diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmObjectCursor.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmObjectCursor.java index 2e90231e7e..2fcdddb626 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmObjectCursor.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmObjectCursor.java @@ -53,7 +53,6 @@ * objects}. */ public abstract class VmObjectCursor { - protected static final VmTyped objectPrototype = BaseModule.getObjectClass().getPrototype(); public enum CursorOption { /** @@ -239,7 +238,7 @@ public final Object value(IndirectCallNode callNode) { if (value != null) return value; // find and evaluate bottom-most member for key (simplified version of VmUtils.readMember()) for (VmObject object = iteratee; - object != null && object.parent != objectPrototype; + object != null && object.parent != BaseModule.getObjectClass().getPrototype(); object = object.parent) { var member = object.getMember(key); if (member != null) { diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmType.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmType.java index fdaf94d1e9..349eb66de1 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmType.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmType.java @@ -264,11 +264,11 @@ protected final boolean doIsSupertypeOf(VmType other) { var goalState = typeArguments.length > 0 ? typeArguments - : nCopies(clazz.getTypeParameterCount(), ClassType.ANY); + : nCopies(clazz.getTypeParameterCount(), new ClassType(BaseModule.getAnyClass())); var state = ct.typeArguments.length > 0 ? ct.typeArguments - : nCopies(ct.clazz.getTypeParameterCount(), ClassType.ANY); + : nCopies(ct.clazz.getTypeParameterCount(), new ClassType(BaseModule.getAnyClass())); for (var c = ct.clazz; c != clazz; c = c.getSuperclass()) { assert c != null; // we know walking parents reaches clazz before null var cSuperclass = c.getSuperclass(); @@ -278,7 +278,8 @@ protected final boolean doIsSupertypeOf(VmType other) { if (cSupertype.typeArguments.length == 0) { // supertype args could be omitted, e.g. class MyList extends List - state = nCopies(cSuperclass.getTypeParameterCount(), ClassType.ANY); + state = + nCopies(cSuperclass.getTypeParameterCount(), new ClassType(BaseModule.getAnyClass())); continue; } else if (state.length == 0) { // subclass may not have type args, e.g. class A extends B @@ -331,8 +332,6 @@ public ClassType(VmClass clazz, VmType[] typeArguments) { super(clazz, typeArguments); } - private static final ClassType ANY = new ClassType(BaseModule.getAnyClass()); - public VmType.ClassType withTypeArguments(VmType[] typeArguments) { return new VmType.ClassType(clazz, typeArguments); } diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java index 6101997df2..499385ca88 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java @@ -89,8 +89,7 @@ public String toString() { public static final int REGEX_FLAGS = Pattern.UNICODE_CHARACTER_CLASS | Pattern.UNICODE_CASE; - // must be a singleton because stdlib modules are singletons; otherwise we get "invalid sharing of - // AST Nodes" + // The legacy Evaluator shares an engine to reuse compiled code and standard-library state. private static final Engine PKL_ENGINE = makePklEngine(); @SuppressWarnings("MagicConstant") @@ -119,7 +118,7 @@ public static Engine getEngine() { } private static Engine makePklEngine() { - return Engine.newBuilder("pkl").option("engine.WarnInterpreterOnly", "false").build(); + return Engine.newBuilder().option("engine.WarnInterpreterOnly", "false").build(); } public static void closeEngine() { diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/XmlModule.java b/pkl-core/src/main/java/org/pkl/core/runtime/XmlModule.java index da9be844ec..f3b665c384 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/XmlModule.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/XmlModule.java @@ -1,5 +1,5 @@ /* - * Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,38 +19,21 @@ import java.net.URI; public final class XmlModule extends StdLibModule { - private static final VmTyped instance = VmUtils.createEmptyModule(); - - static { - loadModule(URI.create("pkl:xml"), instance); - } public static VmTyped getModule() { - return instance; + return getModule(URI.create("pkl:xml")); } public static VmClass getInlineClass() { - return InlineClass.instance; + return loadClass("Inline"); } public static VmClass getCommentClass() { - return CommentClass.instance; + return loadClass("Comment"); } public static VmClass getCDataClass() { - return CDataClass.instance; - } - - private static final class InlineClass { - static final VmClass instance = loadClass("Inline"); - } - - private static final class CommentClass { - static final VmClass instance = loadClass("Comment"); - } - - private static final class CDataClass { - static final VmClass instance = loadClass("CData"); + return loadClass("CData"); } @TruffleBoundary diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/VmObjectFactory.java b/pkl-core/src/main/java/org/pkl/core/stdlib/VmObjectFactory.java index 2561335969..16a8bb329c 100644 --- a/pkl-core/src/main/java/org/pkl/core/stdlib/VmObjectFactory.java +++ b/pkl-core/src/main/java/org/pkl/core/stdlib/VmObjectFactory.java @@ -20,6 +20,7 @@ import com.oracle.truffle.api.frame.VirtualFrame; import java.util.function.Supplier; import org.graalvm.collections.EconomicMap; +import org.graalvm.collections.UnmodifiableEconomicMap; import org.jspecify.annotations.Nullable; import org.pkl.core.ast.ExpressionNode; import org.pkl.core.ast.VmModifier; @@ -32,7 +33,8 @@ public final class VmObjectFactory { private final Supplier classSupplier; - private final EconomicMap members = EconomicMaps.create(); + private boolean initialized; + private final EconomicMap> definitions = EconomicMaps.create(); // not static to avoid compile-time evaluation by native-image private final boolean isPropertyTypeChecked = IoUtils.isTestMode(); @@ -41,79 +43,92 @@ public VmObjectFactory(Supplier classSupplier) { } public VmObjectFactory addIntProperty(String name, IntProperty impl) { - return doAddProperty(name, new IntPropertyNode<>(impl)); + return doAddProperty(name, () -> new IntPropertyNode<>(impl)); } public VmObjectFactory addBooleanProperty(String name, BooleanProperty impl) { - return doAddProperty(name, new BooleanPropertyNode<>(impl)); + return doAddProperty(name, () -> new BooleanPropertyNode<>(impl)); } public VmObjectFactory addStringProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addValueProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addDurationProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addTypedProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addListProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addSetProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addMapProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addClassProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } public VmObjectFactory addProperty(String name, Property impl) { - return doAddProperty(name, new PropertyNode<>(impl)); + return doAddProperty(name, () -> new PropertyNode<>(impl)); } @TruffleBoundary - private VmObjectFactory doAddProperty(String name, ExpressionNode bodyNode) { - var section = VmUtils.unavailableSourceSection(); - var identifier = Identifier.get(name); - var member = new ObjectMember(section, section, VmModifier.NONE, identifier, name); - var node = - isPropertyTypeChecked - ? TypeCheckedPropertyNodeGen.create(null, new FrameDescriptor(), member, bodyNode) - : new UntypedObjectMemberNode(null, new FrameDescriptor(), member, bodyNode); - member.initMemberNode(node); - //noinspection ConstantValue - if (members.put(identifier, member) != null) { - throw new VmExceptionBuilder() - .bug( - "Duplicate definition of property `" - + name - + "` for object of type `" - + classSupplier.get().getDisplayName() - + "`.") - .build(); - } + private synchronized VmObjectFactory doAddProperty( + String name, Supplier body) { + if (initialized) + throw new IllegalStateException("Configure factory properties before creating instances."); + if (definitions.containsKey(name)) + throw new IllegalArgumentException("Duplicate factory property: " + name); + definitions.put(name, body); return this; } + private synchronized UnmodifiableEconomicMap createMembers() { + initialized = true; + var members = EconomicMaps.create(); + var cursor = definitions.getEntries(); + while (cursor.advance()) { + var name = cursor.getKey(); + var bodyNode = cursor.getValue().get(); + var section = VmUtils.unavailableSourceSection(); + var identifier = Identifier.get(name); + var member = new ObjectMember(section, section, VmModifier.NONE, identifier, name); + var node = + isPropertyTypeChecked + ? TypeCheckedPropertyNodeGen.create( + VmLanguage.get(null), new FrameDescriptor(), member, bodyNode) + : new UntypedObjectMemberNode( + VmLanguage.get(null), new FrameDescriptor(), member, bodyNode); + member.initMemberNode(node); + members.put(identifier, member); + } + return members; + } + @TruffleBoundary public VmTyped create(@Nullable E extraStorage) { var clazz = classSupplier.get(); var result = - new VmTyped(VmUtils.createEmptyMaterializedFrame(), clazz.getPrototype(), clazz, members); + new VmTyped( + VmUtils.createEmptyMaterializedFrame(), + clazz.getPrototype(), + clazz, + StdLibModule.factoryMembers(this, this::createMembers)); result.setExtraStorage(extraStorage); return result; } @@ -122,7 +137,12 @@ public VmTyped create(@Nullable E extraStorage) { public VmTyped create(@Nullable E extraStorage, VmTyped parent) { var clazz = classSupplier.get(); assert parent.getVmClass() == clazz; - var result = new VmTyped(VmUtils.createEmptyMaterializedFrame(), parent, clazz, members); + var result = + new VmTyped( + VmUtils.createEmptyMaterializedFrame(), + parent, + clazz, + StdLibModule.factoryMembers(this, this::createMembers)); result.setExtraStorage(extraStorage); return result; } diff --git a/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java b/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java index 8c1e4ce34f..6dd26d6ab8 100644 --- a/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java +++ b/pkl-core/src/main/java/org/pkl/core/stdlib/protobuf/RendererNodes.java @@ -83,10 +83,6 @@ public final class RendererNodes { private static final ArrayCharEscaper fieldNameEscaper = ArrayCharEscaper.builder().withEscape('.', "_").withEscape('#', "_").build(); - private static final StringTypeNode STRING_TYPE = - new StringTypeNode(VmUtils.unavailableSourceSection()); - private static final IntTypeNode INT_TYPE = new IntTypeNode(VmUtils.unavailableSourceSection()); - private RendererNodes() {} public abstract static class renderDocument extends ExternalMethod1Node { @@ -582,10 +578,10 @@ public void visitReference(VmReference value) { || type instanceof BooleanTypeNode) { return type; } else if (type instanceof IntSlotTypeNode) { - return INT_TYPE; + return new IntTypeNode(VmUtils.unavailableSourceSection()); } else if (type instanceof UnionOfStringLiteralsTypeNode || type instanceof StringLiteralTypeNode) { - return STRING_TYPE; + return new StringTypeNode(VmUtils.unavailableSourceSection()); } else if (type instanceof NullableTypeNode nullableType) { return resolveType(nullableType.getElementTypeNode()); } else if (type instanceof TypeAliasTypeNode typeAliasType) {