-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/anatawa12/fixRtm/scripting/nashorn/CompiledImportedScriptCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.anatawa12.fixRtm.scripting.nashorn | ||
|
||
import com.anatawa12.fixRtm.utils.DigestUtils | ||
import jdk.nashorn.api.scripting.NashornScriptEngine | ||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory | ||
import java.util.concurrent.ConcurrentHashMap | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
import javax.script.CompiledScript | ||
import javax.script.ScriptContext | ||
|
||
//* | ||
object CompiledImportedScriptCache { | ||
private val local = ThreadLocal.withInitial { Threaded() } | ||
private val threaded get() = local.get()!! | ||
private val threadedByEngine = ConcurrentHashMap<NashornScriptEngine, Threaded>() | ||
|
||
private fun threadedByEngine(engine: NashornScriptEngine?): Threaded { | ||
if (engine == null) return threaded | ||
else return threadedByEngine[engine] ?: throw IllegalArgumentException("the engine is not threaded") | ||
} | ||
|
||
val engineFactory = NashornScriptEngineFactory() | ||
val engines = CopyOnWriteArraySet<NashornScriptEngine>() | ||
|
||
val engine get() = threaded.engine | ||
|
||
fun makeCompiled(script: String, fileName: String): CompiledScript | ||
= threaded.makeCompiled(script, fileName) | ||
|
||
fun makeCompiled(engine: NashornScriptEngine?, script: String, fileName: String): CompiledScript | ||
= threadedByEngine(engine).makeCompiled(script, fileName) | ||
|
||
fun load() { | ||
// load | ||
} | ||
|
||
private class Threaded { | ||
val engine = engineFactory.scriptEngine as NashornScriptEngine | ||
|
||
init { | ||
engines.add(engine) | ||
threadedByEngine[engine] = this | ||
} | ||
|
||
private val compiledScripts = mutableMapOf<String, CompiledScript>() | ||
|
||
fun makeCompiled(script: String, fileName: String): CompiledScript { | ||
engine.context.setAttribute("javax.script.filename", fileName, ScriptContext.ENGINE_SCOPE) | ||
val compile = engine.compile(script) | ||
compiledScripts[DigestUtils.sha1Hex(script)] = compile | ||
engine.context.removeAttribute("javax.script.filename", ScriptContext.ENGINE_SCOPE) | ||
return compile | ||
} | ||
|
||
} | ||
} | ||
// */ |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/anatawa12/fixRtm/scripting/nashorn/FIXNashornScriptEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.anatawa12.fixRtm.scripting.nashorn | ||
|
||
import jdk.nashorn.api.scripting.NashornScriptEngine | ||
import java.io.Reader | ||
import javax.script.* | ||
|
||
class FIXNashornScriptEngine( | ||
val baseEngine: NashornScriptEngine, | ||
private val context: ScriptContext | ||
) : ScriptEngine by baseEngine, Compilable, Invocable { | ||
|
||
override fun getFactory(): ScriptEngineFactory { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun setContext(context: ScriptContext) { | ||
throw UnsupportedOperationException("setContext is not supported by FIXNashornScriptEngine") | ||
} | ||
|
||
override fun eval(script: String): Any? = baseEngine.eval(script, context) | ||
|
||
override fun eval(reader: Reader): Any? = baseEngine.eval(reader, context) | ||
|
||
override fun getBindings(scope: Int): Bindings = context.getBindings(scope) | ||
|
||
override fun put(name: String, value: Any?) { | ||
context.getBindings(ScriptContext.ENGINE_SCOPE)?.put(name, value) | ||
} | ||
|
||
override fun get(name: String): Any? = context.getBindings(ScriptContext.ENGINE_SCOPE)?.get(name) | ||
|
||
override fun setBindings(bindings: Bindings?, scope: Int) { | ||
context.setBindings(bindings, scope) | ||
} | ||
|
||
override fun getContext(): ScriptContext = context | ||
|
||
override fun compile(script: String): CompiledScript { | ||
val compiled = baseEngine.compile(script) | ||
return object : CompiledScript() { | ||
override fun eval(context: ScriptContext): Any = compiled.eval(context) | ||
|
||
override fun getEngine(): ScriptEngine = this@FIXNashornScriptEngine | ||
} | ||
} | ||
|
||
override fun compile(script: Reader): CompiledScript { | ||
val compiled = baseEngine.compile(script) | ||
return object : CompiledScript() { | ||
override fun eval(context: ScriptContext): Any = compiled.eval(context) | ||
|
||
override fun getEngine(): ScriptEngine = this@FIXNashornScriptEngine | ||
} | ||
} | ||
|
||
override fun <T : Any> getInterface(clasz: Class<T>): T? { | ||
throw UnsupportedOperationException("getInterface is not supported by FIXNashornScriptEngine") | ||
} | ||
|
||
override fun <T : Any> getInterface(thiz: Any, clasz: Class<T>): T? { | ||
throw UnsupportedOperationException("getInterface is not supported by FIXNashornScriptEngine") | ||
} | ||
|
||
override fun invokeMethod(thiz: Any, name: String, vararg args: Any?): Any? { | ||
return baseEngine.invokeMethod(thiz, name, *args) | ||
} | ||
|
||
override fun invokeFunction(name: String, vararg args: Any?): Any? { | ||
return baseEngine.invokeMethod(context.getBindings(ScriptContext.ENGINE_SCOPE), name, *args) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/anatawa12/fixRtm/scripting/nashorn/ImportScriptNashornFunctionImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.anatawa12.fixRtm.scripting.nashorn | ||
|
||
import com.anatawa12.fixRtm.io.FIXFileLoader | ||
import jdk.nashorn.api.scripting.AbstractJSObject | ||
import jdk.nashorn.api.scripting.NashornScriptEngine | ||
import jdk.nashorn.api.scripting.ScriptObjectMirror | ||
import net.minecraft.util.ResourceLocation | ||
import javax.script.CompiledScript | ||
import javax.script.ScriptContext | ||
import javax.script.SimpleScriptContext | ||
|
||
//* | ||
object ImportScriptNashornFunctionImpl : AbstractJSObject() { | ||
override fun isStrictFunction(): Boolean = false | ||
|
||
private val ctx = SimpleScriptContext() | ||
|
||
private fun getCompiled(engine: NashornScriptEngine?, name: String): CompiledScript { | ||
val resourceLocation = ResourceLocation(name) | ||
val resource = FIXFileLoader.getResource(resourceLocation) | ||
val script = resource.inputStream.reader().use { it.readText() } | ||
return NashornScriptRuntimeImpl.compile(resourceLocation, script, resource.pack, | ||
engine = engine?.let { FIXNashornScriptEngine(it, ctx) }) | ||
} | ||
|
||
override fun call(thiz: Any?, vararg args: Any?): Any? { | ||
val str = args.getOrNull(0) | ||
|
||
if (str !is String) throw IllegalArgumentException("argument#1 must be string") | ||
if (thiz !is ScriptObjectMirror) throw IllegalArgumentException("this must be a scope") | ||
|
||
val engine = thiz.getMember("engine") as? NashornScriptEngine | ||
|
||
if (engine != null && engine !in CompiledImportedScriptCache.engines) | ||
throw IllegalStateException("current engine is not current thread engine") | ||
|
||
val compiled = getCompiled(engine, str.toString()) | ||
|
||
val context = SimpleScriptContext() | ||
context.setBindings(thiz, ScriptContext.ENGINE_SCOPE) | ||
|
||
return compiled.eval(context) | ||
} | ||
|
||
override fun isFunction(): Boolean = true | ||
|
||
override fun getClassName(): String = "Function" | ||
|
||
override fun isArray(): Boolean = false | ||
} | ||
// */ |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/anatawa12/fixRtm/scripting/nashorn/NashornScriptRuntimeImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
@file:JvmName("FIXScriptUtil") | ||
|
||
package com.anatawa12.fixRtm.scripting.nashorn | ||
|
||
import com.anatawa12.fixRtm.io.FIXModelPack | ||
import com.anatawa12.fixRtm.io.FIXResource | ||
import com.anatawa12.fixRtm.scripting.IScriptRuntime | ||
import com.anatawa12.fixRtm.scripting.ScriptImporter | ||
import net.minecraft.util.ResourceLocation | ||
import javax.script.CompiledScript | ||
import javax.script.ScriptContext | ||
import javax.script.SimpleScriptContext | ||
|
||
object NashornScriptRuntimeImpl : IScriptRuntime<CompiledScript, FIXNashornScriptEngine> { | ||
/** | ||
* nashorn does not support executed cache so always return null. | ||
* @return always null | ||
*/ | ||
override fun getCachedEngine(filePath: ResourceLocation, resource: FIXResource, dependencies: Map<ResourceLocation, String>): FIXNashornScriptEngine? { | ||
return null | ||
} | ||
|
||
override fun compile(script: String, fileName: String, engine: FIXNashornScriptEngine?): CompiledScript { | ||
return CompiledImportedScriptCache.makeCompiled(engine?.baseEngine, script, fileName) | ||
} | ||
|
||
override fun exec(script: CompiledScript): FIXNashornScriptEngine { | ||
val engine = CompiledImportedScriptCache.engine | ||
|
||
val context = SimpleScriptContext() | ||
val bindings = engine.createBindings() | ||
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE) | ||
|
||
engine.eval("""load("nashorn:mozilla_compat.js");""", context) | ||
|
||
bindings[ScriptImporter.importScriptFunctionName] = ImportScriptNashornFunctionImpl | ||
|
||
script.eval(context) | ||
|
||
return FIXNashornScriptEngine(engine, context) | ||
} | ||
|
||
override fun cache(pack: FIXModelPack, filePath: ResourceLocation, dependencies: Map<ResourceLocation, String>, engine: FIXNashornScriptEngine) { | ||
// not supported so nop | ||
} | ||
} |