Skip to content

Commit

Permalink
changed engine to instance variable, documented thread-safety; change…
Browse files Browse the repository at this point in the history
…d kotlin version
  • Loading branch information
Simon committed Nov 18, 2018
1 parent 76d0b4f commit 4b58682
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import org.gradle.api.publish.maven.MavenPom
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion

project.group = "de.swirtz"
project.version = "0.0.5"
project.version = "0.0.6"
val artifactID = "ktsRunner"

plugins {
kotlin("jvm") version "1.2.51"
kotlin("jvm") version "1.3.10"
`maven-publish`
`java-library`
id("com.jfrog.bintray") version "1.8.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import java.io.InputStream
import java.io.Reader
import javax.script.ScriptEngineManager

/**
*
* This class is not thread-safe, don't use it for parallel executions and create new instances instead.
*/
class KtsObjectLoader {

companion object {
val engine = ScriptEngineManager().getEngineByExtension("kts")
}
val engine = ScriptEngineManager().getEngineByExtension("kts")

inline fun <reified T> load(script: String): T = engine.eval(script).takeIf { it is T } as T
?: throw IllegalStateException("Could not load script from .kts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class KtsObjectLoaderTest {

@Test
fun `general ScriptEngineFactory test`() {
KtsObjectLoader.engine.factory.apply {
KtsObjectLoader().engine.factory.apply {
assertEquals("kotlin", languageName)
assertEquals(KotlinCompilerVersion.VERSION, languageVersion)
assertEquals("kotlin", engineName)
Expand All @@ -33,7 +33,7 @@ class KtsObjectLoaderTest {

@Test
fun `simple evaluations should work`() {
with(KtsObjectLoader.engine as KotlinJsr223JvmLocalScriptEngine) {
with(KtsObjectLoader().engine as KotlinJsr223JvmLocalScriptEngine) {
val res1 = eval("val x = 3")
assertNull(res1, "No returned value expected")
val res2 = eval("x + 2")
Expand Down

0 comments on commit 4b58682

Please sign in to comment.