Skip to content

Commit

Permalink
move classes for rhino
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 committed Jun 2, 2020
1 parent bb4006c commit 4042cc8
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 91 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 @@ -6,8 +6,8 @@ import com.anatawa12.fixRtm.io.FIXFileLoader
import com.anatawa12.fixRtm.network.NetworkHandler
import com.anatawa12.fixRtm.rtm.modelpack.modelset.dummies.*
import com.anatawa12.fixRtm.scripting.ExecutedScriptCache
import com.anatawa12.fixRtm.scripting.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.RhinoHooks
import com.anatawa12.fixRtm.scripting.rhino.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.rhino.RhinoHooks
import com.anatawa12.fixRtm.scripting.loadFIXScriptUtil
import jp.ngt.ngtlib.NGTCore
import jp.ngt.rtm.RTMCore
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/com/anatawa12/fixRtm/asm/RhinoTransformer.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.anatawa12.fixRtm.asm

import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_compileToClassFiles_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_compileToClassFiles_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.ClassCompiler_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_implDecompile_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_implDecompile_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.CodeGenHooks_internal
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_generateNativeFunctionOverrides_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_generateNativeFunctionOverrides_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_internal
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_scriptOrFnNodes_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_scriptOrFnNodes_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_sourceString_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.Codegen_sourceString_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_decompile_desc
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_decompile_name
import com.anatawa12.fixRtm.scripting.CodeGenHooks.NativeFunction_name
import com.anatawa12.fixRtm.scripting.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.RhinoHooks
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_compileToClassFiles_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_compileToClassFiles_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.ClassCompiler_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_implDecompile_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_implDecompile_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.CodeGenHooks_internal
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_generateNativeFunctionOverrides_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_generateNativeFunctionOverrides_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_internal
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_scriptOrFnNodes_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_scriptOrFnNodes_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_sourceString_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.Codegen_sourceString_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_decompile_desc
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_decompile_name
import com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks.NativeFunction_name
import com.anatawa12.fixRtm.scripting.rhino.PrimitiveJavaHelper
import com.anatawa12.fixRtm.scripting.rhino.RhinoHooks
import net.minecraft.launchwrapper.IClassTransformer
import org.objectweb.asm.*
import org.objectweb.asm.Opcodes.*
Expand Down Expand Up @@ -346,7 +346,7 @@ class RhinoTransformer : IClassTransformer {
companion object {
const val NativeJavaObject_name = "org.mozilla.javascript.NativeJavaObject"
const val NativeJavaObject_internal = "org/mozilla/javascript/NativeJavaObject"
const val CoerceTypeImplFailed_internal = "com/anatawa12/fixRtm/scripting/CoerceTypeImplFailed"
const val CoerceTypeImplFailed_internal = "com/anatawa12/fixRtm/scripting/rhino/CoerceTypeImplFailed"
const val IdScriptableObject_internal = "org/mozilla/javascript/IdScriptableObject"
const val Scriptable_internal = "org/mozilla/javascript/Scriptable"
const val Scriptable_NOT_FOUND_name = "NOT_FOUND"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object MainConfig {
init {
val cachedScriptsEnabled = config.getCategory(categoryModelLoading).remove("cachedScriptsEnabled")
if (cachedScriptsEnabled != null) { // cachedScriptsEnabledがある
val categoryModelLoadingProp = config.getCategory("scriptingMode").get(categoryModelLoading)!!
val categoryModelLoadingProp = config.getCategory(categoryModelLoading).get("scriptingMode")!!
if (categoryModelLoadingProp.string.toLowerCase() == ScriptingMode.defaultConfigValue) { // scriptingModeがuse default
if (!cachedScriptsEnabled.boolean) { // cached scriptがdisable
categoryModelLoadingProp.setValue(ScriptingMode.BetterWithNashorn.configValue)
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/anatawa12/fixRtm/scripting/FIXScriptUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import com.anatawa12.fixRtm.caching.ModelPackBasedCache
import com.anatawa12.fixRtm.fixCacheDir
import com.anatawa12.fixRtm.io.FIXFileLoader
import com.anatawa12.fixRtm.io.FIXModelPack
import com.anatawa12.fixRtm.scripting.rhino.FIXRhinoScriptEngine
import com.anatawa12.fixRtm.scripting.rhino.ImportScriptRhinoFunctionImpl
import com.anatawa12.fixRtm.scripting.rhino.ScriptCompiledClassCache
import com.anatawa12.fixRtm.scripting.rhino.usingContext
import com.anatawa12.fixRtm.utils.DigestUtils
import jp.ngt.rtm.modelpack.ModelPackManager
import net.minecraft.util.ResourceLocation
Expand All @@ -19,7 +23,7 @@ val baseScope = usingContext {

it.initStandardObjects(scope)

ScriptImporter.init(scope)
ImportScriptRhinoFunctionImpl.init(scope)

scope.sealObject()

Expand Down Expand Up @@ -59,17 +63,17 @@ fun ModelPackManager.getScriptAndDoScript(fileName: String): ScriptEngine {

// first, try cache
getScriptAndDoScriptByCache(filePath, resource.pack, dependencies)?.let { scope ->
val engine = FIXScriptEngine()
val engine = FIXRhinoScriptEngine()
engine.scope = scope
return engine
}

// then evalute
val engine = FIXScriptEngine()
val engine = FIXRhinoScriptEngine()
usingContext { cx ->
val scope = makeNewScope()

val script = ScriptImporter.makeScript(filePath, scriptStr, resource.pack)
val script = ImportScriptRhinoFunctionImpl.makeScript(filePath, scriptStr, resource.pack)

script.exec(cx, scope)

Expand Down
50 changes: 1 addition & 49 deletions src/main/java/com/anatawa12/fixRtm/scripting/ScriptImporter.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
package com.anatawa12.fixRtm.scripting

import com.anatawa12.fixRtm.io.FIXFileLoader
import com.anatawa12.fixRtm.io.FIXModelPack
import com.anatawa12.fixRtm.io.FIXResource
import net.minecraft.client.resources.IResource
import net.minecraft.util.ResourceLocation
import org.mozilla.javascript.*
import org.mozilla.javascript.ScriptRuntime
import java.util.concurrent.ConcurrentHashMap


object ScriptImporter {
const val importScriptFunctionName = "__\$\$fixrtm_internal_function_importScript\$\$__"

private val importScriptFunction = ImportScriptFunctionImpl as BaseFunction

fun importScript(cx: Context, name: String, scope: Scriptable) {
val script = getScript(name)
script.exec(cx, scope)
}

fun getScript(name: String): Script {
val resourceLocation = ResourceLocation(name)
val resource = FIXFileLoader.getResource(resourceLocation)
val script = resource.inputStream.reader().use { it.readText() }
return makeScript(resourceLocation, script, resource.pack)
}

fun makeScript(location: ResourceLocation, script: String, pack: FIXModelPack? = null): Script {
val script = preprocessScript(script)
val name = if (pack != null) "$location(${pack.file.name})" else "$location"
return ScriptCompiledClassCache.compile(script, name)
}

private val allDependenceScripts = ConcurrentHashMap<ResourceLocation, Map<ResourceLocation, String>>()

fun getAllDependenceScripts(name: String): Map<ResourceLocation, String> {
Expand Down Expand Up @@ -59,29 +36,4 @@ object ScriptImporter {
""";$importScriptFunctionName("${ScriptRuntime.escapeString(result.groupValues[1])}");"""
}
}


private object ImportScriptFunctionImpl : BaseFunction() {
override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable, args: Array<Any>): Any? {
if (args.size != 1)
ScriptRuntime.typeError("invalid arguments: argument length must be 1")
val name = ScriptRuntime.toString(args[0])

importScript(cx, name, scope)

return Undefined.instance
}

override fun getFunctionName(): String = importScriptFunctionName
}

fun init(scope: ScriptableObject) {
usingContext { cx ->
scope.defineProperty(
importScriptFunction.functionName,
importScriptFunction,
ScriptableObject.READONLY or ScriptableObject.DONTENUM
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import org.mozilla.classfile.ByteCode
import org.mozilla.classfile.ByteCode.*
import org.mozilla.classfile.ClassFileWriter
import org.mozilla.classfile.ClassFileWriter.classNameToSignature
Expand Down Expand Up @@ -95,8 +94,8 @@ object CodeGenHooks {
const val NativeFunction_decompile_name = "decompile"
const val NativeFunction_decompile_desc = "(II)L${"java/lang/String"};"

const val CodeGenHooks_name = "com.anatawa12.fixRtm.scripting.CodeGenHooks"
const val CodeGenHooks_internal = "com/anatawa12/fixRtm/scripting/CodeGenHooks"
const val CodeGenHooks_name = "com.anatawa12.fixRtm.scripting.rhino.CodeGenHooks"
const val CodeGenHooks_internal = "com/anatawa12/fixRtm/scripting/rhino/CodeGenHooks"

const val CodeGenHooks_implDecompile_name = "implDecompile"
const val CodeGenHooks_implDecompile_desc = "(L${"org/mozilla/classfile/ClassFileWriter"};[L${"org/mozilla/javascript/ast/ScriptNode"};L${"java/lang/String"};)V"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import org.mozilla.javascript.Function
import org.mozilla.javascript.NativeObject
import org.mozilla.javascript.ScriptableObject
import java.io.Reader
import javax.script.*

class FIXScriptEngine : ScriptEngine, Invocable {
class FIXRhinoScriptEngine : ScriptEngine, Invocable {
lateinit var scope: ScriptableObject

override fun createBindings(): Bindings {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.anatawa12.fixRtm.scripting.rhino

import com.anatawa12.fixRtm.io.FIXFileLoader
import com.anatawa12.fixRtm.io.FIXModelPack
import com.anatawa12.fixRtm.scripting.ScriptImporter
import net.minecraft.util.ResourceLocation
import org.mozilla.javascript.*


object ImportScriptRhinoFunctionImpl : BaseFunction() {
fun getScript(name: String): Script {
val resourceLocation = ResourceLocation(name)
val resource = FIXFileLoader.getResource(resourceLocation)
val script = resource.inputStream.reader().use { it.readText() }
return makeScript(resourceLocation, script, resource.pack)
}

fun makeScript(location: ResourceLocation, script: String, pack: FIXModelPack? = null): Script {
val script = ScriptImporter.preprocessScript(script)
val name = if (pack != null) "$location(${pack.file.name})" else "$location"
return ScriptCompiledClassCache.compile(script, name)
}

fun importScript(cx: Context, name: String, scope: Scriptable) {
val script = getScript(name)
script.exec(cx, scope)
}

override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable, args: Array<Any>): Any? {
if (args.size != 1)
ScriptRuntime.typeError("invalid arguments: argument length must be 1")
val name = ScriptRuntime.toString(args[0])

importScript(cx, name, scope)

return Undefined.instance
}

override fun getFunctionName(): String = ScriptImporter.importScriptFunctionName

fun init(scope: ScriptableObject) {
usingContext { cx ->
scope.defineProperty(
ImportScriptRhinoFunctionImpl.functionName,
ImportScriptRhinoFunctionImpl,
ScriptableObject.READONLY or ScriptableObject.DONTENUM
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import com.anatawa12.fixRtm.scripting.baseScope
import org.mozilla.javascript.*

@Suppress("FunctionName")
object PrimitiveJavaHelper {
const val internalClassName = "com/anatawa12/fixRtm/scripting/PrimitiveJavaHelper"
const val internalClassName = "com/anatawa12/fixRtm/scripting/rhino/PrimitiveJavaHelper"

const val NativeString_name = "org.mozilla.javascript.NativeString"
const val NativeString_internal = "org/mozilla/javascript/NativeString"
Expand All @@ -25,7 +26,7 @@ object PrimitiveJavaHelper {

private fun wrapFunctionForString(scriptable: Any?): Any? {
if (scriptable is NativeJavaMethod)
return FixRTMWrappedFunction(scriptable, ::getStringInstance)
return FixRTMWrappedFunction(scriptable, PrimitiveJavaHelper::getStringInstance)
else
return scriptable
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import org.mozilla.javascript.*

Expand All @@ -8,8 +8,8 @@ object CoerceTypeImplFailed
* hooks used from Rhino by transformer
*/
object RhinoHooks {
const val className = "com.anatawa12.fixRtm.scripting.RhinoHooks"
const val internalClassName = "com/anatawa12/fixRtm/scripting/RhinoHooks"
const val className = "com.anatawa12.fixRtm.scripting.rhino.RhinoHooks"
const val internalClassName = "com/anatawa12/fixRtm/scripting/rhino/RhinoHooks"

const val coerceTypeImpl_name = "coerceTypeImpl"
const val coerceTypeImpl_desc = "(L${"java/lang/Class"};L${"java/lang/Object"};)L${"java/lang/Object"};"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import com.anatawa12.fixRtm.Loggers
import com.anatawa12.fixRtm.fixCacheDir
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.anatawa12.fixRtm.scripting
package com.anatawa12.fixRtm.scripting.rhino

import org.mozilla.javascript.Context

Expand Down

0 comments on commit 4042cc8

Please sign in to comment.