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