Sorting default jvm props and basic implementations
This commit is contained in:
+2
-3
@@ -1,11 +1,10 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package kotlin.script.experimental.jvm
|
||||
package kotlin.script.experimental.jvm.compat
|
||||
|
||||
import kotlin.script.dependencies.ScriptContents
|
||||
import kotlin.script.dependencies.ScriptDependenciesResolver
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@@ -16,8 +16,8 @@ import kotlin.script.experimental.dependencies.ScriptDependencies
|
||||
import kotlin.script.experimental.dependencies.ScriptReport
|
||||
import kotlin.script.experimental.host.toScriptSource
|
||||
import kotlin.script.experimental.jvm.JvmDependency
|
||||
import kotlin.script.experimental.jvm.mapToLegacyScriptReportPosition
|
||||
import kotlin.script.experimental.jvm.mapToLegacyScriptReportSeverity
|
||||
import kotlin.script.experimental.jvm.compat.mapToLegacyScriptReportPosition
|
||||
import kotlin.script.experimental.jvm.compat.mapToLegacyScriptReportSeverity
|
||||
|
||||
class BridgeDependenciesResolver(
|
||||
val scriptDefinition: ScriptDefinition,
|
||||
|
||||
@@ -6,10 +6,16 @@
|
||||
package kotlin.script.experimental.jvm
|
||||
|
||||
import org.jetbrains.kotlin.script.util.scriptCompilationClasspathFromContext
|
||||
import java.io.File
|
||||
import kotlin.script.experimental.api.ScriptDefinitionProperties
|
||||
import kotlin.script.experimental.api.ScriptDependency
|
||||
import kotlin.script.experimental.api.ScriptingProperties
|
||||
import kotlin.script.experimental.api.addToListProperty
|
||||
|
||||
class JvmDependency(val classpath: List<File>) : ScriptDependency {
|
||||
constructor(vararg classpathEntries: File) : this(classpathEntries.asList())
|
||||
}
|
||||
|
||||
fun ScriptingProperties.jvmDependenciesFromCurrentContext(vararg libraries: String, wholeClasspath: Boolean = false) {
|
||||
jvmDependenciesFromClassloader(*libraries, wholeClasspath = wholeClasspath)
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package kotlin.script.experimental.jvm
|
||||
|
||||
import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.util.chainPropertyBags
|
||||
|
||||
open class JvmScriptCompiler(
|
||||
val compilerProxy: KJVMCompilerProxy,
|
||||
val cache: CompiledJvmScriptsCache
|
||||
) : ScriptCompiler {
|
||||
|
||||
override suspend operator fun invoke(
|
||||
script: ScriptSource,
|
||||
scriptDefinition: ScriptDefinition,
|
||||
additionalConfiguration: ScriptCompileConfiguration?
|
||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
||||
val baseConfiguration = chainPropertyBags(additionalConfiguration, scriptDefinition)
|
||||
val refineConfigurationFn = baseConfiguration.getOrNull(ScriptDefinitionProperties.refineConfigurationHandler)
|
||||
val refinedConfiguration =
|
||||
if (baseConfiguration.getOrNull(ScriptDefinitionProperties.refineConfigurationBeforeParsing) == true) {
|
||||
if (refineConfigurationFn == null) {
|
||||
return ResultWithDiagnostics.Failure("Non-null configurator expected".asErrorDiagnostics())
|
||||
}
|
||||
refineConfigurationFn(script, baseConfiguration).let {
|
||||
when (it) {
|
||||
is ResultWithDiagnostics.Failure -> return it
|
||||
is ResultWithDiagnostics.Success -> it.value
|
||||
}
|
||||
}
|
||||
} else {
|
||||
baseConfiguration
|
||||
}
|
||||
val cached = cache.get(script, refinedConfiguration)
|
||||
|
||||
if (cached != null) return cached.asSuccess()
|
||||
|
||||
return compilerProxy.compile(script, scriptDefinition, refinedConfiguration).also {
|
||||
if (it is ResultWithDiagnostics.Success) {
|
||||
cache.store(it.value, refinedConfiguration)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface CompiledJvmScriptsCache {
|
||||
fun get(script: ScriptSource, configuration: ScriptCompileConfiguration): CompiledScript<*>?
|
||||
fun store(compiledScript: CompiledScript<*>, configuration: ScriptCompileConfiguration)
|
||||
}
|
||||
|
||||
interface KJVMCompilerProxy {
|
||||
fun compile(
|
||||
script: ScriptSource,
|
||||
scriptDefinition: ScriptDefinition,
|
||||
additionalConfiguration: ScriptCompileConfiguration
|
||||
): ResultWithDiagnostics<CompiledScript<*>>
|
||||
}
|
||||
|
||||
class DummyCompiledJvmScriptCache : CompiledJvmScriptsCache {
|
||||
override fun get(script: ScriptSource, configuration: ScriptCompileConfiguration): CompiledScript<*>? = null
|
||||
override fun store(compiledScript: CompiledScript<*>, configuration: ScriptCompileConfiguration) {}
|
||||
}
|
||||
|
||||
+8
-10
@@ -1,21 +1,19 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package kotlin.script.experimental.jvm
|
||||
|
||||
import java.io.File
|
||||
import kotlin.script.experimental.api.PropertiesGroup
|
||||
import kotlin.script.experimental.api.ScriptingProperties
|
||||
import kotlin.script.experimental.util.typedKey
|
||||
|
||||
val jvmJavaHomeParams = with(JvmScriptCompileConfigurationProperties) {
|
||||
listOf(javaHomeDir to File(System.getProperty("java.home")))
|
||||
object JvmScriptCompileConfigurationProperties : PropertiesGroup {
|
||||
val javaHomeDir by typedKey<File>(File(System.getProperty("java.home")))
|
||||
}
|
||||
|
||||
object JvmJavaHomeScriptingProperties : ScriptingProperties(
|
||||
{
|
||||
JvmScriptCompileConfigurationProperties.javaHomeDir(File(System.getProperty("java.home")))
|
||||
})
|
||||
|
||||
val jvmJavaHomeScriptingProperties = JvmJavaHomeScriptingProperties
|
||||
|
||||
val ScriptingProperties.jvmCompileConfiguration get() = JvmScriptCompileConfigurationProperties
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package kotlin.script.experimental.jvm
|
||||
|
||||
import java.io.File
|
||||
import kotlin.script.experimental.api.PropertiesGroup
|
||||
import kotlin.script.experimental.api.ScriptDependency
|
||||
import kotlin.script.experimental.api.ScriptingProperties
|
||||
import kotlin.script.experimental.util.typedKey
|
||||
|
||||
object JvmScriptCompileConfigurationProperties : PropertiesGroup {
|
||||
val javaHomeDir by typedKey<File>()
|
||||
}
|
||||
|
||||
val ScriptingProperties.jvmCompileConfiguration get() = JvmScriptCompileConfigurationProperties
|
||||
|
||||
class JvmDependency(val classpath: List<File>) : ScriptDependency {
|
||||
constructor(vararg classpathEntries: File) : this(classpathEntries.asList())
|
||||
}
|
||||
|
||||
@@ -9,6 +9,15 @@ import java.net.URLClassLoader
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.experimental.api.*
|
||||
|
||||
private object DefaultJvmScriptingEnvironmentPropertiesBuilder : ScriptingProperties() {
|
||||
init {
|
||||
ScriptingEnvironmentProperties.getScriptingClass(JvmGetScriptingClass())
|
||||
}
|
||||
}
|
||||
|
||||
val defaultJvmScriptingEnvironment = DefaultJvmScriptingEnvironmentPropertiesBuilder.build()
|
||||
|
||||
|
||||
class JvmGetScriptingClass : GetScriptingClass {
|
||||
|
||||
private var dependencies: List<ScriptDependency>? = null
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused")
|
||||
|
||||
package kotlin.script.experimental.jvm
|
||||
|
||||
import kotlin.script.experimental.api.ScriptEvaluator
|
||||
import kotlin.script.experimental.host.BasicScriptingHost
|
||||
import kotlin.script.experimental.util.typedKey
|
||||
|
||||
open class JvmBasicScriptingHost(
|
||||
compiler: JvmScriptCompiler,
|
||||
evaluator: ScriptEvaluator
|
||||
) : BasicScriptingHost(compiler, evaluator)
|
||||
|
||||
object JvmScriptEvaluationEnvironmentProperties {
|
||||
val baseClassLoader by typedKey<ClassLoader?>()
|
||||
}
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.script.experimental.jvm.runners
|
||||
|
||||
import kotlin.script.experimental.api.*
|
||||
|
||||
open class BasicJvmScriptEvaluator() : ScriptEvaluator {
|
||||
|
||||
override suspend operator fun invoke(
|
||||
compiledScript: CompiledScript<*>,
|
||||
scriptEvaluationEnvironment: ScriptEvaluationEnvironment
|
||||
): ResultWithDiagnostics<EvaluationResult> =
|
||||
try {
|
||||
val obj = compiledScript.instantiate(scriptEvaluationEnvironment)
|
||||
when (obj) {
|
||||
is ResultWithDiagnostics.Failure -> obj
|
||||
is ResultWithDiagnostics.Success -> {
|
||||
// in the future, when (if) we'll stop to compile everything into constructor
|
||||
// run as SAM
|
||||
// return res
|
||||
val scriptObject = obj.value
|
||||
if (scriptObject !is Class<*>)
|
||||
ResultWithDiagnostics.Failure(ScriptDiagnostic("expecting class in this implementation, got ${scriptObject?.javaClass}"))
|
||||
else {
|
||||
val receivers = scriptEvaluationEnvironment.getOrNull(ScriptEvaluationEnvironmentParams.implicitReceivers)
|
||||
val instance = if (receivers == null) {
|
||||
scriptObject.getConstructor().newInstance()
|
||||
} else {
|
||||
scriptObject.getConstructor(Array<Any>::class.java).newInstance(receivers.toTypedArray())
|
||||
}
|
||||
|
||||
// TODO: fix result value
|
||||
ResultWithDiagnostics.Success(EvaluationResult(ResultValue.Value("", instance, ""), scriptEvaluationEnvironment))
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
ResultWithDiagnostics.Failure(e.asDiagnostics())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user