Skip to content

Commit

Permalink
rename to sai
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Nov 29, 2020
1 parent 59cde39 commit a70d250
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/anatawa12/fixRtm/FixRtm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/anatawa12/fixRtm/asm/config/MainConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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]

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/anatawa12/fixRtm/scripting/ExecutedScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -16,9 +16,9 @@ class ExecutedScript private constructor(
val scopeData: ByteArray?
) {
constructor(
dependencies: Map<String, ByteArray>,
scope: ScriptableObject,
base: Scriptable
dependencies: Map<String, ByteArray>,
scope: ScriptableObject,
base: Scriptable
): this(
dependencies, writeScopeData(scope, base)
) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Script, FIXRhinoScriptEngine> {
override fun getCachedEngine(filePath: ResourceLocation, resource: FIXResource, dependencies: Map<ResourceLocation, String>): FIXRhinoScriptEngine? {
object SaiScriptRuntimeImpl : IScriptRuntime<Script, FIXSaiScriptEngine> {
override fun getCachedEngine(filePath: ResourceLocation, resource: FIXResource, dependencies: Map<ResourceLocation, String>): 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()

Expand All @@ -36,7 +36,7 @@ object RhinoScriptRuntimeImpl : IScriptRuntime<Script, FIXRhinoScriptEngine> {
return engine
}

override fun cache(pack: FIXModelPack, filePath: ResourceLocation, dependencies: Map<ResourceLocation, String>, engine: FIXRhinoScriptEngine) {
override fun cache(pack: FIXModelPack, filePath: ResourceLocation, dependencies: Map<ResourceLocation, String>, engine: FIXSaiScriptEngine) {
ExecutedScriptCache.add(pack, filePath, makeExecutedScript(makeDependenciesData(dependencies), engine.scope))
}

Expand All @@ -45,7 +45,7 @@ object RhinoScriptRuntimeImpl : IScriptRuntime<Script, FIXRhinoScriptEngine> {

it.initStandardObjects(scope)

ImportScriptRhinoFunctionImpl.init(scope)
ImportScriptSaiFunctionImpl.init(scope)

scope.sealObject()

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T> usingContext(script: (Context) -> T): T{
val context = Context.enter()
Expand Down

0 comments on commit a70d250

Please sign in to comment.