优化
This commit is contained in:
@@ -54,6 +54,10 @@ abstract class AbstractScriptEngine(val bindings: Bindings? = null) : ScriptEngi
|
|||||||
return getBindings(ENGINE_SCOPE)?.get(key)
|
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? {
|
override suspend fun evalSuspend(script: String, scope: Scriptable): Any? {
|
||||||
return this.evalSuspend(StringReader(script), scope)
|
return this.evalSuspend(StringReader(script), scope)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import kotlin.coroutines.CoroutineContext
|
|||||||
class RhinoContext(factory: ContextFactory) : Context(factory) {
|
class RhinoContext(factory: ContextFactory) : Context(factory) {
|
||||||
|
|
||||||
var coroutineContext: CoroutineContext? = null
|
var coroutineContext: CoroutineContext? = null
|
||||||
|
var allowScriptRun = false
|
||||||
|
|
||||||
@Throws(RhinoInterruptError::class)
|
@Throws(RhinoInterruptError::class)
|
||||||
fun ensureActive() {
|
fun ensureActive() {
|
||||||
|
|||||||
@@ -86,32 +86,6 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
return eval(js, bindings)
|
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(
|
override fun eval(
|
||||||
reader: Reader,
|
reader: Reader,
|
||||||
scope: Scriptable,
|
scope: Scriptable,
|
||||||
@@ -120,6 +94,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
val cx = Context.enter() as RhinoContext
|
val cx = Context.enter() as RhinoContext
|
||||||
val previousCoroutineContext = cx.coroutineContext
|
val previousCoroutineContext = cx.coroutineContext
|
||||||
cx.coroutineContext = coroutineContext
|
cx.coroutineContext = coroutineContext
|
||||||
|
cx.allowScriptRun = true
|
||||||
val ret: Any?
|
val ret: Any?
|
||||||
try {
|
try {
|
||||||
var filename = this["javax.script.filename"] as? String
|
var filename = this["javax.script.filename"] as? String
|
||||||
@@ -139,6 +114,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
throw ScriptException(var14)
|
throw ScriptException(var14)
|
||||||
} finally {
|
} finally {
|
||||||
cx.coroutineContext = previousCoroutineContext
|
cx.coroutineContext = previousCoroutineContext
|
||||||
|
cx.allowScriptRun = false
|
||||||
Context.exit()
|
Context.exit()
|
||||||
}
|
}
|
||||||
return unwrapReturnValue(ret)
|
return unwrapReturnValue(ret)
|
||||||
@@ -146,9 +122,10 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
|
|
||||||
@Throws(ContinuationPending::class)
|
@Throws(ContinuationPending::class)
|
||||||
override suspend fun evalSuspend(reader: Reader, scope: Scriptable): Any? {
|
override suspend fun evalSuspend(reader: Reader, scope: Scriptable): Any? {
|
||||||
val cx = Context.enter()
|
val cx = Context.enter() as RhinoContext
|
||||||
var ret: Any?
|
var ret: Any?
|
||||||
withContext(VMBridgeReflect.contextLocal.asContextElement()) {
|
withContext(VMBridgeReflect.contextLocal.asContextElement()) {
|
||||||
|
cx.allowScriptRun = true
|
||||||
try {
|
try {
|
||||||
var filename = this@RhinoScriptEngine["javax.script.filename"] as? String
|
var filename = this@RhinoScriptEngine["javax.script.filename"] as? String
|
||||||
filename = filename ?: "<Unknown source>"
|
filename = filename ?: "<Unknown source>"
|
||||||
@@ -186,6 +163,7 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
} catch (var14: IOException) {
|
} catch (var14: IOException) {
|
||||||
throw ScriptException(var14)
|
throw ScriptException(var14)
|
||||||
} finally {
|
} finally {
|
||||||
|
cx.allowScriptRun = false
|
||||||
Context.exit()
|
Context.exit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -395,7 +373,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|||||||
args: Array<Any>
|
args: Array<Any>
|
||||||
): Any? {
|
): Any? {
|
||||||
try {
|
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)
|
return super.doTopCall(callable, cx, scope, thisObj, args)
|
||||||
} catch (e: RhinoInterruptError) {
|
} catch (e: RhinoInterruptError) {
|
||||||
throw e.cause
|
throw e.cause
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ object RhinoWrapFactory : WrapFactory() {
|
|||||||
is ClassLoader,
|
is ClassLoader,
|
||||||
is Class<*>,
|
is Class<*>,
|
||||||
is Member,
|
is Member,
|
||||||
|
is Context,
|
||||||
is android.content.Context -> {
|
is android.content.Context -> {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user