Skip to content

Commit 6c60575

Browse files
Make RunTimeWrapper
1 parent e050cb6 commit 6c60575

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data class SerializedVariablesState(
2626
val value: String? = null,
2727
val isContainer: Boolean = false
2828
) {
29+
// todo: not null
2930
val fieldDescriptor: MutableMap<String, SerializedVariablesState?> = mutableMapOf()
3031
}
3132

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ enum class MessageType(val contentClass: KClass<out MessageContent>) {
8888
LIST_ERRORS_REQUEST(ListErrorsRequest::class),
8989
LIST_ERRORS_REPLY(ListErrorsReply::class);
9090

91+
// TODO: add custom commands
92+
// this custom message should be supported on client-side. either JS or Idea Plugin
93+
9194
val type: String
9295
get() = name.lowercase()
9396
}

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ data class ProcessedDescriptorsState(
2828
val instancesPerState: MutableMap<SerializedVariablesState, Any?> = mutableMapOf()
2929
)
3030

31+
// TODO: add as a key map
32+
data class RuntimeObjectWrapper(
33+
val objectInstance: Any?
34+
) {
35+
override fun equals(other: Any?): Boolean {
36+
if (other == null) return objectInstance == null
37+
if (objectInstance == null) return false
38+
if (other is RuntimeObjectWrapper) return objectInstance === other.objectInstance
39+
return objectInstance === other
40+
}
41+
42+
override fun hashCode(): Int {
43+
return objectInstance?.hashCode() ?: 0
44+
}
45+
}
46+
47+
fun Any?.toObjectWrapper(): RuntimeObjectWrapper = RuntimeObjectWrapper(this)
48+
3149
class VariablesSerializer(private val serializationStep: Int = 2, private val serializationLimit: Int = 10000) {
3250

3351
/**
@@ -36,7 +54,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
3654
* Second Key: actual value
3755
* Value: serialized VariableState
3856
*/
39-
private val seenObjectsPerCell: MutableMap<Int, MutableMap<Any, SerializedVariablesState>> = mutableMapOf()
57+
private val seenObjectsPerCell: MutableMap<Int, MutableMap<RuntimeObjectWrapper, SerializedVariablesState>> = mutableMapOf()
4058

4159
private var currentSerializeCount: Int = 0
4260

@@ -96,15 +114,15 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
96114

97115
private fun serializeVariableState(cellId: Int, name: String, property: Field?, value: Any?, isOverride: Boolean = true): SerializedVariablesState {
98116
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
99-
return doActualSerialization(cellId, processedData, value, isOverride)
117+
return doActualSerialization(cellId, processedData, value.toObjectWrapper(), isOverride)
100118
}
101119

102120
private fun serializeVariableState(cellId: Int, name: String, property: KProperty<*>, value: Any?, isOverride: Boolean = true): SerializedVariablesState {
103121
val processedData = createSerializeVariableState(name, getSimpleTypeNameFrom(property, value), value)
104-
return doActualSerialization(cellId, processedData, value, isOverride)
122+
return doActualSerialization(cellId, processedData, value.toObjectWrapper(), isOverride)
105123
}
106124

107-
private fun doActualSerialization(cellId: Int, processedData: ProcessedSerializedVarsState, value: Any?, isOverride: Boolean = true): SerializedVariablesState {
125+
private fun doActualSerialization(cellId: Int, processedData: ProcessedSerializedVarsState, value: RuntimeObjectWrapper, isOverride: Boolean = true): SerializedVariablesState {
108126
fun isCanBeComputed(fieldDescriptors: MutableMap<String, SerializedVariablesState?>): Boolean {
109127
return (fieldDescriptors.isEmpty() || (fieldDescriptors.isNotEmpty() && fieldDescriptors.entries.first().value?.fieldDescriptor!!.isEmpty()))
110128
}
@@ -120,7 +138,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
120138
currentCellDescriptors!!.processedSerializedVarsToKProperties[serializedVersion] = processedData.propertiesData
121139
// currentCellDescriptors.processedSerializedVarsToJvmFields[serializedVersion] = processedData.jvmOnlyFields
122140

123-
if (value != null) {
141+
if (value.objectInstance != null) {
124142
seenObjectsPerCell[cellId]!!.putIfAbsent(value, serializedVersion)
125143
}
126144
if (serializedVersion.isContainer) {
@@ -129,14 +147,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
129147
val previouslySerializedState = seenObjectsPerCell[cellId]!![value] ?: return processedData.serializedVariablesState
130148
serializedVersion.fieldDescriptor += previouslySerializedState.fieldDescriptor
131149
if (isCanBeComputed(serializedVersion.fieldDescriptor)) {
132-
iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
150+
iterateThroughContainerMembers(cellId, value.objectInstance, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
133151
}
134152
} else {
135153
// add jvm descriptors
136154
processedData.jvmOnlyFields?.forEach {
137155
serializedVersion.fieldDescriptor[it.name] = serializeVariableState(cellId, it.name, it, value)
138156
}
139-
iterateThroughContainerMembers(cellId, value, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
157+
iterateThroughContainerMembers(cellId, value.objectInstance, serializedVersion.fieldDescriptor, currentCellDescriptors.processedSerializedVarsToKProperties[serializedVersion])
140158
}
141159
}
142160
return processedData.serializedVariablesState
@@ -159,17 +177,17 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
159177
}
160178
it as KProperty1<Any, *>
161179
val name = it.name
162-
val value = tryGetValueFromProperty(it, callInstance)
180+
val value = tryGetValueFromProperty(it, callInstance).toObjectWrapper()
163181

164182
if (!seenObjectsPerCell!!.containsKey(value)) {
165183
serializedIteration[name] = createSerializeVariableState(name, getSimpleTypeNameFrom(it, value), value)
166184
descriptor[name] = serializedIteration[name]!!.serializedVariablesState
167185
}
168186
if (descriptor[name] != null) {
169-
instancesPerState[descriptor[name]!!] = value
187+
instancesPerState[descriptor[name]!!] = value.objectInstance
170188
}
171189

172-
if (value != null && !seenObjectsPerCell.containsKey(value)) {
190+
if (!seenObjectsPerCell.containsKey(value)) {
173191
if (descriptor[name] != null) {
174192
seenObjectsPerCell[value] = descriptor[name]!!
175193
}
@@ -217,7 +235,7 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
217235
else -> {
218236
null
219237
}
220-
}
238+
}.toObjectWrapper()
221239
if (isArrayType) {
222240
if (callInstance is List<*>) {
223241
callInstance.forEach { arrayElem ->
@@ -249,14 +267,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
249267
it.value.jvmOnlyFields?.forEach { field ->
250268
serializedVariablesState.fieldDescriptor[field.name] = serializeVariableState(cellId, field.name, field, neededCallInstance)
251269
val properInstance = serializedVariablesState.fieldDescriptor[field.name]
252-
instancesPerState[properInstance!!] = neededCallInstance
253-
seenObjectsPerCell?.set(neededCallInstance!!, serializedVariablesState)
270+
instancesPerState[properInstance!!] = neededCallInstance.objectInstance
271+
seenObjectsPerCell?.set(neededCallInstance, serializedVariablesState)
254272
}
255273
computedDescriptorsPerCell[cellId]!!.instancesPerState += instancesPerState
256274
// computedDescriptorsPerCell[cellId]!!.processedSerializedVarsToJvmFields[serializedVariablesState] = it.value.jvmOnlyFields
257275
iterateThroughContainerMembers(
258276
cellId,
259-
neededCallInstance,
277+
neededCallInstance.objectInstance,
260278
serializedVariablesState.fieldDescriptor,
261279
it.value.propertiesData,
262280
currentDepth + 1
@@ -289,6 +307,14 @@ class VariablesSerializer(private val serializationStep: Int = 2, private val se
289307
}
290308

291309
private fun createSerializeVariableState(name: String, simpleTypeName: String?, value: Any?): ProcessedSerializedVarsState {
310+
return doCreateSerializedVarsState(simpleTypeName, value)
311+
}
312+
313+
private fun createSerializeVariableState(name: String, simpleTypeName: String?, value: RuntimeObjectWrapper): ProcessedSerializedVarsState {
314+
return doCreateSerializedVarsState(simpleTypeName, value.objectInstance)
315+
}
316+
317+
private fun doCreateSerializedVarsState(simpleTypeName: String?, value: Any?): ProcessedSerializedVarsState {
292318
// make it exception-safe
293319
val membersProperties = try {
294320
if (value != null) value::class.declaredMemberProperties else null

0 commit comments

Comments
 (0)