优化
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package io.legado.app
|
package io.legado.app
|
||||||
|
|
||||||
import io.legado.app.rhino.Rhino
|
import io.legado.app.rhino.Rhino
|
||||||
|
import io.legado.app.rhino.putBinding
|
||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
@@ -46,15 +47,31 @@ class AndroidJsTest {
|
|||||||
val result1 = Rhino.use {
|
val result1 = Rhino.use {
|
||||||
evaluateString(initStandardObjects(), js1, "xx", 1, null)
|
evaluateString(initStandardObjects(), js1, "xx", 1, null)
|
||||||
}
|
}
|
||||||
Assert.assertEquals(result1, "未知错误,请联系开发者!")
|
Assert.assertEquals(result1, "未知错误,请联系开发者!").let {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testReturnNull() {
|
fun testMap() {
|
||||||
|
val map = hashMapOf("id" to "3242532321")
|
||||||
|
|
||||||
|
@Language("js")
|
||||||
|
val jsMap = "$=result;id=$.id;id"
|
||||||
val result = Rhino.use {
|
val result = Rhino.use {
|
||||||
evaluateString(initStandardObjects(), "null", "xx", 1, null)
|
val scope = initStandardObjects()
|
||||||
|
scope.putBinding("result", map)
|
||||||
|
evaluateString(scope, jsMap, "xxx", 1, null)
|
||||||
}
|
}
|
||||||
Assert.assertEquals(null, result)
|
Assert.assertEquals("3242532321", result)
|
||||||
|
@Language("js")
|
||||||
|
val jsMap1 = """result.get("id")"""
|
||||||
|
val result1 = Rhino.use {
|
||||||
|
val scope = initStandardObjects()
|
||||||
|
scope.putBinding("result", map)
|
||||||
|
evaluateString(scope, jsMap1, "xxx", 1, null)
|
||||||
|
}
|
||||||
|
Assert.assertEquals("3242532321", result1)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
@file:Suppress("unused")
|
||||||
|
|
||||||
|
package io.legado.app.rhino
|
||||||
|
|
||||||
|
import org.mozilla.javascript.Context
|
||||||
|
import org.mozilla.javascript.Scriptable
|
||||||
|
import org.mozilla.javascript.ScriptableObject
|
||||||
|
import java.io.Reader
|
||||||
|
|
||||||
|
fun Context.evaluateString(scope: Scriptable, source: String, sourceName: String) {
|
||||||
|
evaluateString(scope, source, sourceName, 1, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.evaluateReader(scope: Scriptable, reader: Reader, sourceName: String) {
|
||||||
|
evaluateReader(scope, reader, sourceName, 1, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Scriptable.putBinding(key: String, value: Any?) {
|
||||||
|
val wrappedOut = Context.javaToJS(value, this)
|
||||||
|
ScriptableObject.putProperty(this, key, wrappedOut)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Scriptable.putBindings(bindings: Map<String, Any?>) {
|
||||||
|
bindings.forEach { (t, u) ->
|
||||||
|
putBinding(t, u)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,10 +3,12 @@ package io.legado.app
|
|||||||
import com.script.SimpleBindings
|
import com.script.SimpleBindings
|
||||||
import io.legado.app.constant.SCRIPT_ENGINE
|
import io.legado.app.constant.SCRIPT_ENGINE
|
||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
|
import io.legado.app.rhino.Rhino
|
||||||
|
import io.legado.app.rhino.putBinding
|
||||||
import org.intellij.lang.annotations.Language
|
import org.intellij.lang.annotations.Language
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.mozilla.javascript.Context
|
import org.mozilla.javascript.Scriptable
|
||||||
|
|
||||||
class JsTest {
|
class JsTest {
|
||||||
|
|
||||||
@@ -29,27 +31,33 @@ class JsTest {
|
|||||||
@Test
|
@Test
|
||||||
fun testMap() {
|
fun testMap() {
|
||||||
val map = hashMapOf("id" to "3242532321")
|
val map = hashMapOf("id" to "3242532321")
|
||||||
val bindings = SimpleBindings()
|
|
||||||
bindings["result"] = map
|
|
||||||
@Language("js")
|
@Language("js")
|
||||||
val jsMap = "$=result;id=$.id;id"
|
val jsMap = "$=result;id=$.id;id"
|
||||||
val result = SCRIPT_ENGINE.eval(jsMap, bindings)?.toString()
|
val result = Rhino.use {
|
||||||
|
val scope = initStandardObjects()
|
||||||
|
scope.putBinding("result", map)
|
||||||
|
evaluateString(scope, jsMap, "xxx", 1, null)
|
||||||
|
}
|
||||||
Assert.assertEquals("3242532321", result)
|
Assert.assertEquals("3242532321", result)
|
||||||
@Language("js")
|
@Language("js")
|
||||||
val jsMap1 = """result.get("id")"""
|
val jsMap1 = """result.get("id")"""
|
||||||
val result1 = SCRIPT_ENGINE.eval(jsMap1, bindings)?.toString()
|
val result1 = Rhino.use {
|
||||||
|
val scope = initStandardObjects()
|
||||||
|
scope.putBinding("result", map)
|
||||||
|
evaluateString(scope, jsMap1, "xxx", 1, null)
|
||||||
|
}
|
||||||
Assert.assertEquals("3242532321", result1)
|
Assert.assertEquals("3242532321", result1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFor() {
|
fun testFor() {
|
||||||
val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings())
|
val scope = Rhino.use {
|
||||||
val scope = SCRIPT_ENGINE.getRuntimeScope(context)
|
val scope = initStandardObjects()
|
||||||
try {
|
evaluateString(scope, printJs, "print", 1, null)
|
||||||
Context.enter().evaluateString(scope, printJs, "print", 1, null)
|
scope
|
||||||
} finally {
|
} as Scriptable
|
||||||
Context.exit()
|
|
||||||
}
|
|
||||||
@Language("js")
|
@Language("js")
|
||||||
val jsFor = """
|
val jsFor = """
|
||||||
let result = 0
|
let result = 0
|
||||||
@@ -69,7 +77,9 @@ class JsTest {
|
|||||||
}
|
}
|
||||||
result
|
result
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
val result = SCRIPT_ENGINE.eval(jsFor, scope).toString()
|
val result = Rhino.use {
|
||||||
|
evaluateString(scope, jsFor, "jsFor", 1, null)
|
||||||
|
}
|
||||||
Assert.assertEquals("12012", result)
|
Assert.assertEquals("12012", result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user