Last active
February 18, 2023 22:33
-
-
Save phiSgr/7045bccac80f267a66a93e23626cd427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// git clone git@github.com:7045bccac80f267a66a93e23626cd427.git yinyang | |
// cd yinyang | |
// kotlinc yinyang.kt -include-runtime -d yinyang.jar && java -jar yinyang.jar; rm yinyang.jar | |
// Copied from https://github.com/arrow-kt/arrow/blob/0.10.1/modules/core/arrow-core-data/src/main/kotlin/arrow/typeclasses/ContinuationUtils.kt | |
import kotlin.coroutines.Continuation | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | |
private val coroutineImplClass by lazy { Class.forName("kotlin.coroutines.jvm.internal.BaseContinuationImpl") } | |
private val completionField by lazy { coroutineImplClass.getDeclaredField("completion").apply { isAccessible = true } } | |
private var <T> Continuation<T>.completion: Continuation<*>? | |
get() = completionField.get(this) as Continuation<*> | |
set(value) = completionField.set(this@completion, value) | |
var <T> Continuation<T>.stateStack: List<Map<String, *>> | |
get() { | |
if (!coroutineImplClass.isInstance(this)) return emptyList() | |
val resultForThis = (this.javaClass.declaredFields) | |
.associate { it.isAccessible = true; it.name to it.get(this@stateStack) } | |
.let(::listOf) | |
val resultForCompletion = completion?.stateStack | |
return resultForCompletion?.let { resultForThis + it } ?: resultForThis | |
} | |
set(value) { | |
if (!coroutineImplClass.isInstance(this)) return | |
val mapForThis = value.first() | |
(this.javaClass.declaredFields).forEach { | |
if (it.name in mapForThis) { | |
it.isAccessible = true | |
val fieldValue = mapForThis[it.name] | |
it.set(this@stateStack, fieldValue) | |
} | |
} | |
completion?.stateStack = value.subList(1, value.size) | |
} | |
// https://medium.com/@georgeleung_7777/the-yin-yang-puzzle-in-kotlin-bb1d5449d6f0 | |
data class Cont( | |
val cont: Continuation<Cont>, | |
val stateStack: List<Map<String, *>> | |
) : (Cont) -> Unit { | |
override fun invoke(p1: Cont) { | |
cont.stateStack = stateStack | |
cont.resume(p1) | |
} | |
} | |
suspend fun captureCont(): Cont = suspendCoroutineUninterceptedOrReturn { c -> | |
Cont(c, stateStack = c.stateStack) | |
} | |
suspend fun main() { | |
val yin = captureCont() | |
println("@") | |
val yang = captureCont() | |
print("*") | |
yin(yang) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kotlinc-jvm 1.8.10