From 6063c01a1c59eb942ccdc23a0777a7aa3d6c64f7 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Wed, 23 May 2012 02:52:30 +0400 Subject: [PATCH] command line script launcher % ./bin/kotlin -script hello.ktscript hello world % cat hello.ktscript fun hello(what: String) = println("hello $what") hello("world") Note it currently takes 8 seconds to evaluate hello world script. --- .../jet/buildtools/core/BytecodeCompiler.java | 4 +-- .../jetbrains/jet/cli/jvm/K2JVMCompiler.java | 7 ++--- .../jet/cli/jvm/K2JVMCompilerArguments.java | 3 +++ .../compiler/KotlinToJVMBytecodeCompiler.java | 27 +++++++++++++++---- .../integration-tests/data/help/help.expected | 1 + .../data/script/hello.ktscript | 1 + .../data/script/script.expected | 2 ++ .../jetbrains/kotlin/CompilerSmokeTest.java | 5 ++++ 8 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 compiler/integration-tests/data/script/hello.ktscript create mode 100644 compiler/integration-tests/data/script/script.expected diff --git a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java index 9c5164fe887..ef2fd970e5e 100644 --- a/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java +++ b/build-tools/core/src/org/jetbrains/jet/buildtools/core/BytecodeCompiler.java @@ -97,7 +97,7 @@ public class BytecodeCompiler { */ public void sourcesToDir ( @NotNull String src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath ) { try { - boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, true + boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), null, output, false, true /* Last arg is ignored anyway */); if ( ! success ) { throw new CompileEnvironmentException( errorMessage( src, false )); @@ -120,7 +120,7 @@ public class BytecodeCompiler { */ public void sourcesToJar ( @NotNull String src, @NotNull String jar, boolean includeRuntime, @Nullable String stdlib, @Nullable String[] classpath ) { try { - boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, includeRuntime); + boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(env(stdlib, classpath), Collections.singletonList(src), jar, null, false, includeRuntime); if ( ! success ) { throw new CompileEnvironmentException( errorMessage( src, false )); } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java index 8bc51c5d4d4..77fd82edfdc 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/jvm/K2JVMCompiler.java @@ -120,9 +120,7 @@ public class K2JVMCompiler extends CLICompiler sources = Lists.newArrayList(); @@ -131,8 +129,7 @@ public class K2JVMCompiler extends CLICompiler scriptClass = classLoader.loadClass("Script"); + scriptClass.newInstance(); + } catch (Exception e) { + throw new RuntimeException("Failed to evaluate script: " + e, e); + } + } else { throw new CompileEnvironmentException("Output directory or jar file is not specified - no files will be saved to the disk"); } @@ -158,24 +176,23 @@ public class KotlinToJVMBytecodeCompiler { public static boolean compileBunchOfSources( K2JVMCompileEnvironmentConfiguration configuration, - - List sourceFilesOrDirs, String jar, String outputDir, boolean includeRuntime) { + List sourceFilesOrDirs, String jar, String outputDir, boolean script, boolean includeRuntime) { for (String sourceFileOrDir : sourceFilesOrDirs) { configuration.getEnvironment().addSources(sourceFileOrDir); } - return compileBunchOfSources(configuration, jar, outputDir, includeRuntime); + return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime); } public static boolean compileBunchOfSourceDirectories( K2JVMCompileEnvironmentConfiguration configuration, - List sources, String jar, String outputDir, boolean includeRuntime) { + List sources, String jar, String outputDir, boolean script, boolean includeRuntime) { for (String source : sources) { configuration.getEnvironment().addSources(source); } - return compileBunchOfSources(configuration, jar, outputDir, includeRuntime); + return compileBunchOfSources(configuration, jar, outputDir, script, includeRuntime); } @Nullable diff --git a/compiler/integration-tests/data/help/help.expected b/compiler/integration-tests/data/help/help.expected index 8f8c48e3972..745b9f878e3 100644 --- a/compiler/integration-tests/data/help/help.expected +++ b/compiler/integration-tests/data/help/help.expected @@ -8,6 +8,7 @@ OUT -altHeaders [String] Path to the alternative library headers paths OUT -mode [String] Special compiler modes: stubs or altHeaders OUT -output [String] output directory OUT -module [String] module to compile +OUT -script [flag] OUT -tags [flag] OUT -verbose [flag] OUT -version [flag] diff --git a/compiler/integration-tests/data/script/hello.ktscript b/compiler/integration-tests/data/script/hello.ktscript new file mode 100644 index 00000000000..3a4e995ffa1 --- /dev/null +++ b/compiler/integration-tests/data/script/hello.ktscript @@ -0,0 +1 @@ +println("hello world") diff --git a/compiler/integration-tests/data/script/script.expected b/compiler/integration-tests/data/script/script.expected new file mode 100644 index 00000000000..23e0cc5bfc6 --- /dev/null +++ b/compiler/integration-tests/data/script/script.expected @@ -0,0 +1,2 @@ +OUT hello world +Return code: 0 diff --git a/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java b/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java index 49874136c71..fe3beafa167 100644 --- a/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java +++ b/compiler/integration-tests/src/org/jetbrains/kotlin/CompilerSmokeTest.java @@ -50,4 +50,9 @@ public class CompilerSmokeTest extends KotlinIntegrationTestBase { runCompiler("hello.compile", "-src", "hello.kt", "-jar", jar); } + + @Test + public void script() throws Exception { + runCompiler("script", "-script", "hello.ktscript"); + } }