JS backend: Fix cached array access (KT-740-2)
This commit is contained in:
+83
-21
@@ -16,18 +16,23 @@
|
|||||||
|
|
||||||
package org.jetbrains.k2js.translate.reference;
|
package org.jetbrains.k2js.translate.reference;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
|
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
|
||||||
import org.jetbrains.jet.lang.psi.JetExpression;
|
import org.jetbrains.jet.lang.psi.JetExpression;
|
||||||
|
import org.jetbrains.jet.lang.psi.ValueArgument;
|
||||||
|
import org.jetbrains.jet.lang.resolve.calls.model.ExpressionValueArgument;
|
||||||
|
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||||
|
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedValueArgument;
|
||||||
|
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
|
|
||||||
public class ArrayAccessTranslator extends AbstractTranslator implements AccessTranslator {
|
public class ArrayAccessTranslator extends AbstractTranslator implements AccessTranslator {
|
||||||
|
|
||||||
@@ -49,49 +54,106 @@ public class ArrayAccessTranslator extends AbstractTranslator implements AccessT
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JsExpression translateAsGet() {
|
public JsExpression translateAsGet() {
|
||||||
return translateAsGet(translateArrayExpression());
|
return translateAsGet(getArrayExpression());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected JsExpression translateAsGet(@NotNull JsExpression arrayExpression) {
|
protected JsExpression translateAsGet(@NotNull JsExpression arrayExpression) {
|
||||||
return translateAsMethodCall(arrayExpression, /*isGetter = */ true);
|
return translateAsMethodCall(arrayExpression, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JsExpression translateAsSet(@NotNull JsExpression setTo) {
|
public JsExpression translateAsSet(@NotNull JsExpression setTo) {
|
||||||
return translateAsSet(translateArrayExpression(), translateIndexExpressions(), setTo);
|
return translateAsSet(getArrayExpression(), setTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected JsExpression translateAsSet(@NotNull JsExpression arrayExpression, @NotNull List<JsExpression> indexExpressions, @NotNull JsExpression toSetTo) {
|
protected JsExpression translateAsSet(@NotNull JsExpression arrayExpression, @NotNull JsExpression toSetTo) {
|
||||||
List<JsExpression> arguments = Lists.newArrayList(indexExpressions);
|
return translateAsMethodCall(arrayExpression, toSetTo);
|
||||||
arguments.add(toSetTo);
|
|
||||||
return translateAsMethodCall(arrayExpression, /*isGetter = */ false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private JsExpression translateAsMethodCall(@NotNull JsExpression arrayExpression, boolean isGetter) {
|
private JsExpression translateAsMethodCall(@NotNull JsExpression arrayExpression, @Nullable JsExpression toSetTo) {
|
||||||
return ReferencePackage.buildCall(context(),
|
boolean isGetter = toSetTo == null;
|
||||||
BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, isGetter),
|
TranslationContext context = context();
|
||||||
arrayExpression);
|
ResolvedCall<FunctionDescriptor> resolvedCall = BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, isGetter);
|
||||||
|
if (!isGetter) {
|
||||||
|
context = contextWithValueParameterAliasInArrayGetAccess(toSetTo);
|
||||||
|
}
|
||||||
|
return ReferencePackage.buildCall(context, resolvedCall, arrayExpression);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected List<JsExpression> translateIndexExpressions() {
|
protected JsExpression getArrayExpression() {
|
||||||
return TranslationUtils.translateExpressionList(context(), expression.getIndexExpressions());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
protected JsExpression translateArrayExpression() {
|
|
||||||
JetExpression arrayExpression = expression.getArrayExpression();
|
JetExpression arrayExpression = expression.getArrayExpression();
|
||||||
assert arrayExpression != null : "Code with parsing errors shouldn't be translated";
|
assert arrayExpression != null : "Code with parsing errors shouldn't be translated";
|
||||||
return Translation.translateAsExpression(arrayExpression, context());
|
return Translation.translateAsExpression(arrayExpression, context());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is hack for a[b]++ -> a.set(b, a.get(b) + 1). Frontend generate fake expression for a.get(b) + 1.
|
||||||
|
@NotNull
|
||||||
|
private TranslationContext contextWithValueParameterAliasInArrayGetAccess(@NotNull JsExpression toSetTo) {
|
||||||
|
ResolvedCall<FunctionDescriptor> resolvedCall =
|
||||||
|
BindingUtils.getResolvedCallForArrayAccess(bindingContext(), expression, /*isGetter = */ false);
|
||||||
|
|
||||||
|
List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
|
||||||
|
ResolvedValueArgument lastArgument = arguments.get(arguments.size() - 1);
|
||||||
|
assert lastArgument instanceof ExpressionValueArgument: "Last argument of array-like setter must be ExpressionValueArgument";
|
||||||
|
|
||||||
|
ValueArgument valueArgument = ((ExpressionValueArgument) lastArgument).getValueArgument();
|
||||||
|
assert valueArgument != null;
|
||||||
|
|
||||||
|
JetExpression element = valueArgument.getArgumentExpression();
|
||||||
|
return context().innerContextWithAliasesForExpressions(Collections.singletonMap(element, toSetTo));
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public CachedAccessTranslator getCached() {
|
public CachedAccessTranslator getCached() {
|
||||||
return new CachedArrayAccessTranslator(expression, context());
|
List<TemporaryVariable> temporaries = new ArrayList<TemporaryVariable>();
|
||||||
|
Map<JetExpression, JsExpression> aliases = new HashMap<JetExpression, JsExpression>();
|
||||||
|
|
||||||
|
TemporaryVariable temporaryArrayExpression = context().declareTemporary(getArrayExpression());
|
||||||
|
temporaries.add(temporaryArrayExpression);
|
||||||
|
|
||||||
|
for (JetExpression jetExpression : expression.getIndexExpressions()) {
|
||||||
|
JsExpression jsExpression = Translation.translateAsExpression(jetExpression, context());
|
||||||
|
TemporaryVariable temporaryVariable = context().declareTemporary(jsExpression);
|
||||||
|
temporaries.add(temporaryVariable);
|
||||||
|
aliases.put(jetExpression, temporaryVariable.reference());
|
||||||
|
}
|
||||||
|
return new CachedArrayAccessTranslator(expression, context().innerContextWithAliasesForExpressions(aliases),
|
||||||
|
temporaryArrayExpression, temporaries);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CachedArrayAccessTranslator extends ArrayAccessTranslator implements CachedAccessTranslator {
|
||||||
|
@NotNull
|
||||||
|
private final TemporaryVariable temporaryArrayExpression;
|
||||||
|
@NotNull
|
||||||
|
private final List<TemporaryVariable> declaredTemporaries;
|
||||||
|
|
||||||
|
protected CachedArrayAccessTranslator(
|
||||||
|
@NotNull JetArrayAccessExpression expression,
|
||||||
|
@NotNull TranslationContext context,
|
||||||
|
@NotNull TemporaryVariable temporaryArrayExpression,
|
||||||
|
@NotNull List<TemporaryVariable> temporaries
|
||||||
|
) {
|
||||||
|
super(expression, context);
|
||||||
|
this.temporaryArrayExpression = temporaryArrayExpression;
|
||||||
|
declaredTemporaries = temporaries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected JsExpression getArrayExpression() {
|
||||||
|
return temporaryArrayExpression.reference();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public List<TemporaryVariable> declaredTemporaries() {
|
||||||
|
return declaredTemporaries;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-65
@@ -1,65 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2010-2013 JetBrains s.r.o.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.k2js.translate.reference;
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
|
|
||||||
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.TemporariesUtils.fromExpressionList;
|
|
||||||
import static org.jetbrains.k2js.translate.utils.TemporariesUtils.toExpressionList;
|
|
||||||
|
|
||||||
public final class CachedArrayAccessTranslator extends ArrayAccessTranslator implements CachedAccessTranslator {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final TemporaryVariable arrayExpression;
|
|
||||||
@NotNull
|
|
||||||
private final List<TemporaryVariable> indexExpressions;
|
|
||||||
|
|
||||||
/*package*/ CachedArrayAccessTranslator(@NotNull JetArrayAccessExpression expression,
|
|
||||||
@NotNull TranslationContext context) {
|
|
||||||
super(expression, context);
|
|
||||||
arrayExpression = context.declareTemporary(translateArrayExpression());
|
|
||||||
indexExpressions = fromExpressionList(translateIndexExpressions(), context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public JsExpression translateAsGet() {
|
|
||||||
return translateAsGet(arrayExpression.reference());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public JsExpression translateAsSet(@NotNull JsExpression setTo) {
|
|
||||||
return translateAsSet(arrayExpression.reference(), toExpressionList(indexExpressions), setTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public List<TemporaryVariable> declaredTemporaries() {
|
|
||||||
List<TemporaryVariable> result = Lists.newArrayList();
|
|
||||||
result.add(arrayExpression);
|
|
||||||
result.addAll(indexExpressions);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,16 @@ package foo
|
|||||||
var c0 = 0
|
var c0 = 0
|
||||||
var c1 = 0
|
var c1 = 0
|
||||||
var c2 = 0
|
var c2 = 0
|
||||||
|
var c3 = 0
|
||||||
|
|
||||||
|
fun cStr(): String {
|
||||||
|
return "${c0}${c1}${c2}${c3}"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun get1(): Int {
|
||||||
|
c3++
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
class A() {
|
class A() {
|
||||||
var p = 0
|
var p = 0
|
||||||
@@ -24,31 +34,24 @@ val a: A = A()
|
|||||||
|
|
||||||
fun box(): String {
|
fun box(): String {
|
||||||
var d = a[1]
|
var d = a[1]
|
||||||
if (c0 != 1) {
|
if (cStr() != "1100") {
|
||||||
return "1"
|
return "Fail: d = a[1], cStr(): ${cStr()}"
|
||||||
}
|
|
||||||
if (c1 != 1) {
|
|
||||||
return "2"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
++a[1]
|
++a[1]
|
||||||
if (c0 != 2) {
|
if (cStr() != "2310") {
|
||||||
return "3"
|
return "Fail: ++a[1], cStr(): ${cStr()}"
|
||||||
}
|
|
||||||
if (c1 != 3) {
|
|
||||||
return "4"
|
|
||||||
}
|
|
||||||
if (c2 != 1) {
|
|
||||||
return "5"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
--a[1]
|
--a[1]
|
||||||
if (c0 != 3) {
|
if (cStr() != "3520") {
|
||||||
return "6"
|
return "Fail: --a[1], cStr(): ${cStr()}"
|
||||||
}
|
}
|
||||||
if (c1 != 5) {
|
|
||||||
return "7"
|
++a[get1()]
|
||||||
}
|
if (cStr() != "4731") {
|
||||||
if (c2 != 2) {
|
return "Fail: ++a[get1()], cStr(): ${cStr()}"
|
||||||
return "8"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user