Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Kotlin/kotlin-jupyter
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e1675bac681a57e09f0d4d5177a4003f5a77ffbd
Choose a base ref
..
head repository: Kotlin/kotlin-jupyter
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4f3281f590705bdc212071aef4f741efc77a8bd0
Choose a head ref
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ interface KotlinKernelHost {
fun addLibrary(library: LibraryDefinition)

/**
* Declares the given variables. They may be used in the notebook cells
* Declares the given properties. They may be used in the notebook cells
*/
fun declareVariables(variables: Iterable<VariableDeclaration>)
fun declareProperties(properties: Iterable<PropertyDeclaration>)
}
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ class FieldHandlerByClass(
override fun acceptsType(type: KType) = type.isSubtypeOf(kClass.starProjectedType)
}

data class VariableDeclaration(
data class PropertyDeclaration(
val name: VariableName,
val value: Any?,
val type: KType,
@@ -56,6 +56,5 @@ data class VariableDeclaration(
)
}

fun KotlinKernelHost.declareVariable(variable: VariableDeclaration) = declareVariables(listOf(variable))
fun KotlinKernelHost.declareVariable(name: VariableName, value: Any?) = declareVariable(VariableDeclaration(name, value))
fun KotlinKernelHost.declareVariables(variables: Map<VariableName, Any?>) = declareVariables(variables.map { VariableDeclaration(it.key, it.value) })
fun KotlinKernelHost.declareProperties(vararg properties: PropertyDeclaration) = declareProperties(properties.toList())
fun KotlinKernelHost.declareProperties(vararg properties: Pair<VariableName, Any?>) = declareProperties(properties.map { PropertyDeclaration(it.first, it.second) })
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
import org.jetbrains.kotlinx.jupyter.api.FieldValue
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
import org.jetbrains.kotlinx.jupyter.api.VariableDeclaration
import org.jetbrains.kotlinx.jupyter.api.PropertyDeclaration
import org.jetbrains.kotlinx.jupyter.api.libraries.CodeExecution
import org.jetbrains.kotlinx.jupyter.api.libraries.ExecutionHost
import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryDefinition
@@ -166,8 +166,8 @@ internal class CellExecutorImpl(private val replContext: SharedReplContext) : Ce
return callback(ExecutionContext(sharedContext, displayHandler, executor))
}

override fun declareVariables(variables: Iterable<VariableDeclaration>) {
val tempDeclarations = variables.joinToString(
override fun declareProperties(properties: Iterable<PropertyDeclaration>) {
val tempDeclarations = properties.joinToString(
"\n",
"object $TEMP_OBJECT_NAME {\n",
"\n}\n$TEMP_OBJECT_NAME"
@@ -176,14 +176,12 @@ internal class CellExecutorImpl(private val replContext: SharedReplContext) : Ce
}
val result = execute(tempDeclarations).value as Any
val resultClass = result::class
val properties = resultClass.declaredMemberProperties.associate {
@Suppress("UNCHECKED_CAST")
it.name to (it as KMutableProperty1<Any, Any?>)
}
val propertiesMap = resultClass.declaredMemberProperties.associateBy { it.name }

val declarations = variables.joinToString("\n") {
val prop = properties[it.name]
prop?.set(result, it.value)
val declarations = properties.joinToString("\n") {
@Suppress("UNCHECKED_CAST")
val prop = propertiesMap[it.name] as KMutableProperty1<Any, Any?>
prop.set(result, it.value)
it.declaration
}
execute(declarations)
@@ -192,9 +190,9 @@ internal class CellExecutorImpl(private val replContext: SharedReplContext) : Ce
companion object {
private const val TEMP_OBJECT_NAME = "___temp_declarations"

private val VariableDeclaration.mutabilityQualifier get() = if (isMutable) "var" else "val"
private val VariableDeclaration.declaration get() = """$mutabilityQualifier `$name`: $type = $TEMP_OBJECT_NAME.`$name` as $type"""
private val VariableDeclaration.tempDeclaration get() = """var `$name`: ${type.withNullability(true)} = null"""
private val PropertyDeclaration.mutabilityQualifier get() = if (isMutable) "var" else "val"
private val PropertyDeclaration.declaration get() = """$mutabilityQualifier `$name`: $type = $TEMP_OBJECT_NAME.`$name` as $type"""
private val PropertyDeclaration.tempDeclaration get() = """var `$name`: ${type.withNullability(true)} = null"""
}
}
}
Original file line number Diff line number Diff line change
@@ -6,9 +6,8 @@ import kotlinx.serialization.SerializationException
import org.jetbrains.kotlinx.jupyter.ReplForJupyter
import org.jetbrains.kotlinx.jupyter.ReplForJupyterImpl
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelVersion.Companion.toMaybeUnspecifiedString
import org.jetbrains.kotlinx.jupyter.api.VariableDeclaration
import org.jetbrains.kotlinx.jupyter.api.declareVariable
import org.jetbrains.kotlinx.jupyter.api.declareVariables
import org.jetbrains.kotlinx.jupyter.api.PropertyDeclaration
import org.jetbrains.kotlinx.jupyter.api.declareProperties
import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryDefinition
import org.jetbrains.kotlinx.jupyter.api.libraries.ResourceType
import org.jetbrains.kotlinx.jupyter.config.defaultRepositories
@@ -437,15 +436,13 @@ class CustomLibraryResolverTests : AbstractReplTest() {
val repl = testOneLibUsage(
library {
onLoaded {
declareVariables(
mapOf(
"x1" to 22,
"x2" to 20
)
declareProperties(
"x1" to 22,
"x2" to 20
)

declareVariable(
VariableDeclaration("x3", mutProp, typeOf<ArrayList<Int>>())
declareProperties(
PropertyDeclaration("x3", mutProp, typeOf<ArrayList<Int>>())
)
}
}