|
|
@@ -25,12 +25,17 @@
|
|
|
package com.script.rhino
|
|
|
|
|
|
import com.script.CompiledScript
|
|
|
-import com.script.ScriptContext
|
|
|
import com.script.ScriptEngine
|
|
|
import com.script.ScriptException
|
|
|
+import kotlinx.coroutines.Job
|
|
|
import kotlinx.coroutines.asContextElement
|
|
|
import kotlinx.coroutines.withContext
|
|
|
-import org.mozilla.javascript.*
|
|
|
+import org.mozilla.javascript.Context
|
|
|
+import org.mozilla.javascript.ContinuationPending
|
|
|
+import org.mozilla.javascript.JavaScriptException
|
|
|
+import org.mozilla.javascript.RhinoException
|
|
|
+import org.mozilla.javascript.Script
|
|
|
+import org.mozilla.javascript.Scriptable
|
|
|
import java.io.IOException
|
|
|
import kotlin.coroutines.Continuation
|
|
|
import kotlin.coroutines.CoroutineContext
|
|
|
@@ -51,56 +56,15 @@ internal class RhinoCompiledScript(
|
|
|
return engine
|
|
|
}
|
|
|
|
|
|
- @Throws(ScriptException::class)
|
|
|
- override fun eval(context: ScriptContext): Any? {
|
|
|
- val cx = Context.enter()
|
|
|
- val result: Any?
|
|
|
- try {
|
|
|
- val scope = engine.getRuntimeScope(context)
|
|
|
- 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 fun eval(scope: Scriptable): Any? {
|
|
|
- val cx = Context.enter()
|
|
|
- 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 fun eval(scope: Scriptable, coroutineContext: CoroutineContext?): Any? {
|
|
|
val cx = Context.enter() as RhinoContext
|
|
|
+ cx.checkRecursive()
|
|
|
val previousCoroutineContext = cx.coroutineContext
|
|
|
- cx.coroutineContext = coroutineContext
|
|
|
+ if (coroutineContext != null && coroutineContext[Job] != null) {
|
|
|
+ cx.coroutineContext = coroutineContext
|
|
|
+ }
|
|
|
+ cx.allowScriptRun = true
|
|
|
+ cx.recursiveCount++
|
|
|
val result: Any?
|
|
|
try {
|
|
|
val ret = script.exec(cx, scope)
|
|
|
@@ -117,6 +81,8 @@ internal class RhinoCompiledScript(
|
|
|
throw se
|
|
|
} finally {
|
|
|
cx.coroutineContext = previousCoroutineContext
|
|
|
+ cx.allowScriptRun = false
|
|
|
+ cx.recursiveCount--
|
|
|
Context.exit()
|
|
|
}
|
|
|
return result
|