From 063fbed75e716f86ae401309607c455531922450 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Fri, 23 Sep 2016 13:30:07 +0200 Subject: [PATCH] Fix params handling in JSR 223 for Idea, add simple test --- .../KotlinJvmJsr223ScriptEngine4Idea.kt | 6 ++- ...mJsr223StandardScriptEngineFactory4Idea.kt | 4 +- .../kotlin/idea/repl/IdeaJsr223Test.kt | 45 +++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/repl/IdeaJsr223Test.kt diff --git a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223ScriptEngine4Idea.kt b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223ScriptEngine4Idea.kt index 4e0fc751bd5..35ddd88bf59 100644 --- a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223ScriptEngine4Idea.kt +++ b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223ScriptEngine4Idea.kt @@ -33,7 +33,9 @@ class KotlinJvmJsr223ScriptEngine4Idea( disposable: Disposable, private val factory: ScriptEngineFactory, templateClasspath: List, - templateClassName: String + templateClassName: String, + scriptArgs: Array?, + scriptArgsTypes: Array>? ) : AbstractScriptEngine(), ScriptEngine { private val daemon by lazy { @@ -61,7 +63,7 @@ class KotlinJvmJsr223ScriptEngine4Idea( } } - val localEvaluator by lazy { GenericReplCompiledEvaluator(emptyList(), Thread.currentThread().contextClassLoader) } + val localEvaluator by lazy { GenericReplCompiledEvaluator(emptyList(), Thread.currentThread().contextClassLoader, scriptArgs, scriptArgsTypes) } private var lineCount = 0 diff --git a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223StandardScriptEngineFactory4Idea.kt b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223StandardScriptEngineFactory4Idea.kt index 200676a1575..d242d54740c 100644 --- a/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223StandardScriptEngineFactory4Idea.kt +++ b/idea/idea-repl/src/org/jetbrains/kotlin/jsr223/KotlinJvmJsr223StandardScriptEngineFactory4Idea.kt @@ -38,7 +38,9 @@ class KotlinJvmJsr223StandardScriptEngineFactory4Idea : ScriptEngineFactory { Disposer.newDisposable(), this, listOf(PathUtil.getKotlinPathsForIdeaPlugin().runtimePath), - "kotlin.script.StandardScriptTemplate") + "kotlin.script.StandardScriptTemplate", + arrayOf(emptyArray()), + null) override fun getOutputStatement(toDisplay: String?): String = "print(\"$toDisplay\")" override fun getMethodCallSyntax(obj: String, m: String, vararg args: String): String = "$obj.$m(${args.joinToString()})" diff --git a/idea/tests/org/jetbrains/kotlin/idea/repl/IdeaJsr223Test.kt b/idea/tests/org/jetbrains/kotlin/idea/repl/IdeaJsr223Test.kt new file mode 100644 index 00000000000..605cdce99a8 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/repl/IdeaJsr223Test.kt @@ -0,0 +1,45 @@ +/* + * 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.idea.repl + +import com.intellij.testFramework.PlatformTestCase +import org.junit.Test +import javax.script.ScriptEngineManager +import javax.script.ScriptException +import kotlin.test.assertFails + +class IdeaJsr223Test : PlatformTestCase() { + + @Test + fun testJsr223Engine() { + val semgr = ScriptEngineManager() + + val engine = semgr.getEngineByName("kotlin") + + assertNotNull(engine) + + val res0 = assertFails { engine.eval("val x =") } + assertTrue("Unexpected check results: $res0", (res0 as? ScriptException)?.message?.contains("incomplete code") ?: false) + + val res1 = engine.eval("val x = 5") + assertNull("Unexpected eval result: $res1", res1) + + val res2 = engine.eval("x + 2") + assertEquals(7, res2) + } + +}