mirror of
https://github.com/gedoor/legado.git
synced 2024-07-04 23:36:56 +08:00
优化
This commit is contained in:
parent
2680173f6c
commit
53bfea9e67
|
@ -3,6 +3,8 @@
|
|||
*/
|
||||
package com.script
|
||||
|
||||
import com.script.ScriptContext.Companion.ENGINE_SCOPE
|
||||
import com.script.ScriptContext.Companion.GLOBAL_SCOPE
|
||||
import org.mozilla.javascript.Scriptable
|
||||
import java.io.Reader
|
||||
import java.io.StringReader
|
||||
|
@ -14,28 +16,28 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
|
|||
|
||||
init {
|
||||
bindings?.let {
|
||||
context.setBindings(bindings, 100)
|
||||
context.setBindings(bindings, ENGINE_SCOPE)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBindings(scope: Int): Bindings? {
|
||||
if (scope == 200) {
|
||||
return context.getBindings(200)
|
||||
if (scope == GLOBAL_SCOPE) {
|
||||
return context.getBindings(GLOBAL_SCOPE)
|
||||
}
|
||||
if (scope == 100) {
|
||||
return context.getBindings(100)
|
||||
if (scope == ENGINE_SCOPE) {
|
||||
return context.getBindings(ENGINE_SCOPE)
|
||||
}
|
||||
throw IllegalArgumentException("Invalid scope value.")
|
||||
}
|
||||
|
||||
override fun setBindings(bindings: Bindings?, scope: Int) {
|
||||
when (scope) {
|
||||
200 -> {
|
||||
context.setBindings(bindings, 200)
|
||||
GLOBAL_SCOPE -> {
|
||||
context.setBindings(bindings, GLOBAL_SCOPE)
|
||||
}
|
||||
|
||||
100 -> {
|
||||
context.setBindings(bindings, 100)
|
||||
ENGINE_SCOPE -> {
|
||||
context.setBindings(bindings, ENGINE_SCOPE)
|
||||
}
|
||||
|
||||
else -> {
|
||||
|
@ -45,11 +47,11 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
|
|||
}
|
||||
|
||||
override fun put(key: String, value: Any?) {
|
||||
getBindings(100)?.put(key, value)
|
||||
getBindings(ENGINE_SCOPE)?.put(key, value)
|
||||
}
|
||||
|
||||
override fun get(key: String): Any? {
|
||||
return getBindings(100)?.get(key)
|
||||
return getBindings(ENGINE_SCOPE)?.get(key)
|
||||
}
|
||||
|
||||
override suspend fun evalSuspend(script: String, scope: Scriptable): Any? {
|
||||
|
@ -96,9 +98,9 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
|
|||
|
||||
override fun getScriptContext(bindings: Bindings): ScriptContext {
|
||||
val ctx = SimpleScriptContext(bindings, context.errorWriter, context.reader, context.writer)
|
||||
val gs = getBindings(200)
|
||||
val gs = getBindings(GLOBAL_SCOPE)
|
||||
if (gs != null) {
|
||||
ctx.setBindings(gs, 200)
|
||||
ctx.setBindings(gs, GLOBAL_SCOPE)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
*/
|
||||
package com.script
|
||||
|
||||
import com.script.ScriptContext.Companion.ENGINE_SCOPE
|
||||
import com.script.ScriptContext.Companion.GLOBAL_SCOPE
|
||||
import org.mozilla.javascript.Scriptable
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
@ -27,8 +29,8 @@ abstract class CompiledScript {
|
|||
var ctxt = getEngine().context
|
||||
if (bindings != null) {
|
||||
val tempContext = SimpleScriptContext()
|
||||
tempContext.setBindings(bindings, 100)
|
||||
tempContext.setBindings(ctxt.getBindings(200), 200)
|
||||
tempContext.setBindings(bindings, ENGINE_SCOPE)
|
||||
tempContext.setBindings(ctxt.getBindings(GLOBAL_SCOPE), GLOBAL_SCOPE)
|
||||
tempContext.writer = ctxt.writer
|
||||
tempContext.reader = ctxt.reader
|
||||
tempContext.errorWriter = ctxt.errorWriter
|
||||
|
|
|
@ -3,11 +3,12 @@
|
|||
*/
|
||||
package com.script
|
||||
|
||||
import com.script.ScriptContext.Companion.ENGINE_SCOPE
|
||||
import com.script.ScriptContext.Companion.GLOBAL_SCOPE
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.io.Reader
|
||||
import java.io.Writer
|
||||
import java.util.*
|
||||
|
||||
open class SimpleScriptContext(
|
||||
private var engineScope: Bindings = SimpleBindings(),
|
||||
|
@ -19,7 +20,7 @@ open class SimpleScriptContext(
|
|||
|
||||
override fun setBindings(bindings: Bindings?, scope: Int) {
|
||||
when (scope) {
|
||||
100 -> {
|
||||
ENGINE_SCOPE -> {
|
||||
if (bindings == null) {
|
||||
throw NullPointerException("Engine scope cannot be null.")
|
||||
}
|
||||
|
@ -27,7 +28,7 @@ open class SimpleScriptContext(
|
|||
return
|
||||
}
|
||||
|
||||
200 -> {
|
||||
GLOBAL_SCOPE -> {
|
||||
globalScope = bindings
|
||||
return
|
||||
}
|
||||
|
@ -36,21 +37,22 @@ open class SimpleScriptContext(
|
|||
}
|
||||
|
||||
override fun getAttribute(name: String): Any? {
|
||||
if (engineScope.containsKey(name)) {
|
||||
return this.getAttribute(name, 100)
|
||||
}
|
||||
return if (globalScope?.containsKey(name) != true) {
|
||||
return if (engineScope.containsKey(name)) {
|
||||
this.getAttribute(name, ENGINE_SCOPE)
|
||||
} else if (globalScope?.containsKey(name) == true) {
|
||||
this.getAttribute(name, GLOBAL_SCOPE)
|
||||
} else {
|
||||
null
|
||||
} else this.getAttribute(name, 200)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getAttribute(name: String, scope: Int): Any? {
|
||||
when (scope) {
|
||||
100 -> {
|
||||
ENGINE_SCOPE -> {
|
||||
return engineScope[name]
|
||||
}
|
||||
|
||||
200 -> {
|
||||
GLOBAL_SCOPE -> {
|
||||
return globalScope?.get(name)
|
||||
}
|
||||
}
|
||||
|
@ -59,12 +61,12 @@ open class SimpleScriptContext(
|
|||
|
||||
override fun removeAttribute(name: String, scope: Int): Any? {
|
||||
when (scope) {
|
||||
100 -> {
|
||||
return getBindings(100)?.remove(name)
|
||||
ENGINE_SCOPE -> {
|
||||
return getBindings(ENGINE_SCOPE)?.remove(name)
|
||||
}
|
||||
|
||||
200 -> {
|
||||
return getBindings(200)?.remove(name)
|
||||
GLOBAL_SCOPE -> {
|
||||
return getBindings(GLOBAL_SCOPE)?.remove(name)
|
||||
}
|
||||
}
|
||||
throw IllegalArgumentException("Illegal scope value.")
|
||||
|
@ -72,33 +74,27 @@ open class SimpleScriptContext(
|
|||
|
||||
override fun setAttribute(name: String, value: Any?, scope: Int) {
|
||||
when (scope) {
|
||||
100 -> {
|
||||
engineScope[name] = value
|
||||
return
|
||||
}
|
||||
|
||||
200 -> {
|
||||
globalScope?.put(name, value)
|
||||
return
|
||||
}
|
||||
ENGINE_SCOPE -> engineScope[name] = value
|
||||
GLOBAL_SCOPE -> globalScope?.put(name, value)
|
||||
else -> throw IllegalArgumentException("Illegal scope value.")
|
||||
}
|
||||
throw IllegalArgumentException("Illegal scope value.")
|
||||
}
|
||||
|
||||
override fun getAttributesScope(name: String): Int {
|
||||
if (engineScope.containsKey(name)) {
|
||||
return 100
|
||||
}
|
||||
return if (globalScope?.containsKey(name) != true) {
|
||||
return if (engineScope.containsKey(name)) {
|
||||
ENGINE_SCOPE
|
||||
} else if (globalScope?.containsKey(name) == true) {
|
||||
GLOBAL_SCOPE
|
||||
} else {
|
||||
-1
|
||||
} else 200
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBindings(scope: Int): Bindings? {
|
||||
if (scope == 100) {
|
||||
if (scope == ENGINE_SCOPE) {
|
||||
return engineScope
|
||||
}
|
||||
if (scope == 200) {
|
||||
if (scope == GLOBAL_SCOPE) {
|
||||
return globalScope
|
||||
}
|
||||
throw IllegalArgumentException("Illegal scope value.")
|
||||
|
@ -108,12 +104,6 @@ open class SimpleScriptContext(
|
|||
get() = Companion.scopes
|
||||
|
||||
companion object {
|
||||
private var scopes: MutableList<Int> = ArrayList(2)
|
||||
|
||||
init {
|
||||
scopes.add(100)
|
||||
scopes.add(200)
|
||||
scopes = Collections.unmodifiableList(scopes)
|
||||
}
|
||||
private val scopes = listOf(ENGINE_SCOPE, GLOBAL_SCOPE)
|
||||
}
|
||||
}
|
|
@ -239,8 +239,13 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||
|
||||
override fun getRuntimeScope(context: ScriptContext): Scriptable {
|
||||
val newScope: Scriptable = ExternalScriptable(context, indexedProps)
|
||||
newScope.prototype = topLevel
|
||||
newScope.put("context", newScope, context)
|
||||
val cx = Context.enter()
|
||||
try {
|
||||
newScope.prototype = RhinoTopLevel(cx, this)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
//newScope.put("context", newScope, context)
|
||||
return newScope
|
||||
}
|
||||
|
||||
|
@ -298,6 +303,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||
cx.setClassShutter(RhinoClassShutter)
|
||||
cx.wrapFactory = RhinoWrapFactory
|
||||
cx.instructionObserverThreshold = 10000
|
||||
cx.maximumInterpreterStackDepth = 1000
|
||||
return cx
|
||||
}
|
||||
|
||||
|
|
|
@ -27,8 +27,12 @@ package com.script.rhino
|
|||
import com.script.Bindings
|
||||
import com.script.ScriptContext
|
||||
import com.script.SimpleScriptContext
|
||||
import org.mozilla.javascript.*
|
||||
import org.mozilla.javascript.Context
|
||||
import org.mozilla.javascript.Function
|
||||
import org.mozilla.javascript.ImporterTopLevel
|
||||
import org.mozilla.javascript.Scriptable
|
||||
import org.mozilla.javascript.Synchronizer
|
||||
import org.mozilla.javascript.Wrapper
|
||||
import java.security.AccessControlContext
|
||||
|
||||
/**
|
||||
|
@ -44,10 +48,10 @@ class RhinoTopLevel(cx: Context, val scriptEngine: RhinoScriptEngine) :
|
|||
ImporterTopLevel(cx, System.getSecurityManager() != null) {
|
||||
|
||||
init {
|
||||
LazilyLoadedCtor(this, "JSAdapter", "com.script.rhino.JSAdapter", false)
|
||||
JavaAdapter.init(cx, this, false)
|
||||
val names = arrayOf("bindings", "scope", "sync")
|
||||
defineFunctionProperties(names, RhinoTopLevel::class.java, 2)
|
||||
// LazilyLoadedCtor(this, "JSAdapter", "com.script.rhino.JSAdapter", false)
|
||||
// JavaAdapter.init(cx, this, false)
|
||||
// val names = arrayOf("bindings", "scope", "sync")
|
||||
// defineFunctionProperties(names, RhinoTopLevel::class.java, 2)
|
||||
}
|
||||
|
||||
val accessContext: AccessControlContext?
|
||||
|
|
Loading…
Reference in New Issue
Block a user