From 4b58682e50b996ac8daf5fda2d8a3a3d4144fed5 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 18 Nov 2018 19:31:57 +0100 Subject: [PATCH] changed engine to instance variable, documented thread-safety; changed kotlin version --- build.gradle.kts | 4 ++-- .../de/swirtz/ktsrunner/objectloader/KtsObjectLoader.kt | 8 +++++--- .../swirtz/ktsrunner/objectloader/KtsObjectLoaderTest.kt | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 813683e..45a73e3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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" diff --git a/src/main/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoader.kt b/src/main/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoader.kt index b475e9e..dcf2dac 100755 --- a/src/main/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoader.kt +++ b/src/main/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoader.kt @@ -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 load(script: String): T = engine.eval(script).takeIf { it is T } as T ?: throw IllegalStateException("Could not load script from .kts") diff --git a/src/test/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoaderTest.kt b/src/test/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoaderTest.kt index 420d7e8..68396dc 100755 --- a/src/test/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoaderTest.kt +++ b/src/test/kotlin/de/swirtz/ktsrunner/objectloader/KtsObjectLoaderTest.kt @@ -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) @@ -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")