Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Mar 8, 2024
1 parent f3be1c3 commit 274ce5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/rhino1.7.3/src/main/java/com/script/CompiledScript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.script

import org.mozilla.javascript.Scriptable
import kotlin.coroutines.CoroutineContext

abstract class CompiledScript {

Expand All @@ -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?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit 274ce5a

Please sign in to comment.