diff --git a/src/main/java/com/anatawa12/fixRtm/FixRtm.kt b/src/main/java/com/anatawa12/fixRtm/FixRtm.kt index 728b6aed..fa6dbda5 100644 --- a/src/main/java/com/anatawa12/fixRtm/FixRtm.kt +++ b/src/main/java/com/anatawa12/fixRtm/FixRtm.kt @@ -7,7 +7,7 @@ import com.anatawa12.fixRtm.network.NetworkHandler import com.anatawa12.fixRtm.rtm.modelpack.modelset.dummies.* import com.anatawa12.fixRtm.scripting.loadFIXScriptUtil import com.anatawa12.fixRtm.scripting.nashorn.CompiledImportedScriptCache -import com.anatawa12.fixRtm.scripting.rhino.ExecutedScriptCache +import com.anatawa12.fixRtm.scripting.sai.ExecutedScriptCache import jp.ngt.ngtlib.NGTCore import jp.ngt.rtm.RTMCore import net.minecraft.block.Block @@ -45,7 +45,7 @@ object FixRtm { fun construct(e: FMLConstructionEvent) { FIXFileLoader.load() // init when (MainConfig.scriptingMode) { - MainConfig.ScriptingMode.CacheWithRhino -> { + MainConfig.ScriptingMode.CacheWithSai -> { loadFIXScriptUtil()// init ExecutedScriptCache.load()// init } diff --git a/src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt b/src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt index 2fe04c17..fbecc479 100644 --- a/src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt +++ b/src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt @@ -30,7 +30,7 @@ object MainConfig { "scriptingMode", categoryModelLoading, "use-default", "scripting mode. the value is one of the list below:\n" + - "cache-with-rhino : the fastest mode but not stable. some script may make error.\n" + + "cache-with-sai : the fastest mode but not stable. some script may make error.\n" + "better-with-nashorn : same runtime as RTM but a little faster than RTM.\n" + "use-rtm-normal : same as RTM. this is the slowest mode.\n" + "use-default : use default mode. currently use-rtm-normal.\n") @@ -95,14 +95,16 @@ object MainConfig { } } - enum class ScriptingMode(val configValue: String) { - CacheWithRhino("cache-with-rhino"), + enum class ScriptingMode(vararg val configValues: String) { + CacheWithSai("cache-with-sai", "cache-with-rhino"), BetterWithNashorn("better-with-nashorn"), UseRtmNormal("use-rtm-normal"), ; companion object { - private val byConfigValue = values().associateBy { it.configValue } + private val byConfigValue = values() + .flatMap { it.configValues.map { k -> k to it } } + .toMap() fun getByConfigValue(value: String) = byConfigValue[value] diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/ExecutedScript.kt b/src/main/java/com/anatawa12/fixRtm/scripting/ExecutedScript.kt index 1bf8d052..40c92f98 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/ExecutedScript.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/ExecutedScript.kt @@ -2,10 +2,10 @@ package com.anatawa12.fixRtm.scripting import com.anatawa12.fixRtm.Loggers import com.anatawa12.fixRtm.caching.TaggedFileManager -import org.mozilla.javascript.Scriptable -import org.mozilla.javascript.ScriptableObject -import org.mozilla.javascript.serialize.ScriptableInputStream -import org.mozilla.javascript.serialize.ScriptableOutputStream +import com.anatawa12.sai.Scriptable +import com.anatawa12.sai.ScriptableObject +import com.anatawa12.sai.serialize.ScriptableInputStream +import com.anatawa12.sai.serialize.ScriptableOutputStream import java.io.* class ExecutedScript private constructor( @@ -16,9 +16,9 @@ class ExecutedScript private constructor( val scopeData: ByteArray? ) { constructor( - dependencies: Map, - scope: ScriptableObject, - base: Scriptable + dependencies: Map, + scope: ScriptableObject, + base: Scriptable ): this( dependencies, writeScopeData(scope, base) ) { diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt b/src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt index b8964d8a..10eb00b5 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt @@ -5,13 +5,13 @@ package com.anatawa12.fixRtm.scripting import com.anatawa12.fixRtm.asm.config.MainConfig import com.anatawa12.fixRtm.io.FIXFileLoader import com.anatawa12.fixRtm.scripting.nashorn.NashornScriptRuntimeImpl -import com.anatawa12.fixRtm.scripting.rhino.RhinoScriptRuntimeImpl +import com.anatawa12.fixRtm.scripting.sai.SaiScriptRuntimeImpl import jp.ngt.rtm.modelpack.ModelPackManager import net.minecraft.util.ResourceLocation import javax.script.ScriptEngine val scriptRuntime: IScriptRuntime<*, *> = when (MainConfig.scriptingMode) { - MainConfig.ScriptingMode.CacheWithRhino -> RhinoScriptRuntimeImpl + MainConfig.ScriptingMode.CacheWithSai -> SaiScriptRuntimeImpl MainConfig.ScriptingMode.BetterWithNashorn -> NashornScriptRuntimeImpl MainConfig.ScriptingMode.UseRtmNormal -> IScriptRuntime.AssertingRuntime } diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/ScriptImporter.kt b/src/main/java/com/anatawa12/fixRtm/scripting/ScriptImporter.kt index 0da925ad..28eb3806 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/ScriptImporter.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/ScriptImporter.kt @@ -1,8 +1,8 @@ package com.anatawa12.fixRtm.scripting import com.anatawa12.fixRtm.io.FIXFileLoader +import com.anatawa12.sai.ScriptRuntime import net.minecraft.util.ResourceLocation -import org.mozilla.javascript.ScriptRuntime import java.util.concurrent.ConcurrentHashMap diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/FIXRhinoScriptEngine.kt b/src/main/java/com/anatawa12/fixRtm/scripting/sai/FIXSaiScriptEngine.kt similarity index 92% rename from src/main/java/com/anatawa12/fixRtm/scripting/rhino/FIXRhinoScriptEngine.kt rename to src/main/java/com/anatawa12/fixRtm/scripting/sai/FIXSaiScriptEngine.kt index b86b98f6..71c76d51 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/FIXRhinoScriptEngine.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/sai/FIXSaiScriptEngine.kt @@ -1,11 +1,11 @@ -package com.anatawa12.fixRtm.scripting.rhino +package com.anatawa12.fixRtm.scripting.sai -import org.mozilla.javascript.Function -import org.mozilla.javascript.ScriptableObject +import com.anatawa12.sai.Function +import com.anatawa12.sai.ScriptableObject import java.io.Reader import javax.script.* -class FIXRhinoScriptEngine : ScriptEngine, Invocable { +class FIXSaiScriptEngine : ScriptEngine, Invocable { lateinit var scope: ScriptableObject override fun createBindings(): Bindings { diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/ImportScriptRhinoFunctionImpl.kt b/src/main/java/com/anatawa12/fixRtm/scripting/sai/ImportScriptSaiFunctionImpl.kt similarity index 86% rename from src/main/java/com/anatawa12/fixRtm/scripting/rhino/ImportScriptRhinoFunctionImpl.kt rename to src/main/java/com/anatawa12/fixRtm/scripting/sai/ImportScriptSaiFunctionImpl.kt index a73c129c..fd622065 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/ImportScriptRhinoFunctionImpl.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/sai/ImportScriptSaiFunctionImpl.kt @@ -1,13 +1,13 @@ -package com.anatawa12.fixRtm.scripting.rhino +package com.anatawa12.fixRtm.scripting.sai import com.anatawa12.fixRtm.io.FIXFileLoader import com.anatawa12.fixRtm.io.FIXModelPack import com.anatawa12.fixRtm.scripting.ScriptImporter +import com.anatawa12.sai.* import net.minecraft.util.ResourceLocation -import org.mozilla.javascript.* -object ImportScriptRhinoFunctionImpl : BaseFunction() { +object ImportScriptSaiFunctionImpl : BaseFunction() { fun getScript(name: String): Script { val resourceLocation = ResourceLocation(name) val resource = FIXFileLoader.getResource(resourceLocation) @@ -41,8 +41,8 @@ object ImportScriptRhinoFunctionImpl : BaseFunction() { fun init(scope: ScriptableObject) { usingContext { cx -> scope.defineProperty( - ImportScriptRhinoFunctionImpl.functionName, - ImportScriptRhinoFunctionImpl, + ImportScriptSaiFunctionImpl.functionName, + ImportScriptSaiFunctionImpl, ScriptableObject.READONLY or ScriptableObject.DONTENUM ) } diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/RhinoScriptRuntimeImpl.kt b/src/main/java/com/anatawa12/fixRtm/scripting/sai/SaiScriptRuntimeImpl.kt similarity index 84% rename from src/main/java/com/anatawa12/fixRtm/scripting/rhino/RhinoScriptRuntimeImpl.kt rename to src/main/java/com/anatawa12/fixRtm/scripting/sai/SaiScriptRuntimeImpl.kt index 4667413d..2880642b 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/RhinoScriptRuntimeImpl.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/sai/SaiScriptRuntimeImpl.kt @@ -1,4 +1,4 @@ -package com.anatawa12.fixRtm.scripting.rhino +package com.anatawa12.fixRtm.scripting.sai import com.anatawa12.fixRtm.caching.ModelPackBasedCache import com.anatawa12.fixRtm.fixCacheDir @@ -7,24 +7,24 @@ import com.anatawa12.fixRtm.io.FIXResource import com.anatawa12.fixRtm.scripting.ExecutedScript import com.anatawa12.fixRtm.scripting.IScriptRuntime import com.anatawa12.fixRtm.utils.DigestUtils +import com.anatawa12.sai.ImporterTopLevel +import com.anatawa12.sai.Script +import com.anatawa12.sai.ScriptableObject +import com.anatawa12.sai.TopLevel import net.minecraft.util.ResourceLocation -import org.mozilla.javascript.ImporterTopLevel -import org.mozilla.javascript.Script -import org.mozilla.javascript.ScriptableObject -import org.mozilla.javascript.TopLevel -object RhinoScriptRuntimeImpl : IScriptRuntime { - override fun getCachedEngine(filePath: ResourceLocation, resource: FIXResource, dependencies: Map): FIXRhinoScriptEngine? { +object SaiScriptRuntimeImpl : IScriptRuntime { + override fun getCachedEngine(filePath: ResourceLocation, resource: FIXResource, dependencies: Map): FIXSaiScriptEngine? { return getScriptAndDoScriptByCache(filePath, resource.pack, makeDependenciesData(dependencies)) - ?.let { scope -> FIXRhinoScriptEngine().also { it.scope = scope } } + ?.let { scope -> FIXSaiScriptEngine().also { it.scope = scope } } } - override fun compile(script: String, fileName: String, engine: FIXRhinoScriptEngine?): Script =usingContext { cx -> + override fun compile(script: String, fileName: String, engine: FIXSaiScriptEngine?): Script =usingContext { cx -> return ScriptCompiledClassCache.compile(script, fileName) } - override fun exec(script: Script): FIXRhinoScriptEngine { - val engine = FIXRhinoScriptEngine() + override fun exec(script: Script): FIXSaiScriptEngine { + val engine = FIXSaiScriptEngine() usingContext { cx -> val scope = makeNewScope() @@ -36,7 +36,7 @@ object RhinoScriptRuntimeImpl : IScriptRuntime { return engine } - override fun cache(pack: FIXModelPack, filePath: ResourceLocation, dependencies: Map, engine: FIXRhinoScriptEngine) { + override fun cache(pack: FIXModelPack, filePath: ResourceLocation, dependencies: Map, engine: FIXSaiScriptEngine) { ExecutedScriptCache.add(pack, filePath, makeExecutedScript(makeDependenciesData(dependencies), engine.scope)) } @@ -45,7 +45,7 @@ object RhinoScriptRuntimeImpl : IScriptRuntime { it.initStandardObjects(scope) - ImportScriptRhinoFunctionImpl.init(scope) + ImportScriptSaiFunctionImpl.init(scope) scope.sealObject() diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/ScriptCompiledClassCache.kt b/src/main/java/com/anatawa12/fixRtm/scripting/sai/ScriptCompiledClassCache.kt similarity index 93% rename from src/main/java/com/anatawa12/fixRtm/scripting/rhino/ScriptCompiledClassCache.kt rename to src/main/java/com/anatawa12/fixRtm/scripting/sai/ScriptCompiledClassCache.kt index aac83af5..a9210b26 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/ScriptCompiledClassCache.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/sai/ScriptCompiledClassCache.kt @@ -1,14 +1,14 @@ -package com.anatawa12.fixRtm.scripting.rhino +package com.anatawa12.fixRtm.scripting.sai import com.anatawa12.fixRtm.Loggers import com.anatawa12.fixRtm.fixCacheDir import com.anatawa12.fixRtm.mkParent import com.anatawa12.fixRtm.utils.DigestUtils -import org.mozilla.javascript.CompilerEnvirons -import org.mozilla.javascript.Context -import org.mozilla.javascript.Script -import org.mozilla.javascript.optimizer.ClassCompiler -import org.mozilla.javascript.tools.ToolErrorReporter +import com.anatawa12.sai.CompilerEnvirons +import com.anatawa12.sai.Context +import com.anatawa12.sai.Script +import com.anatawa12.sai.optimizer.ClassCompiler +import com.anatawa12.sai.tools.ToolErrorReporter import java.io.IOException import java.util.* import java.util.concurrent.ConcurrentHashMap diff --git a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/Util.kt b/src/main/java/com/anatawa12/fixRtm/scripting/sai/Util.kt similarity index 68% rename from src/main/java/com/anatawa12/fixRtm/scripting/rhino/Util.kt rename to src/main/java/com/anatawa12/fixRtm/scripting/sai/Util.kt index 3acada86..f22d49d0 100644 --- a/src/main/java/com/anatawa12/fixRtm/scripting/rhino/Util.kt +++ b/src/main/java/com/anatawa12/fixRtm/scripting/sai/Util.kt @@ -1,7 +1,6 @@ -package com.anatawa12.fixRtm.scripting.rhino - -import org.mozilla.javascript.Context +package com.anatawa12.fixRtm.scripting.sai +import com.anatawa12.sai.Context inline fun usingContext(script: (Context) -> T): T{ val context = Context.enter()