commit 84fc7c6ee344860cb9a4f5bfe81d2afaff05995d
parent c8abd8162bfa6da40e3142c432b533455c715fb7
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Tue, 28 May 2024 21:11:56 +0200

fix(scripting/java_class_wrapper): newInstance compat

Diffstat:
Mcommon/src/main/kotlin/me/rhunk/snapenhance/common/scripting/JSModule.kt | 7++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/common/src/main/kotlin/me/rhunk/snapenhance/common/scripting/JSModule.kt b/common/src/main/kotlin/me/rhunk/snapenhance/common/scripting/JSModule.kt @@ -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) } + } } }