Implement -X arguments passing from kotlin runner to compiler

This commit is contained in:
Ilya Chernikov
2021-01-04 13:06:18 +01:00
parent 9a7d1948a7
commit d2ecc1e361
6 changed files with 52 additions and 16 deletions
@@ -42,6 +42,7 @@ object Main {
var collectingExpressions = false
var needsCompiler = false
val arguments = arrayListOf<String>()
val compilerArguments = arrayListOf<String>()
var expression: String? = null
var noReflect = false
@@ -103,6 +104,10 @@ object Main {
}
else if ("-no-reflect" == arg) {
noReflect = true
compilerArguments.add(arg)
}
else if (arg.startsWith("-X")) {
compilerArguments.add(arg)
}
else if (arg.startsWith("-")) {
throw RunnerException("unsupported argument: $arg")
@@ -146,7 +151,7 @@ object Main {
}
}
runner.run(classpath, arguments, compilerClasspath)
runner.run(classpath, compilerArguments, arguments, compilerClasspath)
}
private fun MutableList<URL>.addPath(path: String) {
@@ -167,19 +172,21 @@ object Main {
private fun printUsageAndExit() {
println("""kotlin: run Kotlin programs, scripts or REPL.
Usage: kotlin <options> <command> <arguments>
Usage: kotlin <options> <command> [<arguments>]
where command may be one of:
foo.Bar Runs the 'main' function from the class with the given qualified name
(compiler arguments are ignored)
app.jar Runs the given JAR file as 'java -jar' would do
(-classpath argument is ignored and no Kotlin runtime is added to the classpath)
""" +
// script.kts Compiles and runs the given script
// -expression (-e) '2+2' Evaluates the expression and prints the result
"""and possible options include:
(compiler arguments are ignored and no Kotlin stdlib is added to the classpath)
script.kts Compiles and runs the given script, passing <arguments> to it
-expression (-e) '2+2' Evaluates the expression and prints the result, passing <arguments> to it
<no command> Runs Kotlin REPL, passing <arguments> to it
and possible options include:
-classpath (-cp) <path> Paths where to find user class files
-Dname=value Set a system JVM property
-J<option> Pass an option directly to JVM
-no-reflect Don't include Kotlin reflection implementation into classpath
-X<flag>[=value] Pass -X argument to the compiler
-version Display Kotlin version
-help (-h) Print a synopsis of options
""")
@@ -21,6 +21,7 @@ import java.net.URL
interface Runner {
fun run(
classpath: List<URL>,
compilerArguments: List<String>,
arguments: List<String>,
compilerClasspath: List<URL>
)
@@ -32,7 +32,7 @@ abstract class AbstractRunner : Runner {
protected abstract fun createClassLoader(classpath: List<URL>): ClassLoader
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
override fun run(classpath: List<URL>, compilerArguments: List<String>, arguments: List<String>, compilerClasspath: List<URL>) {
val classLoader = createClassLoader(classpath)
val mainClass = try {
@@ -128,17 +128,19 @@ private fun MutableList<String>.addClasspathArgIfNeeded(classpath: List<URL>) {
}
class ReplRunner : RunnerWithCompiler() {
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
override fun run(classpath: List<URL>, compilerArguments: List<String>, arguments: List<String>, compilerClasspath: List<URL>) {
val compilerArgs = ArrayList<String>()
compilerArgs.addClasspathArgIfNeeded(classpath)
compilerArgs.addAll(compilerArguments)
runCompiler(compilerClasspath, compilerArgs)
}
}
class ScriptRunner(private val path: String) : RunnerWithCompiler() {
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
override fun run(classpath: List<URL>, compilerArguments: List<String>, arguments: List<String>, compilerClasspath: List<URL>) {
val compilerArgs = ArrayList<String>().apply {
addClasspathArgIfNeeded(classpath)
addAll(compilerArguments)
add("-script")
add(path)
if (arguments.isNotEmpty() && arguments.first() != "--") {
@@ -151,9 +153,10 @@ class ScriptRunner(private val path: String) : RunnerWithCompiler() {
}
class ExpressionRunner(private val code: String) : RunnerWithCompiler() {
override fun run(classpath: List<URL>, arguments: List<String>, compilerClasspath: List<URL>) {
override fun run(classpath: List<URL>, compilerArguments: List<String>, arguments: List<String>, compilerClasspath: List<URL>) {
val compilerArgs = ArrayList<String>().apply {
addClasspathArgIfNeeded(classpath)
addAll(compilerArguments)
add("-expression")
add(code)
if (arguments.isNotEmpty() && arguments.first() != "--") {