REPL, Refactoring: Replace strange callback function with an execution interceptor

This commit is contained in:
Yan Zhulanow
2017-12-01 18:06:18 +03:00
parent b35676c1bd
commit 07363f2904
3 changed files with 45 additions and 27 deletions
@@ -27,16 +27,23 @@ interface ReplConfiguration {
val commandReader: ReplCommandReader
val allowIncompleteLines: Boolean
val executionInterceptor: SnippetExecutionInterceptor
fun createDiagnosticHolder(): DiagnosticMessageHolder
fun onUserCodeExecuting(isExecuting: Boolean)
}
class ReplForIdeConfiguration : ReplConfiguration {
override val allowIncompleteLines: Boolean
get() = false
override fun onUserCodeExecuting(isExecuting: Boolean) {
sinWrapper.isReplScriptExecuting = isExecuting
override val executionInterceptor: SnippetExecutionInterceptor = object : SnippetExecutionInterceptor {
override fun <T> execute(block: () -> T): T {
try {
sinWrapper.isReplScriptExecuting = true
return block()
} finally {
sinWrapper.isReplScriptExecuting = false
}
}
}
override fun createDiagnosticHolder() = ReplIdeDiagnosticMessageHolder()
@@ -66,22 +73,18 @@ class ReplForIdeConfiguration : ReplConfiguration {
}
class ConsoleReplConfiguration : ReplConfiguration {
override val writer = ReplConsoleWriter()
override val exceptionReporter
get() = ReplExceptionReporter.DoNothing
override val commandReader = ConsoleReplCommandReader()
override val allowIncompleteLines: Boolean
get() = true
override fun onUserCodeExecuting(isExecuting: Boolean) {
// do nothing
}
override val executionInterceptor
get() = SnippetExecutionInterceptor.Plain
override fun createDiagnosticHolder() = ReplTerminalDiagnosticMessageHolder()
override val writer: ReplWriter
override val exceptionReporter: ReplExceptionReporter
override val commandReader: ReplCommandReader
init {
writer = ReplConsoleWriter()
exceptionReporter = ReplExceptionReporter.DoNothing
commandReader = ConsoleReplCommandReader()
}
}
@@ -92,7 +92,7 @@ class ReplInterpreter(
try {
val evalRes = scriptEvaluator.compileAndEval(evalState, ReplCodeLine(lineNumber.getAndIncrement(), 0, fullText), null, object : InvokeWrapper {
override fun <T> invoke(body: () -> T): T = executeUserCode { body() }
override fun <T> invoke(body: () -> T): T = executionInterceptor.execute(body)
})
when {
@@ -110,16 +110,6 @@ class ReplInterpreter(
}
}
private fun <T> executeUserCode(body: () -> T): T {
try {
onUserCodeExecuting(true)
return body()
}
finally {
onUserCodeExecuting(false)
}
}
fun dumpClasses(out: PrintWriter) {
classLoader.dumpClasses(out)
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2017 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.cli.jvm.repl
interface SnippetExecutionInterceptor {
fun <T> execute(block: () -> T): T
companion object Plain : SnippetExecutionInterceptor {
override fun <T> execute(block: () -> T) = block()
}
}