@@ -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,20 +33,21 @@ 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 {
50- property.asAccessible { prop ->
50+ property.asAccessible(scriptInstance) { prop ->
5151 try {
5252 Result .success(prop.get(scriptInstance))
5353 } catch (ex: Throwable ) {
@@ -63,75 +63,49 @@ 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 {
72- val wasAccessible = isAccessible
71+ private fun <R > Field .asAccessible (instance : Any , action : (Field ) -> R ): R {
72+ val wasAccessible = this .canAccess(instance)
7373 isAccessible = true
7474 val res = action(this )
7575 isAccessible = wasAccessible
7676 return res
7777 }
7878 }
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- }
10679
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
80+ private class VariableStateCache <T >(
81+ val equalityChecker : (T , T ) -> Boolean = { x, y -> x == y },
82+ val calculate : (T ? ) -> T
83+ ) {
84+ private var cachedVal: T ? = null
85+ private var shouldRenew: Boolean = true
11386
114- fun getOrNull (): T ? {
115- return if (shouldRenew) {
116- calculate(cachedVal).also {
117- cachedVal = it
118- shouldRenew = false
87+ fun getOrNull (): T ? {
88+ return if (shouldRenew) {
89+ calculate(cachedVal).also {
90+ cachedVal = it
91+ shouldRenew = false
92+ }
93+ } else {
94+ cachedVal
11995 }
120- } else {
121- cachedVal
12296 }
123- }
12497
125- fun get (): T = getOrNull()!!
98+ fun get (): T = getOrNull()!!
12699
127- fun update () {
128- shouldRenew = true
129- }
100+ fun update () {
101+ shouldRenew = true
102+ }
130103
131- fun forceUpdate (): Boolean {
132- val oldVal = getOrNull()
133- update()
134- val newVal = get()
135- return oldVal != null && equalityChecker(oldVal, newVal)
104+ fun forceUpdate (): Boolean {
105+ val oldVal = getOrNull()
106+ update()
107+ val newVal = get()
108+ return oldVal != null && equalityChecker(oldVal, newVal)
109+ }
136110 }
137111}
0 commit comments