From 8562cf7f2f28a390776c64d98d663d06e714b278 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 25 Mar 2023 17:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/test/java/io/legado/app/JsTest.kt | 26 +++++++++++++-- .../java/com/script/AbstractScriptEngine.kt | 20 +++++++++-- .../src/main/java/com/script/ScriptEngine.kt | 11 +++++++ .../com/script/rhino/RhinoScriptEngine.kt | 33 +++++++++++++++---- 4 files changed, 80 insertions(+), 10 deletions(-) diff --git a/app/src/test/java/io/legado/app/JsTest.kt b/app/src/test/java/io/legado/app/JsTest.kt index 81cee16cd..c1b4f35e2 100644 --- a/app/src/test/java/io/legado/app/JsTest.kt +++ b/app/src/test/java/io/legado/app/JsTest.kt @@ -5,9 +5,26 @@ import io.legado.app.constant.SCRIPT_ENGINE import org.intellij.lang.annotations.Language import org.junit.Assert import org.junit.Test +import org.mozilla.javascript.Context class JsTest { + @Language("js") + private val printJs = """ + function print(str, newline) { + if (typeof(str) == 'undefined') { + str = 'undefined'; + } else if (str == null) { + str = 'null'; + } + java.lang.System.out.print(String(str)); + if (newline) java.lang.System.out.print("\n"); + } + function println(str) { + print(str, true); + } + """.trimIndent() + @Test fun testMap() { val map = hashMapOf("id" to "3242532321") @@ -20,9 +37,11 @@ class JsTest { Assert.assertEquals("12314123", result) } - @Test fun testFor() { + val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings()) + val scope = SCRIPT_ENGINE.getRuntimeScope(context) + Context.enter().evaluateString(scope, printJs, "print", 1, null) @Language("js") val jsFor = """ let result = 0 @@ -30,16 +49,19 @@ class JsTest { let l=a.length for (let i = 0;i private val implementor: InterfaceImplementor + @Throws(ScriptException::class) + override fun eval(reader: Reader, scope: Scriptable): Any? { + val cx = Context.enter() + val ret: Any? + try { + var filename = this["javax.script.filename"] as? String + filename = filename ?: "" + 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(ScriptException::class) override fun eval(reader: Reader, context: ScriptContext): Any? { val cx = Context.enter() @@ -75,11 +101,6 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { return unwrapReturnValue(ret) } - @Throws(ScriptException::class) - override fun eval(script: String, context: ScriptContext): Any? { - return this.eval(StringReader(script) as Reader, context) - } - override fun createBindings(): Bindings { return SimpleBindings() } @@ -152,7 +173,7 @@ class RhinoScriptEngine : AbstractScriptEngine(), Invocable, Compilable { } } - fun getRuntimeScope(context: ScriptContext): Scriptable { + override fun getRuntimeScope(context: ScriptContext): Scriptable { val newScope: Scriptable = ExternalScriptable(context, indexedProps) newScope.prototype = topLevel newScope.put("context", newScope, context)