From 07363f2904460a02a6336647859cf661af7e57b4 Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Fri, 1 Dec 2017 18:06:18 +0300 Subject: [PATCH] REPL, Refactoring: Replace strange callback function with an execution interceptor --- .../kotlin/cli/jvm/repl/ReplConfiguration.kt | 35 ++++++++++--------- .../kotlin/cli/jvm/repl/ReplInterpreter.kt | 12 +------ .../jvm/repl/SnippetExecutionInterceptor.kt | 25 +++++++++++++ 3 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt index dd917885a80..b85dbf66edd 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplConfiguration.kt @@ -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 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() - } } \ No newline at end of file diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt index ff1326c4454..806c66c0cad 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/ReplInterpreter.kt @@ -92,7 +92,7 @@ class ReplInterpreter( try { val evalRes = scriptEvaluator.compileAndEval(evalState, ReplCodeLine(lineNumber.getAndIncrement(), 0, fullText), null, object : InvokeWrapper { - override fun invoke(body: () -> T): T = executeUserCode { body() } + override fun invoke(body: () -> T): T = executionInterceptor.execute(body) }) when { @@ -110,16 +110,6 @@ class ReplInterpreter( } } - private fun executeUserCode(body: () -> T): T { - try { - onUserCodeExecuting(true) - return body() - } - finally { - onUserCodeExecuting(false) - } - } - fun dumpClasses(out: PrintWriter) { classLoader.dumpClasses(out) } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt new file mode 100644 index 00000000000..79811a2e0c1 --- /dev/null +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/repl/SnippetExecutionInterceptor.kt @@ -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 execute(block: () -> T): T + + companion object Plain : SnippetExecutionInterceptor { + override fun execute(block: () -> T) = block() + } +} \ No newline at end of file