Scripting: fix JSR-223 invocable handling of receivers

This commit is contained in:
Ilya Chernikov
2022-04-22 18:48:52 +02:00
committed by teamcity
parent 49902bb851
commit f7fb586ee5
2 changed files with 12 additions and 14 deletions
@@ -174,26 +174,27 @@ class KotlinJsr223ScriptEngineIT {
@Test @Test
fun testInvocable() { fun testInvocable() {
val engine = ScriptEngineManager().getEngineByExtension("kts")!! val engine = ScriptEngineManager().getEngineByExtension("kts")!!
val res1 = engine.eval(""" val res0 = engine.eval("""
fun fn(x: Int) = x + 2 fun fn(x: Int) = x + 2
val obj = object { val obj = object {
fun fn1(x: Int) = x + 3 fun fn1(x: Int) = x + 3
} }
obj obj
""") """)
Assert.assertNotNull(res1) Assert.assertNotNull(res0)
val invocator = engine as? Invocable val invocator = engine as? Invocable
Assert.assertNotNull(invocator) Assert.assertNotNull(invocator)
val res1 = invocator!!.invokeFunction("fn", 6)
Assert.assertEquals(8, res1)
assertThrows(NoSuchMethodException::class.java) { assertThrows(NoSuchMethodException::class.java) {
invocator!!.invokeFunction("fn1", 3) invocator.invokeFunction("fn1", 3)
} }
val res2 = invocator!!.invokeFunction("fn", 3) val res2 = invocator.invokeFunction("fn", 3)
Assert.assertEquals(5, res2) Assert.assertEquals(5, res2)
// TODO: fix and restore assertThrows(NoSuchMethodException::class.java) {
// assertThrows(NoSuchMethodException::class.java) { invocator.invokeMethod(res0, "fn", 3)
// invocator!!.invokeMethod(res1, "fn", 3) }
// } val res3 = invocator.invokeMethod(res0, "fn1", 3)
val res3 = invocator.invokeMethod(res1, "fn1", 3)
Assert.assertEquals(6, res3) Assert.assertEquals(6, res3)
} }
@@ -24,9 +24,6 @@ interface KotlinJsr223InvocableScriptEngine : Invocable {
val baseClassLoader: ClassLoader val baseClassLoader: ClassLoader
fun instancesForInvokeSearch(requestedReceiver: Any) =
sequenceOf(requestedReceiver) + backwardInstancesHistory.filterNot { it == requestedReceiver }
override fun invokeFunction(name: String?, vararg args: Any?): Any? { override fun invokeFunction(name: String?, vararg args: Any?): Any? {
if (name == null) throw AssertionError("function name cannot be null") if (name == null) throw AssertionError("function name cannot be null")
return invokeImpl(backwardInstancesHistory, name, args) return invokeImpl(backwardInstancesHistory, name, args)
@@ -35,7 +32,7 @@ interface KotlinJsr223InvocableScriptEngine : Invocable {
override fun invokeMethod(thiz: Any?, name: String?, vararg args: Any?): Any? { override fun invokeMethod(thiz: Any?, name: String?, vararg args: Any?): Any? {
if (name == null) throw java.lang.NullPointerException("method name cannot be null") if (name == null) throw java.lang.NullPointerException("method name cannot be null")
if (thiz == null) throw IllegalArgumentException("cannot invoke method on the null object") if (thiz == null) throw IllegalArgumentException("cannot invoke method on the null object")
return invokeImpl(instancesForInvokeSearch(thiz), name, args) return invokeImpl(sequenceOf(thiz), name, args)
} }
private fun invokeImpl(possibleReceivers: Sequence<Any>, name: String, args: Array<out Any?>): Any? { private fun invokeImpl(possibleReceivers: Sequence<Any>, name: String, args: Array<out Any?>): Any? {
@@ -68,7 +65,7 @@ interface KotlinJsr223InvocableScriptEngine : Invocable {
override fun <T : Any> getInterface(thiz: Any?, clasz: Class<T>?): T? { override fun <T : Any> getInterface(thiz: Any?, clasz: Class<T>?): T? {
if (thiz == null) throw IllegalArgumentException("object cannot be null") if (thiz == null) throw IllegalArgumentException("object cannot be null")
return proxyInterface(instancesForInvokeSearch(thiz), clasz) return proxyInterface(sequenceOf(thiz), clasz)
} }
private fun <T : Any> proxyInterface(possibleReceivers: Sequence<Any>, clasz: Class<T>?): T? { private fun <T : Any> proxyInterface(possibleReceivers: Sequence<Any>, clasz: Class<T>?): T? {