Refactor scripting - get rid of unused type parameter in CompiledScript
This commit is contained in:
@@ -317,13 +317,13 @@ interface ScriptCompiler {
|
|||||||
suspend operator fun invoke(
|
suspend operator fun invoke(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>>
|
): ResultWithDiagnostics<CompiledScript>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The interface to the compiled script
|
* The interface to the compiled script
|
||||||
*/
|
*/
|
||||||
interface CompiledScript<out ScriptBase : Any> {
|
interface CompiledScript {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The location identifier for the script source, taken from SourceCode.locationId
|
* The location identifier for the script source, taken from SourceCode.locationId
|
||||||
@@ -346,7 +346,7 @@ interface CompiledScript<out ScriptBase : Any> {
|
|||||||
/**
|
/**
|
||||||
* The scripts compiled along with this one in one module, imported or otherwise included into compilation
|
* The scripts compiled along with this one in one module, imported or otherwise included into compilation
|
||||||
*/
|
*/
|
||||||
val otherScripts: List<CompiledScript<*>>
|
val otherScripts: List<CompiledScript>
|
||||||
get() = emptyList()
|
get() = emptyList()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ val ScriptEvaluationContextDataKeys.commandLineArgs by PropertiesCollection.key<
|
|||||||
* The facade to the script data for evaluation configuration refinement callbacks
|
* The facade to the script data for evaluation configuration refinement callbacks
|
||||||
*/
|
*/
|
||||||
data class ScriptEvaluationConfigurationRefinementContext(
|
data class ScriptEvaluationConfigurationRefinementContext(
|
||||||
val compiledScript: CompiledScript<*>,
|
val compiledScript: CompiledScript,
|
||||||
val evaluationConfiguration: ScriptEvaluationConfiguration,
|
val evaluationConfiguration: ScriptEvaluationConfiguration,
|
||||||
val contextData: ScriptEvaluationContextData? = null
|
val contextData: ScriptEvaluationContextData? = null
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ data class RefineEvaluationConfigurationData(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun ScriptEvaluationConfiguration.refineBeforeEvaluation(
|
fun ScriptEvaluationConfiguration.refineBeforeEvaluation(
|
||||||
script: CompiledScript<*>,
|
script: CompiledScript,
|
||||||
contextData: ScriptEvaluationContextData? = null
|
contextData: ScriptEvaluationContextData? = null
|
||||||
): ResultWithDiagnostics<ScriptEvaluationConfiguration> {
|
): ResultWithDiagnostics<ScriptEvaluationConfiguration> {
|
||||||
val hostConfiguration = get(ScriptEvaluationConfiguration.hostConfiguration)
|
val hostConfiguration = get(ScriptEvaluationConfiguration.hostConfiguration)
|
||||||
@@ -191,7 +191,7 @@ interface ScriptEvaluator {
|
|||||||
* @param scriptEvaluationConfiguration evaluation configuration
|
* @param scriptEvaluationConfiguration evaluation configuration
|
||||||
*/
|
*/
|
||||||
suspend operator fun invoke(
|
suspend operator fun invoke(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
scriptEvaluationConfiguration: ScriptEvaluationConfiguration = ScriptEvaluationConfiguration.Default
|
scriptEvaluationConfiguration: ScriptEvaluationConfiguration = ScriptEvaluationConfiguration.Default
|
||||||
): ResultWithDiagnostics<EvaluationResult>
|
): ResultWithDiagnostics<EvaluationResult>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class JsEvaluationState(lock: ReentrantReadWriteLock, val engine: ScriptEngine)
|
|||||||
class JsCompiledScript(
|
class JsCompiledScript(
|
||||||
val jsCode: String,
|
val jsCode: String,
|
||||||
override val compilationConfiguration: ScriptCompilationConfiguration
|
override val compilationConfiguration: ScriptCompilationConfiguration
|
||||||
) : CompiledScript<Any> {
|
) : CompiledScript {
|
||||||
override suspend fun getClass(scriptEvaluationConfiguration: ScriptEvaluationConfiguration?): ResultWithDiagnostics<KClass<*>> {
|
override suspend fun getClass(scriptEvaluationConfiguration: ScriptEvaluationConfiguration?): ResultWithDiagnostics<KClass<*>> {
|
||||||
throw IllegalStateException("Class is not available for JS implementation")
|
throw IllegalStateException("Class is not available for JS implementation")
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ class JsScriptEvaluator : ScriptEvaluator {
|
|||||||
private val engine = ScriptEngineNashorn()
|
private val engine = ScriptEngineNashorn()
|
||||||
|
|
||||||
override suspend fun invoke(
|
override suspend fun invoke(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
||||||
): ResultWithDiagnostics<EvaluationResult> {
|
): ResultWithDiagnostics<EvaluationResult> {
|
||||||
return try {
|
return try {
|
||||||
|
|||||||
+1
-1
@@ -377,7 +377,7 @@ class ScriptingHostTest : TestCase() {
|
|||||||
val compiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration)
|
val compiler = JvmScriptCompiler(defaultJvmScriptingHostConfiguration)
|
||||||
val compiledScript = runBlocking {
|
val compiledScript = runBlocking {
|
||||||
val res = compiler(script.toScriptSource(), scriptCompilationConfiguration).throwOnFailure()
|
val res = compiler(script.toScriptSource(), scriptCompilationConfiguration).throwOnFailure()
|
||||||
(res as ResultWithDiagnostics.Success<CompiledScript<*>>).value
|
(res as ResultWithDiagnostics.Success<CompiledScript>).value
|
||||||
}
|
}
|
||||||
val compiledScriptClass = runBlocking { compiledScript.getClass(null).throwOnFailure().valueOrNull()!! as KClass<*> }
|
val compiledScriptClass = runBlocking { compiledScript.getClass(null).throwOnFailure().valueOrNull()!! as KClass<*> }
|
||||||
val classLoader = compiledScriptClass.java.classLoader
|
val classLoader = compiledScriptClass.java.classLoader
|
||||||
|
|||||||
+3
-3
@@ -22,7 +22,7 @@ import kotlin.script.experimental.jvm.jvm
|
|||||||
open class CompiledScriptJarsCache(val scriptToFile: (SourceCode, ScriptCompilationConfiguration) -> File?) :
|
open class CompiledScriptJarsCache(val scriptToFile: (SourceCode, ScriptCompilationConfiguration) -> File?) :
|
||||||
CompiledJvmScriptsCache {
|
CompiledJvmScriptsCache {
|
||||||
|
|
||||||
override fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>? {
|
override fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript? {
|
||||||
val file = scriptToFile(script, scriptCompilationConfiguration)
|
val file = scriptToFile(script, scriptCompilationConfiguration)
|
||||||
?: throw IllegalArgumentException("Unable to find a mapping to a file for the script $script")
|
?: throw IllegalArgumentException("Unable to find a mapping to a file for the script $script")
|
||||||
|
|
||||||
@@ -42,14 +42,14 @@ open class CompiledScriptJarsCache(val scriptToFile: (SourceCode, ScriptCompilat
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun store(
|
override fun store(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
) {
|
) {
|
||||||
val file = scriptToFile(script, scriptCompilationConfiguration)
|
val file = scriptToFile(script, scriptCompilationConfiguration)
|
||||||
?: throw IllegalArgumentException("Unable to find a mapping to a file for the script $script")
|
?: throw IllegalArgumentException("Unable to find a mapping to a file for the script $script")
|
||||||
|
|
||||||
val jvmScript = (compiledScript as? KJvmCompiledScript<*>)
|
val jvmScript = (compiledScript as? KJvmCompiledScript)
|
||||||
?: throw IllegalArgumentException("Unsupported script type ${compiledScript::class.java.name}")
|
?: throw IllegalArgumentException("Unsupported script type ${compiledScript::class.java.name}")
|
||||||
|
|
||||||
jvmScript.saveToJar(file)
|
jvmScript.saveToJar(file)
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ open class JvmScriptCompiler(
|
|||||||
override suspend operator fun invoke(
|
override suspend operator fun invoke(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> =
|
): ResultWithDiagnostics<CompiledScript> =
|
||||||
compilerProxy.compile(
|
compilerProxy.compile(
|
||||||
script,
|
script,
|
||||||
scriptCompilationConfiguration.with {
|
scriptCompilationConfiguration.with {
|
||||||
|
|||||||
+5
-5
@@ -22,11 +22,11 @@ import kotlin.script.experimental.jvm.util.scriptCompilationClasspathFromContext
|
|||||||
open class BasicJvmScriptClassFilesGenerator(val outputDir: File) : ScriptEvaluator {
|
open class BasicJvmScriptClassFilesGenerator(val outputDir: File) : ScriptEvaluator {
|
||||||
|
|
||||||
override suspend operator fun invoke(
|
override suspend operator fun invoke(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
||||||
): ResultWithDiagnostics<EvaluationResult> {
|
): ResultWithDiagnostics<EvaluationResult> {
|
||||||
try {
|
try {
|
||||||
if (compiledScript !is KJvmCompiledScript<*>)
|
if (compiledScript !is KJvmCompiledScript)
|
||||||
return failure("Cannot generate classes: unsupported compiled script type $compiledScript")
|
return failure("Cannot generate classes: unsupported compiled script type $compiledScript")
|
||||||
val module = (compiledScript.compiledModule as? KJvmCompiledModuleInMemory)
|
val module = (compiledScript.compiledModule as? KJvmCompiledModuleInMemory)
|
||||||
?: return failure("Cannot generate classes: unsupported module type ${compiledScript.compiledModule}")
|
?: return failure("Cannot generate classes: unsupported module type ${compiledScript.compiledModule}")
|
||||||
@@ -47,7 +47,7 @@ open class BasicJvmScriptClassFilesGenerator(val outputDir: File) : ScriptEvalua
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KJvmCompiledScript<*>.saveToJar(outputJar: File) {
|
fun KJvmCompiledScript.saveToJar(outputJar: File) {
|
||||||
val module = (compiledModule as? KJvmCompiledModuleInMemory)
|
val module = (compiledModule as? KJvmCompiledModuleInMemory)
|
||||||
?: throw IllegalArgumentException("Unsupported module type $compiledModule")
|
?: throw IllegalArgumentException("Unsupported module type $compiledModule")
|
||||||
val dependenciesFromScript = compilationConfiguration[ScriptCompilationConfiguration.dependencies]
|
val dependenciesFromScript = compilationConfiguration[ScriptCompilationConfiguration.dependencies]
|
||||||
@@ -90,11 +90,11 @@ fun KJvmCompiledScript<*>.saveToJar(outputJar: File) {
|
|||||||
open class BasicJvmScriptJarGenerator(val outputJar: File) : ScriptEvaluator {
|
open class BasicJvmScriptJarGenerator(val outputJar: File) : ScriptEvaluator {
|
||||||
|
|
||||||
override suspend operator fun invoke(
|
override suspend operator fun invoke(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
||||||
): ResultWithDiagnostics<EvaluationResult> {
|
): ResultWithDiagnostics<EvaluationResult> {
|
||||||
try {
|
try {
|
||||||
if (compiledScript !is KJvmCompiledScript<*>)
|
if (compiledScript !is KJvmCompiledScript)
|
||||||
return failure("Cannot generate jar: unsupported compiled script type $compiledScript")
|
return failure("Cannot generate jar: unsupported compiled script type $compiledScript")
|
||||||
compiledScript.saveToJar(outputJar)
|
compiledScript.saveToJar(outputJar)
|
||||||
return ResultWithDiagnostics.Success(EvaluationResult(ResultValue.NotEvaluated, scriptEvaluationConfiguration))
|
return ResultWithDiagnostics.Success(EvaluationResult(ResultValue.NotEvaluated, scriptEvaluationConfiguration))
|
||||||
|
|||||||
+1
-1
@@ -35,7 +35,7 @@ class JvmReplEvaluator(
|
|||||||
): ReplEvalResult = state.lock.write {
|
): ReplEvalResult = state.lock.write {
|
||||||
val evalState = state.asState(JvmReplEvaluatorState::class.java)
|
val evalState = state.asState(JvmReplEvaluatorState::class.java)
|
||||||
val history = evalState.history as ReplStageHistoryWithReplace
|
val history = evalState.history as ReplStageHistoryWithReplace
|
||||||
val compiledScript = (compileResult.data as? KJvmCompiledScript<*>)
|
val compiledScript = (compileResult.data as? KJvmCompiledScript)
|
||||||
?: return ReplEvalResult.Error.CompileTime("Unable to access compiled script: ${compileResult.data}")
|
?: return ReplEvalResult.Error.CompileTime("Unable to access compiled script: ${compileResult.data}")
|
||||||
|
|
||||||
val lastSnippetClass = history.peek()?.item?.first
|
val lastSnippetClass = history.peek()?.item?.first
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ import kotlin.script.experimental.jvm.impl.getConfigurationWithClassloader
|
|||||||
open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||||
|
|
||||||
override suspend operator fun invoke(
|
override suspend operator fun invoke(
|
||||||
compiledScript: CompiledScript<*>,
|
compiledScript: CompiledScript,
|
||||||
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
scriptEvaluationConfiguration: ScriptEvaluationConfiguration
|
||||||
): ResultWithDiagnostics<EvaluationResult> = try {
|
): ResultWithDiagnostics<EvaluationResult> = try {
|
||||||
val configuration = getConfigurationWithClassloader(compiledScript, scriptEvaluationConfiguration)
|
val configuration = getConfigurationWithClassloader(compiledScript, scriptEvaluationConfiguration)
|
||||||
|
|||||||
+16
-16
@@ -18,7 +18,7 @@ internal class KJvmCompiledScriptData(
|
|||||||
var compilationConfiguration: ScriptCompilationConfiguration,
|
var compilationConfiguration: ScriptCompilationConfiguration,
|
||||||
var scriptClassFQName: String,
|
var scriptClassFQName: String,
|
||||||
var resultField: Pair<String, KotlinType>?,
|
var resultField: Pair<String, KotlinType>?,
|
||||||
var otherScripts: List<CompiledScript<*>> = emptyList()
|
var otherScripts: List<CompiledScript> = emptyList()
|
||||||
) : Serializable {
|
) : Serializable {
|
||||||
|
|
||||||
private fun writeObject(outputStream: ObjectOutputStream) {
|
private fun writeObject(outputStream: ObjectOutputStream) {
|
||||||
@@ -33,7 +33,7 @@ internal class KJvmCompiledScriptData(
|
|||||||
private fun readObject(inputStream: ObjectInputStream) {
|
private fun readObject(inputStream: ObjectInputStream) {
|
||||||
compilationConfiguration = inputStream.readObject() as ScriptCompilationConfiguration
|
compilationConfiguration = inputStream.readObject() as ScriptCompilationConfiguration
|
||||||
sourceLocationId = inputStream.readObject() as String?
|
sourceLocationId = inputStream.readObject() as String?
|
||||||
otherScripts = inputStream.readObject() as List<CompiledScript<*>>
|
otherScripts = inputStream.readObject() as List<CompiledScript>
|
||||||
scriptClassFQName = inputStream.readObject() as String
|
scriptClassFQName = inputStream.readObject() as String
|
||||||
resultField = inputStream.readObject() as Pair<String, KotlinType>?
|
resultField = inputStream.readObject() as Pair<String, KotlinType>?
|
||||||
}
|
}
|
||||||
@@ -44,17 +44,17 @@ internal class KJvmCompiledScriptData(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class KJvmCompiledScript<out ScriptBase : Any> internal constructor(
|
class KJvmCompiledScript internal constructor(
|
||||||
internal var data: KJvmCompiledScriptData,
|
internal var data: KJvmCompiledScriptData,
|
||||||
var compiledModule: KJvmCompiledModule? // module should be null for imported (other) scripts, so only one reference to the module is kept
|
var compiledModule: KJvmCompiledModule? // module should be null for imported (other) scripts, so only one reference to the module is kept
|
||||||
) : CompiledScript<ScriptBase>, Serializable {
|
) : CompiledScript, Serializable {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
sourceLocationId: String?,
|
sourceLocationId: String?,
|
||||||
compilationConfiguration: ScriptCompilationConfiguration,
|
compilationConfiguration: ScriptCompilationConfiguration,
|
||||||
scriptClassFQName: String,
|
scriptClassFQName: String,
|
||||||
resultField: Pair<String, KotlinType>?,
|
resultField: Pair<String, KotlinType>?,
|
||||||
otherScripts: List<CompiledScript<*>> = emptyList(),
|
otherScripts: List<CompiledScript> = emptyList(),
|
||||||
compiledModule: KJvmCompiledModule? // module should be null for imported (other) scripts, so only one reference to the module is kept
|
compiledModule: KJvmCompiledModule? // module should be null for imported (other) scripts, so only one reference to the module is kept
|
||||||
) : this(
|
) : this(
|
||||||
KJvmCompiledScriptData(sourceLocationId, compilationConfiguration, scriptClassFQName, resultField, otherScripts),
|
KJvmCompiledScriptData(sourceLocationId, compilationConfiguration, scriptClassFQName, resultField, otherScripts),
|
||||||
@@ -67,7 +67,7 @@ class KJvmCompiledScript<out ScriptBase : Any> internal constructor(
|
|||||||
override val compilationConfiguration: ScriptCompilationConfiguration
|
override val compilationConfiguration: ScriptCompilationConfiguration
|
||||||
get() = data.compilationConfiguration
|
get() = data.compilationConfiguration
|
||||||
|
|
||||||
override val otherScripts: List<CompiledScript<*>>
|
override val otherScripts: List<CompiledScript>
|
||||||
get() = data.otherScripts
|
get() = data.otherScripts
|
||||||
|
|
||||||
val scriptClassFQName: String
|
val scriptClassFQName: String
|
||||||
@@ -111,7 +111,7 @@ class KJvmCompiledScript<out ScriptBase : Any> internal constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun KJvmCompiledScript<*>.getOrCreateActualClassloader(evaluationConfiguration: ScriptEvaluationConfiguration): ClassLoader =
|
fun KJvmCompiledScript.getOrCreateActualClassloader(evaluationConfiguration: ScriptEvaluationConfiguration): ClassLoader =
|
||||||
evaluationConfiguration[ScriptEvaluationConfiguration.jvm.actualClassLoader] ?: run {
|
evaluationConfiguration[ScriptEvaluationConfiguration.jvm.actualClassLoader] ?: run {
|
||||||
val module = compiledModule
|
val module = compiledModule
|
||||||
?: throw IllegalStateException("Illegal call sequence, actualClassloader should be set before calling function on the class without module")
|
?: throw IllegalStateException("Illegal call sequence, actualClassloader should be set before calling function on the class without module")
|
||||||
@@ -123,12 +123,12 @@ fun KJvmCompiledScript<*>.getOrCreateActualClassloader(evaluationConfiguration:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun getConfigurationWithClassloader(
|
fun getConfigurationWithClassloader(
|
||||||
script: CompiledScript<*>, baseConfiguration: ScriptEvaluationConfiguration
|
script: CompiledScript, baseConfiguration: ScriptEvaluationConfiguration
|
||||||
): ScriptEvaluationConfiguration =
|
): ScriptEvaluationConfiguration =
|
||||||
if (baseConfiguration.containsKey(ScriptEvaluationConfiguration.jvm.actualClassLoader))
|
if (baseConfiguration.containsKey(ScriptEvaluationConfiguration.jvm.actualClassLoader))
|
||||||
baseConfiguration
|
baseConfiguration
|
||||||
else {
|
else {
|
||||||
val jvmScript = (script as? KJvmCompiledScript<*>)
|
val jvmScript = (script as? KJvmCompiledScript)
|
||||||
?: throw IllegalArgumentException("Unexpected compiled script type: $script")
|
?: throw IllegalArgumentException("Unexpected compiled script type: $script")
|
||||||
|
|
||||||
val classloader = jvmScript.getOrCreateActualClassloader(baseConfiguration)
|
val classloader = jvmScript.getOrCreateActualClassloader(baseConfiguration)
|
||||||
@@ -141,9 +141,9 @@ fun getConfigurationWithClassloader(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun CompiledScript<*>.makeClassLoaderFromDependencies(baseClassLoader: ClassLoader?): ClassLoader? {
|
private fun CompiledScript.makeClassLoaderFromDependencies(baseClassLoader: ClassLoader?): ClassLoader? {
|
||||||
val processedScripts = mutableSetOf<CompiledScript<*>>()
|
val processedScripts = mutableSetOf<CompiledScript>()
|
||||||
fun recursiveScriptsSeq(res: Sequence<CompiledScript<*>>, script: CompiledScript<*>): Sequence<CompiledScript<*>> =
|
fun recursiveScriptsSeq(res: Sequence<CompiledScript>, script: CompiledScript): Sequence<CompiledScript> =
|
||||||
if (processedScripts.add(script)) script.otherScripts.asSequence().fold(res + script, ::recursiveScriptsSeq)
|
if (processedScripts.add(script)) script.otherScripts.asSequence().fold(res + script, ::recursiveScriptsSeq)
|
||||||
else res
|
else res
|
||||||
|
|
||||||
@@ -188,9 +188,9 @@ const val KOTLIN_SCRIPT_METADATA_EXTENSION_WITH_DOT = ".kotlin_script"
|
|||||||
fun scriptMetadataPath(scriptClassFQName: String) =
|
fun scriptMetadataPath(scriptClassFQName: String) =
|
||||||
"$KOTLIN_SCRIPT_METADATA_PATH/$scriptClassFQName$KOTLIN_SCRIPT_METADATA_EXTENSION_WITH_DOT"
|
"$KOTLIN_SCRIPT_METADATA_PATH/$scriptClassFQName$KOTLIN_SCRIPT_METADATA_EXTENSION_WITH_DOT"
|
||||||
|
|
||||||
fun <T : Any> KJvmCompiledScript<T>.copyWithoutModule(): KJvmCompiledScript<T> = KJvmCompiledScript(data, null)
|
fun KJvmCompiledScript.copyWithoutModule(): KJvmCompiledScript = KJvmCompiledScript(data, null)
|
||||||
|
|
||||||
fun KJvmCompiledScript<*>.toBytes(): ByteArray {
|
fun KJvmCompiledScript.toBytes(): ByteArray {
|
||||||
val bos = ByteArrayOutputStream()
|
val bos = ByteArrayOutputStream()
|
||||||
var oos: ObjectOutputStream? = null
|
var oos: ObjectOutputStream? = null
|
||||||
try {
|
try {
|
||||||
@@ -206,11 +206,11 @@ fun KJvmCompiledScript<*>.toBytes(): ByteArray {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createScriptFromClassLoader(scriptClassFQName: String, classLoader: ClassLoader): KJvmCompiledScript<*> {
|
fun createScriptFromClassLoader(scriptClassFQName: String, classLoader: ClassLoader): KJvmCompiledScript {
|
||||||
val scriptDataStream = classLoader.getResourceAsStream(scriptMetadataPath(scriptClassFQName))
|
val scriptDataStream = classLoader.getResourceAsStream(scriptMetadataPath(scriptClassFQName))
|
||||||
?: throw IllegalArgumentException("Cannot find metadata for script $scriptClassFQName")
|
?: throw IllegalArgumentException("Cannot find metadata for script $scriptClassFQName")
|
||||||
val script = ObjectInputStream(scriptDataStream).use {
|
val script = ObjectInputStream(scriptDataStream).use {
|
||||||
it.readObject() as KJvmCompiledScript<*>
|
it.readObject() as KJvmCompiledScript
|
||||||
}
|
}
|
||||||
script.compiledModule = KJvmCompiledModuleFromClassLoader(classLoader)
|
script.compiledModule = KJvmCompiledModuleFromClassLoader(classLoader)
|
||||||
return script
|
return script
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ import kotlin.script.experimental.api.SourceCode
|
|||||||
import kotlin.script.experimental.util.PropertiesCollection
|
import kotlin.script.experimental.util.PropertiesCollection
|
||||||
|
|
||||||
interface CompiledJvmScriptsCache {
|
interface CompiledJvmScriptsCache {
|
||||||
fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript<*>?
|
fun get(script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration): CompiledScript?
|
||||||
fun store(compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
|
fun store(compiledScript: CompiledScript, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration)
|
||||||
|
|
||||||
object NoCache : CompiledJvmScriptsCache {
|
object NoCache : CompiledJvmScriptsCache {
|
||||||
override fun get(
|
override fun get(
|
||||||
script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): CompiledScript<*>? = null
|
): CompiledScript? = null
|
||||||
|
|
||||||
override fun store(
|
override fun store(
|
||||||
compiledScript: CompiledScript<*>, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
compiledScript: CompiledScript, script: SourceCode, scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -14,5 +14,5 @@ interface ScriptCompilerProxy {
|
|||||||
fun compile(
|
fun compile(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>>
|
): ResultWithDiagnostics<CompiledScript>
|
||||||
}
|
}
|
||||||
+1
-1
@@ -69,7 +69,7 @@ class KJvmReplCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) :
|
|||||||
snippetId: ReplSnippetId,
|
snippetId: ReplSnippetId,
|
||||||
// TODO: replace history with some interface based on CompiledScript
|
// TODO: replace history with some interface based on CompiledScript
|
||||||
history: IReplStageHistory<ScriptDescriptor>
|
history: IReplStageHistory<ScriptDescriptor>
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> =
|
): ResultWithDiagnostics<CompiledScript> =
|
||||||
withMessageCollector(snippet) { messageCollector ->
|
withMessageCollector(snippet) { messageCollector ->
|
||||||
|
|
||||||
val context = (compilationState as? ReplCompilationState)?.context
|
val context = (compilationState as? ReplCompilationState)?.context
|
||||||
|
|||||||
+1
-1
@@ -31,7 +31,7 @@ class JsScriptCompilerWithDependenciesProxy(private val environment: KotlinCoreE
|
|||||||
override fun compile(
|
override fun compile(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
): ResultWithDiagnostics<CompiledScript> {
|
||||||
val parentMessageCollector = environment.configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]
|
val parentMessageCollector = environment.configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]
|
||||||
return withMessageCollector(script = script, parentMessageCollector = parentMessageCollector) { messageCollector ->
|
return withMessageCollector(script = script, parentMessageCollector = parentMessageCollector) { messageCollector ->
|
||||||
environment.configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
environment.configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
||||||
|
|||||||
+6
-6
@@ -37,7 +37,7 @@ class ScriptJvmCompilerIsolated(val hostConfiguration: ScriptingHostConfiguratio
|
|||||||
override fun compile(
|
override fun compile(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> =
|
): ResultWithDiagnostics<CompiledScript> =
|
||||||
withMessageCollectorAndDisposable(script = script) { messageCollector, disposable ->
|
withMessageCollectorAndDisposable(script = script) { messageCollector, disposable ->
|
||||||
withScriptCompilationCache(script, scriptCompilationConfiguration, messageCollector) {
|
withScriptCompilationCache(script, scriptCompilationConfiguration, messageCollector) {
|
||||||
val initialConfiguration = scriptCompilationConfiguration.refineBeforeParsing(script).valueOr {
|
val initialConfiguration = scriptCompilationConfiguration.refineBeforeParsing(script).valueOr {
|
||||||
@@ -58,7 +58,7 @@ class ScriptJvmCompilerFromEnvironment(val environment: KotlinCoreEnvironment) :
|
|||||||
override fun compile(
|
override fun compile(
|
||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
scriptCompilationConfiguration: ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
): ResultWithDiagnostics<CompiledScript> {
|
||||||
val parentMessageCollector = environment.configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]
|
val parentMessageCollector = environment.configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]
|
||||||
return withMessageCollector(script = script, parentMessageCollector = parentMessageCollector) { messageCollector ->
|
return withMessageCollector(script = script, parentMessageCollector = parentMessageCollector) { messageCollector ->
|
||||||
withScriptCompilationCache(script, scriptCompilationConfiguration, messageCollector) {
|
withScriptCompilationCache(script, scriptCompilationConfiguration, messageCollector) {
|
||||||
@@ -86,8 +86,8 @@ private fun withScriptCompilationCache(
|
|||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
scriptCompilationConfiguration: ScriptCompilationConfiguration,
|
scriptCompilationConfiguration: ScriptCompilationConfiguration,
|
||||||
messageCollector: ScriptDiagnosticsMessageCollector,
|
messageCollector: ScriptDiagnosticsMessageCollector,
|
||||||
body: () -> ResultWithDiagnostics<CompiledScript<*>>
|
body: () -> ResultWithDiagnostics<CompiledScript>
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
): ResultWithDiagnostics<CompiledScript> {
|
||||||
val cache = scriptCompilationConfiguration[ScriptCompilationConfiguration.hostConfiguration]?.get(ScriptingHostConfiguration.jvm.compilationCache)
|
val cache = scriptCompilationConfiguration[ScriptCompilationConfiguration.hostConfiguration]?.get(ScriptingHostConfiguration.jvm.compilationCache)
|
||||||
|
|
||||||
val cached = cache?.get(script, scriptCompilationConfiguration)
|
val cached = cache?.get(script, scriptCompilationConfiguration)
|
||||||
@@ -104,7 +104,7 @@ private fun compileImpl(
|
|||||||
script: SourceCode,
|
script: SourceCode,
|
||||||
context: SharedScriptCompilationContext,
|
context: SharedScriptCompilationContext,
|
||||||
messageCollector: ScriptDiagnosticsMessageCollector
|
messageCollector: ScriptDiagnosticsMessageCollector
|
||||||
): ResultWithDiagnostics<CompiledScript<*>> {
|
): ResultWithDiagnostics<CompiledScript> {
|
||||||
val mainKtFile =
|
val mainKtFile =
|
||||||
getScriptKtFile(
|
getScriptKtFile(
|
||||||
script,
|
script,
|
||||||
@@ -168,7 +168,7 @@ private fun doCompile(
|
|||||||
sourceDependencies: List<ScriptsCompilationDependencies.SourceDependencies>,
|
sourceDependencies: List<ScriptsCompilationDependencies.SourceDependencies>,
|
||||||
messageCollector: ScriptDiagnosticsMessageCollector,
|
messageCollector: ScriptDiagnosticsMessageCollector,
|
||||||
getScriptConfiguration: (KtFile) -> ScriptCompilationConfiguration
|
getScriptConfiguration: (KtFile) -> ScriptCompilationConfiguration
|
||||||
): ResultWithDiagnostics<KJvmCompiledScript<Any>> {
|
): ResultWithDiagnostics<KJvmCompiledScript> {
|
||||||
|
|
||||||
registerPackageFragmentProvidersIfNeeded(getScriptConfiguration(sourceFiles.first()), context.environment)
|
registerPackageFragmentProvidersIfNeeded(getScriptConfiguration(sourceFiles.first()), context.environment)
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -105,12 +105,12 @@ internal fun makeCompiledScript(
|
|||||||
ktFile: KtFile,
|
ktFile: KtFile,
|
||||||
sourceDependencies: List<ScriptsCompilationDependencies.SourceDependencies>,
|
sourceDependencies: List<ScriptsCompilationDependencies.SourceDependencies>,
|
||||||
getScriptConfiguration: (KtFile) -> ScriptCompilationConfiguration
|
getScriptConfiguration: (KtFile) -> ScriptCompilationConfiguration
|
||||||
): KJvmCompiledScript<Any> {
|
): KJvmCompiledScript {
|
||||||
val scriptDependenciesStack = ArrayDeque<KtScript>()
|
val scriptDependenciesStack = ArrayDeque<KtScript>()
|
||||||
val ktScript = ktFile.declarations.firstIsInstanceOrNull<KtScript>()
|
val ktScript = ktFile.declarations.firstIsInstanceOrNull<KtScript>()
|
||||||
?: throw IllegalStateException("Expecting script file: KtScript is not found in ${ktFile.name}")
|
?: throw IllegalStateException("Expecting script file: KtScript is not found in ${ktFile.name}")
|
||||||
|
|
||||||
fun makeOtherScripts(script: KtScript): List<KJvmCompiledScript<*>> {
|
fun makeOtherScripts(script: KtScript): List<KJvmCompiledScript> {
|
||||||
|
|
||||||
// TODO: ensure that it is caught earlier (as well) since it would be more economical
|
// TODO: ensure that it is caught earlier (as well) since it would be more economical
|
||||||
if (scriptDependenciesStack.contains(script))
|
if (scriptDependenciesStack.contains(script))
|
||||||
@@ -118,10 +118,10 @@ internal fun makeCompiledScript(
|
|||||||
scriptDependenciesStack.push(script)
|
scriptDependenciesStack.push(script)
|
||||||
|
|
||||||
val containingKtFile = script.containingKtFile
|
val containingKtFile = script.containingKtFile
|
||||||
val otherScripts: List<KJvmCompiledScript<*>> =
|
val otherScripts: List<KJvmCompiledScript> =
|
||||||
sourceDependencies.find { it.scriptFile == containingKtFile }?.sourceDependencies?.valueOrThrow()?.mapNotNull { sourceFile ->
|
sourceDependencies.find { it.scriptFile == containingKtFile }?.sourceDependencies?.valueOrThrow()?.mapNotNull { sourceFile ->
|
||||||
sourceFile.declarations.firstIsInstanceOrNull<KtScript>()?.let {
|
sourceFile.declarations.firstIsInstanceOrNull<KtScript>()?.let {
|
||||||
KJvmCompiledScript<Any>(
|
KJvmCompiledScript(
|
||||||
containingKtFile.virtualFile?.path,
|
containingKtFile.virtualFile?.path,
|
||||||
getScriptConfiguration(sourceFile),
|
getScriptConfiguration(sourceFile),
|
||||||
it.fqName.asString(),
|
it.fqName.asString(),
|
||||||
|
|||||||
+1
-1
@@ -30,7 +30,7 @@ interface KJvmReplCompilerProxy {
|
|||||||
snippet: SourceCode,
|
snippet: SourceCode,
|
||||||
snippetId: ReplSnippetId,
|
snippetId: ReplSnippetId,
|
||||||
history: IReplStageHistory<ScriptDescriptor>
|
history: IReplStageHistory<ScriptDescriptor>
|
||||||
): ResultWithDiagnostics<CompiledScript<*>>
|
): ResultWithDiagnostics<CompiledScript>
|
||||||
}
|
}
|
||||||
|
|
||||||
class JvmReplCompilerStageHistory(private val state: JvmReplCompilerState) :
|
class JvmReplCompilerStageHistory(private val state: JvmReplCompilerState) :
|
||||||
|
|||||||
Reference in New Issue
Block a user