diff --git a/libraries/scripting/jvm-host-test/test/kotlin/script/experimental/jvmhost/test/ReplTest.kt b/libraries/scripting/jvm-host-test/test/kotlin/script/experimental/jvmhost/test/ReplTest.kt index 0977f4392c0..5e7782dfbcd 100644 --- a/libraries/scripting/jvm-host-test/test/kotlin/script/experimental/jvmhost/test/ReplTest.kt +++ b/libraries/scripting/jvm-host-test/test/kotlin/script/experimental/jvmhost/test/ReplTest.kt @@ -113,12 +113,36 @@ class ReplTest : TestCase() { ) ) } + + @Test + fun testLongEval() { + checkEvaluateInRepl( + sequence { + var count = 0 + while (true) { + val prev = if (count == 0) "0" else "obj${count - 1}.prop${count - 1} + $count" + yield("object obj$count { val prop$count = $prev }; $prev") + count++ + } + }, + sequence { + var acc = 0 + var count = 0 + while (true) { + yield(acc) + acc += ++count + } + }, + limit = 100 + ) + } } fun evaluateInRepl( snippets: Sequence, compilationConfiguration: ScriptCompilationConfiguration = simpleScriptompilationConfiguration, - evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration + evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration, + limit: Int = 0 ): Sequence> { val replCompilerProxy = KJvmReplCompilerImpl(defaultJvmScriptingHostConfiguration) @@ -126,7 +150,8 @@ fun evaluateInRepl( val compilationHistory = BasicReplStageHistory() val replEvaluator = BasicJvmScriptEvaluator() var currentEvalConfig = evaluationConfiguration ?: ScriptEvaluationConfiguration() - return snippets.mapIndexed { snippetNo, snippetText -> + val snipetsLimited = if (limit == 0) snippets else snippets.take(limit) + return snipetsLimited.mapIndexed { snippetNo, snippetText -> val snippetSource = snippetText.toScriptSource("Line_$snippetNo.${compilationConfiguration[ScriptCompilationConfiguration.fileExtension]}") val snippetId = ReplSnippetIdImpl(snippetNo, 0, snippetSource) replCompilerProxy.compileReplSnippet(compilationState, snippetSource, snippetId, compilationHistory) @@ -154,10 +179,11 @@ fun checkEvaluateInReplDiags( snippets: Sequence, expected: Sequence>, compilationConfiguration: ScriptCompilationConfiguration = simpleScriptompilationConfiguration, - evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration + evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration, + limit: Int = 0 ) { - val expectedIter = expected.iterator() - evaluateInRepl(snippets, compilationConfiguration, evaluationConfiguration).forEachIndexed { index, res -> + val expectedIter = (if (limit == 0) expected else expected.take(limit)).iterator() + evaluateInRepl(snippets, compilationConfiguration, evaluationConfiguration, limit).forEachIndexed { index, res -> val expectedRes = expectedIter.next() when { res is ResultWithDiagnostics.Failure && expectedRes is ResultWithDiagnostics.Failure -> { @@ -187,16 +213,19 @@ fun checkEvaluateInReplDiags( } } } + if (expectedIter.hasNext()) { + Assert.fail("Expected ${expectedIter.next()} got end of results stream") + } } fun checkEvaluateInRepl( snippets: Sequence, expected: Sequence, compilationConfiguration: ScriptCompilationConfiguration = simpleScriptompilationConfiguration, - evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration + evaluationConfiguration: ScriptEvaluationConfiguration? = simpleScriptEvaluationConfiguration, + limit: Int = 0 ) = checkEvaluateInReplDiags( - snippets, expected.map { ResultWithDiagnostics.Success(it) }, compilationConfiguration, - evaluationConfiguration + snippets, expected.map { ResultWithDiagnostics.Success(it) }, compilationConfiguration, evaluationConfiguration, limit ) class TestReceiver(val prop1: Int = 3) diff --git a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/repl/ReplCodeAnalyzer.kt b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/repl/ReplCodeAnalyzer.kt index db2a75a2876..df5779ff3ea 100644 --- a/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/repl/ReplCodeAnalyzer.kt +++ b/plugins/scripting/scripting-compiler/src/org/jetbrains/kotlin/scripting/compiler/plugin/repl/ReplCodeAnalyzer.kt @@ -164,7 +164,11 @@ class ReplCodeAnalyzer(environment: KotlinCoreEnvironment) { private var delegateProvider: PackageMemberDeclarationProvider ) : DelegatePackageMemberDeclarationProvider(delegateProvider) { fun addDelegateProvider(provider: PackageMemberDeclarationProvider) { - delegateProvider = CombinedPackageMemberDeclarationProvider(listOf(provider, delegateProvider)) + val combinedDelegateProvider = delegateProvider as? CombinedPackageMemberDeclarationProvider + val providers = + if (combinedDelegateProvider != null) listOf(provider) + combinedDelegateProvider.providers + else listOf(provider, delegateProvider) + delegateProvider = CombinedPackageMemberDeclarationProvider(providers) delegate = delegateProvider }