Skip to content

Commit

Permalink
Change main reflection to jvm Fields; use KReflection for built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolay-egorov committed Aug 3, 2021
1 parent 89eb9bd commit 8cfc15e
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package org.jetbrains.kotlinx.jupyter.api

import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.isAccessible
import java.lang.reflect.Field

interface VariableState {
val property: KProperty<*>
// val property: KProperty<*>
val property: Field
val scriptInstance: Any?
val stringValue: String?
val value: Any?
}

data class VariableStateImpl(
override val property: KProperty1<Any, *>,
// override val property: KProperty1<Any, *>,
override val property: Field,
override val scriptInstance: Any,
) : VariableState {
private var cachedValue: Any? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import org.jetbrains.kotlinx.jupyter.exceptions.ReplCompilerException
import org.jetbrains.kotlinx.jupyter.repl.ContextUpdater
import org.jetbrains.kotlinx.jupyter.repl.InternalEvalResult
import org.jetbrains.kotlinx.jupyter.repl.InternalEvaluator
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KProperty1
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import kotlin.reflect.full.declaredMemberProperties
import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ResultWithDiagnostics
Expand Down Expand Up @@ -159,16 +159,21 @@ internal class InternalEvaluatorImpl(
val kClass = target.scriptClass ?: return emptyMap()
val cellClassInstance = target.scriptInstance!!

val fields = kClass.declaredMemberProperties
val fields = kClass.java.declaredFields
// ignore implementation details of top level like script instance and result value
val memberKPropertiesNames = kClass.declaredMemberProperties.map {
it.name
}.toHashSet()
val ans = mutableMapOf<String, VariableStateImpl>()
fields.forEach { property ->
@Suppress("UNCHECKED_CAST")
property as KProperty1<Any, *>
// if (property.name.startsWith("script$")) return@forEach
if (!memberKPropertiesNames.contains(property.name)) return@forEach

val state = VariableStateImpl(property, cellClassInstance)
variablesWatcher.addDeclaration(cellId, property.name)

// it was val, now it's var
if (property is KMutableProperty1) {
if (isValField(property)) {
variablesHolder.remove(property.name)
} else {
variablesHolder[property.name] = state
Expand All @@ -180,6 +185,10 @@ internal class InternalEvaluatorImpl(
return ans
}

private fun isValField(property: Field): Boolean {
return property.modifiers and Modifier.FINAL != 0
}

private fun updateDataAfterExecution(lastExecutionCellId: Int, resultValue: ResultValue) {
variablesWatcher.ensureStorageCreation(lastExecutionCellId)
variablesHolder += getVisibleVariables(resultValue, lastExecutionCellId)
Expand Down
Loading

0 comments on commit 8cfc15e

Please sign in to comment.