Switch to templates in the separate script runtime

This commit is contained in:
Ilya Chernikov
2016-10-11 10:03:41 +02:00
parent c2b5c11781
commit 846797ff61
36 changed files with 99 additions and 128 deletions
@@ -344,6 +344,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
}
if (!arguments.noStdlib) {
classpath.add(paths.runtimePath)
classpath.add(paths.scriptRuntimePath)
}
// "-no-stdlib" implies "-no-reflect": otherwise we would be able to transitively read stdlib classes through kotlin-reflect,
// which is likely not what user wants since s/he manually provided "-no-stdlib"
@@ -122,8 +122,16 @@ public class CompileEnvironmentUtil {
if (!runtimePath.exists()) {
throw new CompileEnvironmentException("Couldn't find runtime library");
}
File scriptRuntimePath = PathUtil.getKotlinPathsForCompiler().getScriptRuntimePath();
if (!scriptRuntimePath.exists()) {
throw new CompileEnvironmentException("Couldn't find script runtime library");
}
JarInputStream jis = new JarInputStream(new FileInputStream(runtimePath));
copyJarImpl(stream, runtimePath);
}
private static void copyJarImpl(JarOutputStream stream, File jarPath) throws IOException {
JarInputStream jis = new JarInputStream(new FileInputStream(jarPath));
try {
while (true) {
JarEntry e = jis.getNextJarEntry();
+1
View File
@@ -46,6 +46,7 @@ messages/**)
-libraryjars '<jssejar>'
-libraryjars '<bootstrap.runtime>'
-libraryjars '<bootstrap.reflect>'
-libraryjars '<bootstrap.script.runtime>'
-target 1.6
-dontoptimize
@@ -77,7 +77,7 @@ open class KotlinJvmReplService(
protected val configuration = CompilerConfiguration().apply {
addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
addJvmClasspathRoots(PathUtil.getKotlinPathsForCompiler().let { listOf(it.runtimePath, it.reflectPath) })
addJvmClasspathRoots(PathUtil.getKotlinPathsForCompiler().let { listOf(it.runtimePath, it.reflectPath, it.scriptRuntimePath) })
addJvmClasspathRoots(templateClasspath)
put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script")
}
+1
View File
@@ -15,5 +15,6 @@
<orderEntry type="module" module-name="container" exported="" />
<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" />
</component>
</module>
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.KtScript
import java.io.File
import kotlin.reflect.KClass
import kotlin.script.StandardScriptTemplate
import kotlin.script.templates.standard.ScriptTemplateWithArgs
open class KotlinScriptDefinition(val template: KClass<out Any>) {
@@ -50,5 +50,5 @@ interface KotlinScriptExternalDependencies {
val scripts: Iterable<File> get() = emptyList()
}
object StandardScriptDefinition : KotlinScriptDefinition(StandardScriptTemplate::class)
object StandardScriptDefinition : KotlinScriptDefinition(ScriptTemplateWithArgs::class)
@@ -1,6 +1,6 @@
package
public final class Script : kotlin.script.StandardScriptTemplate {
public final class Script : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor Script(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -9,7 +9,7 @@ package test {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class A : kotlin.script.StandardScriptTemplate {
public final class A : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor A(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val x: kotlin.Int = 1
@@ -1,6 +1,6 @@
package
public final class AccessForwardDeclarationInScript : kotlin.script.StandardScriptTemplate {
public final class AccessForwardDeclarationInScript : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor AccessForwardDeclarationInScript(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val x: kotlin.Int = 2
@@ -1,6 +1,6 @@
package
public final class ComplexScript : kotlin.script.StandardScriptTemplate {
public final class ComplexScript : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor ComplexScript(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val y: kotlin.Int = 2
@@ -1,6 +1,6 @@
package
public final class NestedInnerClass : kotlin.script.StandardScriptTemplate {
public final class NestedInnerClass : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor NestedInnerClass(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val property: kotlin.String = ""
@@ -1,6 +1,6 @@
package
public final class SimpleScript : kotlin.script.StandardScriptTemplate {
public final class SimpleScript : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor SimpleScript(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val x: kotlin.Int = 1
+1 -1
View File
@@ -1,6 +1,6 @@
package
public final class Imports : kotlin.script.StandardScriptTemplate {
public final class Imports : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor Imports(/*0*/ args: kotlin.Array<kotlin.String>)
public final val al: java.util.ArrayList<kotlin.String>? = null
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
@@ -1,6 +1,6 @@
package
public final class TopLevelVariable : kotlin.script.StandardScriptTemplate {
public final class TopLevelVariable : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor TopLevelVariable(/*0*/ args: kotlin.Array<kotlin.String>)
public final val a: kotlin.Int
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
+1 -1
View File
@@ -1,6 +1,6 @@
package
public final class Script : kotlin.script.StandardScriptTemplate {
public final class Script : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor Script(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
@@ -1,6 +1,6 @@
package
public final class Script : kotlin.script.StandardScriptTemplate {
public final class Script : kotlin.script.templates.standard.ScriptTemplateWithArgs {
public constructor Script(/*0*/ args: kotlin.Array<kotlin.String>)
public final override /*1*/ /*fake_override*/ val args: kotlin.Array<kotlin.String>
public final val prop: kotlin.Int
@@ -51,6 +51,11 @@ public class ForTestCompileRuntime {
return assertExists(new File("dist/kotlinc/lib/kotlin-reflect.jar"));
}
@NotNull
public static File scriptRuntimeJarForTests() {
return assertExists(new File("dist/kotlinc/lib/kotlin-script-runtime.jar"));
}
@NotNull
private static File assertExists(@NotNull File file) {
if (!file.exists()) {
@@ -63,7 +68,7 @@ public class ForTestCompileRuntime {
public static synchronized ClassLoader runtimeAndReflectJarClassLoader() {
ClassLoader loader = reflectJarClassLoader.get();
if (loader == null) {
loader = createClassLoader(runtimeJarForTests(), reflectJarForTests(), kotlinTestJarForTests());
loader = createClassLoader(runtimeJarForTests(), reflectJarForTests(), scriptRuntimeJarForTests(), kotlinTestJarForTests());
reflectJarClassLoader = new SoftReference<ClassLoader>(loader);
}
return loader;
@@ -73,7 +78,7 @@ public class ForTestCompileRuntime {
public static synchronized ClassLoader runtimeJarClassLoader() {
ClassLoader loader = runtimeJarClassLoader.get();
if (loader == null) {
loader = createClassLoader(runtimeJarForTests());
loader = createClassLoader(runtimeJarForTests(), scriptRuntimeJarForTests());
runtimeJarClassLoader = new SoftReference<ClassLoader>(loader);
}
return loader;
@@ -468,10 +468,12 @@ public class KotlinTestUtils {
if (configurationKind.getWithRuntime()) {
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.runtimeJarForTests());
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.scriptRuntimeJarForTests());
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.kotlinTestJarForTests());
}
else if (configurationKind.getWithMockRuntime()) {
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.mockRuntimeJarForTests());
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.scriptRuntimeJarForTests());
}
if (configurationKind.getWithReflection()) {
JvmContentRootsKt.addJvmClasspathRoot(configuration, ForTestCompileRuntime.reflectJarForTests());
@@ -43,7 +43,7 @@ class GenericReplTest : TestCase() {
val repl = TestRepl(disposable,
listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")),
"kotlin.script.StandardScriptTemplate")
"kotlin.script.templates.standard.ScriptTemplateWithArgs")
val res1 = repl.replCompiler?.check(ReplCodeLine(0, "val x ="), emptyList())
TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCheckResult.Incomplete)
@@ -494,7 +494,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
withDisposable { disposable ->
val repl = KotlinRemoteReplCompiler(disposable, daemon!!, null, CompileService.TargetPlatform.JVM,
classpathFromClassloader(),
"kotlin.script.StandardScriptTemplate",
"kotlin.script.templates.standard.ScriptTemplateWithArgs",
System.err)
val localEvaluator = GenericReplCompiledEvaluator(emptyList(),
@@ -543,7 +543,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
val repl = KotlinRemoteReplEvaluator(disposable, daemon!!, null, CompileService.TargetPlatform.JVM,
listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")),
"kotlin.script.StandardScriptTemplate",
"kotlin.script.templates.standard.ScriptTemplateWithArgs",
arrayOf(emptyArray<String>()), null,
System.err, evalOut, evalErr, evalIn)
@@ -37,12 +37,11 @@ import org.junit.Test
import java.io.File
import java.lang.Exception
import java.lang.reflect.InvocationTargetException
import java.net.URISyntaxException
import java.net.URL
import java.net.URLClassLoader
import java.util.concurrent.Future
import kotlin.reflect.KClass
import kotlin.script.StandardScriptTemplate
import kotlin.script.templates.standard.ScriptTemplateWithArgs
// TODO: the contetnts of this file should go into ScriptTest.kt and replace appropriate xml-based functionality,
// as soon as the the latter is removed from the codebase
@@ -179,7 +178,7 @@ class ScriptTemplateTest {
@Test
fun testScriptWithStandardTemplate() {
val aClass = compileScript("fib_std.kts", StandardScriptTemplate::class, runIsolated = false)
val aClass = compileScript("fib_std.kts", ScriptTemplateWithArgs::class, runIsolated = false)
Assert.assertNotNull(aClass)
captureOut {
aClass!!.getConstructor(Array<String>::class.java).newInstance(arrayOf("4", "other"))
@@ -324,12 +323,12 @@ class TestKotlinScriptDependenciesResolver : TestKotlinScriptDummyDependenciesRe
{
val cp = script.annotations.flatMap {
when (it) {
is DependsOn -> listOf(if (it.path == "@{runtime}") kotlinPaths.runtimePath else File(it.path))
is DependsOnTwo -> listOf(it.path1, it.path2).mapNotNull {
is DependsOn -> if (it.path == "@{runtime}") listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath) else listOf(File(it.path))
is DependsOnTwo -> listOf(it.path1, it.path2).flatMap {
when {
it.isBlank() -> null
it == "@{runtime}" -> kotlinPaths.runtimePath
else -> File(it)
it.isBlank() -> emptyList()
it == "@{runtime}" -> listOf(kotlinPaths.runtimePath, kotlinPaths.scriptRuntimePath)
else -> listOf(File(it))
}
}
is InvalidScriptResolverAnnotation -> throw Exception("Invalid annotation ${it.name}", it.error)
@@ -33,6 +33,9 @@ public interface KotlinPaths {
@NotNull
File getReflectPath();
@NotNull
File getScriptRuntimePath();
@NotNull
File getKotlinTestPath();
@@ -52,6 +52,12 @@ public class KotlinPathsFromHomeDir implements KotlinPaths {
return getLibraryFile(PathUtil.KOTLIN_JAVA_REFLECT_JAR);
}
@Override
@NotNull
public File getScriptRuntimePath() {
return getLibraryFile(PathUtil.KOTLIN_JAVA_SCRIPT_RUNTIME_JAR);
}
@NotNull
@Override
public File getKotlinTestPath() {
@@ -32,6 +32,7 @@ public class PathUtil {
public static final String JS_LIB_SRC_JAR_NAME = "kotlin-jslib-sources.jar";
public static final String KOTLIN_JAVA_RUNTIME_JAR = "kotlin-runtime.jar";
public static final String KOTLIN_JAVA_REFLECT_JAR = "kotlin-reflect.jar";
public static final String KOTLIN_JAVA_SCRIPT_RUNTIME_JAR = "kotlin-script-runtime.jar";
public static final String KOTLIN_TEST_JAR = "kotlin-test.jar";
public static final String KOTLIN_JAVA_RUNTIME_SRC_JAR = "kotlin-runtime-sources.jar";
public static final String KOTLIN_COMPILER_JAR = "kotlin-compiler.jar";