|
@@ -35,6 +35,7 @@ import java.io.StringReader
|
|
|
import java.lang.reflect.Method
|
|
import java.lang.reflect.Method
|
|
|
import java.security.*
|
|
import java.security.*
|
|
|
import kotlin.coroutines.Continuation
|
|
import kotlin.coroutines.Continuation
|
|
|
|
|
+import kotlin.coroutines.CoroutineContext
|
|
|
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
|
|
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -84,6 +85,38 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|
|
return unwrapReturnValue(ret)
|
|
return unwrapReturnValue(ret)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ override fun eval(
|
|
|
|
|
+ reader: Reader,
|
|
|
|
|
+ scope: Scriptable,
|
|
|
|
|
+ coroutineContext: CoroutineContext?
|
|
|
|
|
+ ): Any? {
|
|
|
|
|
+ val cx = Context.enter()
|
|
|
|
|
+ if (cx is RhinoContext) {
|
|
|
|
|
+ cx.coroutineContext = coroutineContext
|
|
|
|
|
+ }
|
|
|
|
|
+ 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)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@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()
|
|
@@ -259,11 +292,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|
|
ContextFactory.initGlobal(object : ContextFactory() {
|
|
ContextFactory.initGlobal(object : ContextFactory() {
|
|
|
|
|
|
|
|
override fun makeContext(): Context {
|
|
override fun makeContext(): Context {
|
|
|
- val cx = super.makeContext()
|
|
|
|
|
|
|
+ val cx = RhinoContext(this)
|
|
|
cx.languageVersion = 200
|
|
cx.languageVersion = 200
|
|
|
cx.optimizationLevel = -1
|
|
cx.optimizationLevel = -1
|
|
|
cx.setClassShutter(RhinoClassShutter)
|
|
cx.setClassShutter(RhinoClassShutter)
|
|
|
cx.wrapFactory = RhinoWrapFactory
|
|
cx.wrapFactory = RhinoWrapFactory
|
|
|
|
|
+ cx.instructionObserverThreshold = 10000
|
|
|
return cx
|
|
return cx
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -275,6 +309,12 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ override fun observeInstructionCount(cx: Context, instructionCount: Int) {
|
|
|
|
|
+ if (cx is RhinoContext) {
|
|
|
|
|
+ cx.ensureActive()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
override fun doTopCall(
|
|
override fun doTopCall(
|
|
|
callable: Callable,
|
|
callable: Callable,
|
|
|
cx: Context,
|
|
cx: Context,
|
|
@@ -308,7 +348,11 @@ object RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable {
|
|
|
thisObj: Scriptable?,
|
|
thisObj: Scriptable?,
|
|
|
args: Array<Any>
|
|
args: Array<Any>
|
|
|
): Any? {
|
|
): Any? {
|
|
|
- return super.doTopCall(callable, cx, scope, thisObj, args)
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ return super.doTopCall(callable, cx, scope, thisObj, args)
|
|
|
|
|
+ } catch (e: RhinoInterruptError) {
|
|
|
|
|
+ throw e.cause
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|