修复某些情况下timer工作不正常

This commit is contained in:
aiselp
2023-06-14 17:26:53 +08:00
parent 9435ca52de
commit 9579db0f25
2 changed files with 15 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ import org.autojs.autojs.runtime.ScriptRuntime
import org.mozilla.javascript.BaseFunction
import org.mozilla.javascript.Context
import org.mozilla.javascript.Scriptable
import org.mozilla.javascript.Undefined
import java.util.concurrent.ConcurrentHashMap
import kotlin.random.Random
@@ -22,6 +23,7 @@ class Timer(
private val mRuntime: ScriptRuntime = runtime
private val mHandler: Handler = Handler(looper)
private val isUiLoop: Boolean = looper == Looper.getMainLooper()
private val context: Context? by lazy { Context.getCurrentContext() }
constructor(runtime: ScriptRuntime, ) : this(runtime, Looper.myLooper()!!)
@@ -36,17 +38,22 @@ class Timer(
return id
}
private fun callFunction(callback: Any, thiz: Any?, args :Any?) {
val myArgs = args?.let { args as Array<*> }?: emptyArray<Any>()
val map = myArgs.map { Context.javaToJS(it, callback as BaseFunction) }
private fun callFunction(callback: Any, thiz: Any?, args: Any?) {
val func = callback as BaseFunction
val map: Array<Any> =
(args as? Array<*>)?.map { Context.javaToJS(it, callback.parentScope) }
?.toTypedArray() ?: emptyArray()
try {
(callback as BaseFunction).call(Context.getCurrentContext(),callback.parentScope,
thiz as? Scriptable?, map.toTypedArray()
func.call(
context ?: Context.enter(), func.parentScope,
thiz as? Scriptable ?: Undefined.SCRIPTABLE_UNDEFINED, map
)
} catch (e: Exception) {
if (isUiLoop) {
mRuntime.exit(e)
} else throw e
}finally {
context ?: Context.exit()
}
}