Port compiler to the script-runtime with script base classes

This commit is contained in:
Ilya Chernikov
2017-03-20 17:06:22 +01:00
parent 7ceaca133a
commit 88652154c9
29 changed files with 69 additions and 132 deletions
+1
View File
@@ -16,5 +16,6 @@
<orderEntry type="library" scope="TEST" name="kotlin-test" level="project" />
<orderEntry type="module" module-name="resolution" exported="" />
<orderEntry type="library" exported="" name="kotlin-script-runtime" level="project" />
<orderEntry type="module" module-name="script.runtime" />
</component>
</module>
@@ -23,9 +23,8 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.KtScript
import java.io.File
import kotlin.comparisons.compareValues
import kotlin.reflect.KClass
import kotlin.script.dependencies.KotlinScriptExternalDependencies
import kotlin.script.templates.standard.ScriptTemplateWithArgs
open class KotlinScriptDefinition(val template: KClass<out Any>) {
@@ -47,33 +46,5 @@ open class KotlinScriptDefinition(val template: KClass<out Any>) {
open fun <TF: Any> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? = null
}
interface KotlinScriptExternalDependencies : Comparable<KotlinScriptExternalDependencies> {
val javaHome: String? get() = null
val classpath: Iterable<File> get() = emptyList()
val imports: Iterable<String> get() = emptyList()
val sources: Iterable<File> get() = emptyList()
val scripts: Iterable<File> get() = emptyList()
override fun compareTo(other: KotlinScriptExternalDependencies): Int =
compareValues(javaHome, other.javaHome)
.chainCompare { compareIterables(classpath, other.classpath) }
.chainCompare { compareIterables(imports, other.imports) }
.chainCompare { compareIterables(sources, other.sources) }
.chainCompare { compareIterables(scripts, other.scripts) }
}
object StandardScriptDefinition : KotlinScriptDefinition(ScriptTemplateWithArgs::class)
private fun<T: Comparable<T>> compareIterables(a: Iterable<T>, b: Iterable<T>): Int {
val ia = a.iterator()
val ib = b.iterator()
while (true) {
if (ia.hasNext() && !ib.hasNext()) return 1
if (!ia.hasNext() && !ib.hasNext()) return 0
if (!ia.hasNext()) return -1
val compRes = compareValues(ia.next(), ib.next())
if (compRes != 0) return compRes
}
}
private inline fun Int.chainCompare(compFn: () -> Int ): Int = if (this != 0) this else compFn()
@@ -34,6 +34,12 @@ import kotlin.reflect.KFunction
import kotlin.reflect.KParameter
import kotlin.reflect.full.memberFunctions
import kotlin.reflect.full.primaryConstructor
import kotlin.script.dependencies.BasicScriptDependenciesResolver
import kotlin.script.dependencies.KotlinScriptExternalDependencies
import kotlin.script.dependencies.ScriptContents
import kotlin.script.dependencies.ScriptDependenciesResolver
import kotlin.script.extensions.SamWithReceiverAnnotations
import kotlin.script.templates.*
open class KotlinScriptDefinitionFromAnnotatedTemplate(
template: KClass<out Any>,
@@ -178,3 +184,10 @@ internal fun logScriptDefMessage(reportSeverity: ScriptDependenciesResolver.Repo
ScriptDependenciesResolver.ReportSeverity.DEBUG -> KotlinScriptDefinitionFromAnnotatedTemplate.log.debug(msg)
}
}
internal fun sameSignature(left: KFunction<*>, right: KFunction<*>): Boolean =
left.parameters.size == right.parameters.size &&
left.parameters.zip(right.parameters).all {
it.first.kind == KParameter.Kind.INSTANCE ||
it.first.type == it.second.type
}
@@ -24,6 +24,7 @@ import java.io.File
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
import kotlin.script.dependencies.KotlinScriptExternalDependencies
class KotlinScriptExternalImportsProvider(val project: Project, private val scriptDefinitionProvider: KotlinScriptDefinitionProvider) {
@@ -1,80 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.script
import java.io.File
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass
const val DEFAULT_SCRIPT_FILE_PATTERN = ".*\\.kts"
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class ScriptTemplateDefinition(val resolver: KClass<out ScriptDependenciesResolver> = BasicScriptDependenciesResolver::class,
val scriptFilePattern: String = DEFAULT_SCRIPT_FILE_PATTERN)
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class SamWithReceiverAnnotations(vararg val annotations: String)
interface ScriptContents {
data class Position(val line: Int, val col: Int)
val file: File?
val annotations: Iterable<Annotation>
val text: CharSequence?
}
class PseudoFuture<T>(private val value: T): Future<T> {
override fun get(): T = value
override fun get(p0: Long, p1: TimeUnit): T = value
override fun cancel(p0: Boolean): Boolean = false
override fun isDone(): Boolean = true
override fun isCancelled(): Boolean = false
}
fun KotlinScriptExternalDependencies?.asFuture(): PseudoFuture<KotlinScriptExternalDependencies?> = PseudoFuture(this)
interface ScriptDependenciesResolver {
enum class ReportSeverity { ERROR, WARNING, INFO, DEBUG }
fun resolve(script: ScriptContents,
environment: Map<String, Any?>?,
report: (ReportSeverity, String, ScriptContents.Position?) -> Unit,
previousDependencies: KotlinScriptExternalDependencies?
): Future<KotlinScriptExternalDependencies?> = PseudoFuture(null)
}
@Suppress("unused") // used in gradle-script-kotlin for a moment
@Deprecated("Use new ScriptDependenciesResolver, this one is left for temporary compatibility of new resolvers to kotlin plugin 1.1-M01",
ReplaceWith("ScriptDependenciesResolver"))
interface ScriptDependenciesResolverEx {
fun resolve(script: ScriptContents,
environment: Map<String, Any?>?,
previousDependencies: KotlinScriptExternalDependencies?
): KotlinScriptExternalDependencies? = null
}
class BasicScriptDependenciesResolver : ScriptDependenciesResolver
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class AcceptedAnnotations(vararg val supportedAnnotationClasses: KClass<out Annotation>)
@@ -22,6 +22,7 @@ import com.intellij.openapi.extensions.Extensions
import com.intellij.openapi.project.Project
import java.io.File
import java.net.URLClassLoader
import kotlin.script.dependencies.ScriptDependenciesResolver
interface ScriptTemplatesProvider {