@@ -5,6 +5,10 @@ import jupyter.kotlin.DependsOn
5
5
import jupyter.kotlin.KotlinContext
6
6
import jupyter.kotlin.KotlinKernelHostProvider
7
7
import jupyter.kotlin.Repository
8
+ import kotlinx.coroutines.Dispatchers
9
+ import kotlinx.coroutines.GlobalScope
10
+ import kotlinx.coroutines.launch
11
+ import org.jetbrains.annotations.TestOnly
8
12
import org.jetbrains.kotlin.config.KotlinCompilerVersion
9
13
import org.jetbrains.kotlinx.jupyter.api.Code
10
14
import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
@@ -48,6 +52,7 @@ import org.jetbrains.kotlinx.jupyter.repl.CellExecutor
48
52
import org.jetbrains.kotlinx.jupyter.repl.CompletionResult
49
53
import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
50
54
import org.jetbrains.kotlinx.jupyter.repl.EvalResult
55
+ import org.jetbrains.kotlinx.jupyter.repl.EvalResultEx
51
56
import org.jetbrains.kotlinx.jupyter.repl.InternalEvaluator
52
57
import org.jetbrains.kotlinx.jupyter.repl.KotlinCompleter
53
58
import org.jetbrains.kotlinx.jupyter.repl.ListErrorsResult
@@ -120,7 +125,8 @@ interface ReplForJupyter {
120
125
121
126
suspend fun serializeVariables (cellId : Int , descriptorsState : Map <String , SerializedVariablesState >, callback : (SerializationReply ) -> Unit )
122
127
123
- suspend fun serializeVariables (topLevelVarName : String , descriptorsState : Map <String , SerializedVariablesState >, callback : (SerializationReply ) -> Unit )
128
+ suspend fun serializeVariables (topLevelVarName : String , descriptorsState : Map <String , SerializedVariablesState >, pathToDescriptor : List <String > = emptyList(),
129
+ callback : (SerializationReply ) -> Unit )
124
130
125
131
val homeDir: File ?
126
132
@@ -191,7 +197,7 @@ class ReplForJupyterImpl(
191
197
192
198
override val variablesSerializer = VariablesSerializer ()
193
199
194
- private val librariesScanner = LibrariesScanner (notebook)
200
+ val librariesScanner = LibrariesScanner (notebook)
195
201
private val resourcesProcessor = LibraryResourcesProcessorImpl ()
196
202
197
203
override var outputConfig
@@ -347,7 +353,7 @@ class ReplForJupyterImpl(
347
353
)
348
354
349
355
private var evalContextEnabled = false
350
- private fun withEvalContext (action : () -> EvalResult ): EvalResult {
356
+ private fun < T > withEvalContext (action : () -> T ): T {
351
357
return synchronized(this ) {
352
358
evalContextEnabled = true
353
359
try {
@@ -365,14 +371,14 @@ class ReplForJupyterImpl(
365
371
else context.compilationConfiguration.asSuccess()
366
372
}
367
373
368
- /* *
369
- * Used for debug purposes.
370
- * @see ReplCommand
371
- */
374
+ @TestOnly
375
+ @Suppress(" unused" )
372
376
private fun printVariables (isHtmlFormat : Boolean = false) = log.debug(
373
377
if (isHtmlFormat) notebook.variablesReportAsHTML() else notebook.variablesReport()
374
378
)
375
379
380
+ @TestOnly
381
+ @Suppress(" unused" )
376
382
private fun printUsagesInfo (cellId : Int , usedVariables : Set <String >? ) {
377
383
log.debug(buildString {
378
384
if (usedVariables == null || usedVariables.isEmpty()) {
@@ -386,7 +392,7 @@ class ReplForJupyterImpl(
386
392
})
387
393
}
388
394
389
- fun evalEx (code : Code , displayHandler : DisplayHandler ? , jupyterId : Int ): EvalResult {
395
+ fun evalEx (code : Code , displayHandler : DisplayHandler ? , jupyterId : Int ): EvalResultEx {
390
396
return withEvalContext {
391
397
rethrowAsLibraryException(LibraryProblemPart .BEFORE_CELL_CALLBACKS ) {
392
398
beforeCellExecution.forEach { executor.execute(it) }
@@ -426,8 +432,15 @@ class ReplForJupyterImpl(
426
432
// printUsagesInfo(jupyterId, cellVariables[jupyterId - 1])
427
433
val serializedData = variablesSerializer.serializeVariables(jupyterId - 1 , notebook.variablesState, notebook.unchangedVariables())
428
434
429
- EvalResult (
435
+ GlobalScope .launch(Dispatchers .Default ) {
436
+ variablesSerializer.tryValidateCache(jupyterId - 1 , notebook.cellVariables)
437
+ }
438
+
439
+ EvalResultEx (
430
440
result.result.value,
441
+ rendered,
442
+ result.scriptInstance,
443
+ result.result.name,
431
444
EvaluatedSnippetMetadata (newClasspath, compiledData, newImports, serializedData),
432
445
)
433
446
}
@@ -525,8 +538,9 @@ class ReplForJupyterImpl(
525
538
doWithLock(SerializationArgs (descriptorsState, cellId = cellId, callback = callback), serializationQueue, SerializationReply (cellId, descriptorsState), ::doSerializeVariables)
526
539
}
527
540
528
- override suspend fun serializeVariables (topLevelVarName : String , descriptorsState : Map <String , SerializedVariablesState >, callback : (SerializationReply ) -> Unit ) {
529
- doWithLock(SerializationArgs (descriptorsState, topLevelVarName = topLevelVarName, callback = callback), serializationQueue, SerializationReply (), ::doSerializeVariables)
541
+ override suspend fun serializeVariables (topLevelVarName : String , descriptorsState : Map <String , SerializedVariablesState >, pathToDescriptor : List <String >,
542
+ callback : (SerializationReply ) -> Unit ) {
543
+ doWithLock(SerializationArgs (descriptorsState, topLevelVarName = topLevelVarName, callback = callback, pathToDescriptor = pathToDescriptor), serializationQueue, SerializationReply (), ::doSerializeVariables)
530
544
}
531
545
532
546
private fun doSerializeVariables (args : SerializationArgs ): SerializationReply {
@@ -537,7 +551,7 @@ class ReplForJupyterImpl(
537
551
finalAns
538
552
}
539
553
args.descriptorsState.forEach { (name, state) ->
540
- resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1 , name, state)
554
+ resultMap[name] = variablesSerializer.doIncrementalSerialization(cellId - 1 , name, state, args.pathToDescriptor )
541
555
}
542
556
log.debug(" Serialization cellID: $cellId " )
543
557
log.debug(" Serialization answer: ${resultMap.entries.first().value.fieldDescriptor} " )
@@ -581,6 +595,7 @@ class ReplForJupyterImpl(
581
595
val descriptorsState : Map <String , SerializedVariablesState >,
582
596
var cellId : Int = -1 ,
583
597
val topLevelVarName : String = " " ,
598
+ val pathToDescriptor : List <String > = emptyList(),
584
599
override val callback : (SerializationReply ) -> Unit
585
600
) : LockQueueArgs<SerializationReply>
586
601
0 commit comments