summaryrefslogtreecommitdiff
path: root/dicore3
diff options
context:
space:
mode:
authorDico <dico.karssiens@gmail.com>2018-09-23 06:34:03 +0100
committerDico <dico.karssiens@gmail.com>2018-09-23 06:34:03 +0100
commit038e698a1421e95d1dc96117cd9a2ae0cfdddf6a (patch)
tree9a9a44bb20869ab8a7ab2287d14c6558a495614b /dicore3
parentf499555f8bff9d3c77340a0aa852cb418e81e6f7 (diff)
Work down some todo items, update to kotlin 1.3-rc
Diffstat (limited to 'dicore3')
-rw-r--r--dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java9
-rw-r--r--dicore3/command/src/main/java/io/dico/dicore/command/ICommandReceiver.java5
-rw-r--r--dicore3/command/src/main/kotlin/io/dico/dicore/command/registration/reflect/KotlinReflectiveRegistration.kt37
3 files changed, 32 insertions, 19 deletions
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java b/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java
index 4c014fb..4450a92 100644
--- a/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java
+++ b/dicore3/command/src/main/java/io/dico/dicore/command/ExecutionContext.java
@@ -200,6 +200,15 @@ public class ExecutionContext {
return originalBuffer.getArrayFromIndex(cursorStart);
}
+ /**
+ * The path used to access this address.
+ *
+ * @return the path used to access this address.
+ */
+ public String[] getRoute() {
+ return Arrays.copyOf(originalBuffer.toArray(), address.getDepth());
+ }
+
public Formatting getFormat(EMessageType type) {
return address.getChatController().getChatFormatForType(type);
}
diff --git a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandReceiver.java b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandReceiver.java
index 88b0d50..6660bf8 100644
--- a/dicore3/command/src/main/java/io/dico/dicore/command/ICommandReceiver.java
+++ b/dicore3/command/src/main/java/io/dico/dicore/command/ICommandReceiver.java
@@ -12,6 +12,11 @@ public interface ICommandReceiver {
Plugin getPlugin();
+ // type is CoroutineContext, but we avoid referring to Kotlin runtime here
+ default Object getCoroutineContext() {
+ return null;
+ }
+
}
}
diff --git a/dicore3/command/src/main/kotlin/io/dico/dicore/command/registration/reflect/KotlinReflectiveRegistration.kt b/dicore3/command/src/main/kotlin/io/dico/dicore/command/registration/reflect/KotlinReflectiveRegistration.kt
index 9d23ee6..c09088e 100644
--- a/dicore3/command/src/main/kotlin/io/dico/dicore/command/registration/reflect/KotlinReflectiveRegistration.kt
+++ b/dicore3/command/src/main/kotlin/io/dico/dicore/command/registration/reflect/KotlinReflectiveRegistration.kt
@@ -4,15 +4,15 @@ import io.dico.dicore.command.CommandException
import io.dico.dicore.command.EMessageType
import io.dico.dicore.command.ExecutionContext
import io.dico.dicore.command.ICommandReceiver
-import kotlinx.coroutines.experimental.CoroutineStart.UNDISPATCHED
-import kotlinx.coroutines.experimental.Deferred
-import kotlinx.coroutines.experimental.asCoroutineDispatcher
-import kotlinx.coroutines.experimental.async
+import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
+import kotlinx.coroutines.Deferred
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.async
import java.lang.reflect.Method
-import java.util.*
import java.util.concurrent.CancellationException
-import java.util.concurrent.Executor
-import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn
+import kotlin.coroutines.CoroutineContext
+import kotlin.coroutines.intrinsics.intercepted
+import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
import kotlin.reflect.jvm.kotlinFunction
fun isSuspendFunction(method: Method): Boolean {
@@ -20,16 +20,21 @@ fun isSuspendFunction(method: Method): Boolean {
return func.isSuspend
}
-fun callAsCoroutine(command: ReflectiveCommand,
- factory: ICommandReceiver.Factory,
- context: ExecutionContext,
- args: Array<Any?>): String? {
- val dispatcher = Executor { task -> factory.plugin.server.scheduler.runTask(factory.plugin, task) }.asCoroutineDispatcher()
+fun callAsCoroutine(
+ command: ReflectiveCommand,
+ factory: ICommandReceiver.Factory,
+ context: ExecutionContext,
+ args: Array<Any?>
+): String? {
// UNDISPATCHED causes the handler to run until the first suspension point on the current thread,
// meaning command handlers that don't have suspension points will run completely synchronously.
// Tasks that take time to compute should suspend the coroutine and resume on another thread.
- val job = async(context = dispatcher, start = UNDISPATCHED) { command.method.invokeSuspend(command.instance, args) }
+ val job = GlobalScope.async(context = factory.coroutineContext as CoroutineContext, start = UNDISPATCHED) {
+ suspendCoroutineUninterceptedOrReturn<Any?> { cont ->
+ command.method.invoke(command.instance, *args, cont.intercepted())
+ }
+ }
if (job.isCompleted) {
return job.getResult()
@@ -48,12 +53,6 @@ fun callAsCoroutine(command: ReflectiveCommand,
return null
}
-private suspend fun Method.invokeSuspend(instance: Any?, args: Array<Any?>): Any? {
- return suspendCoroutineOrReturn { cont ->
- invoke(instance, *args, cont)
- }
-}
-
@Throws(CommandException::class)
private fun Deferred<Any?>.getResult(): String? {
getCompletionExceptionOrNull()?.let { ex ->