From 5482b8b28a88e907bb209b0aeb449fae0e1903b0 Mon Sep 17 00:00:00 2001 From: SokolovaMaria Date: Tue, 29 Dec 2020 19:30:48 +0300 Subject: [PATCH] Receivers of nested inline functions correctly resolved by the JS transformer (#175) --- .../transformer/AtomicFUTransformerJS.kt | 10 +++++---- .../test/NestedInlineFunctionsTest.kt | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 atomicfu/src/commonTest/kotlin/kotlinx/atomicfu/test/NestedInlineFunctionsTest.kt diff --git a/atomicfu-transformer/src/main/kotlin/kotlinx/atomicfu/transformer/AtomicFUTransformerJS.kt b/atomicfu-transformer/src/main/kotlin/kotlinx/atomicfu/transformer/AtomicFUTransformerJS.kt index 6fda7537..91f30372 100644 --- a/atomicfu-transformer/src/main/kotlin/kotlinx/atomicfu/transformer/AtomicFUTransformerJS.kt +++ b/atomicfu-transformer/src/main/kotlin/kotlinx/atomicfu/transformer/AtomicFUTransformerJS.kt @@ -382,7 +382,8 @@ class AtomicFUTransformerJS( } // other cases with $receiver.kotlinx$atomicfu$value in inline functions else if (node.target.toSource().matches(Regex(RECEIVER))) { - val rr = ReceiverResolver() + val receiverName = node.target.toSource() // $receiver_i + val rr = ReceiverResolver(receiverName) node.enclosingFunction.visit(rr) rr.receiver?.let { node.target = it } } @@ -475,11 +476,11 @@ class AtomicFUTransformerJS( // receiver data flow - inner class ReceiverResolver : NodeVisitor { + inner class ReceiverResolver(private val receiverName: String) : NodeVisitor { var receiver: AstNode? = null override fun visit(node: AstNode): Boolean { if (node is VariableInitializer) { - if (node.target.toSource().matches(Regex(RECEIVER))) { + if (node.target.toSource() == receiverName) { receiver = node.initializer return false } @@ -496,7 +497,8 @@ class AtomicFUTransformerJS( val funcName = (node.target as PropertyGet).property var field = (node.target as PropertyGet).target if (field.toSource().matches(Regex(RECEIVER))) { - val rr = ReceiverResolver() + val receiverName = field.toSource() // $receiver_i + val rr = ReceiverResolver(receiverName) node.enclosingFunction.visit(rr) if (rr.receiver != null) { field = rr.receiver diff --git a/atomicfu/src/commonTest/kotlin/kotlinx/atomicfu/test/NestedInlineFunctionsTest.kt b/atomicfu/src/commonTest/kotlin/kotlinx/atomicfu/test/NestedInlineFunctionsTest.kt new file mode 100644 index 00000000..4a2c76d9 --- /dev/null +++ b/atomicfu/src/commonTest/kotlin/kotlinx/atomicfu/test/NestedInlineFunctionsTest.kt @@ -0,0 +1,21 @@ +package kotlinx.atomicfu.test + +import kotlinx.atomicfu.* +import kotlin.test.* + +// Ensures that inline function receivers are resolved correctly by the JS transformer +class NestedInlineFunctionsTest { + val _a = atomic(5) + val _b = atomic(42) + + @Test + fun testNestedInlineFunctions() { + _a.loop { a -> + _b.loop { b -> + assertEquals(5, a) + assertEquals(42, b) + return + } + } + } +} \ No newline at end of file