mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
863b41ac4f
commit
cd7f139dd3
@ -1,6 +1,7 @@
|
||||
package io.legado.app
|
||||
|
||||
import io.legado.app.rhino.Rhino
|
||||
import io.legado.app.rhino.putBinding
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
@ -46,15 +47,31 @@ class AndroidJsTest {
|
||||
val result1 = Rhino.use {
|
||||
evaluateString(initStandardObjects(), js1, "xx", 1, null)
|
||||
}
|
||||
Assert.assertEquals(result1, "未知错误,请联系开发者!")
|
||||
Assert.assertEquals(result1, "未知错误,请联系开发者!").let {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testReturnNull() {
|
||||
fun testMap() {
|
||||
val map = hashMapOf("id" to "3242532321")
|
||||
|
||||
@Language("js")
|
||||
val jsMap = "$=result;id=$.id;id"
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
27
app/src/main/java/io/legado/app/rhino/RhinoExtensions.kt
Normal file
27
app/src/main/java/io/legado/app/rhino/RhinoExtensions.kt
Normal file
@ -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 io.legado.app.constant.SCRIPT_ENGINE
|
||||
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.junit.Assert
|
||||
import org.junit.Test
|
||||
import org.mozilla.javascript.Context
|
||||
import org.mozilla.javascript.Scriptable
|
||||
|
||||
class JsTest {
|
||||
|
||||
@ -29,27 +31,33 @@ class JsTest {
|
||||
@Test
|
||||
fun testMap() {
|
||||
val map = hashMapOf("id" to "3242532321")
|
||||
val bindings = SimpleBindings()
|
||||
bindings["result"] = map
|
||||
|
||||
@Language("js")
|
||||
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)
|
||||
@Language("js")
|
||||
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)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFor() {
|
||||
val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings())
|
||||
val scope = SCRIPT_ENGINE.getRuntimeScope(context)
|
||||
try {
|
||||
Context.enter().evaluateString(scope, printJs, "print", 1, null)
|
||||
} finally {
|
||||
Context.exit()
|
||||
}
|
||||
val scope = Rhino.use {
|
||||
val scope = initStandardObjects()
|
||||
evaluateString(scope, printJs, "print", 1, null)
|
||||
scope
|
||||
} as Scriptable
|
||||
|
||||
@Language("js")
|
||||
val jsFor = """
|
||||
let result = 0
|
||||
@ -69,7 +77,9 @@ class JsTest {
|
||||
}
|
||||
result
|
||||
""".trimIndent()
|
||||
val result = SCRIPT_ENGINE.eval(jsFor, scope).toString()
|
||||
val result = Rhino.use {
|
||||
evaluateString(scope, jsFor, "jsFor", 1, null)
|
||||
}
|
||||
Assert.assertEquals("12012", result)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user