JS backend: refactor: extract DefineInvocation
This commit is contained in:
@@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* 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.declaration;
|
||||||
|
|
||||||
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||||
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
|
import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
||||||
|
|
||||||
|
import java.util.AbstractList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DefineInvocation {
|
||||||
|
|
||||||
|
/* package */
|
||||||
|
@NotNull
|
||||||
|
static DefineInvocation createDefineInvocation(
|
||||||
|
@NotNull NamespaceDescriptor descriptor,
|
||||||
|
@Nullable JsExpression initializer,
|
||||||
|
@NotNull JsObjectLiteral members,
|
||||||
|
@NotNull TranslationContext context
|
||||||
|
) {
|
||||||
|
return new DefineInvocation(initializer == null ? JsLiteral.NULL : initializer,
|
||||||
|
new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, context.getQualifiedReference(descriptor)),
|
||||||
|
members);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsExpression initializer;
|
||||||
|
@NotNull
|
||||||
|
private final JsDocComment jsDocComment;
|
||||||
|
@NotNull
|
||||||
|
private final JsObjectLiteral membersObjectLiteral;
|
||||||
|
|
||||||
|
private DefineInvocation(
|
||||||
|
@NotNull JsExpression initializer,
|
||||||
|
@NotNull JsDocComment jsDocComment,
|
||||||
|
@NotNull JsObjectLiteral membersObjectLiteral
|
||||||
|
) {
|
||||||
|
this.initializer = initializer;
|
||||||
|
this.jsDocComment = jsDocComment;
|
||||||
|
this.membersObjectLiteral = membersObjectLiteral;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public JsExpression getInitializer() {
|
||||||
|
return initializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInitializer(@NotNull JsExpression initializer) {
|
||||||
|
this.initializer = initializer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<JsPropertyInitializer> getMembers() {
|
||||||
|
return membersObjectLiteral.getPropertyInitializers();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public List<JsExpression> asList() {
|
||||||
|
return new AbstractList<JsExpression>() { // because initializer is mutable
|
||||||
|
@Override
|
||||||
|
public JsExpression get(int index) {
|
||||||
|
switch (index) {
|
||||||
|
case 0:
|
||||||
|
return initializer;
|
||||||
|
case 1:
|
||||||
|
return jsDocComment;
|
||||||
|
case 2:
|
||||||
|
return membersObjectLiteral;
|
||||||
|
}
|
||||||
|
throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+5
-22
@@ -19,7 +19,6 @@ package org.jetbrains.k2js.translate.declaration;
|
|||||||
import com.google.dart.compiler.backend.js.ast.*;
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
import gnu.trove.THashMap;
|
import gnu.trove.THashMap;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||||
@@ -27,11 +26,11 @@ import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
|||||||
import org.jetbrains.k2js.translate.context.Namer;
|
import org.jetbrains.k2js.translate.context.Namer;
|
||||||
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.utils.JsAstUtils;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static com.google.dart.compiler.backend.js.ast.JsVars.JsVar;
|
import static com.google.dart.compiler.backend.js.ast.JsVars.JsVar;
|
||||||
|
import static org.jetbrains.k2js.translate.declaration.DefineInvocation.createDefineInvocation;
|
||||||
|
|
||||||
public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||||
private final Iterable<JetFile> files;
|
private final Iterable<JetFile> files;
|
||||||
@@ -51,7 +50,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private List<JsStatement> translate() {
|
private List<JsStatement> translate() {
|
||||||
// predictable order
|
// predictable order
|
||||||
Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation = new THashMap<NamespaceDescriptor, List<JsExpression>>();
|
Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation = new THashMap<NamespaceDescriptor, DefineInvocation>();
|
||||||
NamespaceDescriptor rootNamespaceDescriptor = null;
|
NamespaceDescriptor rootNamespaceDescriptor = null;
|
||||||
|
|
||||||
for (JetFile file : files) {
|
for (JetFile file : files) {
|
||||||
@@ -95,7 +94,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private NamespaceDescriptor getRootPackageDescriptor(
|
private NamespaceDescriptor getRootPackageDescriptor(
|
||||||
@NotNull Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation,
|
@NotNull Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation,
|
||||||
@NotNull NamespaceDescriptor descriptor
|
@NotNull NamespaceDescriptor descriptor
|
||||||
) {
|
) {
|
||||||
NamespaceDescriptor rootNamespace = descriptor;
|
NamespaceDescriptor rootNamespace = descriptor;
|
||||||
@@ -107,24 +106,8 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
|||||||
return rootNamespace;
|
return rootNamespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<JsExpression> createDefineInvocation(
|
private JsVar getRootPackageDeclaration(@NotNull DefineInvocation defineInvocation) {
|
||||||
@NotNull NamespaceDescriptor descriptor,
|
JsExpression rootPackageVar = new JsInvocation(context().namer().rootPackageDefinitionMethodReference(), defineInvocation.asList());
|
||||||
@Nullable JsExpression initializer,
|
|
||||||
@NotNull JsObjectLiteral members,
|
|
||||||
@NotNull TranslationContext context
|
|
||||||
) {
|
|
||||||
if (context.isEcma5()) {
|
|
||||||
return Arrays.asList(initializer == null ? JsLiteral.NULL : initializer,
|
|
||||||
new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, context.getQualifiedReference(descriptor)),
|
|
||||||
members);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return Collections.<JsExpression>singletonList(members);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsVar getRootPackageDeclaration(@NotNull List<JsExpression> defineInvocation) {
|
|
||||||
JsExpression rootPackageVar = new JsInvocation(context().namer().rootPackageDefinitionMethodReference(), defineInvocation);
|
|
||||||
return new JsVar(context().scope().declareName(Namer.getRootNamespaceName()), rootPackageVar);
|
return new JsVar(context().scope().declareName(Namer.getRootNamespaceName()), rootPackageVar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-24
@@ -39,7 +39,7 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.declaration.NamespaceDeclarationTranslator.createDefineInvocation;
|
import static org.jetbrains.k2js.translate.declaration.DefineInvocation.createDefineInvocation;
|
||||||
import static org.jetbrains.k2js.translate.expression.LiteralFunctionTranslator.createPlace;
|
import static org.jetbrains.k2js.translate.expression.LiteralFunctionTranslator.createPlace;
|
||||||
import static org.jetbrains.k2js.translate.initializer.InitializerUtils.generateInitializerForDelegate;
|
import static org.jetbrains.k2js.translate.initializer.InitializerUtils.generateInitializerForDelegate;
|
||||||
import static org.jetbrains.k2js.translate.initializer.InitializerUtils.generateInitializerForProperty;
|
import static org.jetbrains.k2js.translate.initializer.InitializerUtils.generateInitializerForProperty;
|
||||||
@@ -55,7 +55,7 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
NamespaceTranslator(
|
NamespaceTranslator(
|
||||||
@NotNull final NamespaceDescriptor descriptor,
|
@NotNull final NamespaceDescriptor descriptor,
|
||||||
@NotNull final Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation,
|
@NotNull final Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation,
|
||||||
@NotNull TranslationContext context
|
@NotNull TranslationContext context
|
||||||
) {
|
) {
|
||||||
super(context.newDeclaration(descriptor));
|
super(context.newDeclaration(descriptor));
|
||||||
@@ -68,12 +68,12 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public Trinity<List<JsPropertyInitializer>, LabelGenerator, JsExpression> compute() {
|
public Trinity<List<JsPropertyInitializer>, LabelGenerator, JsExpression> compute() {
|
||||||
List<JsExpression> defineInvocation = descriptorToDefineInvocation.get(descriptor);
|
DefineInvocation defineInvocation = descriptorToDefineInvocation.get(descriptor);
|
||||||
if (defineInvocation == null) {
|
if (defineInvocation == null) {
|
||||||
defineInvocation = createDefinitionPlace(null, descriptorToDefineInvocation);
|
defineInvocation = createDefinitionPlace(null, descriptorToDefineInvocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
return createPlace(getListFromPlace(defineInvocation), context().getQualifiedReference(descriptor));
|
return createPlace(defineInvocation.getMembers(), context().getQualifiedReference(descriptor));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -88,15 +88,17 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
context().literalFunctionTranslator().setDefinitionPlace(null);
|
context().literalFunctionTranslator().setDefinitionPlace(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<JsExpression> createDefinitionPlace(@Nullable JsExpression initializer,
|
private DefineInvocation createDefinitionPlace(
|
||||||
Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation) {
|
@Nullable JsExpression initializer,
|
||||||
List<JsExpression> place = createDefineInvocation(descriptor, initializer, new JsObjectLiteral(visitor.getResult(), true), context());
|
Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation
|
||||||
|
) {
|
||||||
|
DefineInvocation place = createDefineInvocation(descriptor, initializer, new JsObjectLiteral(visitor.getResult(), true), context());
|
||||||
descriptorToDefineInvocation.put(descriptor, place);
|
descriptorToDefineInvocation.put(descriptor, place);
|
||||||
addToParent((NamespaceDescriptor) descriptor.getContainingDeclaration(), getEntry(descriptor, place), descriptorToDefineInvocation);
|
addToParent((NamespaceDescriptor) descriptor.getContainingDeclaration(), getEntry(descriptor, place), descriptorToDefineInvocation);
|
||||||
return place;
|
return place;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(@NotNull Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation,
|
public void add(@NotNull Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation,
|
||||||
@NotNull List<JsStatement> initializers) {
|
@NotNull List<JsStatement> initializers) {
|
||||||
JsExpression initializer;
|
JsExpression initializer;
|
||||||
if (visitor.initializerStatements.isEmpty()) {
|
if (visitor.initializerStatements.isEmpty()) {
|
||||||
@@ -110,19 +112,19 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<JsExpression> defineInvocation = descriptorToDefineInvocation.get(descriptor);
|
DefineInvocation defineInvocation = descriptorToDefineInvocation.get(descriptor);
|
||||||
if (defineInvocation == null) {
|
if (defineInvocation == null) {
|
||||||
if (initializer != null || !visitor.getResult().isEmpty()) {
|
if (initializer != null || !visitor.getResult().isEmpty()) {
|
||||||
createDefinitionPlace(initializer, descriptorToDefineInvocation);
|
createDefinitionPlace(initializer, descriptorToDefineInvocation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (context().isEcma5() && initializer != null) {
|
if (initializer != null) {
|
||||||
assert defineInvocation.get(0) == JsLiteral.NULL;
|
assert defineInvocation.getInitializer() == JsLiteral.NULL;
|
||||||
defineInvocation.set(0, initializer);
|
defineInvocation.setInitializer(initializer);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<JsPropertyInitializer> listFromPlace = getListFromPlace(defineInvocation);
|
List<JsPropertyInitializer> listFromPlace = defineInvocation.getMembers();
|
||||||
// if equals, so, inner functions was added
|
// if equals, so, inner functions was added
|
||||||
if (listFromPlace != visitor.getResult()) {
|
if (listFromPlace != visitor.getResult()) {
|
||||||
listFromPlace.addAll(visitor.getResult());
|
listFromPlace.addAll(visitor.getResult());
|
||||||
@@ -130,21 +132,17 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<JsPropertyInitializer> getListFromPlace(List<JsExpression> defineInvocation) {
|
private JsPropertyInitializer getEntry(@NotNull NamespaceDescriptor descriptor, DefineInvocation defineInvocation) {
|
||||||
return ((JsObjectLiteral) defineInvocation.get(context().isEcma5() ? 2 : 0)).getPropertyInitializers();
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsPropertyInitializer getEntry(@NotNull NamespaceDescriptor descriptor, List<JsExpression> defineInvocation) {
|
|
||||||
return new JsPropertyInitializer(context().getNameForDescriptor(descriptor).makeRef(),
|
return new JsPropertyInitializer(context().getNameForDescriptor(descriptor).makeRef(),
|
||||||
new JsInvocation(context().namer().packageDefinitionMethodReference(), defineInvocation));
|
new JsInvocation(context().namer().packageDefinitionMethodReference(), defineInvocation.asList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean addEntryIfParentExists(NamespaceDescriptor parentDescriptor,
|
private boolean addEntryIfParentExists(NamespaceDescriptor parentDescriptor,
|
||||||
JsPropertyInitializer entry,
|
JsPropertyInitializer entry,
|
||||||
Map<NamespaceDescriptor, List<JsExpression>> descriptorToDeclarationPlace) {
|
Map<NamespaceDescriptor, DefineInvocation> descriptorToDeclarationPlace) {
|
||||||
List<JsExpression> parentDefineInvocation = descriptorToDeclarationPlace.get(parentDescriptor);
|
DefineInvocation parentDefineInvocation = descriptorToDeclarationPlace.get(parentDescriptor);
|
||||||
if (parentDefineInvocation != null) {
|
if (parentDefineInvocation != null) {
|
||||||
getListFromPlace(parentDefineInvocation).add(entry);
|
parentDefineInvocation.getMembers().add(entry);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -152,10 +150,10 @@ final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
private void addToParent(NamespaceDescriptor parentDescriptor,
|
private void addToParent(NamespaceDescriptor parentDescriptor,
|
||||||
JsPropertyInitializer entry,
|
JsPropertyInitializer entry,
|
||||||
Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation) {
|
Map<NamespaceDescriptor, DefineInvocation> descriptorToDefineInvocation) {
|
||||||
while (!addEntryIfParentExists(parentDescriptor, entry, descriptorToDefineInvocation)) {
|
while (!addEntryIfParentExists(parentDescriptor, entry, descriptorToDefineInvocation)) {
|
||||||
JsObjectLiteral members = new JsObjectLiteral(new SmartList<JsPropertyInitializer>(entry), true);
|
JsObjectLiteral members = new JsObjectLiteral(new SmartList<JsPropertyInitializer>(entry), true);
|
||||||
List<JsExpression> defineInvocation = createDefineInvocation(parentDescriptor, null, members, context());
|
DefineInvocation defineInvocation = createDefineInvocation(parentDescriptor, null, members, context());
|
||||||
entry = getEntry(parentDescriptor, defineInvocation);
|
entry = getEntry(parentDescriptor, defineInvocation);
|
||||||
|
|
||||||
descriptorToDefineInvocation.put(parentDescriptor, defineInvocation);
|
descriptorToDefineInvocation.put(parentDescriptor, defineInvocation);
|
||||||
|
|||||||
Reference in New Issue
Block a user