diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index d2455d68..cf7956af 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -14,6 +14,7 @@ "isJavaClass/isJavaPackage 等全局方法无效的问题", "timers.keepAlive 方法 timeout 参数功能无效的问题", "floaty.window/rawWindow 方法无法接受字符串参数的问题", + "util.class[Name]/getClass[Name] 可能返回错误结果的问题", "使用 XML 语法将 JavaScript 表达式作为属性值时, this 对象可能出现指向错误的问题", "canvas 元素控件 setMaxFps 方法内部帧率计算错误", "images.concat 方法纵向拼接时宽度值计算错误", diff --git a/app/src/main/java/org/autojs/autojs/runtime/ScriptRuntime.kt b/app/src/main/java/org/autojs/autojs/runtime/ScriptRuntime.kt index 2c99e116..68374b58 100644 --- a/app/src/main/java/org/autojs/autojs/runtime/ScriptRuntime.kt +++ b/app/src/main/java/org/autojs/autojs/runtime/ScriptRuntime.kt @@ -158,6 +158,7 @@ import org.autojs.autojs.runtime.api.Threads as ApiThreads import org.autojs.autojs.runtime.api.Timers as ApiTimers import org.autojs.autojs.runtime.api.Toaster as ApiToaster import org.autojs.autojs.runtime.api.UI as ApiUI +import org.autojs.autojs.runtime.api.Util as ApiUtil import org.autojs.autojs.runtime.api.augment.autojs.Version as AutojsVersion import org.autojs.autojs.runtime.api.augment.global.Legacy as GlobalLegacy import org.autojs.autojs.runtime.api.augment.notice.Channel as NoticeChannel @@ -241,6 +242,10 @@ class ScriptRuntime private constructor(builder: Builder) { @ScriptVariable val ui: ApiUI + @JvmField + @ScriptVariable + val util: ApiUtil + @JvmField @ScriptVariable val dialogs = ApiDialogs(this) @@ -453,6 +458,7 @@ class ScriptRuntime private constructor(builder: Builder) { floaty = ApiFloaty(uiHandler, this) ui = ApiUI(mUiHandlerAppContext, this) + util = ApiUtil(mUiHandlerAppContext, this) images = ApiImages(mUiHandlerAppContext, this) device = ApiDevice(mUiHandlerAppContext) media = ApiMedia(mUiHandlerAppContext, this) @@ -711,7 +717,7 @@ class ScriptRuntime private constructor(builder: Builder) { Global(this).assignWithGlobal(target, topLevelScope, GlobalClasses) GlobalLegacy(this).assignWithGlobal(target, topLevelScope) IsNullish.augmentWithGlobal(target, topLevelScope, false) - Util.augmentWithGlobal(target, topLevelScope, true).apply { + Util.augmentWithGlobal(target, topLevelScope, util, true).apply { UtilJava.augmentWithGlobal(this, topLevelScope, false) UtilVersion.augmentWithGlobal(this, topLevelScope, false) UtilVersionCodes.augmentWithGlobal(this, topLevelScope, VersionCodesInfo.obj, false) diff --git a/app/src/main/java/org/autojs/autojs/runtime/api/Util.kt b/app/src/main/java/org/autojs/autojs/runtime/api/Util.kt new file mode 100644 index 00000000..bc7f3cd0 --- /dev/null +++ b/app/src/main/java/org/autojs/autojs/runtime/api/Util.kt @@ -0,0 +1,23 @@ +package org.autojs.autojs.runtime.api + +import org.autojs.autojs.extension.AnyExtensions.jsBrief +import org.autojs.autojs.runtime.ScriptRuntime + +class Util(private val context: android.content.Context, private val scriptRuntime: ScriptRuntime) { + + fun `class`(o: Any?) = getClassInternal(o, "class") + + fun getClass(o: Any?) = getClassInternal(o, "getClass") + + fun className(o: Any?) = getClassNameInternal(o, "className") + + fun getClassName(o: Any?) = getClassNameInternal(o, "getClassName") + + private fun getClassInternal(o: Any?, methodName: String): Class { + require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.$methodName must be non-null" } + return o as? Class<*> ?: o.javaClass + } + + private fun getClassNameInternal(o: Any?, methodName: String): String = getClassInternal(o, methodName).name + +} diff --git a/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Util.kt b/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Util.kt index f3d5321b..c5ce7906 100644 --- a/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Util.kt +++ b/app/src/main/java/org/autojs/autojs/runtime/api/augment/util/Util.kt @@ -89,10 +89,6 @@ object Util : Augmentable() { ::deprecate.name, ::debuglog.name, ::log.name, - ::`class`.name, - ::getClass.name, - ::className.name, - ::getClassName.name, ::checkStringArgument.name, ::assureStringStartsWith.name, ::assureStringEndsWith.name, @@ -458,40 +454,6 @@ object Util : Augmentable() { AutoJs.instance.globalConsole.log("%s - %s", timestamp, formatRhino(*arguments)) } - @JvmStatic - @RhinoSingletonFunctionInterface - fun `class`(args: Array): Scriptable = ensureArgumentsOnlyOne(args) { o -> - require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.class must be non-null" } - getClassInternal(o) - } - - @JvmStatic - @RhinoSingletonFunctionInterface - fun getClass(args: Array): Scriptable = ensureArgumentsOnlyOne(args) { o -> - require(o != null) { "Argument \"o\" ${o.jsBrief()} for util.getClass must be non-null" } - getClassInternal(o) - } - - @JvmStatic - @RhinoSingletonFunctionInterface - fun className(args: Array): String = ensureArgumentsOnlyOne(args) { - when (it) { - null -> throw WrappedIllegalArgumentException("Argument \"o\" ${it.jsBrief()} for util.className must be non-null") - is Class<*> -> it.name - else -> it.javaClass.name - } - } - - @JvmStatic - @RhinoSingletonFunctionInterface - fun getClassName(args: Array): String = ensureArgumentsOnlyOne(args) { - when (it) { - null -> throw WrappedIllegalArgumentException("Argument \"o\" ${it.jsBrief()} for util.getClassName must be non-null") - is Class<*> -> it.name - else -> it.javaClass.name - } - } - @JvmStatic @Deprecated("Deprecated in Java", ReplaceWith("checkStringArgument(args)")) @RhinoSingletonFunctionInterface @@ -768,11 +730,6 @@ object Util : Augmentable() { coerceNumber(DisplayUtils.pxToSp(coerceFloatNumber(it))) } - private fun getClassInternal(o: Any): Scriptable = when (o) { - is Class<*> -> o - else -> o.javaClass - }.let { cls -> withRhinoContext { cx -> cx.wrapFactory.wrapJavaClass(cx, ImporterTopLevel(cx), cls) } } - internal class RegularFunction(private val func: BaseFunction) : BaseFunction() { override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable?, args: Array): Any {