Skip to content

Commit

Permalink
Receivers of nested inline functions correctly resolved by the JS tra…
Browse files Browse the repository at this point in the history
…nsformer (#175)
  • Loading branch information
mvicsokolova authored Dec 29, 2020
1 parent d5c5580 commit 5482b8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

0 comments on commit 5482b8b

Please sign in to comment.