From 6c91b77a85b61b92d0ff726f42b118650a217f68 Mon Sep 17 00:00:00 2001 From: sergeych Date: Tue, 21 Apr 2026 16:43:02 +0300 Subject: [PATCH] Use fast lambda calls in scope facade --- .../kotlin/net/sergeych/lyng/ScopeFacade.kt | 3 +++ .../kotlin/CompilerVmReviewRegressionTest.kt | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt index 02fef09..1bd1ad4 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/ScopeFacade.kt @@ -66,6 +66,9 @@ internal class ScopeBridge(internal val scope: Scope) : ScopeFacade { override fun raiseIllegalState(message: String): Nothing = scope.raiseIllegalState(message) override fun raiseNotImplemented(what: String): Nothing = scope.raiseNotImplemented(what) override suspend fun call(callee: Obj, args: Arguments, newThisObj: Obj?): Obj { + if (newThisObj == null) { + (callee as? BytecodeArgCallable)?.callWithArgsFast(scope, args)?.let { return it } + } val child = scope.createChildScope(scope.pos, args = args, newThisObj = newThisObj) return (callee as? BytecodeCallable)?.callOnFast(child) ?: callee.callOn(child) } diff --git a/lynglib/src/commonTest/kotlin/CompilerVmReviewRegressionTest.kt b/lynglib/src/commonTest/kotlin/CompilerVmReviewRegressionTest.kt index f3ca041..5641b56 100644 --- a/lynglib/src/commonTest/kotlin/CompilerVmReviewRegressionTest.kt +++ b/lynglib/src/commonTest/kotlin/CompilerVmReviewRegressionTest.kt @@ -16,11 +16,13 @@ import kotlinx.coroutines.test.runTest import net.sergeych.lyng.Compiler +import net.sergeych.lyng.Arguments import net.sergeych.lyng.Scope import net.sergeych.lyng.Script import net.sergeych.lyng.ScriptError import net.sergeych.lyng.Source import net.sergeych.lyng.asFacade +import net.sergeych.lyng.obj.ObjInt import net.sergeych.lyng.obj.ObjString import net.sergeych.lyng.obj.toInt import net.sergeych.lyng.pacman.ImportManager @@ -68,6 +70,24 @@ class CompilerVmReviewRegressionTest { assertContains(ex.errorMessage, "module binding 'answer'") } + @Test + fun facadeCallUsesPreparedLambdaWithArgs() = runTest { + val lambda = Compiler.compile( + Source( + "", + """ + val base = 2 + { x -> x + base } + """.trimIndent() + ), + Script.defaultImportManager + ) + + val scope = Script.newScope() + val callable = lambda.execute(scope) + assertEquals(42, scope.asFacade().call(callable, Arguments(ObjInt.of(40))).toInt()) + } + @Test fun subjectlessWhenReportsScriptError() = runTest { val ex = assertFailsWith {