Skip to content

Commit

Permalink
Add inHandlingContext and tests for it
Browse files Browse the repository at this point in the history
 Fix debug message in Continuation.forEach
 Fix logic in compilerCloning for JVM when running in debugger
  • Loading branch information
kyay10 committed Jun 8, 2024
1 parent f013aba commit fdcb28e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library/src/commonMain/kotlin/cloning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal inline fun Continuation<*>.forEach(block: (Continuation<*>) -> Unit) {
current = when (current) {
in CompilerGenerated -> current.completion
is CopyableContinuation -> current.completion
else -> error("Continuation $this is not see-through, so its stack can't be traversed")
else -> error("Continuation $current is not see-through, so its stack can't be traversed")
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions library/src/commonMain/kotlin/reset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ public suspend fun <T, R> Prompt<R>.takeSubCont(
body.startCoroutine(SubCont(subchain), if(deleteDelimiter) hole.completion else hole)
}

// Acts like shift0/shift { it(body()) }
// This is NOT multishot
@ResetDsl
public suspend fun <T> Prompt<*>.inHandlingContext(
includeBodyContext: Boolean = false, body: suspend () -> T
): T = suspendCoroutineUnintercepted { k ->
val hole = k.context.holeFor(this, !includeBodyContext)
body.startCoroutine(Continuation(hole.context) {
val exception = it.exceptionOrNull()
if (exception is SeekingCoroutineContextException) exception.use(hole.context)
else k.resumeWith(it)
})
}

@Suppress("UNCHECKED_CAST")
internal fun <R> Prompt<R>.abortWith(deleteDelimiter: Boolean, value: Result<R>): Nothing =
throw AbortWithValueException(this as Prompt<Any?>, value, deleteDelimiter)
Expand Down
50 changes: 50 additions & 0 deletions library/src/commonTest/kotlin/ContinuationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,56 @@ class ContinuationTest {
x + 1
} shouldBe 4
}

@Test
fun handlingContext() = runTest {
runCC {
runReader(1) {
newReset<Int> {
pushReader(2) {
ask() shouldBe 2
shift { k -> k(ask()) } shouldBe 1
shift0 { k -> k(ask()) } shouldBe 1
ask() shouldBe 2
inHandlingContext { ask() } shouldBe 1
inHandlingContext(includeBodyContext = true) { ask() } shouldBe 1
ask() shouldBe 2
}
}
}
}
runCC {
runReader(1) {
newReset<Int> {
pushReader(2) {
ask() shouldBe 2
shift { k ->
val v = ask()
pushReader(3) { k(v) }
} shouldBe 1
ask() shouldBe 2
shift0 { k -> k(ask()) } shouldBe 3
shift0 { k -> shift0 { k(ask()) } } shouldBe 1
ask() shouldBe 2
shift { k ->
val v = ask()
pushReader(4) { k(v) }
} shouldBe 1
ask() shouldBe 2
// Introduces an extra delimiter
shift { k -> k(ask()) } shouldBe 4
ask() shouldBe 2
inHandlingContext { ask() } shouldBe 4
inHandlingContext { inHandlingContext { ask() } } shouldBe 4
inHandlingContext { inHandlingContext { inHandlingContext { ask() } } } shouldBe 1
inHandlingContext(includeBodyContext = true) { inHandlingContext { inHandlingContext { ask() } } } shouldBe 4
inHandlingContext(includeBodyContext = true) { inHandlingContext { inHandlingContext { inHandlingContext { ask() } } } } shouldBe 1
ask() shouldBe 2
}
}
}
}
}
}

val stackSafeIterations: Int = when (platform) {
Expand Down
3 changes: 1 addition & 2 deletions library/src/jvmMain/kotlin/compilerCloning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ internal actual fun <T> Continuation<T>.copy(completion: Continuation<*>): Conti
}

else -> {
val delegate = delegateField.get(this) as Continuation<*>
coroutineOwnerConstructor.newInstance(delegate.copy(completion), infoField.get(this)) as Continuation<T>
coroutineOwnerConstructor.newInstance(completion, infoField.get(this)) as Continuation<T>
}
}

0 comments on commit fdcb28e

Please sign in to comment.