Remove examples/tests based on obsolete script-util
also mark the rest of the API as Obsolete related to #KT-58367
This commit is contained in:
committed by
Space Team
parent
2e393e8fb7
commit
8c8ca7bb70
@@ -261,8 +261,6 @@
|
||||
/libraries/examples/annotation-processor-example/ "Kotlin Compiler Core"
|
||||
/libraries/examples/kotlin-gradle-subplugin-example/ "Kotlin Compiler Core"
|
||||
/libraries/examples/kotlin-java-example/ "Kotlin JVM"
|
||||
/libraries/examples/kotlin-jsr223-daemon-local-eval-example/ "Kotlin Compiler Core"
|
||||
/libraries/examples/kotlin-jsr223-local-example/ "Kotlin Compiler Core"
|
||||
/libraries/examples/scripting/ "Kotlin Compiler Core"
|
||||
/libraries/kotlin.test/ A.Qurbonzoda Vsevolod.Tolstopyato Ilya.Gorbunov
|
||||
/libraries/kotlinx-metadata/ Alexander.Udalov A.Qurbonzoda Vsevolod.Tolstopyato Ilya.Gorbunov
|
||||
|
||||
@@ -693,7 +693,6 @@ tasks {
|
||||
|
||||
register("scriptingJvmTest") {
|
||||
dependsOn("dist")
|
||||
dependsOn(":kotlin-script-util:test")
|
||||
dependsOn(":kotlin-scripting-compiler:test")
|
||||
dependsOn(":kotlin-scripting-compiler:testWithIr")
|
||||
dependsOn(":kotlin-scripting-common:test")
|
||||
@@ -706,7 +705,6 @@ tasks {
|
||||
// see comments on the task in kotlin-scripting-jvm-host-test
|
||||
// dependsOn(":kotlin-scripting-jvm-host-test:embeddableTest")
|
||||
dependsOn(":kotlin-scripting-jsr223-test:embeddableTest")
|
||||
dependsOn(":kotlin-main-kts-test:test")
|
||||
dependsOn(":kotlin-scripting-ide-services-test:test")
|
||||
dependsOn(":kotlin-scripting-ide-services-test:embeddableTest")
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import org.jetbrains.kotlin.pill.PillExtension
|
||||
|
||||
description = "Sample Kotlin JSR 223 scripting jar with daemon (out-of-process) compilation and local (in-process) evaluation"
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("jps-compatible")
|
||||
}
|
||||
|
||||
pill {
|
||||
variant = PillExtension.Variant.FULL
|
||||
}
|
||||
|
||||
val compilerClasspath by configurations.creating {
|
||||
attributes {
|
||||
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
|
||||
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testApi(kotlinStdlib())
|
||||
testApi(project(":kotlin-script-runtime"))
|
||||
testApi(project(":kotlin-script-util"))
|
||||
testApi(project(":kotlin-daemon-client"))
|
||||
testApi(project(":kotlin-daemon-embeddable"))
|
||||
testApi(project(":kotlin-compiler-embeddable"))
|
||||
testApi(commonDependency("junit:junit"))
|
||||
testApi(project(":kotlin-test:kotlin-test-junit"))
|
||||
compilerClasspath(commonDependency("org.jetbrains.kotlin:kotlin-reflect")) { isTransitive = false }
|
||||
compilerClasspath(kotlinStdlib())
|
||||
compilerClasspath(commonDependency("org.jetbrains.intellij.deps", "trove4j"))
|
||||
compilerClasspath(commonDependency("org.jetbrains.kotlinx", "kotlinx-coroutines-core"))
|
||||
compilerClasspath(project(":kotlin-compiler-embeddable"))
|
||||
compilerClasspath(project(":kotlin-scripting-compiler-embeddable"))
|
||||
compilerClasspath(project(":kotlin-scripting-compiler-impl-embeddable"))
|
||||
compilerClasspath(project(":kotlin-script-runtime"))
|
||||
compilerClasspath(project(":kotlin-scripting-jvm"))
|
||||
compileOnly(project(":compiler:cli-common")) // TODO: fix import (workaround for jps build)
|
||||
testCompileOnly(project(":core:util.runtime")) // TODO: fix import (workaround for jps build)
|
||||
testCompileOnly(project(":daemon-common")) // TODO: fix import (workaround for jps build)
|
||||
}
|
||||
|
||||
projectTest {
|
||||
dependsOn(compilerClasspath)
|
||||
val compilerClasspathProvider = project.provider { compilerClasspath.asPath }
|
||||
doFirst {
|
||||
systemProperty("kotlin.compiler.classpath", compilerClasspathProvider.get())
|
||||
}
|
||||
}
|
||||
-1
@@ -1 +0,0 @@
|
||||
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory
|
||||
-180
@@ -1,180 +0,0 @@
|
||||
/*
|
||||
* 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.config.KotlinCompilerVersion
|
||||
import org.jetbrains.kotlin.daemon.common.threadCpuTime
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.script.*
|
||||
|
||||
class KotlinJsr223DaemonCompileScriptEngineIT {
|
||||
|
||||
@Test
|
||||
fun testEngineFactory() {
|
||||
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
|
||||
Assert.assertNotNull(factory)
|
||||
factory!!.apply {
|
||||
Assert.assertEquals("kotlin", languageName)
|
||||
Assert.assertEquals(KotlinCompilerVersion.VERSION, languageVersion)
|
||||
Assert.assertEquals("kotlin", engineName)
|
||||
Assert.assertEquals(KotlinCompilerVersion.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(KotlinCompilerVersion.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? KotlinJsr223JvmDaemonCompileScriptEngine)
|
||||
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)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInvocable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
val res1 = engine.eval("""
|
||||
fun fn(x: Int) = x + 2
|
||||
val obj = object {
|
||||
fun fn1(x: Int) = x + 3
|
||||
}
|
||||
obj
|
||||
""")
|
||||
Assert.assertNotNull(res1)
|
||||
val invocator = engine as? Invocable
|
||||
Assert.assertNotNull(invocator)
|
||||
assertThrows(NoSuchMethodException::class.java) {
|
||||
invocator!!.invokeFunction("fn1", 3)
|
||||
}
|
||||
val res2 = invocator!!.invokeFunction("fn", 3)
|
||||
Assert.assertEquals(5, res2)
|
||||
// TODO: fix and restore
|
||||
// assertThrows(NoSuchMethodException::class.java) {
|
||||
// invocator!!.invokeMethod(res1, "fn", 3)
|
||||
// }
|
||||
val res3 = invocator.invokeMethod(res1, "fn1", 3)
|
||||
Assert.assertEquals(6, res3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEvalWithContext() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
|
||||
engine.put("z", 33)
|
||||
|
||||
engine.eval("""val x = 10 + bindings["z"] as Int""")
|
||||
|
||||
val result = engine.eval("""x + 20""")
|
||||
Assert.assertEquals(63, result)
|
||||
|
||||
// in the current implementation the history is shared between contexts, so "x" could also be used in this line,
|
||||
// but this behaviour probably will not be preserved in the future, since contexts may become completely isolated
|
||||
val result2 = engine.eval("""11 + bindings["boundValue"] as Int""", engine.createBindings().apply {
|
||||
put("boundValue", 100)
|
||||
})
|
||||
Assert.assertEquals(111, result2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleEvalInEval() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
val res1 = engine.eval("val x = 3")
|
||||
Assert.assertNull(res1)
|
||||
val res2 = engine.eval("val y = eval(\"\$x + 2\") as Int\ny")
|
||||
Assert.assertEquals(5, res2)
|
||||
val res3 = engine.eval("y + 2")
|
||||
Assert.assertEquals(7, res3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleCompilableWithBindings() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")
|
||||
engine.put("z", 33)
|
||||
val comp = (engine as Compilable).compile("val x = 10 + bindings[\"z\"] as Int\nx + 20")
|
||||
val res1 = comp.eval()
|
||||
Assert.assertEquals(63, res1)
|
||||
engine.put("z", 44)
|
||||
val res2 = comp.eval()
|
||||
Assert.assertEquals(74, res2)
|
||||
}
|
||||
|
||||
// Note: the test is flaky, because it is statistical and the thresholds are not big enough.
|
||||
// Therefore it was decided to disable it, but leave in the code in order to be able to quickly check overheads when needed.
|
||||
// @Test
|
||||
fun testEvalInEvalBench() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
|
||||
val script = "val x = 3\nx + 10"
|
||||
|
||||
val mxBeans = ManagementFactory.getThreadMXBean()
|
||||
|
||||
engine.eval("val bnd = createBindings()")
|
||||
|
||||
val times = generateSequence {
|
||||
val t0 = mxBeans.threadCpuTime()
|
||||
|
||||
engine.eval(script)
|
||||
val t1 = mxBeans.threadCpuTime()
|
||||
|
||||
engine.eval("eval(\"\"\"$script\"\"\", bnd)")
|
||||
val t2 = mxBeans.threadCpuTime()
|
||||
|
||||
Triple(t1 - t0, t2 - t1, t2 - t1)
|
||||
}.take(10).toList()
|
||||
|
||||
val adjustedMaxDiff = times.sortedByDescending { (_, _, diff) -> diff }.drop(2).first()
|
||||
|
||||
fun Long.ms() = TimeUnit.NANOSECONDS.toMillis(this)
|
||||
Assert.assertTrue("eval in eval is too long: ${times.joinToString { "(${it.first.ms()}, ${it.second.ms()})" }} (expecting no more than 5x difference)",
|
||||
adjustedMaxDiff.third < 10 || adjustedMaxDiff.first * 5 > adjustedMaxDiff.second )
|
||||
}
|
||||
}
|
||||
|
||||
fun assertThrows(exceptionClass: Class<*>, body: () -> Unit) {
|
||||
try {
|
||||
body()
|
||||
Assert.fail("Expecting an exception of type ${exceptionClass.name}")
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
if (!exceptionClass.isAssignableFrom(e.javaClass)) {
|
||||
Assert.fail("Expecting an exception of type ${exceptionClass.name} but got ${e.javaClass.name}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import org.jetbrains.kotlin.pill.PillExtension
|
||||
|
||||
description = "Sample Kotlin JSR 223 scripting jar with local (in-process) compilation and evaluation"
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("jps-compatible")
|
||||
}
|
||||
|
||||
pill {
|
||||
variant = PillExtension.Variant.FULL
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(kotlinStdlib())
|
||||
api(project(":kotlin-script-runtime"))
|
||||
api(project(":kotlin-compiler-embeddable"))
|
||||
api(project(":kotlin-script-util"))
|
||||
runtimeOnly(project(":kotlin-scripting-compiler-embeddable"))
|
||||
testApi(project(":kotlin-test:kotlin-test-junit"))
|
||||
testApi(commonDependency("junit:junit"))
|
||||
compileOnly(project(":compiler:cli-common")) // TODO: fix import (workaround for jps build)
|
||||
testCompileOnly(project(":core:util.runtime")) // TODO: fix import (workaround for jps build)
|
||||
testCompileOnly(project(":daemon-common")) // TODO: fix import (workaround for jps build)
|
||||
testRuntimeOnly(project(":kotlin-scripting-compiler-embeddable"))
|
||||
}
|
||||
|
||||
projectTest()
|
||||
-2
@@ -1,2 +0,0 @@
|
||||
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
|
||||
|
||||
-259
@@ -1,259 +0,0 @@
|
||||
/*
|
||||
* 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.config.KotlinCompilerVersion
|
||||
import org.jetbrains.kotlin.daemon.common.threadCpuTime
|
||||
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import java.lang.management.ManagementFactory
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.script.*
|
||||
|
||||
class KotlinJsr223LocalScriptEngineIT {
|
||||
|
||||
init {
|
||||
setIdeaIoUseFallback()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEngineFactory() {
|
||||
val factory = ScriptEngineManager().getEngineByExtension("kts").factory
|
||||
Assert.assertNotNull(factory)
|
||||
factory!!.apply {
|
||||
Assert.assertEquals("kotlin", languageName)
|
||||
Assert.assertEquals(KotlinCompilerVersion.VERSION, languageVersion)
|
||||
Assert.assertEquals("kotlin", engineName)
|
||||
Assert.assertEquals(KotlinCompilerVersion.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(KotlinCompilerVersion.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? KotlinJsr223JvmLocalScriptEngine)
|
||||
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)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEvalWithError() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
|
||||
try {
|
||||
engine.eval("java.lang.fish")
|
||||
Assert.fail("Script error expected")
|
||||
}
|
||||
catch (e: ScriptException) {}
|
||||
|
||||
val res1 = engine.eval("val x = 3")
|
||||
Assert.assertNull(res1)
|
||||
|
||||
try {
|
||||
engine.eval("y")
|
||||
Assert.fail("Script error expected")
|
||||
}
|
||||
catch (e: ScriptException) {
|
||||
Assert.assertTrue("Expected message to contain \"unresolved reference: y\", actual: \"${e.message}\"",
|
||||
e.message?.contains("unresolved reference: y") ?: false)
|
||||
}
|
||||
|
||||
val res3 = engine.eval("x + 2")
|
||||
Assert.assertEquals(5, res3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEngineRepeatWithReset() {
|
||||
val code = "open class A {}\n" +
|
||||
"class B : A() {}"
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223JvmLocalScriptEngine
|
||||
|
||||
val res1 = engine.eval(code)
|
||||
Assert.assertNull(res1)
|
||||
|
||||
engine.state.history.reset()
|
||||
|
||||
engine.eval(code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInvocable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
val res1 = engine.eval("""
|
||||
fun fn(x: Int) = x + 2
|
||||
val obj = object {
|
||||
fun fn1(x: Int) = x + 3
|
||||
}
|
||||
obj
|
||||
""")
|
||||
Assert.assertNotNull(res1)
|
||||
val invocator = engine as? Invocable
|
||||
Assert.assertNotNull(invocator)
|
||||
assertThrows(NoSuchMethodException::class.java) {
|
||||
invocator!!.invokeFunction("fn1", 3)
|
||||
}
|
||||
val res2 = invocator!!.invokeFunction("fn", 3)
|
||||
Assert.assertEquals(5, res2)
|
||||
// TODO: fix and restore
|
||||
// assertThrows(NoSuchMethodException::class.java) {
|
||||
// invocator!!.invokeMethod(res1, "fn", 3)
|
||||
// }
|
||||
val res3 = invocator.invokeMethod(res1, "fn1", 3)
|
||||
Assert.assertEquals(6, res3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleCompilable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223JvmLocalScriptEngine
|
||||
val comp1 = engine.compile("val x = 3")
|
||||
val comp2 = engine.compile("x + 2")
|
||||
val res1 = comp1.eval()
|
||||
Assert.assertNull(res1)
|
||||
val res2 = comp2.eval()
|
||||
Assert.assertEquals(5, res2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMultipleCompilable() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts") as KotlinJsr223JvmLocalScriptEngine
|
||||
val compiled1 = engine.compile("""listOf(1,2,3).joinToString(",")""")
|
||||
val compiled2 = engine.compile("""val x = bindings["boundValue"] as Int + bindings["z"] as Int""")
|
||||
val compiled3 = engine.compile("""x""")
|
||||
|
||||
Assert.assertEquals("1,2,3", compiled1.eval())
|
||||
Assert.assertEquals("1,2,3", compiled1.eval())
|
||||
Assert.assertEquals("1,2,3", compiled1.eval())
|
||||
Assert.assertEquals("1,2,3", compiled1.eval())
|
||||
|
||||
engine.getBindings(ScriptContext.ENGINE_SCOPE).apply {
|
||||
put("boundValue", 100)
|
||||
put("z", 33)
|
||||
}
|
||||
|
||||
compiled2.eval()
|
||||
|
||||
Assert.assertEquals(133, compiled3.eval())
|
||||
Assert.assertEquals(133, compiled3.eval())
|
||||
Assert.assertEquals(133, compiled3.eval())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEvalWithContext() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
|
||||
engine.put("z", 33)
|
||||
|
||||
engine.eval("""val x = 10 + bindings["z"] as Int""")
|
||||
|
||||
val result = engine.eval("""x + 20""")
|
||||
Assert.assertEquals(63, result)
|
||||
|
||||
// in the current implementation the history is shared between contexts, so "x" could also be used in this line,
|
||||
// but this behaviour probably will not be preserved in the future, since contexts may become completely isolated
|
||||
val result2 = engine.eval("""11 + bindings["boundValue"] as Int""", engine.createBindings().apply {
|
||||
put("boundValue", 100)
|
||||
})
|
||||
Assert.assertEquals(111, result2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleEvalInEval() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
val res1 = engine.eval("val x = 3")
|
||||
Assert.assertNull(res1)
|
||||
val res2 = engine.eval("val y = eval(\"\$x + 2\") as Int\ny")
|
||||
Assert.assertEquals(5, res2)
|
||||
val res3 = engine.eval("y + 2")
|
||||
Assert.assertEquals(7, res3)
|
||||
}
|
||||
|
||||
// Note: the test is flaky, because it is statistical and the thresholds are not big enough.
|
||||
// Therefore it was decided to disable it, but leave in the code in order to be able to quickly check overheads when needed.
|
||||
// @Test
|
||||
fun testEvalInEvalBench() {
|
||||
val engine = ScriptEngineManager().getEngineByExtension("kts")!!
|
||||
|
||||
val script = "val x = 3\nx + 10"
|
||||
|
||||
val mxBeans = ManagementFactory.getThreadMXBean()
|
||||
|
||||
engine.eval("val bnd = createBindings()")
|
||||
|
||||
val times = generateSequence {
|
||||
val t0 = mxBeans.threadCpuTime()
|
||||
|
||||
engine.eval(script)
|
||||
val t1 = mxBeans.threadCpuTime()
|
||||
|
||||
engine.eval("eval(\"\"\"$script\"\"\", bnd)")
|
||||
val t2 = mxBeans.threadCpuTime()
|
||||
|
||||
Triple(t1 - t0, t2 - t1, t2 - t1)
|
||||
}.take(10).toList()
|
||||
|
||||
val adjustedMaxDiff = times.sortedByDescending { (_, _, diff) -> diff }.drop(2).first()
|
||||
|
||||
fun Long.ms() = TimeUnit.NANOSECONDS.toMillis(this)
|
||||
Assert.assertTrue("eval in eval is too long: ${times.joinToString { "(${it.first.ms()}, ${it.second.ms()})" }} (expecting no more than 5x difference)",
|
||||
adjustedMaxDiff.third < 20 /* assuming it measurement error */ || adjustedMaxDiff.first * 5 > adjustedMaxDiff.second )
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `kotlin script evaluation should support functional return types`() {
|
||||
val scriptEngine = KotlinJsr223JvmLocalScriptEngineFactory().scriptEngine
|
||||
|
||||
val script = "{1 + 2}"
|
||||
val result = scriptEngine.eval(script)
|
||||
|
||||
Assert.assertTrue(result is Function0<*>)
|
||||
Assert.assertEquals(3, (result as Function0<*>).invoke())
|
||||
}
|
||||
}
|
||||
|
||||
fun assertThrows(exceptionClass: Class<*>, body: () -> Unit) {
|
||||
try {
|
||||
body()
|
||||
Assert.fail("Expecting an exception of type ${exceptionClass.name}")
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
if (!exceptionClass.isAssignableFrom(e.javaClass)) {
|
||||
Assert.fail("Expecting an exception of type ${exceptionClass.name} but got ${e.javaClass.name}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ plugins {
|
||||
dependencies {
|
||||
api(project(":examples:scripting-jvm-simple-script"))
|
||||
api(project(":kotlin-scripting-jvm-host-unshaded"))
|
||||
api(project(":kotlin-script-util"))
|
||||
testRuntimeOnly(project(":kotlin-compiler"))
|
||||
testRuntimeOnly(project(":kotlin-scripting-compiler"))
|
||||
testApi(commonDependency("junit"))
|
||||
|
||||
+1
@@ -35,6 +35,7 @@ import kotlin.reflect.KClass
|
||||
|
||||
// TODO: need to manage resources here, i.e. call replCompiler.dispose when engine is collected
|
||||
|
||||
@Deprecated("Use kotlin-scripting-jsr223 instead")
|
||||
class KotlinJsr223JvmDaemonCompileScriptEngine(
|
||||
factory: ScriptEngineFactory,
|
||||
compilerClasspath: List<File>,
|
||||
|
||||
+1
@@ -38,6 +38,7 @@ import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@Deprecated("Use kotlin-scripting-jsr223 instead")
|
||||
class KotlinJsr223JvmLocalScriptEngine(
|
||||
factory: ScriptEngineFactory,
|
||||
val templateClasspath: List<File>,
|
||||
|
||||
+3
-1
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
@file:Suppress("unused") // could be used externally in javax.script.ScriptEngineFactory META-INF file
|
||||
@file:Suppress("unused", "DEPRECATION") // could be used externally in javax.script.ScriptEngineFactory META-INF file
|
||||
|
||||
package org.jetbrains.kotlin.script.jsr223
|
||||
|
||||
@@ -26,6 +26,7 @@ import javax.script.ScriptEngine
|
||||
import kotlin.script.experimental.jvm.util.KotlinJars
|
||||
import kotlin.script.experimental.jvm.util.scriptCompilationClasspathFromContext
|
||||
|
||||
@Deprecated("Use kotlin-scripting-jsr223 instead")
|
||||
class KotlinJsr223JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
@@ -38,6 +39,7 @@ class KotlinJsr223JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFacto
|
||||
)
|
||||
}
|
||||
|
||||
@Deprecated("Use kotlin-scripting-jsr223 instead")
|
||||
class KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
|
||||
+1
@@ -9,6 +9,7 @@ import kotlin.script.templates.standard.ScriptTemplateWithBindings
|
||||
|
||||
@Suppress("unused")
|
||||
@ScriptTemplateDefinition
|
||||
@Deprecated("Use kotlin-scripting-jsr223 instead")
|
||||
abstract class KotlinStandardJsr223ScriptTemplate(val jsr223Bindings: Bindings) : ScriptTemplateWithBindings(jsr223Bindings) {
|
||||
|
||||
private val myEngine: ScriptEngine? get() = bindings[KOTLIN_SCRIPT_ENGINE_BINDINGS_KEY]?.let { it as? ScriptEngine }
|
||||
|
||||
@@ -226,8 +226,6 @@ include ":kotlin-imports-dumper-compiler-plugin",
|
||||
":kotlin-annotation-processing-runtime",
|
||||
":kotlin-annotation-processing-embeddable",
|
||||
":kotlin-daemon-embeddable",
|
||||
":examples:kotlin-jsr223-local-example",
|
||||
":examples:kotlin-jsr223-daemon-local-eval-example",
|
||||
":kotlin-annotations-jvm",
|
||||
":kotlin-scripting-common",
|
||||
":kotlin-scripting-jvm",
|
||||
@@ -751,8 +749,6 @@ project(':kotlin-annotation-processing').projectDir = "$rootDir/plugins/kapt3/ka
|
||||
project(':kotlin-annotation-processing-cli').projectDir = "$rootDir/plugins/kapt3/kapt3-cli" as File
|
||||
project(':kotlin-annotation-processing-base').projectDir = "$rootDir/plugins/kapt3/kapt3-base" as File
|
||||
project(':kotlin-annotation-processing-runtime').projectDir = "$rootDir/plugins/kapt3/kapt3-runtime" as File
|
||||
project(':examples:kotlin-jsr223-local-example').projectDir = "$rootDir/libraries/examples/kotlin-jsr223-local-example" as File
|
||||
project(':examples:kotlin-jsr223-daemon-local-eval-example').projectDir = "$rootDir/libraries/examples/kotlin-jsr223-daemon-local-eval-example" as File
|
||||
project(':kotlin-annotations-jvm').projectDir = "$rootDir/libraries/tools/kotlin-annotations-jvm" as File
|
||||
project(':kotlin-scripting-common').projectDir = "$rootDir/libraries/scripting/common" as File
|
||||
project(':kotlin-scripting-jvm').projectDir = "$rootDir/libraries/scripting/jvm" as File
|
||||
|
||||
Reference in New Issue
Block a user