Skip to content

Commit ddc0bd4

Browse files
committed
Fixed do notation label hack; Updated README.md; Updated to 1.1.0-beta-22.
1 parent 48fd464 commit ddc0bd4

File tree

4 files changed

+43
-74
lines changed

4 files changed

+43
-74
lines changed

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.1.0-beta-22'
2+
ext.kotlin_version = '1.1.0-beta-38'
33

44
repositories {
55
mavenCentral()
@@ -17,10 +17,16 @@ repositories {
1717
maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1"}
1818
}
1919

20+
kotlin {
21+
experimental {
22+
coroutines 'enable'
23+
}
24+
}
25+
2026
dependencies {
2127
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
2228
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
23-
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.4-beta'
29+
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.6-beta'
2430
testCompile "junit:junit:4.12"
2531
}
2632

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.github.h0tk3y.kotlinMonads
2+
3+
import kotlin.coroutines.experimental.Continuation
4+
5+
private val coroutineImplClass by lazy { Class.forName("kotlin.coroutines.experimental.jvm.internal.CoroutineImpl") }
6+
7+
private val labelField by lazy { coroutineImplClass.getDeclaredField("label").apply { isAccessible = true } }
8+
private val completionField by lazy { coroutineImplClass.getDeclaredField("completion").apply { isAccessible = true } }
9+
10+
private var <T> Continuation<T>.label
11+
get() = labelField.get(this)
12+
set(value) = labelField.set(this@label, value)
13+
14+
private var <T> Continuation<T>.completion: Continuation<*>?
15+
get() = completionField.get(this) as Continuation<*>
16+
set(value) = completionField.set(this@completion, value)
17+
18+
internal var <T> Continuation<T>.stackLabels: List<Any>
19+
get() = if (coroutineImplClass.isInstance(this)) listOf(label) + completion?.stackLabels.orEmpty() else emptyList()
20+
set(value) {
21+
if (coroutineImplClass.isInstance(this)) {
22+
label = value.first()
23+
completion?.stackLabels = value.subList(1, value.size)
24+
}
25+
}

src/main/kotlin/com/github/h0tk3y/kotlinMonads/DoNotation.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
package com.github.h0tk3y.kotlinMonads
44

55
import java.io.Serializable
6-
import kotlin.coroutines.Continuation
7-
import kotlin.coroutines.EmptyCoroutineContext
8-
import kotlin.coroutines.RestrictsSuspension
9-
import kotlin.coroutines.intrinsics.SUSPENDED_MARKER
10-
import kotlin.coroutines.intrinsics.suspendCoroutineOrReturn
11-
import kotlin.coroutines.startCoroutine
6+
import kotlin.coroutines.experimental.Continuation
7+
import kotlin.coroutines.experimental.EmptyCoroutineContext
8+
import kotlin.coroutines.experimental.RestrictsSuspension
9+
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED
10+
import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn
11+
import kotlin.coroutines.experimental.startCoroutine
1212

1313
fun <M : Monad<M, *>, T, R> Monad<M, T>.bindDo(c: suspend DoController<M>.(T) -> Monad<M, R>): Monad<M, R> =
1414
bind { t ->
@@ -26,23 +26,14 @@ fun <M : Monad<M, *>, R> doReturning(aReturn: Return<M>,
2626
return controller.returnedMonad as Monad<M, R>
2727
}
2828

29-
private val labelField by lazy {
30-
val jClass = Class.forName("kotlin.jvm.internal.CoroutineImpl")
31-
return@lazy jClass.getDeclaredField("label").apply { isAccessible = true }
32-
}
33-
34-
private var <T> Continuation<T>.label
35-
get() = labelField.get(this)
36-
set(value) = labelField.set(this@label, value)
37-
3829
@RestrictsSuspension
3930
class DoController<M : Monad<M, *>>(private val returning: Return<M>) :
4031
Serializable, Return<M> by returning, Continuation<Monad<M, *>> {
4132

4233
override val context = EmptyCoroutineContext
4334

4435
override fun resume(value: Monad<M, *>) {
45-
returnedMonad = value //here is where the biggest magic happens
36+
returnedMonad = value
4637
}
4738

4839
override fun resumeWithException(exception: Throwable) {
@@ -52,13 +43,13 @@ class DoController<M : Monad<M, *>>(private val returning: Return<M>) :
5243
internal lateinit var returnedMonad: Monad<M, *>
5344

5445
suspend fun <T> bind(m: Monad<M, T>): T = suspendCoroutineOrReturn { c ->
55-
val labelHere = c.label // save the label
46+
val labelHere = c.stackLabels // save the whole coroutine stack labels
5647
returnedMonad = m.bind { x ->
57-
c.label = labelHere
48+
c.stackLabels = labelHere
5849
c.resume(x)
5950
returnedMonad
6051
}
61-
SUSPENDED_MARKER
52+
COROUTINE_SUSPENDED
6253
}
6354

6455
suspend fun <T> then(m: Monad<M, T>): Unit {

src/main/kotlin/com/github/h0tk3y/kotlinMonads/JustATest.kt

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)