package me.eternal.purrfect.common.scripting.impl import me.eternal.purrfect.common.scripting.bindings.AbstractBinding import me.eternal.purrfect.common.scripting.bindings.BindingSide import me.eternal.purrfect.common.scripting.ktx.contextScope import me.eternal.purrfect.common.scripting.ktx.putFunction import me.eternal.purrfect.common.scripting.ktx.scriptableObject import java.lang.reflect.Proxy import kotlin.concurrent.thread class JavaInterfaces : AbstractBinding("java-interfaces", BindingSide.COMMON) { override fun getObject() = scriptableObject { putFunction("runnable") { val function = it?.get(0) as? org.mozilla.javascript.Function ?: return@putFunction null Runnable { contextScope { function.call( this, this@scriptableObject, this@scriptableObject, emptyArray() ) } } } putFunction("newProxy") { arguments -> val javaInterface = arguments?.get(0) as? Class<*> ?: return@putFunction null val function = arguments[1] as? org.mozilla.javascript.Function ?: return@putFunction null Proxy.newProxyInstance( javaInterface.classLoader, arrayOf(javaInterface) ) { instance, method, args -> contextScope { function.call( this, this@scriptableObject, this@scriptableObject, arrayOf(instance, method.name, (args ?: emptyArray()).toList()) ) } } } putFunction("thread") { arguments -> val function = arguments?.get(0) as? org.mozilla.javascript.Function ?: return@putFunction null thread(start = false) { contextScope { function.call( this, this@scriptableObject, this@scriptableObject, emptyArray() ) } } } } }