From c54c4a6ce8d425707de69daa24f49bffb7d3609a Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 9 Nov 2017 19:59:13 +0300 Subject: [PATCH] JS: fix support of JsSwitch in coroutines --- .../js/coroutine/CoroutineBodyTransformer.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt index 4a794713060..d6fbda517b3 100644 --- a/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt +++ b/js/js.inliner/src/org/jetbrains/kotlin/js/coroutine/CoroutineBodyTransformer.kt @@ -88,6 +88,27 @@ class CoroutineBodyTransformer(private val context: CoroutineTransformationConte currentBlock = jointBlock } + override fun visit(x: JsSwitch) = splitIfNecessary(x) { + val switchBlock = currentBlock + val jointBlock = CoroutineBlock() + + withBreakAndContinue(x, jointBlock, null) { + for (jsCase in x.cases) { + val caseBlock = CoroutineBlock() + currentBlock = caseBlock + + jsCase.statements.forEach { accept(it) } + currentBlock.statements += stateAndJump(jointBlock, x) + + jsCase.statements.clear() + jsCase.statements += caseBlock.statements + } + } + switchBlock.statements += x + + currentBlock = jointBlock + } + override fun visitLabel(x: JsLabel) { val inner = x.statement when (inner) { @@ -370,7 +391,7 @@ class CoroutineBodyTransformer(private val context: CoroutineTransformationConte val invocationStatement = JsAstUtils.assignment(resultRef, invocation).makeStmt() val suspendCondition = JsAstUtils.equality(resultRef.deepCopy(), context.metadata.suspendObjectRef.deepCopy()).source(psi) val suspendIfNeeded = JsIf(suspendCondition, JsReturn(context.metadata.suspendObjectRef.deepCopy().source(psi))) - currentStatements += listOf(invocationStatement, suspendIfNeeded, JsBreak().apply { source = psi }) + currentStatements += listOf(invocationStatement, suspendIfNeeded, JsContinue().apply { source = psi }) currentBlock = nextBlock }