Reuse ScriptEngine

This commit is contained in:
Shadowfacts 2017-08-05 14:11:19 -04:00
parent fe378f453d
commit 05b7cd3892
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 6 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package net.shadowfacts.ekt
import java.io.File import java.io.File
import javax.script.ScriptContext import javax.script.ScriptContext
import javax.script.ScriptEngineManager import javax.script.ScriptEngineManager
import javax.script.SimpleScriptContext
/** /**
* @author shadowfacts * @author shadowfacts
@ -35,6 +36,10 @@ _result.toString()
ScriptEngineManager() ScriptEngineManager()
} }
private val engine by lazy {
manager.getEngineByExtension("kts")
}
fun render(template: String, dumpGeneratedScript: File? = null, dataProvider: DataProviderContext.() -> Unit): String { fun render(template: String, dumpGeneratedScript: File? = null, dataProvider: DataProviderContext.() -> Unit): String {
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
var template = template var template = template
@ -75,7 +80,7 @@ _result.toString()
} }
internal fun eval(script: String, data: Map<String, TypedValue> = mapOf()): Any? { internal fun eval(script: String, data: Map<String, TypedValue> = mapOf()): Any? {
val engine = manager.getEngineByExtension("kts") engine.context = SimpleScriptContext()
val bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE) val bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE)
bindings.putAll(data) bindings.putAll(data)