Skip to content

Commit 5da4e1b

Browse files
Add variable serializer to repl state; fixed some tests
1 parent 930c8ce commit 5da4e1b

File tree

5 files changed

+294
-146
lines changed

5 files changed

+294
-146
lines changed

jupyter-lib/shared-compiler/src/main/kotlin/org/jetbrains/kotlinx/jupyter/compiler/util/serializedCompiledScript.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class EvaluatedSnippetMetadata(
3636
val newClasspath: Classpath = emptyList(),
3737
val compiledData: SerializedCompiledScriptsData = SerializedCompiledScriptsData.EMPTY,
3838
val newImports: List<String> = emptyList(),
39-
val evaluatedVariablesState: SerializedVariablesState = SerializedVariablesState()
39+
val evaluatedVariablesState: Map<String, SerializedVariablesState> = emptyMap()
4040
) {
4141
companion object {
4242
val EMPTY = EvaluatedSnippetMetadata()

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import jupyter.kotlin.DependsOn
55
import jupyter.kotlin.KotlinContext
66
import jupyter.kotlin.KotlinKernelHostProvider
77
import jupyter.kotlin.Repository
8-
import org.jetbrains.annotations.TestOnly
98
import org.jetbrains.kotlin.config.KotlinCompilerVersion
109
import org.jetbrains.kotlinx.jupyter.api.Code
1110
import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
@@ -27,7 +26,6 @@ import org.jetbrains.kotlinx.jupyter.compiler.ScriptImportsCollector
2726
import org.jetbrains.kotlinx.jupyter.compiler.util.Classpath
2827
import org.jetbrains.kotlinx.jupyter.compiler.util.EvaluatedSnippetMetadata
2928
import org.jetbrains.kotlinx.jupyter.compiler.util.SerializedCompiledScriptsData
30-
import org.jetbrains.kotlinx.jupyter.compiler.util.SerializedVariablesState
3129
import org.jetbrains.kotlinx.jupyter.config.catchAll
3230
import org.jetbrains.kotlinx.jupyter.config.getCompilationConfiguration
3331
import org.jetbrains.kotlinx.jupyter.dependencies.JupyterScriptDependenciesResolverImpl
@@ -49,7 +47,6 @@ import org.jetbrains.kotlinx.jupyter.repl.CellExecutor
4947
import org.jetbrains.kotlinx.jupyter.repl.CompletionResult
5048
import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
5149
import org.jetbrains.kotlinx.jupyter.repl.EvalResult
52-
import org.jetbrains.kotlinx.jupyter.repl.EvalResultEx
5350
import org.jetbrains.kotlinx.jupyter.repl.InternalEvaluator
5451
import org.jetbrains.kotlinx.jupyter.repl.KotlinCompleter
5552
import org.jetbrains.kotlinx.jupyter.repl.ListErrorsResult
@@ -134,6 +131,8 @@ interface ReplForJupyter {
134131

135132
val notebook: NotebookImpl
136133

134+
val variablesSerializer: VariablesSerializer
135+
137136
val fileExtension: String
138137

139138
val isEmbedded: Boolean
@@ -185,7 +184,9 @@ class ReplForJupyterImpl(
185184

186185
override val notebook = NotebookImpl(runtimeProperties)
187186

188-
val librariesScanner = LibrariesScanner(notebook)
187+
override val variablesSerializer = VariablesSerializer()
188+
189+
private val librariesScanner = LibrariesScanner(notebook)
189190
private val resourcesProcessor = LibraryResourcesProcessorImpl()
190191

191192
override var outputConfig
@@ -341,7 +342,7 @@ class ReplForJupyterImpl(
341342
)
342343

343344
private var evalContextEnabled = false
344-
private fun <T> withEvalContext(action: () -> T): T {
345+
private fun withEvalContext(action: () -> EvalResult): EvalResult {
345346
return synchronized(this) {
346347
evalContextEnabled = true
347348
try {
@@ -359,14 +360,14 @@ class ReplForJupyterImpl(
359360
else context.compilationConfiguration.asSuccess()
360361
}
361362

362-
@TestOnly
363-
@Suppress("unused")
363+
/**
364+
* Used for debug purposes.
365+
* @see ReplCommand
366+
*/
364367
private fun printVariables(isHtmlFormat: Boolean = false) = log.debug(
365368
if (isHtmlFormat) notebook.variablesReportAsHTML() else notebook.variablesReport()
366369
)
367370

368-
@TestOnly
369-
@Suppress("unused")
370371
private fun printUsagesInfo(cellId: Int, usedVariables: Set<String>?) {
371372
log.debug(buildString {
372373
if (usedVariables == null || usedVariables.isEmpty()) {
@@ -380,7 +381,7 @@ class ReplForJupyterImpl(
380381
})
381382
}
382383

383-
fun evalEx(code: Code, displayHandler: DisplayHandler?, jupyterId: Int): EvalResultEx {
384+
fun evalEx(code: Code, displayHandler: DisplayHandler?, jupyterId: Int): EvalResult {
384385
return withEvalContext {
385386
rethrowAsLibraryException(LibraryProblemPart.BEFORE_CELL_CALLBACKS) {
386387
beforeCellExecution.forEach { executor.execute(it) }
@@ -418,15 +419,13 @@ class ReplForJupyterImpl(
418419
notebook.updateVariablesState(internalEvaluator)
419420
// printVars()
420421
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
422+
val entry = notebook.variablesState.entries.lastOrNull()
423+
val serializedVarsState = variablesSerializer.serializeVariableState(jupyterId - 1, entry?.key, entry?.value)
424+
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1, notebook.variablesState)
421425

422-
423-
val variablesStateUpdate = notebook.variablesState.mapValues { it.value.stringValue }
424-
EvalResultEx(
426+
EvalResult(
425427
result.result.value,
426-
rendered,
427-
result.scriptInstance,
428-
result.result.name,
429-
EvaluatedSnippetMetadata(newClasspath, compiledData, newImports, SerializedVariablesState()),
428+
EvaluatedSnippetMetadata(newClasspath, compiledData, newImports, serializedData),
430429
)
431430
}
432431
}

0 commit comments

Comments
 (0)