6.7.0 - Alpha21 - 修复 util.java.array 无法正常使用 Boolean 等类型参数的问题
This commit is contained in:
@@ -2,19 +2,23 @@ package org.autojs.autojs.runtime.api.augment.util
|
||||
|
||||
import org.autojs.autojs.annotation.RhinoFunctionBody
|
||||
import org.autojs.autojs.annotation.RhinoSingletonFunctionInterface
|
||||
import org.autojs.autojs.rhino.ArgumentGuards.Companion.component1
|
||||
import org.autojs.autojs.rhino.ArgumentGuards.Companion.component2
|
||||
import org.autojs.autojs.rhino.extension.AnyExtensions.isJsNullish
|
||||
import org.autojs.autojs.rhino.extension.AnyExtensions.jsBrief
|
||||
import org.autojs.autojs.rhino.extension.IterableExtensions.toNativeArray
|
||||
import org.autojs.autojs.rhino.extension.ScriptableExtensions.prop
|
||||
import org.autojs.autojs.runtime.api.augment.Augmentable
|
||||
import org.autojs.autojs.runtime.exception.WrappedIllegalArgumentException
|
||||
import org.autojs.autojs.util.RhinoUtils
|
||||
import org.autojs.autojs.util.RhinoUtils.newNativeArray
|
||||
import org.autojs.autojs.util.RhinoUtils.newNativeObject
|
||||
import org.mozilla.javascript.Context
|
||||
import org.mozilla.javascript.NativeArray
|
||||
import org.mozilla.javascript.NativeJavaClass
|
||||
import org.mozilla.javascript.NativeObject
|
||||
import org.mozilla.javascript.ScriptableObject
|
||||
import java.lang.reflect.Array.newInstance
|
||||
import org.mozilla.javascript.Wrapper
|
||||
|
||||
@Suppress("unused")
|
||||
object Java : Augmentable() {
|
||||
@@ -31,9 +35,20 @@ object Java : Augmentable() {
|
||||
@RhinoSingletonFunctionInterface
|
||||
fun instanceof(args: Array<out Any?>): Any = ensureArgumentsLength(args, 2) {
|
||||
val (obj, clazz) = it
|
||||
obj ?: return@ensureArgumentsLength false
|
||||
clazz ?: return@ensureArgumentsLength false
|
||||
Class.forName(Context.toString(clazz)).isAssignableFrom(obj.javaClass)
|
||||
obj ?: throw WrappedIllegalArgumentException("Argument \"obj\" for util.java.instanceof must not be null")
|
||||
|
||||
val resolvedClass = when (clazz) {
|
||||
null -> return@ensureArgumentsLength false
|
||||
is Class<*> -> clazz
|
||||
is NativeJavaClass -> clazz.classObject
|
||||
is Wrapper -> when (val unwrapped = clazz.unwrap()) {
|
||||
is Class<*> -> unwrapped
|
||||
is NativeJavaClass -> unwrapped.classObject
|
||||
else -> unwrapped.resolveClass()
|
||||
}
|
||||
else -> clazz.resolveClass()
|
||||
}
|
||||
resolvedClass.isAssignableFrom(obj.javaClass)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@@ -44,33 +59,15 @@ object Java : Augmentable() {
|
||||
|
||||
val dims = it.drop(1).map { arg -> Context.toNumber(arg).toInt() }
|
||||
|
||||
// @Hint by SuperMonster003 on Jun 3, 2024.
|
||||
// ! Reflection may cause some performance overhead,
|
||||
// ! and potential exceptions will become harder to trace.
|
||||
// ! zh-CN:
|
||||
// ! 反射会带来一定的额外性能开销, 潜在的异常也难以追踪.
|
||||
// !
|
||||
// # val method = this::class.members.find { mem ->
|
||||
// # mem is KFunction<*> && mem.name == "newInstance" && mem.parameters.size == dims.size + 1
|
||||
// # } as? KFunction<*>
|
||||
// # method?.call(clazz, *dims.toIntArray().toTypedArray()) ?: throw IllegalArgumentException("Too many arguments for util.java")
|
||||
when (dims.size) {
|
||||
+0 -> newInstance(clazz, 0)
|
||||
+1 -> newInstance(clazz, dims[0])
|
||||
+2 -> newInstance(clazz, dims[0], dims[1])
|
||||
+3 -> newInstance(clazz, dims[0], dims[1], dims[2])
|
||||
+4 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3])
|
||||
+5 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4])
|
||||
+6 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5])
|
||||
+7 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6])
|
||||
+8 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7])
|
||||
+9 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8])
|
||||
10 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9])
|
||||
11 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10])
|
||||
12 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10], dims[11])
|
||||
13 -> newInstance(clazz, dims[0], dims[1], dims[2], dims[3], dims[4], dims[5], dims[6], dims[7], dims[8], dims[9], dims[10], dims[11], dims[12])
|
||||
else -> throw WrappedIllegalArgumentException("Too many arguments for util.java")
|
||||
if (dims.isEmpty()) {
|
||||
// Keep behavior consistent with Rhino JavaScript implementation: calling array(type) throws.
|
||||
// zh-CN: 保持与 Rhino JavaScript 实现一致: 调用 array(type) 时抛出异常.
|
||||
throw WrappedIllegalArgumentException("Missing array dimensions for util.java.array")
|
||||
}
|
||||
|
||||
// Use java.lang.reflect.Array.newInstance(Class, int[]) to support arbitrary dimensions.
|
||||
// zh-CN: 使用 java.lang.reflect.Array.newInstance(Class, int[]) 以支持任意维度数量.
|
||||
java.lang.reflect.Array.newInstance(clazz, *dims.toIntArray())
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@@ -96,7 +93,9 @@ object Java : Augmentable() {
|
||||
if (o !is NativeObject) throw WrappedIllegalArgumentException("Argument \"o\" ${o.jsBrief()} for util.objectToMap must be a JavaScript Object")
|
||||
hashMapOf<String, Any?>().also { map ->
|
||||
for (key in ScriptableObject.getPropertyIds(o)) {
|
||||
map[key.toString()] = o.prop(key.toString())
|
||||
if (RhinoUtils.js_object_hasOwnProperty(o, key.toString())) {
|
||||
map[key.toString()] = o.prop(key.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,14 +115,27 @@ object Java : Augmentable() {
|
||||
}
|
||||
|
||||
private fun typeToClass(type: Any?) = when (type) {
|
||||
"string" -> String::class.java
|
||||
"int" -> Integer.TYPE
|
||||
"long" -> java.lang.Long.TYPE
|
||||
"double" -> java.lang.Double.TYPE
|
||||
"char" -> Character.TYPE
|
||||
"byte" -> java.lang.Byte.TYPE
|
||||
"float" -> java.lang.Float.TYPE
|
||||
else -> Class.forName(Context.toString(type))
|
||||
is Class<*> -> type
|
||||
is NativeJavaClass -> type.classObject
|
||||
is Wrapper -> when (val unwrapped = type.unwrap()) {
|
||||
is Class<*> -> unwrapped
|
||||
is NativeJavaClass -> unwrapped.classObject
|
||||
else -> unwrapped.resolveClass()
|
||||
}
|
||||
is String -> when (type) {
|
||||
"string" -> String::class.java
|
||||
"int" -> Integer.TYPE
|
||||
"long" -> java.lang.Long.TYPE
|
||||
"double" -> java.lang.Double.TYPE
|
||||
"char" -> Character.TYPE
|
||||
"byte" -> java.lang.Byte.TYPE
|
||||
"float" -> java.lang.Float.TYPE
|
||||
"short" -> java.lang.Short.TYPE
|
||||
"boolean" -> java.lang.Boolean.TYPE
|
||||
else -> type.resolveClass()
|
||||
}
|
||||
else -> type.resolveClass()
|
||||
}
|
||||
|
||||
private fun Any?.resolveClass(): Class<*> = Class.forName(Context.toString(this))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user