6.6.1 - Alpha2 - 修复 UiObjectCollection 对象

This commit is contained in:
SuperMonster003
2024-12-09 20:34:13 +08:00
parent ea76e29322
commit c48cb3d589
3 changed files with 19 additions and 11 deletions

View File

@@ -75,7 +75,7 @@ open class AndroidContextFactory(private val cacheDirectory: File) : ContextFact
open class WrapFactory : org.mozilla.javascript.WrapFactory() {
override fun wrap(cx: Context, scope: Scriptable, obj: Any?, staticType: Class<*>?): Any? = when {
obj is String -> bridges.toString(obj.toString())
staticType == UiObjectCollection::class.java -> (obj as? UiObjectCollection)?.let { bridges.asArray(it.nodes) } ?: UiObjectCollection.EMPTY
staticType == UiObjectCollection::class.java -> (obj as? UiObjectCollection)?.let { bridges.asArray(it) } ?: UiObjectCollection.EMPTY
else -> super.wrap(cx, scope, obj, staticType)
}
}

View File

@@ -9,7 +9,7 @@ interface IScriptBridges {
fun toArray(o: Iterable<*>?): NativeArray
fun asArray(list: Iterable<*>): NativeArray
fun asArray(listLike: Any): NativeArray
fun toString(obj: Any?): String

View File

@@ -1,7 +1,9 @@
package org.autojs.autojs.runtime
import org.autojs.autojs.core.automator.UiObjectCollection
import org.autojs.autojs.engine.RhinoJavaScriptEngine
import org.autojs.autojs.util.RhinoUtils.callFunction
import org.autojs.autojs.util.RhinoUtils.newNativeArray
import org.mozilla.javascript.BaseFunction
import org.mozilla.javascript.BoundFunction
import org.mozilla.javascript.Context
@@ -41,10 +43,14 @@ class ScriptBridges : IScriptBridges {
context.newArray(scope, o?.map { Context.javaToJS(it, scope) }?.toTypedArray()) as NativeArray
}
override fun asArray(list: Iterable<*>): NativeArray = useJsContext { context ->
val arr = toArray(list)
val boundThis = Context.javaToJS(list, arr) as Scriptable
list::class.java.methods.forEach {
override fun asArray(listLike: Any): NativeArray = useJsContext { context ->
if (listLike is Iterable<*>) {
return@useJsContext toArray(listLike)
}
if (listLike is UiObjectCollection) {
val arr = toArray(listLike.nodes)
val boundThis = Context.javaToJS(listLike, arr) as Scriptable
listLike::class.java.methods.forEach {
val name = it.name
val method = NativeJavaMethod(it, name)
val bound = BoundFunction(context, arr, method, boundThis, emptyArray())
@@ -52,6 +58,8 @@ class ScriptBridges : IScriptBridges {
}
return@useJsContext arr
}
return@useJsContext newNativeArray()
}
override fun toString(obj: Any?): String = Context.toString(obj)