Skip to content

Commit 9c7c59e

Browse files
Get unchangedVariables from notebook to reflect after execution state properly
1 parent d269070 commit 9c7c59e

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ internal class InternalEvaluatorImpl(
5252
return variablesWatcher.findDeclarationAddress(variableName) ?: -1
5353
}
5454

55+
override fun getUnchangedVariables(): Set<String> {
56+
return variablesWatcher.getUnchangedVariables()
57+
}
58+
5559
override var writeCompiledClasses: Boolean
5660
get() = classWriter != null
5761
set(value) {
@@ -168,7 +172,6 @@ internal class InternalEvaluatorImpl(
168172
}.toHashSet()
169173
val ans = mutableMapOf<String, VariableStateImpl>()
170174
fields.forEach { property ->
171-
// if (property.name.startsWith("script$")) return@forEach
172175
if (!memberKPropertiesNames.contains(property.name)) return@forEach
173176

174177
val state = VariableStateImpl(property, cellClassInstance)

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class VariablesUsagesPerCellWatcher<K : Any, V : Any> {
8383
*/
8484
private val variablesDeclarationInfo: MutableMap<V, K> = mutableMapOf()
8585

86+
private val unchangedVariables: MutableSet<V> = mutableSetOf()
87+
// private val unchangedVariables: MutableSet<V> = mutableSetOf()
88+
8689
fun addDeclaration(address: K, variableRef: V) {
8790
ensureStorageCreation(address)
8891

@@ -91,21 +94,35 @@ class VariablesUsagesPerCellWatcher<K : Any, V : Any> {
9194
val oldCellId = variablesDeclarationInfo[variableRef]
9295
if (oldCellId != address) {
9396
cellVariables[oldCellId]?.remove(variableRef)
97+
unchangedVariables.remove(variableRef)
9498
}
99+
} else {
100+
unchangedVariables.add(variableRef)
95101
}
96102
variablesDeclarationInfo[variableRef] = address
97103
cellVariables[address]?.add(variableRef)
98104
}
99105

100-
fun addUsage(address: K, variableRef: V) = cellVariables[address]?.add(variableRef)
106+
fun addUsage(address: K, variableRef: V) {
107+
cellVariables[address]?.add(variableRef)
108+
if (variablesDeclarationInfo[variableRef] != address) {
109+
unchangedVariables.remove(variableRef)
110+
}
111+
}
101112

102113
fun removeOldUsages(newAddress: K) {
103114
// remove known modifying usages in this cell
104115
cellVariables[newAddress]?.removeIf {
105-
variablesDeclarationInfo[it] != newAddress
116+
val predicate = variablesDeclarationInfo[it] != newAddress
117+
if (predicate) {
118+
unchangedVariables.add(it)
119+
}
120+
predicate
106121
}
107122
}
108123

124+
fun getUnchangedVariables(): Set<V> = unchangedVariables
125+
109126
fun findDeclarationAddress(variableRef: V) = variablesDeclarationInfo[variableRef]
110127

111128
fun ensureStorageCreation(address: K) = cellVariables.putIfAbsent(address, mutableSetOf())

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlin.script.experimental.api.SourceCode
2424
import kotlin.test.assertEquals
2525
import kotlin.test.assertFails
2626
import kotlin.test.assertFalse
27+
import kotlin.test.assertNotEquals
2728
import kotlin.test.fail
2829

2930
class ReplTests : AbstractSingleReplTest() {
@@ -1029,4 +1030,43 @@ class ReplVarsSerializationTest : AbstractSingleReplTest() {
10291030
}
10301031
}
10311032
}
1033+
1034+
1035+
@Test
1036+
fun testUnchangedVariables() {
1037+
eval(
1038+
"""
1039+
private val x = "abcd"
1040+
var f = 47
1041+
internal val z = 47
1042+
""".trimIndent(),
1043+
jupyterId = 1
1044+
)
1045+
val state = repl.notebook.unchangedVariables()
1046+
val setOfCell = setOf("x", "f", "z")
1047+
assertTrue(state.isNotEmpty())
1048+
assertEquals(setOfCell, state)
1049+
1050+
eval(
1051+
"""
1052+
private val x = 341
1053+
f += x
1054+
protected val z = "abcd"
1055+
""".trimIndent(),
1056+
jupyterId = 2
1057+
)
1058+
assertTrue(state.isEmpty())
1059+
val setOfPrevCell = setOf("f")
1060+
assertNotEquals(setOfCell, setOfPrevCell)
1061+
1062+
eval(
1063+
"""
1064+
private val x = 341
1065+
protected val z = "abcd"
1066+
""".trimIndent(),
1067+
jupyterId = 2
1068+
)
1069+
assertTrue(state.isNotEmpty())
1070+
assertEquals(state, setOfPrevCell)
1071+
}
10321072
}

0 commit comments

Comments
 (0)