Skip to content

Commit 1b49ca2

Browse files
committed
Allow repl to be embeded and share classes/variables with its caller
1 parent 8375cab commit 1b49ca2

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/main/kotlin/org/jetbrains/kotlin/jupyter/repl.kt

+8-5
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class ReplForJupyterImpl(
135135
override val resolverConfig: ResolverConfig? = null,
136136
override val runtimeProperties: ReplRuntimeProperties = defaultRuntimeProperties,
137137
private val scriptReceivers: List<Any> = emptyList(),
138+
private val embedded: Boolean = false,
138139
) : ReplForJupyter, ReplOptions, KotlinKernelHost {
139140

140141
constructor(config: KernelConfig, runtimeProperties: ReplRuntimeProperties, scriptReceivers: List<Any> = emptyList()):
@@ -316,12 +317,14 @@ class ReplForJupyterImpl(
316317

317318
private val evaluatorConfiguration = ScriptEvaluationConfiguration {
318319
implicitReceivers.invoke(v = scriptReceivers)
319-
jvm {
320-
val filteringClassLoader = FilteringClassLoader(ClassLoader.getSystemClassLoader()) {
321-
it.startsWith("jupyter.kotlin.") || it.startsWith("kotlin.") || (it.startsWith("org.jetbrains.kotlin.") && !it.startsWith("org.jetbrains.kotlin.jupyter."))
320+
if (!embedded) {
321+
jvm {
322+
val filteringClassLoader = FilteringClassLoader(ClassLoader.getSystemClassLoader()) {
323+
it.startsWith("jupyter.kotlin.") || it.startsWith("kotlin.") || (it.startsWith("org.jetbrains.kotlin.") && !it.startsWith("org.jetbrains.kotlin.jupyter."))
324+
}
325+
val scriptClassloader = URLClassLoader(scriptClasspath.map { it.toURI().toURL() }.toTypedArray(), filteringClassLoader)
326+
baseClassLoader(scriptClassloader)
322327
}
323-
val scriptClassloader = URLClassLoader(scriptClasspath.map { it.toURI().toURL() }.toTypedArray(), filteringClassLoader)
324-
baseClassLoader(scriptClassloader)
325328
}
326329
constructorArgs(this@ReplForJupyterImpl as KotlinKernelHost)
327330
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.jetbrains.kotlin.jupyter.test
2+
3+
4+
import org.jetbrains.kotlin.jupyter.ReplForJupyterImpl
5+
import org.junit.jupiter.api.Test
6+
import java.io.File
7+
import kotlin.test.assertEquals
8+
9+
10+
class SomeSingleton {
11+
companion object {
12+
var initialized: Boolean = false
13+
}
14+
}
15+
16+
17+
class EmbedReplTest : AbstractReplTest() {
18+
19+
@Test
20+
fun testRepl() {
21+
val embeddedClasspath: List<File> = System.getProperty("java.class.path").split(File.pathSeparator).map(::File)
22+
val repl = ReplForJupyterImpl(libraryFactory, embeddedClasspath, embedded=true)
23+
24+
var res = repl.eval("org.jetbrains.kotlin.jupyter.test.SomeSingleton.initialized")
25+
assertEquals(false, res.resultValue)
26+
27+
SomeSingleton.initialized = true
28+
29+
res = repl.eval("org.jetbrains.kotlin.jupyter.test.SomeSingleton.initialized")
30+
assertEquals(true, res.resultValue)
31+
32+
}
33+
}

0 commit comments

Comments
 (0)