Refactor scripting - rename env vars to provided properties everywhere

This commit is contained in:
Ilya Chernikov
2018-12-06 16:31:58 +01:00
parent 9e5d04b836
commit 0b9770f8d1
11 changed files with 39 additions and 39 deletions
@@ -170,10 +170,10 @@ class ScriptCodegen private constructor(
genFieldFromParam(typeMapper.mapClass(receiver), receiversParamIndex, name) genFieldFromParam(typeMapper.mapClass(receiver), receiversParamIndex, name)
} }
scriptDescriptor.scriptEnvironmentProperties.forEachIndexed { envVarIndex, envVar -> scriptDescriptor.scriptProvidedProperties.forEachIndexed { envVarIndex, envVar ->
val fieldClassType = typeMapper.mapType(envVar) val fieldClassType = typeMapper.mapType(envVar)
val envVarParamIndex = frameMap.enterTemp(fieldClassType) val envVarParamIndex = frameMap.enterTemp(fieldClassType)
val name = scriptContext.getEnvironmentVarName(envVarIndex) val name = scriptContext.getProvidedPropertyName(envVarIndex)
genFieldFromParam(fieldClassType, envVarParamIndex, name) genFieldFromParam(fieldClassType, envVarParamIndex, name)
} }
@@ -209,12 +209,12 @@ class ScriptCodegen private constructor(
null null
) )
} }
for (envVarIndex in scriptDescriptor.scriptEnvironmentProperties.indices) { for (envVarIndex in scriptDescriptor.scriptProvidedProperties.indices) {
classBuilder.newField( classBuilder.newField(
NO_ORIGIN, NO_ORIGIN,
ACC_PUBLIC or ACC_FINAL, ACC_PUBLIC or ACC_FINAL,
scriptContext.getEnvironmentVarName(envVarIndex), scriptContext.getProvidedPropertyName(envVarIndex),
scriptContext.getEnvironmentVarType(envVarIndex).descriptor, scriptContext.getProvidedPropertyType(envVarIndex).descriptor,
null, null,
null null
) )
@@ -33,7 +33,7 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
import org.jetbrains.kotlin.resolve.jvm.AsmTypes import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ScriptEnvironmentDescriptor import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ScriptProvidedPropertiesDescriptor
import org.jetbrains.org.objectweb.asm.Type import org.jetbrains.org.objectweb.asm.Type
import kotlin.reflect.KClass import kotlin.reflect.KClass
@@ -76,7 +76,7 @@ class ScriptContext(
private val ctorImplicitReceiversParametersStart = private val ctorImplicitReceiversParametersStart =
ctorValueParametersStart + (scriptDescriptor.getSuperClassNotAny()?.unsubstitutedPrimaryConstructor?.valueParameters?.size ?: 0) ctorValueParametersStart + (scriptDescriptor.getSuperClassNotAny()?.unsubstitutedPrimaryConstructor?.valueParameters?.size ?: 0)
private val ctorEnvironmentVarsParametersStart = private val ctorProvidedPropertiesParametersStart =
ctorImplicitReceiversParametersStart + scriptDescriptor.implicitReceivers.size ctorImplicitReceiversParametersStart + scriptDescriptor.implicitReceivers.size
fun getImplicitReceiverName(index: Int): String = fun getImplicitReceiverName(index: Int): String =
@@ -88,13 +88,13 @@ class ScriptContext(
return kClass?.java?.classId?.let(AsmUtil::asmTypeByClassId) return kClass?.java?.classId?.let(AsmUtil::asmTypeByClassId)
} }
fun getEnvironmentVarName(index: Int): String = fun getProvidedPropertyName(index: Int): String =
scriptDescriptor.unsubstitutedPrimaryConstructor.valueParameters[ctorEnvironmentVarsParametersStart + index].name.identifier scriptDescriptor.unsubstitutedPrimaryConstructor.valueParameters[ctorProvidedPropertiesParametersStart + index].name.identifier
fun getEnvironmentVarType(index: Int): Type = typeMapper.mapType(scriptDescriptor.scriptEnvironmentProperties[index].type) fun getProvidedPropertyType(index: Int): Type = typeMapper.mapType(scriptDescriptor.scriptProvidedProperties[index].type)
fun getOuterReceiverExpression(prefix: StackValue?, thisOrOuterClass: ClassDescriptor): StackValue { fun getOuterReceiverExpression(prefix: StackValue?, thisOrOuterClass: ClassDescriptor): StackValue {
if (thisOrOuterClass is ScriptEnvironmentDescriptor) { if (thisOrOuterClass is ScriptProvidedPropertiesDescriptor) {
return prefix ?: StackValue.LOCAL_0 return prefix ?: StackValue.LOCAL_0
} }
receiverDescriptors.forEachIndexed { index, outerReceiver -> receiverDescriptors.forEachIndexed { index, outerReceiver ->
@@ -101,7 +101,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_BASE_CLASS = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_BASE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_STANDARD_TEMPLATE = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_STANDARD_TEMPLATE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_RECEIVER_CLASS = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_RECEIVER_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_ENVIRONMENT_PROPERTY_CLASS = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<PsiElement, String> MISSING_SCRIPT_PROVIDED_PROPERTY_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, String> PRE_RELEASE_CLASS = DiagnosticFactory1.create(ERROR); DiagnosticFactory1<PsiElement, String> PRE_RELEASE_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory2<PsiElement, String, IncompatibleVersionErrorData<?>> INCOMPATIBLE_CLASS = DiagnosticFactory2.create(ERROR); DiagnosticFactory2<PsiElement, String, IncompatibleVersionErrorData<?>> INCOMPATIBLE_CLASS = DiagnosticFactory2.create(ERROR);
@@ -365,7 +365,7 @@ public class DefaultErrorMessages {
MAP.put(MISSING_SCRIPT_BASE_CLASS, "Cannot access script base class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING); MAP.put(MISSING_SCRIPT_BASE_CLASS, "Cannot access script base class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(MISSING_SCRIPT_STANDARD_TEMPLATE, "No script runtime was found in the classpath: class ''{0}'' not found. Please add kotlin-script-runtime.jar to the module dependencies.", TO_STRING); MAP.put(MISSING_SCRIPT_STANDARD_TEMPLATE, "No script runtime was found in the classpath: class ''{0}'' not found. Please add kotlin-script-runtime.jar to the module dependencies.", TO_STRING);
MAP.put(MISSING_SCRIPT_RECEIVER_CLASS, "Cannot access implicit script receiver class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING); MAP.put(MISSING_SCRIPT_RECEIVER_CLASS, "Cannot access implicit script receiver class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(MISSING_SCRIPT_ENVIRONMENT_PROPERTY_CLASS, "Cannot access script environment property class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING); MAP.put(MISSING_SCRIPT_PROVIDED_PROPERTY_CLASS, "Cannot access script provided property class ''{0}''. Check your module classpath for missing or conflicting dependencies", TO_STRING);
MAP.put(PRE_RELEASE_CLASS, "{0} is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler", TO_STRING); MAP.put(PRE_RELEASE_CLASS, "{0} is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler", TO_STRING);
MAP.put(INCOMPATIBLE_CLASS, MAP.put(INCOMPATIBLE_CLASS,
"{0} was compiled with an incompatible version of Kotlin. {1}", "{0} was compiled with an incompatible version of Kotlin. {1}",
@@ -43,8 +43,8 @@ class LazyScriptClassMemberScope(
scriptDescriptor.implicitReceivers.mapIndexed { idx, receiver -> scriptDescriptor.implicitReceivers.mapIndexed { idx, receiver ->
"$IMPLICIT_RECEIVER_PARAM_NAME_PREFIX$idx" to receiver.defaultType "$IMPLICIT_RECEIVER_PARAM_NAME_PREFIX$idx" to receiver.defaultType
} }
val environmentVarsParamTypes = val providedPropertiesParamTypes =
scriptDescriptor.scriptEnvironmentProperties.map { scriptDescriptor.scriptProvidedProperties.map {
it.name.identifier to it.type it.name.identifier to it.type
} }
val annotations = baseConstructorDescriptor.annotations val annotations = baseConstructorDescriptor.annotations
@@ -53,7 +53,7 @@ class LazyScriptClassMemberScope(
) )
var paramsIndexBase = baseConstructorDescriptor.valueParameters.lastIndex + 1 var paramsIndexBase = baseConstructorDescriptor.valueParameters.lastIndex + 1
val syntheticParameters = val syntheticParameters =
(implicitReceiversParamTypes + environmentVarsParamTypes).map { param: Pair<String, KotlinType> -> (implicitReceiversParamTypes + providedPropertiesParamTypes).map { param: Pair<String, KotlinType> ->
ValueParameterDescriptorImpl( ValueParameterDescriptorImpl(
constructorDescriptor, constructorDescriptor,
null, null,
@@ -37,7 +37,7 @@ import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.data.KtScriptInfo import org.jetbrains.kotlin.resolve.lazy.data.KtScriptInfo
import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider import org.jetbrains.kotlin.resolve.lazy.declarations.ClassMemberDeclarationProvider
import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ReplResultPropertyDescriptor import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ReplResultPropertyDescriptor
import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ScriptEnvironmentDescriptor import org.jetbrains.kotlin.resolve.lazy.descriptors.script.ScriptProvidedPropertiesDescriptor
import org.jetbrains.kotlin.resolve.lazy.descriptors.script.classId import org.jetbrains.kotlin.resolve.lazy.descriptors.script.classId
import org.jetbrains.kotlin.resolve.scopes.LexicalScope import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl import org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl
@@ -174,19 +174,19 @@ class LazyScriptDescriptor(
override fun getImplicitReceivers(): List<ClassDescriptor> = scriptImplicitReceivers() override fun getImplicitReceivers(): List<ClassDescriptor> = scriptImplicitReceivers()
private val scriptEnvironment: () -> ScriptEnvironmentDescriptor = resolveSession.storageManager.createLazyValue { private val scriptProvidedProperties: () -> ScriptProvidedPropertiesDescriptor = resolveSession.storageManager.createLazyValue {
ScriptEnvironmentDescriptor(this) ScriptProvidedPropertiesDescriptor(this)
} }
override fun getScriptEnvironmentProperties(): List<PropertyDescriptor> = scriptEnvironment().properties() override fun getScriptProvidedProperties(): List<PropertyDescriptor> = scriptProvidedProperties().properties()
private val scriptOuterScope: () -> LexicalScope = resolveSession.storageManager.createLazyValue { private val scriptOuterScope: () -> LexicalScope = resolveSession.storageManager.createLazyValue {
var outerScope = super.getOuterScope() var outerScope = super.getOuterScope()
val outerScopeReceivers = implicitReceivers.let { val outerScopeReceivers = implicitReceivers.let {
if (scriptDefinition().environmentVariables.isEmpty()) { if (scriptDefinition().providedProperties.isEmpty()) {
it it
} else { } else {
it + ScriptEnvironmentDescriptor(this) it + ScriptProvidedPropertiesDescriptor(this)
} }
} }
for (receiverClassDescriptor in outerScopeReceivers.asReversed()) { for (receiverClassDescriptor in outerScopeReceivers.asReversed()) {
@@ -17,11 +17,11 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.Printer
class ScriptEnvironmentDescriptor(script: LazyScriptDescriptor) : class ScriptProvidedPropertiesDescriptor(script: LazyScriptDescriptor) :
MutableClassDescriptor( MutableClassDescriptor(
script, script,
ClassKind.CLASS, false, false, ClassKind.CLASS, false, false,
Name.special("<synthetic script environment for ${script.name}>"), Name.special("<synthetic script provided properties for ${script.name}>"),
SourceElement.NO_SOURCE, SourceElement.NO_SOURCE,
LockBasedStorageManager.NO_LOCKS LockBasedStorageManager.NO_LOCKS
) { ) {
@@ -33,8 +33,8 @@ class ScriptEnvironmentDescriptor(script: LazyScriptDescriptor) :
createTypeConstructor() createTypeConstructor()
} }
private val memberScope: () -> ScriptEnvironmentMemberScope = script.resolveSession.storageManager.createLazyValue { private val memberScope: () -> ScriptProvidedPropertiesMemberScope = script.resolveSession.storageManager.createLazyValue {
ScriptEnvironmentMemberScope( ScriptProvidedPropertiesMemberScope(
script.name.identifier, script.name.identifier,
properties() properties()
) )
@@ -42,13 +42,13 @@ class ScriptEnvironmentDescriptor(script: LazyScriptDescriptor) :
override fun getUnsubstitutedMemberScope(): MemberScope = memberScope() override fun getUnsubstitutedMemberScope(): MemberScope = memberScope()
val properties: () -> List<ScriptEnvironmentPropertyDescriptor> = script.resolveSession.storageManager.createLazyValue { val properties: () -> List<ScriptProvidedPropertyDescriptor> = script.resolveSession.storageManager.createLazyValue {
script.scriptDefinition().environmentVariables.mapNotNull { (name, type) -> script.scriptDefinition().providedProperties.mapNotNull { (name, type) ->
script.findTypeDescriptor(type, Errors.MISSING_SCRIPT_ENVIRONMENT_PROPERTY_CLASS)?.let { script.findTypeDescriptor(type, Errors.MISSING_SCRIPT_PROVIDED_PROPERTY_CLASS)?.let {
name to it name to it
} }
}.map { (name, classDescriptor) -> }.map { (name, classDescriptor) ->
ScriptEnvironmentPropertyDescriptor( ScriptProvidedPropertyDescriptor(
Name.identifier(name), Name.identifier(name),
classDescriptor, classDescriptor,
thisAsReceiverParameter, thisAsReceiverParameter,
@@ -58,21 +58,21 @@ class ScriptEnvironmentDescriptor(script: LazyScriptDescriptor) :
} }
} }
private class ScriptEnvironmentMemberScope( private class ScriptProvidedPropertiesMemberScope(
private val scriptId: String, private val scriptId: String,
private val environmentProperties: List<PropertyDescriptor> private val providedProperties: List<PropertyDescriptor>
) : MemberScopeImpl() { ) : MemberScopeImpl() {
override fun getContributedDescriptors( override fun getContributedDescriptors(
kindFilter: DescriptorKindFilter, kindFilter: DescriptorKindFilter,
nameFilter: (Name) -> Boolean nameFilter: (Name) -> Boolean
): Collection<DeclarationDescriptor> = ): Collection<DeclarationDescriptor> =
environmentProperties providedProperties
override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> = override fun getContributedVariables(name: Name, location: LookupLocation): Collection<PropertyDescriptor> =
environmentProperties.filter { it.name == name } providedProperties.filter { it.name == name }
override fun printScopeStructure(p: Printer) { override fun printScopeStructure(p: Printer) {
p.println("Scope of script environment: $scriptId") p.println("Scope of script provided properties: $scriptId")
} }
} }
} }
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyScriptDescriptor import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyScriptDescriptor
class ScriptEnvironmentPropertyDescriptor( class ScriptProvidedPropertyDescriptor(
name: Name, name: Name,
typeDescriptor: ClassDescriptor, typeDescriptor: ClassDescriptor,
receiver: ReceiverParameterDescriptor?, receiver: ReceiverParameterDescriptor?,
@@ -60,7 +60,7 @@ open class KotlinScriptDefinition(open val template: KClass<out Any>) : UserData
open val implicitReceivers: List<KType> get() = emptyList() open val implicitReceivers: List<KType> get() = emptyList()
open val environmentVariables: List<Pair<String, KType>> get() = emptyList() open val providedProperties: List<Pair<String, KType>> get() = emptyList()
open val targetClassAnnotations: List<Annotation> get() = emptyList() open val targetClassAnnotations: List<Annotation> get() = emptyList()
@@ -31,5 +31,5 @@ public interface ScriptDescriptor extends ClassDescriptor {
List<ClassDescriptor> getImplicitReceivers(); List<ClassDescriptor> getImplicitReceivers();
@NotNull @NotNull
List<PropertyDescriptor> getScriptEnvironmentProperties(); List<PropertyDescriptor> getScriptProvidedProperties();
} }
@@ -67,7 +67,7 @@ abstract class KotlinScriptDefinitionAdapterFromNewAPIBase : KotlinScriptDefinit
.map { getScriptingClass(it).starProjectedType } .map { getScriptingClass(it).starProjectedType }
} }
override val environmentVariables: List<Pair<String, KType>> by lazy(LazyThreadSafetyMode.PUBLICATION) { override val providedProperties: List<Pair<String, KType>> by lazy(LazyThreadSafetyMode.PUBLICATION) {
scriptCompilationConfiguration[ScriptCompilationConfiguration.providedProperties] scriptCompilationConfiguration[ScriptCompilationConfiguration.providedProperties]
?.map { (k, v) -> k to getScriptingClass(v).starProjectedType }.orEmpty() ?.map { (k, v) -> k to getScriptingClass(v).starProjectedType }.orEmpty()
} }