mirror of
https://github.com/gedoor/legado.git
synced 2024-07-04 23:36:56 +08:00
优化
This commit is contained in:
parent
f3be1c36b8
commit
274ce5a6ea
|
@ -4,6 +4,7 @@
|
|||
package com.script
|
||||
|
||||
import org.mozilla.javascript.Scriptable
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
abstract class CompiledScript {
|
||||
|
||||
|
@ -15,6 +16,9 @@ abstract class CompiledScript {
|
|||
@Throws(ScriptException::class)
|
||||
abstract fun eval(scope: Scriptable): Any?
|
||||
|
||||
@Throws(ScriptException::class)
|
||||
abstract fun eval(scope: Scriptable, coroutineContext: CoroutineContext?): Any?
|
||||
|
||||
@Throws(ScriptException::class)
|
||||
abstract suspend fun evalSuspend(scope: Scriptable): Any?
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ import kotlinx.coroutines.withContext
|
|||
import org.mozilla.javascript.*
|
||||
import java.io.IOException
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
|
||||
|
||||
/**
|
||||
|
@ -96,6 +97,31 @@ internal class RhinoCompiledScript(
|
|||
return result
|
||||
}
|
||||
|
||||
override fun eval(scope: Scriptable, coroutineContext: CoroutineContext?): Any? {
|
||||
val cx = Context.enter()
|
||||
if (cx is RhinoContext) {
|
||||
cx.coroutineContext = coroutineContext
|
||||
}
|
||||
val result: Any?
|
||||
try {
|
||||
val ret = script.exec(cx, scope)
|
||||
result = engine.unwrapReturnValue(ret)
|
||||
} 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
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
override suspend fun evalSuspend(scope: Scriptable): Any? {
|
||||
val cx = Context.enter()
|
||||
var ret: Any?
|
||||
|
|
Loading…
Reference in New Issue
Block a user