diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.kt index b1d685da04e..8eff84767d4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ScriptCodegen.kt @@ -111,12 +111,10 @@ class ScriptCodegen private constructor( fun genFieldFromArrayElement(descriptor: ClassDescriptor, paramIndex: Int, elementIndex: Int, name: String) { val elementClassType = typeMapper.mapClass(descriptor) - iv.load(0, classType) - iv.load(paramIndex, elementClassType) - iv.aconst(elementIndex) - iv.aload(OBJECT_TYPE) - iv.checkcast(elementClassType) - iv.putfield(classType.internalName, name, elementClassType.descriptor) + val array = StackValue.local(paramIndex, AsmUtil.getArrayType(OBJECT_TYPE)) + val value = StackValue.arrayElement(OBJECT_TYPE, null, array, StackValue.constant(elementIndex, Type.INT_TYPE)) + val field = StackValue.field(elementClassType, classType, name, false, StackValue.local(0, classType)) + field.store(value, iv) } if (!scriptContext.earlierScripts.isEmpty()) { diff --git a/compiler/frontend.script/src/org/jetbrains/kotlin/script/scriptTypeUtil.kt b/compiler/frontend.script/src/org/jetbrains/kotlin/script/scriptTypeUtil.kt index 455a4c8abb6..849e88403da 100644 --- a/compiler/frontend.script/src/org/jetbrains/kotlin/script/scriptTypeUtil.kt +++ b/compiler/frontend.script/src/org/jetbrains/kotlin/script/scriptTypeUtil.kt @@ -16,9 +16,10 @@ package org.jetbrains.kotlin.script -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.NotFoundClasses +import org.jetbrains.kotlin.descriptors.ScriptDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations -import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.descriptors.findNonGenericClassAcrossDependencies import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.resolve.lazy.descriptors.script.classId @@ -36,22 +37,10 @@ fun KotlinScriptDefinition.getScriptParameters(scriptDescriptor: ScriptDescripto ?: emptyList() fun getKotlinTypeByKClass(scriptDescriptor: ScriptDescriptor, kClass: KClass): KotlinType = - getClassDescriptorByKClassOrMock(scriptDescriptor, kClass).defaultType - -fun getClassDescriptorByKClass(scriptDescriptor: ScriptDescriptor, kClass: KClass): ClassDescriptor? = - scriptDescriptor.module.findClassAcrossModuleDependencies(kClass.classId) - -fun getMockClassDescriptor(scriptDescriptor: ScriptDescriptor, kClass: KClass): ClassDescriptor { - val classId = kClass.classId - val typeParametersCount = generateSequence(classId, ClassId::getOuterClassId).map { 0 }.toList() - return NotFoundClasses(LockBasedStorageManager.NO_LOCKS, scriptDescriptor.module).getClass(classId, typeParametersCount) -} - -fun getClassDescriptorByKClassOrMock(scriptDescriptor: ScriptDescriptor, kClass: KClass): ClassDescriptor = scriptDescriptor.module.findNonGenericClassAcrossDependencies( kClass.classId, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, scriptDescriptor.module) - ) + ).defaultType // TODO: support star projections // TODO: support annotations on types and type parameters diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt index fdc604a1030..9071174eff7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/LazyScriptClassMemberScope.kt @@ -54,7 +54,7 @@ class LazyScriptClassMemberScope( val constructorDescriptor = ClassConstructorDescriptorImpl.create( scriptDescriptor, annotations, baseConstructorDescriptor.isPrimary, scriptDescriptor.source ) - var paramsIndexBase = baseConstructorDescriptor.valueParameters.let { if (it.isEmpty()) 0 else it.last().index + 1 } + var paramsIndexBase = baseConstructorDescriptor.valueParameters.lastIndex + 1 val syntheticParameters = listOf(implicitReceiversParamType, environmentVarsParamType).mapNotNull { param: Pair? -> if (param == null) null diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/CustomScriptCodegenTest.kt b/compiler/tests/org/jetbrains/kotlin/codegen/CustomScriptCodegenTest.kt index 151361d0ff7..e29e4807936 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/CustomScriptCodegenTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/codegen/CustomScriptCodegenTest.kt @@ -20,9 +20,6 @@ import org.jetbrains.kotlin.utils.PathUtil.KOTLIN_SCRIPTING_MISC_JAR import java.io.File import kotlin.reflect.KClass import kotlin.script.experimental.annotations.KotlinScript -import kotlin.script.experimental.annotations.KotlinScriptDefaultCompilationConfiguration -import kotlin.script.experimental.api.ScriptCompileConfigurationProperties -import kotlin.script.experimental.util.TypedKey class CustomScriptCodegenTest : CodegenTestCase() { @@ -54,7 +51,7 @@ class CustomScriptCodegenTest : CodegenTestCase() { arrayOf( KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR, KOTLIN_SCRIPTING_COMMON_JAR, KOTLIN_SCRIPTING_JVM_JAR, KOTLIN_SCRIPTING_MISC_JAR - ).mapNotNull { File(libPath, it).takeIf { it.exists() } } + ).mapNotNull { File(libPath, it).also { assertTrue("$it not found", it.exists()) } } } val configuration = createConfiguration( diff --git a/plugins/scripting/scripting-cli/src/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptiDefinitionsFromClasspathDiscoverySource.kt b/plugins/scripting/scripting-cli/src/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptiDefinitionsFromClasspathDiscoverySource.kt index d5c77ae6169..f504cbfc9cd 100644 --- a/plugins/scripting/scripting-cli/src/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptiDefinitionsFromClasspathDiscoverySource.kt +++ b/plugins/scripting/scripting-cli/src/org/jetbrains/kotlin/scripting/compiler/plugin/ScriptiDefinitionsFromClasspathDiscoverySource.kt @@ -65,7 +65,7 @@ internal fun discoverScriptTemplatesInClasspath( if (it.isDirectory || !it.name.startsWith(SCRIPT_DEFINITION_MARKERS_PATH)) null else it.name.removePrefix(SCRIPT_DEFINITION_MARKERS_PATH) }.toList() - val (loadedDefinitions, _, notFoundClasses) = + val (loadedDefinitions, notFoundClasses) = definitionNames.partitionLoadJarDefinitions(jar, loader, scriptResolverEnv, messageCollector) if (notFoundClasses.isNotEmpty()) { messageCollector.report( @@ -83,7 +83,7 @@ internal fun discoverScriptTemplatesInClasspath( defferedDirDependencies.add(dep) // there is no way to know that the dependency is fully "used" so we add it to the list anyway val discoveryDir = File(dep, SCRIPT_DEFINITION_MARKERS_PATH) if (discoveryDir.isDirectory) { - val (foundDefinitionClasses, _, notFoundDefinitions) = discoveryDir.listFiles().map { it.name } + val (foundDefinitionClasses, notFoundDefinitions) = discoveryDir.listFiles().map { it.name } .partitionLoadDirDefinitions(dep, loader, scriptResolverEnv, messageCollector) foundDefinitionClasses.forEach { yield(it) @@ -97,7 +97,9 @@ internal fun discoverScriptTemplatesInClasspath( } } } catch (e: IOException) { - messageCollector.report(CompilerMessageSeverity.WARNING, "Configure scripting: unable to process classpath entry $dep: $e") + messageCollector.report( + CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e" + ) } } var remainingDefinitionCandidates: List = defferedDefinitionCandidates @@ -111,7 +113,9 @@ internal fun discoverScriptTemplatesInClasspath( } remainingDefinitionCandidates = notFoundDefinitions } catch (e: IOException) { - messageCollector.report(CompilerMessageSeverity.WARNING, "Configure scripting: unable to process classpath entry $dep: $e") + messageCollector.report( + CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e" + ) } } if (remainingDefinitionCandidates.isNotEmpty()) { @@ -147,7 +151,7 @@ internal fun loadScriptTemplatesFromClasspath( if (remainingTemplates.isEmpty()) break try { - val (loadedDefinitions, _, notFoundTemplates) = when { + val (loadedDefinitions, notFoundTemplates) = when { dep.isFile && dep.extension == "jar" -> { // checking for extension is the compiler current behaviour, so the same logic is implemented here JarFile(dep).use { jar -> remainingTemplates.partitionLoadJarDefinitions(jar, classpathAndLoader, scriptResolverEnv, messageCollector) @@ -159,7 +163,7 @@ internal fun loadScriptTemplatesFromClasspath( else -> { // assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here messageCollector.report(CompilerMessageSeverity.LOGGING, "Configure scripting: Unknown classpath entry $dep") - DefinitionsLoadPartitionResult(listOf(), listOf(), remainingTemplates) + DefinitionsLoadPartitionResult(listOf(), remainingTemplates) } } if (loadedDefinitions.isNotEmpty()) { @@ -170,7 +174,7 @@ internal fun loadScriptTemplatesFromClasspath( } } catch (e: IOException) { messageCollector.report( - CompilerMessageSeverity.WARNING, + CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e" ) } @@ -186,7 +190,6 @@ internal fun loadScriptTemplatesFromClasspath( private data class DefinitionsLoadPartitionResult( val loaded: List, - val notLoaded: List, val notFound: List ) @@ -197,7 +200,6 @@ private inline fun List.partitionLoadDefinitions( getBytes: (String) -> ByteArray? ): DefinitionsLoadPartitionResult { val loaded = ArrayList() - val notLoaded = ArrayList() val notFound = ArrayList() for (definitionName in this) { val classBytes = getBytes(definitionName) @@ -206,11 +208,11 @@ private inline fun List.partitionLoadDefinitions( } when { definition != null -> loaded.add(definition) - classBytes != null -> notLoaded.add(definitionName) + classBytes != null -> {} else -> notFound.add(definitionName) } } - return DefinitionsLoadPartitionResult(loaded, notLoaded, notFound) + return DefinitionsLoadPartitionResult(loaded, notFound) } private fun List.partitionLoadJarDefinitions( @@ -302,8 +304,8 @@ private fun loadScriptDefinition( } private class LazyClasspathWithClassLoader(baseClassLoader: ClassLoader, getClasspath: () -> List) { - val classpath by lazy(LazyThreadSafetyMode.PUBLICATION) { getClasspath() } - val classLoader by lazy(LazyThreadSafetyMode.PUBLICATION) { + val classpath by lazy { getClasspath() } + val classLoader by lazy { URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseClassLoader) } } diff --git a/plugins/scripting/scripting-cli/testData/lazyDefinitions/definitions/TestScriptWithOtherAnnotation.kt b/plugins/scripting/scripting-cli/testData/lazyDefinitions/definitions/TestScriptWithOtherAnnotation.kt index 85b824bab98..69431823d08 100644 --- a/plugins/scripting/scripting-cli/testData/lazyDefinitions/definitions/TestScriptWithOtherAnnotation.kt +++ b/plugins/scripting/scripting-cli/testData/lazyDefinitions/definitions/TestScriptWithOtherAnnotation.kt @@ -1,5 +1,5 @@ import kotlin.script.templates.* -@ScriptTemplateAdditionalCompilerArguments(["-v"]) +@ScriptTemplateAdditionalCompilerArguments(["-version"]) abstract class TestScriptWithOtherAnnotation