JS: decomposition of array access
This commit is contained in:
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.js.inline.util
|
||||
import com.google.dart.compiler.backend.js.ast.*
|
||||
import com.google.dart.compiler.backend.js.ast.metadata.*
|
||||
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils.*
|
||||
import org.jetbrains.kotlin.js.translate.utils.ast.*
|
||||
|
||||
import kotlin.platform.platformStatic
|
||||
import kotlin.properties.Delegates
|
||||
@@ -81,6 +82,21 @@ class ExpressionDecomposer private (
|
||||
return false
|
||||
}
|
||||
|
||||
override fun visit(x: JsArrayAccess, ctx: JsContext<*>): Boolean {
|
||||
x.process()
|
||||
return false
|
||||
}
|
||||
|
||||
private fun JsArrayAccess.process() {
|
||||
array = accept(array)
|
||||
|
||||
if (array in containsNodeWithSideEffect && index in containsExtractable) {
|
||||
array = array.extractToTemporary()
|
||||
}
|
||||
|
||||
index = accept(index)
|
||||
}
|
||||
|
||||
override fun visit(x: JsInvocation, ctx: JsContext<*>): Boolean {
|
||||
CallableInvocationAdapter(x).process()
|
||||
return false
|
||||
|
||||
@@ -55,3 +55,11 @@ public fun JsNode.any(predicate: (JsNode) -> Boolean): Boolean {
|
||||
visitor.accept(this)
|
||||
return visitor.matched
|
||||
}
|
||||
|
||||
public var JsArrayAccess.index: JsExpression
|
||||
get() = getIndexExpression()
|
||||
set(value) = setIndexExpression(value)
|
||||
|
||||
public var JsArrayAccess.array: JsExpression
|
||||
get() = getArrayExpression()
|
||||
set(value) = setArrayExpression(value)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(2, fizz(array(1, 2))[buzz(1)])
|
||||
assertEquals("fizz(1,2);buzz(1);", pullLog())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(2, array(1, 2)[fizz(0) + buzz(1)])
|
||||
assertEquals("fizz(0);buzz(1);", pullLog())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(2, array(1, fizz(2))[fizz(0) + buzz(1)])
|
||||
assertEquals("fizz(2);fizz(0);buzz(1);", pullLog())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user