优化
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import com.script.rhino.RhinoContext
|
||||
import com.script.rhino.RhinoScriptEngine
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
@@ -7,6 +9,9 @@ import org.mozilla.javascript.Context
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.coroutines.ContinuationInterceptor
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
@Suppress("LEAKED_IN_PLACE_LAMBDA", "WRONG_INVOCATION_KIND")
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
@@ -29,3 +34,28 @@ inline fun <T> suspendContinuation(crossinline block: suspend CoroutineScope.()
|
||||
Context.exit()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> runScriptWithContext(context: CoroutineContext, block: () -> T): T {
|
||||
RhinoScriptEngine
|
||||
val rhinoContext = Context.enter() as RhinoContext
|
||||
val previousCoroutineContext = rhinoContext.coroutineContext
|
||||
rhinoContext.coroutineContext = context.minusKey(ContinuationInterceptor)
|
||||
try {
|
||||
return block()
|
||||
} finally {
|
||||
rhinoContext.coroutineContext = previousCoroutineContext
|
||||
Context.exit()
|
||||
}
|
||||
}
|
||||
|
||||
suspend inline fun <T> runScriptWithContext(block: () -> T): T {
|
||||
val rhinoContext = Context.enter() as RhinoContext
|
||||
val previousCoroutineContext = rhinoContext.coroutineContext
|
||||
rhinoContext.coroutineContext = coroutineContext.minusKey(ContinuationInterceptor)
|
||||
try {
|
||||
return block()
|
||||
} finally {
|
||||
rhinoContext.coroutineContext = previousCoroutineContext
|
||||
Context.exit()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,9 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
||||
): Any? {
|
||||
val cx = Context.enter() as RhinoContext
|
||||
val previousCoroutineContext = cx.coroutineContext
|
||||
cx.coroutineContext = coroutineContext
|
||||
if (coroutineContext != null) {
|
||||
cx.coroutineContext = coroutineContext
|
||||
}
|
||||
cx.allowScriptRun = true
|
||||
val ret: Any?
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user