Skip to content

Commit

Permalink
Experimental | Optimize Script Env initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFloodDragon committed Jul 26, 2024
1 parent 6586bf1 commit 5587e3b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object ActionParser {
continue
}
// 构建脚本块
val block = ScriptBlockBuilder.build(raw.value.toBasic(), ScriptManager.defaultScriptLanguage.executor)
val block = ScriptBlockBuilder.build(raw.value.toBasic(), ScriptManager.defaultLanguage.executor)
// 创建脚本动作, 放入表中
map[type] = ScriptedAction(block)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public interface ScriptExecutor {
@Nullable
Object evaluate(@NotNull ScriptContent script, @NotNull ScriptEnvironment environment) throws ScriptException;

/**
* 初始化脚本执行环境
*/
default void initEnv() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package cn.fd.ratziel.script

import cn.fd.ratziel.common.config.Settings
import cn.fd.ratziel.script.api.ScriptType
import taboolib.common.ClassAppender
import taboolib.common.LifeCycle
import taboolib.common.env.JarRelocation
import taboolib.common.env.RuntimeEnv
import taboolib.common.platform.Awake
import taboolib.library.configuration.ConfigurationSection


/**
* ScriptManager
*
Expand All @@ -15,17 +18,20 @@ import taboolib.library.configuration.ConfigurationSection
*/
object ScriptManager {

var defaultScriptLanguage: ScriptType = ScriptTypes.KETHER
/**
* 默认脚本语言
*/
var defaultLanguage: ScriptType = ScriptTypes.KETHER
internal set

@Awake(LifeCycle.LOAD)
@Awake(LifeCycle.INIT)
private fun init() {
// Read config
val conf = Settings.conf.getConfigurationSection("Script")

// Default Language
val defLanguage = conf?.getString("Default")?.let { ScriptTypes.match(it) }
if (defLanguage != null) defaultScriptLanguage = defLanguage
if (defLanguage != null) defaultLanguage = defLanguage

// Options
val optionsConf = conf?.getConfigurationSection("Options")
Expand All @@ -45,11 +51,21 @@ object ScriptManager {
val options = getOptions(lang.names)
lang.enabled = options?.getBoolean("enabled", true) ?: true
// If the script is enabled
if (lang.enabled) {
// LoadEnv
RuntimeEnv.ENV.loadDependency(lang.executor::class.java)
}
if (lang.enabled) lang.executor.initEnv()
}

}

/**
* 加载指定环境环境
*/
fun loadEnv(value: String, test: String = "", transitive: Boolean = false, relocations: List<JarRelocation> = emptyList()) {
fun real(url: String) = if (url.startsWith("!")) url.substring(1) else url
// 检查测试类是否存在
val testClass = real(test)
if (testClass.isNotEmpty() && !ClassAppender.isExists(testClass)) return
// 加载类
RuntimeEnv.ENV.loadDependency((real(value)), transitive, relocations)
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.fd.ratziel.script.jexl

import cn.fd.ratziel.script.ScriptManager
import cn.fd.ratziel.script.api.CompilableScriptExecutor
import cn.fd.ratziel.script.api.ScriptContent
import cn.fd.ratziel.script.api.ScriptEnvironment
import taboolib.common.env.RuntimeDependency
import taboolib.common.platform.Ghost
import javax.script.Compilable
import javax.script.CompiledScript
import javax.script.ScriptEngine
Expand All @@ -16,12 +15,6 @@ import javax.script.ScriptEngineManager
* @author TheFloodDragon
* @since 2024/7/14 21:41
*/
@Ghost // 避免依赖注入, 主动触发
@RuntimeDependency(
value = "!org.apache.commons:commons-jexl3:3.4.0",
test = "!org.apache.commons.jexl3.JexlEngine",
transitive = false
)
object JexlExecutor : CompilableScriptExecutor {

override fun compile(script: String?): CompiledScript {
Expand All @@ -36,4 +29,10 @@ object JexlExecutor : CompilableScriptExecutor {
ScriptEngineManager(this::class.java.classLoader).getEngineByName("Jexl")
?: throw NullPointerException("Cannot find ScriptEngine for JexlExecutor")

override fun initEnv() = ScriptManager.loadEnv(
value = "!org.apache.commons:commons-jexl3:3.4.0",
test = "!org.apache.commons.jexl3.JexlEngine",
transitive = false
)

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.fd.ratziel.script.js

import cn.fd.ratziel.script.ScriptManager
import cn.fd.ratziel.script.api.CompilableScriptExecutor
import cn.fd.ratziel.script.api.ScriptContent
import cn.fd.ratziel.script.api.ScriptEnvironment
import taboolib.common.env.RuntimeDependency
import taboolib.common.platform.Ghost
import javax.script.Compilable
import javax.script.CompiledScript
import javax.script.ScriptEngine
Expand All @@ -16,11 +15,6 @@ import javax.script.ScriptEngineManager
* @author TheFloodDragon
* @since 2024/7/14 21:40
*/
@Ghost // 避免依赖注入, 主动触发
@RuntimeDependency(
value = "!org.openjdk.nashorn:nashorn-core:15.4",
test = "!org.openjdk.nashorn.api.scripting.NashornScriptEngine"
)
object JavaScriptExecutor : CompilableScriptExecutor {

override fun compile(script: String?): CompiledScript {
Expand All @@ -35,4 +29,10 @@ object JavaScriptExecutor : CompilableScriptExecutor {
ScriptEngineManager(this::class.java.classLoader).getEngineByName("js")
?: throw NullPointerException("Cannot find ScriptEngine for JavaScriptExecutor")

override fun initEnv() = ScriptManager.loadEnv(
value = "!org.openjdk.nashorn:nashorn-core:15.4",
test = "!org.openjdk.nashorn.api.scripting.NashornScriptEngine",
transitive = true
)

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package cn.fd.ratziel.script.kts

import cn.fd.ratziel.core.env.CoreEnv
import cn.fd.ratziel.script.ScriptManager.loadEnv
import cn.fd.ratziel.script.api.CompilableScriptExecutor
import cn.fd.ratziel.script.api.ScriptContent
import cn.fd.ratziel.script.api.ScriptEnvironment
import taboolib.common.env.RuntimeDependencies
import taboolib.common.env.RuntimeDependency
import taboolib.common.platform.Ghost
import javax.script.Compilable
import javax.script.CompiledScript
import javax.script.ScriptEngine
Expand All @@ -18,47 +16,6 @@ import javax.script.ScriptEngineManager
* @author TheFloodDragon
* @since 2024/7/14 21:41
*/
@Ghost // 避免依赖注入, 主动触发
@RuntimeDependencies(
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-reflect:" + CoreEnv.KOTLIN_VERSION,
test = "!kotlin.reflect.jvm.ReflectLambdaKt",
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-compiler:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-script-runtime:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-scripting-common:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-scripting-jvm:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-scripting-jvm-host-unshaded:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-scripting-compiler:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.kotlin:kotlin-scripting-compiler-impl:" + CoreEnv.KOTLIN_VERSION,
transitive = false
),
RuntimeDependency(
value = "!org.jetbrains.intellij.deps:trove4j:1.0.20200330",
test = "!gnu.trove.TObjectHashingStrategy",
transitive = false
)
)
object KotlinScriptExecutor : CompilableScriptExecutor {

override fun compile(script: String): CompiledScript {
Expand All @@ -72,4 +29,37 @@ object KotlinScriptExecutor : CompilableScriptExecutor {
fun newEngine(): ScriptEngine =
ScriptEngineManager(this::class.java.classLoader).getEngineByName("kotlin")

override fun initEnv() {
loadEnv(
value = "!org.jetbrains.kotlin:kotlin-reflect:" + CoreEnv.KOTLIN_VERSION,
test = "!kotlin.reflect.jvm.ReflectLambdaKt",
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-compiler:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-script-runtime:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-scripting-common:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-scripting-jvm:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-scripting-jvm-host-unshaded:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-scripting-compiler:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.kotlin:kotlin-scripting-compiler-impl:" + CoreEnv.KOTLIN_VERSION,
transitive = false
);loadEnv(
value = "!org.jetbrains.intellij.deps:trove4j:1.0.20200330",
test = "!gnu.trove.TObjectHashingStrategy",
transitive = false
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object ActionElement : ElementHandler {

override fun handle(element: Element) {
element.property.let { json ->
val block = ScriptBlockBuilder.build(json.toBasic(), ScriptManager.defaultScriptLanguage.executor)
val block = ScriptBlockBuilder.build(json.toBasic(), ScriptManager.defaultLanguage.executor)
println(block)
val result = block.evaluate(SimpleScriptEnv())
println(result)
Expand Down

0 comments on commit 5587e3b

Please sign in to comment.