From f125600af1d7912153963802ec9445b49da2c691 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Mon, 28 Jan 2019 10:47:01 +0100 Subject: [PATCH] Restore returning of functional types from repl eval add tests for the broken case as well for the return value representation #KT-29490 fixed --- .../kotlin/cli/common/repl/GenericReplEvaluator.kt | 2 +- compiler/testData/repl/functionResult.repl | 5 +++++ .../kotlin/repl/ReplInterpreterTestGenerated.java | 5 +++++ .../script/jsr223/KotlinJsr223ScriptEngineIT.kt | 11 +++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/repl/functionResult.repl diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt index 7f8f79de8b1..162799db033 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/repl/GenericReplEvaluator.kt @@ -110,7 +110,7 @@ open class GenericReplEvaluator( val resultFieldName = scriptResultFieldName(compileResult.lineId.no) val resultField = scriptClass.getDeclaredField(resultFieldName).apply { isAccessible = true } - val resultValue: Any? = if (!compileResult.isFunctionType) resultField.get(scriptInstance) else "" + val resultValue: Any? = resultField.get(scriptInstance) return if (compileResult.hasResult) ReplEvalResult.ValueResult(resultFieldName, resultValue, compileResult.type) else ReplEvalResult.UnitResult() diff --git a/compiler/testData/repl/functionResult.repl b/compiler/testData/repl/functionResult.repl new file mode 100644 index 00000000000..bffe5f42ba4 --- /dev/null +++ b/compiler/testData/repl/functionResult.repl @@ -0,0 +1,5 @@ +>>> { 1 + 2 } +() -> kotlin.Int +>>> val x = { "abc" } +>>> x +() -> kotlin.String diff --git a/compiler/tests/org/jetbrains/kotlin/repl/ReplInterpreterTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/repl/ReplInterpreterTestGenerated.java index 3a05e9671aa..8f13c560035 100644 --- a/compiler/tests/org/jetbrains/kotlin/repl/ReplInterpreterTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/repl/ReplInterpreterTestGenerated.java @@ -84,6 +84,11 @@ public class ReplInterpreterTestGenerated extends AbstractReplInterpreterTest { runTest("compiler/testData/repl/functionReferencesPrev.repl"); } + @TestMetadata("functionResult.repl") + public void testFunctionResult() throws Exception { + runTest("compiler/testData/repl/functionResult.repl"); + } + @TestMetadata("imports.repl") public void testImports() throws Exception { runTest("compiler/testData/repl/imports.repl"); diff --git a/libraries/examples/kotlin-jsr223-local-example/src/test/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223ScriptEngineIT.kt b/libraries/examples/kotlin-jsr223-local-example/src/test/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223ScriptEngineIT.kt index c2128c720e5..99d9d838a53 100644 --- a/libraries/examples/kotlin-jsr223-local-example/src/test/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223ScriptEngineIT.kt +++ b/libraries/examples/kotlin-jsr223-local-example/src/test/kotlin/org/jetbrains/kotlin/script/jsr223/KotlinJsr223ScriptEngineIT.kt @@ -233,6 +233,17 @@ obj 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) {