Refactor repl and JSR 223 support code, add separate example jars for each type of JSR 223 repl (with tests)

This commit is contained in:
Ilya Chernikov
2016-09-26 17:56:26 +02:00
parent 079fece9b4
commit 10d259771c
22 changed files with 925 additions and 152 deletions
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven-plugin-anno.version>1.4.1</maven-plugin-anno.version>
<maven.version>3.0.4</maven.version>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-jsr223-daemon-local-eval-example</artifactId>
<packaging>jar</packaging>
<description>Sample Kotlin JSR 223 scripting jar with daemon (out-of-process) compilation and local (in-process) evaluation</description>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-util</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<KOTLIN_COMPILER_JAR>${org.jetbrains.kotlin:kotlin-compiler:jar}</KOTLIN_COMPILER_JAR>
<KOTLIN_JAVA_RUNTIME_JAR>${org.jetbrains.kotlin:kotlin-runtime:jar}</KOTLIN_JAVA_RUNTIME_JAR>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1 @@
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.script.jsr223
import org.jetbrains.kotlin.cli.common.KotlinVersion
import org.junit.Assert
import org.junit.Test
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
class KotlinJsr223ScriptEngineIT {
@Test
fun testEngineFactory() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
factory!!.apply {
Assert.assertEquals("kotlin", languageName)
Assert.assertEquals(KotlinVersion.VERSION, languageVersion)
Assert.assertEquals("kotlin", engineName)
Assert.assertEquals(KotlinVersion.VERSION, engineVersion)
Assert.assertEquals(listOf("kts"), extensions)
Assert.assertEquals(listOf("text/x-kotlin"), mimeTypes)
Assert.assertEquals(listOf("kotlin"), names)
Assert.assertEquals("obj.method(arg1, arg2, arg3)", getMethodCallSyntax("obj", "method", "arg1", "arg2", "arg3"))
Assert.assertEquals("print(\"Hello, world!\")", getOutputStatement("Hello, world!"))
Assert.assertEquals(KotlinVersion.VERSION, getParameter(ScriptEngine.LANGUAGE_VERSION))
val sep = System.getProperty("line.separator")
val prog = arrayOf("val x: Int = 3", "var y = x + 2")
Assert.assertEquals(prog.joinToString(sep) + sep, getProgram(*prog))
}
}
@Test
fun testEngine() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
val engine = factory!!.scriptEngine
Assert.assertNotNull(engine as? KotlinJsr223JvmDaemonLocalEvalScriptEngine)
Assert.assertSame(factory, engine!!.factory)
val bindings = engine.createBindings()
Assert.assertTrue(bindings is SimpleBindings)
}
@Test
fun testSimpleEval() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval("val x = 3")
Assert.assertNull(res1)
val res2 = engine.eval("x + 2")
Assert.assertEquals(5, res2)
}
}
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven-plugin-anno.version>1.4.1</maven-plugin-anno.version>
<maven.version>3.0.4</maven.version>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-jsr223-daemon-remote-eval-example</artifactId>
<packaging>jar</packaging>
<description>Sample Kotlin JSR 223 scripting jar with daemon (out-of-process) compilation and evaluation</description>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-util</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<KOTLIN_COMPILER_JAR>${org.jetbrains.kotlin:kotlin-compiler:jar}</KOTLIN_COMPILER_JAR>
<KOTLIN_JAVA_RUNTIME_JAR>${org.jetbrains.kotlin:kotlin-runtime:jar}</KOTLIN_JAVA_RUNTIME_JAR>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1 @@
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonRemoteEvalScriptEngineFactory
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.script.jsr223
import org.jetbrains.kotlin.cli.common.KotlinVersion
import org.junit.Assert
import org.junit.Test
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
class KotlinJsr223ScriptEngineIT {
@Test
fun testEngineFactory() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
factory!!.apply {
Assert.assertEquals("kotlin", languageName)
Assert.assertEquals(KotlinVersion.VERSION, languageVersion)
Assert.assertEquals("kotlin", engineName)
Assert.assertEquals(KotlinVersion.VERSION, engineVersion)
Assert.assertEquals(listOf("kts"), extensions)
Assert.assertEquals(listOf("text/x-kotlin"), mimeTypes)
Assert.assertEquals(listOf("kotlin"), names)
Assert.assertEquals("obj.method(arg1, arg2, arg3)", getMethodCallSyntax("obj", "method", "arg1", "arg2", "arg3"))
Assert.assertEquals("print(\"Hello, world!\")", getOutputStatement("Hello, world!"))
Assert.assertEquals(KotlinVersion.VERSION, getParameter(ScriptEngine.LANGUAGE_VERSION))
val sep = System.getProperty("line.separator")
val prog = arrayOf("val x: Int = 3", "var y = x + 2")
Assert.assertEquals(prog.joinToString(sep) + sep, getProgram(*prog))
}
}
@Test
fun testEngine() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
val engine = factory!!.scriptEngine
Assert.assertNotNull(engine as? KotlinJsr223JvmDaemonRemoteEvalScriptEngine)
Assert.assertSame(factory, engine!!.factory)
val bindings = engine.createBindings()
Assert.assertTrue(bindings is SimpleBindings)
}
@Test
fun testSimpleEval() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval("val x = 3")
Assert.assertNull(res1)
val res2 = engine.eval("x + 2")
Assert.assertEquals(5, res2)
}
}
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven-plugin-anno.version>1.4.1</maven-plugin-anno.version>
<maven.version>3.0.4</maven.version>
</properties>
<parent>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-project</artifactId>
<version>1.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>kotlin-jsr223-local-example</artifactId>
<packaging>jar</packaging>
<description>Sample Kotlin JSR 223 scripting jar with local (in-process) compilation and evaluation</description>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-runtime</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-util</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${project.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<configuration>
<systemPropertyVariables>
<KOTLIN_COMPILER_JAR>${org.jetbrains.kotlin:kotlin-compiler:jar}</KOTLIN_COMPILER_JAR>
<KOTLIN_JAVA_RUNTIME_JAR>${org.jetbrains.kotlin:kotlin-runtime:jar}</KOTLIN_JAVA_RUNTIME_JAR>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1 @@
org.jetbrains.kotlin.script.jsr223.KotlinJsr232JvmLocalScriptEngineFactory
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.script.jsr223
import org.jetbrains.kotlin.cli.common.KotlinVersion
import org.junit.Assert
import org.junit.Test
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import javax.script.SimpleBindings
class KotlinJsr223ScriptEngineIT {
@Test
fun testEngineFactory() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
factory!!.apply {
Assert.assertEquals("kotlin", languageName)
Assert.assertEquals(KotlinVersion.VERSION, languageVersion)
Assert.assertEquals("kotlin", engineName)
Assert.assertEquals(KotlinVersion.VERSION, engineVersion)
Assert.assertEquals(listOf("kts"), extensions)
Assert.assertEquals(listOf("text/x-kotlin"), mimeTypes)
Assert.assertEquals(listOf("kotlin"), names)
Assert.assertEquals("obj.method(arg1, arg2, arg3)", getMethodCallSyntax("obj", "method", "arg1", "arg2", "arg3"))
Assert.assertEquals("print(\"Hello, world!\")", getOutputStatement("Hello, world!"))
Assert.assertEquals(KotlinVersion.VERSION, getParameter(ScriptEngine.LANGUAGE_VERSION))
val sep = System.getProperty("line.separator")
val prog = arrayOf("val x: Int = 3", "var y = x + 2")
Assert.assertEquals(prog.joinToString(sep) + sep, getProgram(*prog))
}
}
@Test
fun testEngine() {
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
Assert.assertNotNull(factory)
val engine = factory!!.scriptEngine
Assert.assertNotNull(engine as? KotlinJsr232JvmLocalScriptEngine)
Assert.assertSame(factory, engine!!.factory)
val bindings = engine.createBindings()
Assert.assertTrue(bindings is SimpleBindings)
}
@Test
fun testSimpleEval() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval("val x = 3")
Assert.assertNull(res1)
val res2 = engine.eval("x + 2")
Assert.assertEquals(5, res2)
}
}