Introduce script names and location ids, use them in diagnostics and...
for virtual file names. Also fix compiled script serialization.
This commit is contained in:
@@ -17,6 +17,7 @@ package kotlin.script.experimental.api
|
||||
data class ScriptDiagnostic(
|
||||
val message: String,
|
||||
val severity: Severity = Severity.ERROR,
|
||||
val sourcePath: String? = null,
|
||||
val location: SourceCode.Location? = null,
|
||||
val exception: Throwable? = null
|
||||
) {
|
||||
@@ -92,21 +93,25 @@ fun <R> R.asSuccess(reports: List<ScriptDiagnostic> = listOf()): ResultWithDiagn
|
||||
ResultWithDiagnostics.Success(this, reports)
|
||||
|
||||
/**
|
||||
* Converts the receiver Throwable to the Failure results wrapper with optional [message] and [location]
|
||||
* Converts the receiver Throwable to the Failure results wrapper with optional [customMessage], [path] and [location]
|
||||
*/
|
||||
fun Throwable.asDiagnostics(customMessage: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, location, this)
|
||||
fun Throwable.asDiagnostics(
|
||||
customMessage: String? = null,
|
||||
path: String? = null,
|
||||
location: SourceCode.Location? = null
|
||||
): ScriptDiagnostic =
|
||||
ScriptDiagnostic(customMessage ?: message ?: "$this", ScriptDiagnostic.Severity.ERROR, path, location, this)
|
||||
|
||||
/**
|
||||
* Converts the receiver String to error diagnostic report with optional [location]
|
||||
* Converts the receiver String to error diagnostic report with optional [path] and [location]
|
||||
*/
|
||||
fun String.asErrorDiagnostics(location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, location)
|
||||
fun String.asErrorDiagnostics(path: String? = null, location: SourceCode.Location? = null): ScriptDiagnostic =
|
||||
ScriptDiagnostic(this, ScriptDiagnostic.Severity.ERROR, path, location)
|
||||
|
||||
/**
|
||||
* Extracts the result value from the receiver wrapper or null if receiver represents a Failure
|
||||
*/
|
||||
fun<R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
|
||||
fun <R> ResultWithDiagnostics<R>.resultOrNull(): R? = when (this) {
|
||||
is ResultWithDiagnostics.Success<R> -> value
|
||||
else -> null
|
||||
}
|
||||
|
||||
@@ -222,6 +222,12 @@ interface ScriptCompiler {
|
||||
*/
|
||||
interface CompiledScript<out ScriptBase : Any> {
|
||||
|
||||
/**
|
||||
* The location identifier for the script source, taken from SourceCode.locationId
|
||||
*/
|
||||
val sourceLocationId: String?
|
||||
get() = null
|
||||
|
||||
/**
|
||||
* The compilation configuration used for script compilation
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,16 @@ interface SourceCode {
|
||||
*/
|
||||
val text: String
|
||||
|
||||
/**
|
||||
* The script file or display name
|
||||
*/
|
||||
val name: String?
|
||||
|
||||
/**
|
||||
* The path or other script location identifier
|
||||
*/
|
||||
val locationId: String?
|
||||
|
||||
/**
|
||||
* The source code position
|
||||
* @param line source code position line
|
||||
|
||||
@@ -45,6 +45,8 @@ fun getMergedScriptText(script: SourceCode, configuration: ScriptCompilationConf
|
||||
open class FileScriptSource(val file: File, private val preloadedText: String? = null) : ExternalSourceCode {
|
||||
override val externalLocation: URL get() = file.toURI().toURL()
|
||||
override val text: String by lazy { preloadedText ?: file.readText() }
|
||||
override val name: String? get() = file.name
|
||||
override val locationId: String? get() = file.path
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,6 +54,8 @@ open class FileScriptSource(val file: File, private val preloadedText: String? =
|
||||
*/
|
||||
open class UrlScriptSource(override val externalLocation: URL) : ExternalSourceCode {
|
||||
override val text: String by lazy { externalLocation.readText() }
|
||||
override val name: String? get() = externalLocation.file
|
||||
override val locationId: String? get() = externalLocation.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +67,11 @@ fun File.toScriptSource(): SourceCode = FileScriptSource(this)
|
||||
* The implementation of the ScriptSource for a script in a String
|
||||
*/
|
||||
open class StringScriptSource(val source: String) : SourceCode {
|
||||
|
||||
override val text: String get() = source
|
||||
|
||||
override val name: String? = null
|
||||
override val locationId: String? = null
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+20
-3
@@ -31,22 +31,33 @@ class KJvmCompiledModule(
|
||||
}
|
||||
|
||||
class KJvmCompiledScript<out ScriptBase : Any>(
|
||||
sourceLocationId: String?,
|
||||
compilationConfiguration: ScriptCompilationConfiguration,
|
||||
private var scriptClassFQName: String,
|
||||
override val otherScripts: List<CompiledScript<*>> = emptyList(),
|
||||
otherScripts: List<CompiledScript<*>> = emptyList(),
|
||||
private var compiledModule: KJvmCompiledModule? = null
|
||||
) : CompiledScript<ScriptBase>, Serializable {
|
||||
|
||||
private var _sourceLocationId: String? = sourceLocationId
|
||||
|
||||
override val sourceLocationId: String?
|
||||
get() = _sourceLocationId
|
||||
|
||||
private var _compilationConfiguration: ScriptCompilationConfiguration? = compilationConfiguration
|
||||
|
||||
override val compilationConfiguration: ScriptCompilationConfiguration
|
||||
get() = _compilationConfiguration!!
|
||||
|
||||
private var _otherScripts: List<CompiledScript<*>> = otherScripts
|
||||
|
||||
override val otherScripts: List<CompiledScript<*>>
|
||||
get() = _otherScripts
|
||||
|
||||
override suspend fun getClass(scriptEvaluationConfiguration: ScriptEvaluationConfiguration?): ResultWithDiagnostics<KClass<*>> = try {
|
||||
val classLoader = scriptEvaluationConfiguration?.get(JvmScriptEvaluationConfiguration.actualClassLoader)
|
||||
?: run {
|
||||
if (compiledModule == null)
|
||||
return ResultWithDiagnostics.Failure("Unable to load class $scriptClassFQName: no compiled module is provided".asErrorDiagnostics())
|
||||
return ResultWithDiagnostics.Failure("Unable to load class $scriptClassFQName: no compiled module is provided".asErrorDiagnostics(path = sourceLocationId))
|
||||
val baseClassLoader = scriptEvaluationConfiguration?.get(JvmScriptEvaluationConfiguration.baseClassLoader)
|
||||
?: Thread.currentThread().contextClassLoader
|
||||
val dependencies = compilationConfiguration[ScriptCompilationConfiguration.dependencies]
|
||||
@@ -64,6 +75,7 @@ class KJvmCompiledScript<out ScriptBase : Any>(
|
||||
ResultWithDiagnostics.Failure(
|
||||
ScriptDiagnostic(
|
||||
"Unable to instantiate class $scriptClassFQName",
|
||||
sourcePath = sourceLocationId,
|
||||
exception = e
|
||||
)
|
||||
)
|
||||
@@ -77,18 +89,23 @@ class KJvmCompiledScript<out ScriptBase : Any>(
|
||||
}
|
||||
|
||||
private fun writeObject(outputStream: ObjectOutputStream) {
|
||||
outputStream.writeObject(sourceLocationId)
|
||||
outputStream.writeObject(otherScripts)
|
||||
outputStream.writeObject(compiledModule)
|
||||
outputStream.writeObject(scriptClassFQName)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun readObject(inputStream: ObjectInputStream) {
|
||||
_compilationConfiguration = null
|
||||
_sourceLocationId = inputStream.readObject() as String?
|
||||
_otherScripts = inputStream.readObject() as List<CompiledScript<*>>
|
||||
compiledModule = inputStream.readObject() as KJvmCompiledModule
|
||||
scriptClassFQName = inputStream.readObject() as String
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
private val serialVersionUID = 0L
|
||||
private val serialVersionUID = 1L
|
||||
}
|
||||
}
|
||||
+19
-10
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.idea.KotlinFileType
|
||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.name.NameUtils
|
||||
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtScript
|
||||
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||
@@ -65,6 +64,12 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
fun failure(vararg diagnostics: ScriptDiagnostic): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(*messageCollector.diagnostics.toTypedArray(), *diagnostics)
|
||||
|
||||
fun failure(message: String): ResultWithDiagnostics.Failure =
|
||||
ResultWithDiagnostics.Failure(
|
||||
*messageCollector.diagnostics.toTypedArray(),
|
||||
message.asErrorDiagnostics(path = script.locationId)
|
||||
)
|
||||
|
||||
try {
|
||||
setIdeaIoUseFallback()
|
||||
|
||||
@@ -130,19 +135,19 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
|
||||
val psiFileFactory: PsiFileFactoryImpl = PsiFileFactory.getInstance(environment.project) as PsiFileFactoryImpl
|
||||
val scriptText = getMergedScriptText(script, updatedConfiguration)
|
||||
val scriptFileName = "script" // TODO: extract from file/url if available
|
||||
val scriptFileName = script.name ?: "script.${updatedConfiguration[ScriptCompilationConfiguration.fileExtension]}"
|
||||
val virtualFile = LightVirtualFile(
|
||||
"$scriptFileName${KotlinParserDefinition.STD_SCRIPT_EXT}",
|
||||
scriptFileName,
|
||||
KotlinLanguage.INSTANCE,
|
||||
StringUtil.convertLineSeparators(scriptText)
|
||||
).apply {
|
||||
charset = CharsetToolkit.UTF8_CHARSET
|
||||
}
|
||||
val psiFile: KtFile = psiFileFactory.trySetupPsiForFile(virtualFile, KotlinLanguage.INSTANCE, true, false) as KtFile?
|
||||
?: return failure("Unable to make PSI file from script".asErrorDiagnostics())
|
||||
?: return failure("Unable to make PSI file from script")
|
||||
|
||||
val ktScript = psiFile.declarations.firstIsInstanceOrNull<KtScript>()
|
||||
?: return failure("Not a script file".asErrorDiagnostics())
|
||||
?: return failure("Not a script file")
|
||||
|
||||
val sourceFiles = arrayListOf(psiFile)
|
||||
val (classpath, newSources, sourceDependencies) =
|
||||
@@ -162,7 +167,7 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
}
|
||||
val analysisResult = analyzerWithCompilerReport.analysisResult
|
||||
|
||||
if (!analysisResult.shouldGenerateCode) return failure("no code to generate".asErrorDiagnostics())
|
||||
if (!analysisResult.shouldGenerateCode) return failure("no code to generate")
|
||||
if (analysisResult.isError() || messageCollector.hasErrors()) return failure()
|
||||
|
||||
val generationState = GenerationState.Builder(
|
||||
@@ -189,7 +194,9 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
val otherScripts: List<KJvmCompiledScript<*>> =
|
||||
sourceDependencies.find { it.scriptFile == containingKtFile }?.sourceDependencies?.mapNotNull { sourceFile ->
|
||||
sourceFile.declarations.firstIsInstanceOrNull<KtScript>()?.let {
|
||||
KJvmCompiledScript<Any>(updatedConfiguration, it.fqName.asString(), makeOtherScripts(it))
|
||||
KJvmCompiledScript<Any>(
|
||||
containingKtFile.virtualFile?.path, updatedConfiguration, it.fqName.asString(), makeOtherScripts(it)
|
||||
)
|
||||
}
|
||||
} ?: emptyList()
|
||||
|
||||
@@ -198,6 +205,7 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
}
|
||||
|
||||
val compiledScript = KJvmCompiledScript<Any>(
|
||||
script.locationId,
|
||||
updatedConfiguration,
|
||||
ktScript.fqName.asString(),
|
||||
makeOtherScripts(ktScript),
|
||||
@@ -206,7 +214,7 @@ class KJvmCompilerImpl(val hostConfiguration: ScriptingHostConfiguration) : KJvm
|
||||
|
||||
return ResultWithDiagnostics.Success(compiledScript, messageCollector.diagnostics)
|
||||
} catch (ex: Throwable) {
|
||||
return failure(ex.asDiagnostics())
|
||||
return failure(ex.asDiagnostics(path = script.locationId))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,9 +245,10 @@ internal class ScriptDiagnosticsMessageCollector : MessageCollector {
|
||||
}
|
||||
if (mappedSeverity != null) {
|
||||
val mappedLocation = location?.let {
|
||||
SourceCode.Location(SourceCode.Position(it.line, it.column))
|
||||
if (it.line < 0 && it.column < 0) null // special location created by CompilerMessageLocation.create
|
||||
else SourceCode.Location(SourceCode.Position(it.line, it.column))
|
||||
}
|
||||
_diagnostics.add(ScriptDiagnostic(message, mappedSeverity, mappedLocation))
|
||||
_diagnostics.add(ScriptDiagnostic(message, mappedSeverity, location?.path, mappedLocation))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,6 +92,6 @@ open class BasicJvmScriptEvaluator : ScriptEvaluator {
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script"))
|
||||
ResultWithDiagnostics.Failure(e.asDiagnostics("Error evaluating script", path = compiledScript.sourceLocationId))
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -75,6 +75,25 @@ class ScriptingHostTest : TestCase() {
|
||||
Assert.assertEquals(greeting, output)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testImportError() {
|
||||
val script = "println(\"Hello from imported \$helloScriptName script!\")"
|
||||
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
|
||||
refineConfiguration {
|
||||
beforeCompiling { ctx ->
|
||||
ScriptCompilationConfiguration(ctx.compilationConfiguration) {
|
||||
importScripts(File(TEST_DATA_DIR, "missing_script.kts").toScriptSource())
|
||||
}.asSuccess()
|
||||
}
|
||||
}
|
||||
}
|
||||
val res = BasicJvmScriptingHost().eval(script.toScriptSource(), compilationConfiguration, null)
|
||||
assertTrue(res is ResultWithDiagnostics.Failure)
|
||||
val report = res.reports.find { it.message.startsWith("Source file or directory not found") }
|
||||
assertNotNull(report)
|
||||
assertEquals("/script.kts", report?.sourcePath)
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun testMemoryCache() {
|
||||
|
||||
Reference in New Issue
Block a user