Skip to content

Commit

Permalink
jsBrides改用原生代码实现
Browse files Browse the repository at this point in the history
  • Loading branch information
aiselp committed Aug 29, 2023
1 parent f1740db commit 7c41af6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ open class RhinoJavaScriptEngine(private val mAndroidContext: android.content.Co
override fun wrap(cx: Context, scope: Scriptable, obj: Any?, staticType: Class<*>?): Any? {
return when {
obj is String -> runtime.bridges.toString(obj.toString())
staticType == UiObjectCollection::class.java -> runtime.bridges.asArray(obj)
staticType == UiObjectCollection::class.java -> runtime.bridges.asArray(obj as UiObjectCollection)
else -> super.wrap(cx, scope, obj, staticType)
}
}
Expand Down
82 changes: 54 additions & 28 deletions autojs/src/main/java/com/stardust/autojs/runtime/ScriptBridges.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stardust.autojs.runtime

import android.os.Build
import androidx.annotation.RequiresApi
import com.stardust.automator.UiObjectCollection
import org.mozilla.javascript.BaseFunction
import org.mozilla.javascript.Context
import org.mozilla.javascript.Scriptable
Expand All @@ -10,6 +13,17 @@ import org.mozilla.javascript.annotations.JSFunction
* Created by Stardust on 2017/7/21.
*/
class ScriptBridges {
companion object {
fun <T> useJsContext(f: (context: Context) -> T): T {
val context = Context.getCurrentContext()
try {
return f(context ?: Context.enter())
} finally {
context ?: Context.exit()
}
}
}

interface Bridges {
fun call(func: Any?, target: Any?, arg: Any?): Any
fun toArray(o: Iterable<*>?): Any
Expand All @@ -22,46 +36,58 @@ class ScriptBridges {
}

var bridges: Bridges? = null
fun callFunction(func: Any?, target: Any?, args: Array<*>): Any {
fun callFunction(func: Any?, target: Any?, args: Array<*>): Any = useJsContext<Any> { context ->
val jsFn = func as BaseFunction
val cx = Context.getCurrentContext()
val scope = jsFn.parentScope
val arr = args.map { Context.javaToJS(it, scope) }.toTypedArray()
try {
return jsFn.call(
cx ?: Context.enter(),
scope,
(Context.javaToJS(target, scope) as? Scriptable) ?: Undefined.SCRIPTABLE_UNDEFINED,
arr
)
} finally {
cx ?: Context.exit()
}
}
val arg = args.map { Context.javaToJS(it, scope) }.toTypedArray()

private fun checkBridges() {
checkNotNull(bridges) { "no bridges set" }
return@useJsContext jsFn.call(
context, scope,
(Context.javaToJS(target, scope) as? Scriptable) ?: Undefined.SCRIPTABLE_UNDEFINED,
arg
)
}

@JSFunction
fun toArray(c: Iterable<*>): Scriptable {
val cx = Context.getCurrentContext()
val te = cx ?: Context.enter()
try {
return te.newArray(te.initStandardObjects(), c.toList().toTypedArray())
} finally {
cx ?: Context.exit()
}
fun toArray(c: Iterable<*>): Scriptable = useJsContext<Scriptable> { context ->
val scope = context.initStandardObjects()
return@useJsContext context.newArray(
context.initStandardObjects(),
c.map { Context.javaToJS(it, scope) }.toTypedArray()
)
}

fun toString(obj: Any?): String {
return Context.toString(obj)
}

fun asArray(list: Any?): Any {
val arr = toArray(emptyList<Any?>())
fun asArray(obj: UiObjectCollection): Any = useJsContext { context ->
val arr = toArray(obj.mNodes)
obj::class.members.forEach {
val name = it.name
val method = object : BaseFunction() {
override fun getFunctionName(): String {
return name
}

checkBridges()
return bridges!!.asArray(list)
override fun call(
cx: Context?,
scope: Scriptable?,
thisObj: Scriptable?,
args: Array<out Any>?
): Any? {
return if (args != null) {
it.call(obj, *args)
} else it.call(obj)
}

@RequiresApi(Build.VERSION_CODES.O)
override fun getLength(): Int {
return it.parameters.size
}
}
arr.put(name, arr, method)
}
return@useJsContext arr
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.stardust.automator

import android.os.Bundle

import com.stardust.util.Consumer

import java.util.ArrayList
import java.util.Collections

import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ARGUMENT_COLUMN_INT
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.ACTION_ARGUMENT_PROGRESS_VALUE
Expand Down Expand Up @@ -38,13 +32,13 @@ import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.Accessibilit
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_SCROLL_UP
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_SET_PROGRESS
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_SHOW_ON_SCREEN
import com.stardust.util.Consumer

/**
* Created by Stardust on 2017/3/9.
*/

class UiObjectCollection private constructor(private val mNodes: List<UiObject?>) {

class UiObjectCollection private constructor(val mNodes: List<UiObject?>) {

val isEmpty: Boolean
get() = mNodes.isEmpty()
Expand Down

0 comments on commit 7c41af6

Please sign in to comment.