mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -54,6 +54,10 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
|
||||
return getBindings(ENGINE_SCOPE)?.get(key)
|
||||
}
|
||||
|
||||
override fun eval(reader: Reader, scope: Scriptable): Any? {
|
||||
return eval(reader, scope, null)
|
||||
}
|
||||
|
||||
override suspend fun evalSuspend(script: String, scope: Scriptable): Any? {
|
||||
return this.evalSuspend(StringReader(script), scope)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import kotlin.coroutines.CoroutineContext
|
||||
class RhinoContext(factory: ContextFactory) : Context(factory) {
|
||||
|
||||
var coroutineContext: CoroutineContext? = null
|
||||
var allowScriptRun = false
|
||||
|
||||
@Throws(RhinoInterruptError::class)
|
||||
fun ensureActive() {
|
||||
|
||||
@@ -86,32 +86,6 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
return eval(js, bindings)
|
||||
}
|
||||
|
||||
@Throws(ScriptException::class)
|
||||
override fun eval(reader: Reader, scope: Scriptable): Any? {
|
||||
val cx = Context.enter()
|
||||
val ret: Any?
|
||||
try {
|
||||
var filename = this["javax.script.filename"] as? String
|
||||
filename = filename ?: "<Unknown source>"
|
||||
ret = cx.evaluateReader(scope, reader, filename, 1, null)
|
||||
} catch (re: RhinoException) {
|
||||
val line = if (re.lineNumber() == 0) -1 else re.lineNumber()
|
||||
val msg: String = if (re is JavaScriptException) {
|
||||
re.value.toString()
|
||||
} else {
|
||||
re.toString()
|
||||
}
|
||||
val se = ScriptException(msg, re.sourceName(), line)
|
||||
se.initCause(re)
|
||||
throw se
|
||||
} catch (var14: IOException) {
|
||||
throw ScriptException(var14)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
return unwrapReturnValue(ret)
|
||||
}
|
||||
|
||||
override fun eval(
|
||||
reader: Reader,
|
||||
scope: Scriptable,
|
||||
@@ -120,6 +94,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
val cx = Context.enter() as RhinoContext
|
||||
val previousCoroutineContext = cx.coroutineContext
|
||||
cx.coroutineContext = coroutineContext
|
||||
cx.allowScriptRun = true
|
||||
val ret: Any?
|
||||
try {
|
||||
var filename = this["javax.script.filename"] as? String
|
||||
@@ -139,6 +114,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
throw ScriptException(var14)
|
||||
} finally {
|
||||
cx.coroutineContext = previousCoroutineContext
|
||||
cx.allowScriptRun = false
|
||||
Context.exit()
|
||||
}
|
||||
return unwrapReturnValue(ret)
|
||||
@@ -146,9 +122,10 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
|
||||
@Throws(ContinuationPending::class)
|
||||
override suspend fun evalSuspend(reader: Reader, scope: Scriptable): Any? {
|
||||
val cx = Context.enter()
|
||||
val cx = Context.enter() as RhinoContext
|
||||
var ret: Any?
|
||||
withContext(VMBridgeReflect.contextLocal.asContextElement()) {
|
||||
cx.allowScriptRun = true
|
||||
try {
|
||||
var filename = this@RhinoScriptEngine["javax.script.filename"] as? String
|
||||
filename = filename ?: "<Unknown source>"
|
||||
@@ -186,6 +163,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
} catch (var14: IOException) {
|
||||
throw ScriptException(var14)
|
||||
} finally {
|
||||
cx.allowScriptRun = false
|
||||
Context.exit()
|
||||
}
|
||||
}
|
||||
@@ -395,7 +373,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
args: Array<Any>
|
||||
): Any? {
|
||||
try {
|
||||
(cx as RhinoContext).ensureActive()
|
||||
if (cx is RhinoContext) {
|
||||
if (!cx.allowScriptRun) {
|
||||
error("Not allow run script in unauthorized way.")
|
||||
}
|
||||
cx.ensureActive()
|
||||
}
|
||||
return super.doTopCall(callable, cx, scope, thisObj, args)
|
||||
} catch (e: RhinoInterruptError) {
|
||||
throw e.cause
|
||||
|
||||
@@ -55,6 +55,7 @@ object RhinoWrapFactory : WrapFactory() {
|
||||
is ClassLoader,
|
||||
is Class<*>,
|
||||
is Member,
|
||||
is Context,
|
||||
is android.content.Context -> {
|
||||
null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user