@@ -2,7 +2,6 @@ package org.jetbrains.kotlinx.jupyter.api
22
33import java.lang.reflect.Field
44import kotlin.reflect.KProperty
5- import kotlin.reflect.KProperty1
65import kotlin.reflect.jvm.isAccessible
76
87interface VariableState {
@@ -34,16 +33,17 @@ data class VariableStateImpl(
3433 try {
3534 value.toString()
3635 } catch (e: Throwable ) {
36+ if (e is StackOverflowError ) {
37+ isRecursive = true
38+ }
3739 " ${value::class .simpleName} : [exception thrown: $e ]"
3840 }
3941 }
4042 }
4143 override var isRecursive: Boolean = false
42- private var isLargeForString: Boolean = false
4344
44- private val valCache = VariableStateCache <Result <Any ?>> (
45- {
46- oldValue, newValue ->
45+ private val valCache = VariableStateCache <Result <Any ?>>(
46+ { oldValue, newValue ->
4747 oldValue.getOrNull() != = newValue.getOrNull()
4848 },
4949 {
@@ -63,75 +63,50 @@ data class VariableStateImpl(
6363 }
6464 }
6565
66- override val stringValue: String? get() = stringCache.get ()
66+ override val stringValue: String? get() = stringCache.getOrNull ()
6767
6868 override val value: Result <Any ?> get() = valCache.get()
6969
7070 companion object {
71- private fun <T : KProperty <* >, R > T.asAccessible (action : (T ) -> R ): R {
71+ @SuppressWarnings(" DEPRECATED" )
72+ private fun <R > Field.asAccessible (action : (Field ) -> R ): R {
7273 val wasAccessible = isAccessible
7374 isAccessible = true
7475 val res = action(this )
7576 isAccessible = wasAccessible
7677 return res
7778 }
7879 }
79- private val customDelegate = DependentLazyDelegate {
80- fun getRecursiveObjectName (): String {
81- val kClassName = cachedValue.getOrNull()!! ::class .simpleName
82- return " $kClassName : recursive structure"
83- }
84- if (cachedValue.getOrNull() == null ) {
85- return @DependentLazyDelegate null
86- }
87- handleIfRecursiveStructure()
88- try {
89- cachedValue.getOrNull().toString()
90- isRecursive = false
91- isLargeForString = false
92- } catch (e: VirtualMachineError ) {
93- when (e) {
94- is StackOverflowError -> {
95- isRecursive = true
96- }
97- is OutOfMemoryError -> {
98- isLargeForString = true
99- }
100- else -> {
101- return @DependentLazyDelegate null
102- }
103- }
104- }
105- }
10680
107- private class VariableStateCache <T >(
108- val equalityChecker : (T , T ) -> Boolean = { x, y -> x == y },
109- val calculate : (T ? ) -> T
110- ) {
111- private var cachedVal: T ? = null
112- private var shouldRenew: Boolean = true
81+ private class VariableStateCache <T >(
82+ val equalityChecker : (T , T ) -> Boolean = { x, y -> x == y },
83+ val calculate : (T ? ) -> T
84+ ) {
85+ private var cachedVal: T ? = null
86+ private var shouldRenew: Boolean = true
11387
114- fun getOrNull (): T ? {
115- return if (shouldRenew) {
116- calculate(cachedVal).also {
117- cachedVal = it
118- shouldRenew = false
88+ fun getOrNull (): T ? {
89+ return if (shouldRenew) {
90+ calculate(cachedVal).also {
91+ cachedVal = it
92+ shouldRenew = false
93+ }
94+ } else {
95+ cachedVal
11996 }
120- } else {
121- cachedVal
12297 }
123- }
12498
125- fun get (): T = getOrNull()!!
99+ fun get (): T = getOrNull()!!
126100
127- fun update () {
128- shouldRenew = true
129- }
101+ fun update () {
102+ shouldRenew = true
103+ }
130104
131- fun forceUpdate (): Boolean {
132- val oldVal = getOrNull()
133- update()
134- val newVal = get()
135- return oldVal != null && equalityChecker(oldVal, newVal)
105+ fun forceUpdate (): Boolean {
106+ val oldVal = getOrNull()
107+ update()
108+ val newVal = get()
109+ return oldVal != null && equalityChecker(oldVal, newVal)
110+ }
136111 }
137112}
0 commit comments