fix(scripting/java_class_wrapper): newInstance compat

This commit is contained in:
rhunk
2024-05-28 21:11:56 +02:00
parent c8abd8162b
commit 84fc7c6ee3

View File

@@ -118,12 +118,13 @@ class JSModule(
}.getOrNull() ?: return@putFunction Undefined.instance
scriptableObject("JavaClassWrapper") {
putFunction("__new__") { args ->
val newInstance: (Array<out Any?>?) -> Any? = { args ->
val constructor = clazz.declaredConstructors.find {
(args ?: emptyArray()).isSameParameters(it.parameterTypes)
}?.also { it.isAccessible = true } ?: throw IllegalArgumentException("Constructor not found with args ${argsToString(args)}")
constructor.newInstance(*args ?: emptyArray())
}
putFunction("__new__") { newInstance(it) }
clazz.declaredMethods.filter { Modifier.isStatic(it.modifiers) }.forEach { method ->
putFunction(method.name) { args ->
@@ -138,6 +139,10 @@ class JSModule(
field.isAccessible = true
defineProperty(field.name, { field.get(null) }, { value -> field.set(null, value) }, 0)
}
if (get("newInstance") == null) {
putFunction("newInstance") { newInstance(it) }
}
}
}