This commit is contained in:
Horis
2024-01-25 22:08:24 +08:00
parent 1ce02e2748
commit bc49727189
17 changed files with 176 additions and 4 deletions
@@ -3,6 +3,8 @@
*/
package com.script
import org.mozilla.javascript.Scriptable
abstract class CompiledScript {
abstract fun getEngine(): ScriptEngine
@@ -10,6 +12,9 @@ abstract class CompiledScript {
@Throws(ScriptException::class)
abstract fun eval(context: ScriptContext): Any?
@Throws(ScriptException::class)
abstract fun eval(scope: Scriptable): Any?
@Throws(ScriptException::class)
fun eval(bindings: Bindings?): Any? {
var ctxt = getEngine().context
@@ -69,4 +69,26 @@ internal class RhinoCompiledScript(
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
}
}