-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
825 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/kotlin/org/jetbrains/kotlin/jupyter/repl/building/KotlinReplProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.jupyter.repl.building | ||
|
||
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration | ||
import java.io.File | ||
import java.util.ArrayList | ||
import java.util.Collections | ||
import java.util.HashSet | ||
import org.jetbrains.kotlin.jupyter.repl.context.KotlinReceiver | ||
|
||
/** | ||
* Class that holds properties for Kotlin REPL creation, | ||
* namely implicit receiver, classpath, preloaded code, directory for class bytecode output, | ||
* max result limit and shortening types flag. | ||
* | ||
* Set its parameters by chaining corresponding methods, e.g. | ||
* properties.outputDir(dir).shortenTypes(false) | ||
* | ||
* Get its parameters via getters. | ||
*/ | ||
class KotlinReplProperties { | ||
|
||
val hostConf = defaultJvmScriptingHostConfiguration | ||
|
||
var receiver: KotlinReceiver? = null | ||
private set | ||
private val classpath: MutableSet<String> | ||
private val codeOnLoad: MutableList<String> | ||
var outputDir: String? = null | ||
private set | ||
var maxResult = 1000 | ||
private set | ||
var shortenTypes = true | ||
private set | ||
|
||
init { | ||
this.receiver = KotlinReceiver() | ||
|
||
this.classpath = HashSet() | ||
val javaClasspath = System.getProperty("java.class.path").split(File.pathSeparator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
Collections.addAll(classpath, *javaClasspath) | ||
|
||
this.codeOnLoad = ArrayList() | ||
} | ||
|
||
fun receiver(receiver: KotlinReceiver): KotlinReplProperties { | ||
this.receiver = receiver | ||
return this | ||
} | ||
|
||
fun classPath(path: String): KotlinReplProperties { | ||
this.classpath.add(path) | ||
return this | ||
} | ||
|
||
fun classPath(paths: Collection<String>): KotlinReplProperties { | ||
this.classpath.addAll(paths) | ||
return this | ||
} | ||
|
||
fun codeOnLoad(code: String): KotlinReplProperties { | ||
this.codeOnLoad.add(code) | ||
return this | ||
} | ||
|
||
fun codeOnLoad(code: Collection<String>): KotlinReplProperties { | ||
this.codeOnLoad.addAll(code) | ||
return this | ||
} | ||
|
||
fun outputDir(outputDir: String): KotlinReplProperties { | ||
this.outputDir = outputDir | ||
return this | ||
} | ||
|
||
fun maxResult(maxResult: Int): KotlinReplProperties { | ||
this.maxResult = maxResult | ||
return this | ||
} | ||
|
||
fun shortenTypes(shortenTypes: Boolean): KotlinReplProperties { | ||
this.shortenTypes = shortenTypes | ||
return this | ||
} | ||
|
||
fun getClasspath(): Set<String> { | ||
return classpath | ||
} | ||
|
||
fun getCodeOnLoad(): List<String> { | ||
return codeOnLoad | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/main/kotlin/org/jetbrains/kotlin/jupyter/repl/building/ReplBuilding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.jupyter.repl.building | ||
|
||
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.KJvmReplCompilerImpl | ||
import java.io.File | ||
import java.util.StringJoiner | ||
import kotlin.script.experimental.api.* | ||
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator | ||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext | ||
import kotlin.script.experimental.jvm.jvm | ||
import kotlin.script.experimental.host.withDefaultsFrom | ||
import kotlin.script.experimental.jvmhost.repl.JvmReplCompiler | ||
import kotlin.script.experimental.jvmhost.repl.JvmReplEvaluator | ||
|
||
/** | ||
* Util class for building REPL components. | ||
*/ | ||
object ReplBuilding { | ||
fun buildCompiler(properties: KotlinReplProperties): JvmReplCompiler { | ||
val receiverClassPath = properties.receiver!!.javaClass | ||
.protectionDomain.codeSource.location.path | ||
properties.classPath(receiverClassPath) | ||
|
||
val compilerImpl = KJvmReplCompilerImpl(properties.hostConf) | ||
|
||
return JvmReplCompiler( | ||
buildCompilationConfiguration(properties), | ||
properties.hostConf, | ||
compilerImpl) | ||
} | ||
|
||
fun buildEvaluator(properties: KotlinReplProperties): JvmReplEvaluator { | ||
return JvmReplEvaluator( | ||
buildEvaluationConfiguration(properties), | ||
BasicJvmScriptEvaluator()) | ||
} | ||
|
||
private fun buildClassPath(p: KotlinReplProperties): String { | ||
val joiner = StringJoiner(File.pathSeparator) | ||
for (path in p.getClasspath()) { | ||
if (path != "") { | ||
joiner.add(path) | ||
} | ||
} | ||
return joiner.toString() | ||
} | ||
|
||
private fun buildCompilationConfiguration( | ||
p: KotlinReplProperties): ScriptCompilationConfiguration { | ||
return ScriptCompilationConfiguration { | ||
hostConfiguration.invoke(p.hostConf) | ||
|
||
val jvmBuilder = jvm | ||
jvmBuilder.dependenciesFromCurrentContext(wholeClasspath = true, unpackJarCollections = false) | ||
|
||
val compilerOptions = listOf("-classpath", buildClassPath(p)) | ||
|
||
this.compilerOptions.invoke(compilerOptions) | ||
|
||
val kt = KotlinType(p.receiver!!.javaClass.canonicalName) | ||
val receivers = listOf(kt) | ||
implicitReceivers.invoke(receivers) | ||
|
||
Unit | ||
} | ||
} | ||
|
||
private fun buildEvaluationConfiguration( | ||
p: KotlinReplProperties): ScriptEvaluationConfiguration { | ||
return ScriptEvaluationConfiguration { | ||
hostConfiguration.invoke(p.hostConf) | ||
|
||
val receivers = listOf<Any?>(p.receiver) | ||
implicitReceivers.invoke(receivers) | ||
|
||
Unit | ||
} | ||
} | ||
} |
Oops, something went wrong.