Clean up and stylistic changes after merging in https://github.com/JetBrains/kotlin/pull/55
This commit is contained in:
@@ -22,5 +22,5 @@ package org.jetbrains.jet.cli.common;
|
|||||||
public class CompilerVersion {
|
public class CompilerVersion {
|
||||||
// The value of this constant is generated by the build script
|
// The value of this constant is generated by the build script
|
||||||
// DON'T MODIFY IT
|
// DON'T MODIFY IT
|
||||||
public static final String VERSION = "snapshot";
|
public static final String VERSION = "@snapshot@";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,6 +137,11 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
|||||||
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
|
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version");
|
||||||
addPathToSourcesDir(args, srcDir);
|
addPathToSourcesDir(args, srcDir);
|
||||||
addOutputPath(outFile, args);
|
addOutputPath(outFile, args);
|
||||||
|
addLibLocationAndTarget(project, args);
|
||||||
|
return ArrayUtil.toStringArray(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addLibLocationAndTarget(@NotNull Project project, @NotNull ArrayList<String> args) {
|
||||||
Pair<String, String> data = JsModuleDetector.getLibLocationAndTargetForProject(project);
|
Pair<String, String> data = JsModuleDetector.getLibLocationAndTargetForProject(project);
|
||||||
if (data.first != null) {
|
if (data.first != null) {
|
||||||
args.add("-libzip");
|
args.add("-libzip");
|
||||||
@@ -146,7 +151,6 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
|||||||
args.add("-target");
|
args.add("-target");
|
||||||
args.add(data.second);
|
args.add(data.second);
|
||||||
}
|
}
|
||||||
return ArrayUtil.toStringArray(args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addPathToSourcesDir(@NotNull ArrayList<String> args, @NotNull VirtualFile srcDir) {
|
private static void addPathToSourcesDir(@NotNull ArrayList<String> args, @NotNull VirtualFile srcDir) {
|
||||||
|
|||||||
@@ -45,14 +45,14 @@ import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.*;
|
|||||||
public final class StaticContext {
|
public final class StaticContext {
|
||||||
|
|
||||||
public static StaticContext generateStaticContext(@NotNull JetStandardLibrary library,
|
public static StaticContext generateStaticContext(@NotNull JetStandardLibrary library,
|
||||||
@NotNull BindingContext bindingContext, Translation.EcmaVersion ecmaVersion) {
|
@NotNull BindingContext bindingContext,
|
||||||
|
@NotNull Translation.EcmaVersion ecmaVersion) {
|
||||||
JsProgram program = new JsProgram("main");
|
JsProgram program = new JsProgram("main");
|
||||||
JsRootScope jsRootScope = program.getRootScope();
|
JsRootScope jsRootScope = program.getRootScope();
|
||||||
Namer namer = Namer.newInstance(jsRootScope);
|
Namer namer = Namer.newInstance(jsRootScope);
|
||||||
NamingScope scope = NamingScope.rootScope(jsRootScope);
|
NamingScope scope = NamingScope.rootScope(jsRootScope);
|
||||||
Intrinsics intrinsics = Intrinsics.standardLibraryIntrinsics(library);
|
Intrinsics intrinsics = Intrinsics.standardLibraryIntrinsics(library);
|
||||||
StandardClasses standardClasses =
|
StandardClasses standardClasses = StandardClasses.bindImplementations(namer.getKotlinScope());
|
||||||
StandardClasses.bindImplementations(namer.getKotlinScope());
|
|
||||||
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, scope, ecmaVersion);
|
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, scope, ecmaVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +170,7 @@ public final class StaticContext {
|
|||||||
if (!(descriptor instanceof NamespaceDescriptor)) {
|
if (!(descriptor instanceof NamespaceDescriptor)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String nameForNamespace = getNameForNamespace((NamespaceDescriptor)descriptor);
|
String nameForNamespace = getNameForNamespace((NamespaceDescriptor) descriptor);
|
||||||
return getRootScope().declareUnobfuscatableName(nameForNamespace);
|
return getRootScope().declareUnobfuscatableName(nameForNamespace);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -200,7 +200,7 @@ public final class StaticContext {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
boolean isGetter = descriptor instanceof PropertyGetterDescriptor;
|
boolean isGetter = descriptor instanceof PropertyGetterDescriptor;
|
||||||
String propertyName = ((PropertyAccessorDescriptor)descriptor).getCorrespondingProperty().getName();
|
String propertyName = ((PropertyAccessorDescriptor) descriptor).getCorrespondingProperty().getName();
|
||||||
String accessorName = Namer.getNameForAccessor(propertyName, isGetter);
|
String accessorName = Namer.getNameForAccessor(propertyName, isGetter);
|
||||||
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
||||||
return enclosingScope.declareObfuscatableName(accessorName);
|
return enclosingScope.declareObfuscatableName(accessorName);
|
||||||
@@ -228,12 +228,13 @@ public final class StaticContext {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: move somewhere
|
||||||
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
||||||
if (isEcma5()) {
|
if (isEcma5()) {
|
||||||
String name = descriptor.getName();
|
String name = descriptor.getName();
|
||||||
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
|
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
|
||||||
if (!isDefaultAccessor(propertyDescriptor.getGetter()) || !isDefaultAccessor(propertyDescriptor.getSetter())) {
|
if (!isDefaultAccessor(propertyDescriptor.getGetter()) || !isDefaultAccessor(propertyDescriptor.getSetter())) {
|
||||||
// _ is more preferable than $ — should be discussed later
|
// _ is more preferable than $ — should be discussed later
|
||||||
name = '_' + name;
|
name = '_' + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +259,7 @@ public final class StaticContext {
|
|||||||
if (!descriptor.getName().equals("toString")) {
|
if (!descriptor.getName().equals("toString")) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (((FunctionDescriptor)descriptor).getValueParameters().isEmpty()) {
|
if (((FunctionDescriptor) descriptor).getValueParameters().isEmpty()) {
|
||||||
return getEnclosingScope(descriptor).declareUnobfuscatableName("toString");
|
return getEnclosingScope(descriptor).declareUnobfuscatableName("toString");
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -272,7 +273,7 @@ public final class StaticContext {
|
|||||||
if (!(descriptor instanceof FunctionDescriptor)) {
|
if (!(descriptor instanceof FunctionDescriptor)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor)descriptor);
|
FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor) descriptor);
|
||||||
if (overriddenDescriptor == null) {
|
if (overriddenDescriptor == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -307,7 +308,7 @@ public final class StaticContext {
|
|||||||
if (!(descriptor instanceof ClassDescriptor)) {
|
if (!(descriptor instanceof ClassDescriptor)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (getSuperclass((ClassDescriptor)descriptor) == null) {
|
if (getSuperclass((ClassDescriptor) descriptor) == null) {
|
||||||
return getRootScope().innerScope("Scope for class " + descriptor.getName());
|
return getRootScope().innerScope("Scope for class " + descriptor.getName());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -319,7 +320,7 @@ public final class StaticContext {
|
|||||||
if (!(descriptor instanceof ClassDescriptor)) {
|
if (!(descriptor instanceof ClassDescriptor)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
ClassDescriptor superclass = getSuperclass((ClassDescriptor)descriptor);
|
ClassDescriptor superclass = getSuperclass((ClassDescriptor) descriptor);
|
||||||
if (superclass == null) {
|
if (superclass == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
|
|||||||
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
import org.jetbrains.jet.lang.types.lang.JetStandardLibrary;
|
||||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||||
import org.jetbrains.k2js.facade.exceptions.MainFunctionNotFoundException;
|
import org.jetbrains.k2js.facade.exceptions.MainFunctionNotFoundException;
|
||||||
import org.jetbrains.k2js.facade.exceptions.TranslationInternalException;
|
|
||||||
import org.jetbrains.k2js.facade.exceptions.TranslationException;
|
import org.jetbrains.k2js.facade.exceptions.TranslationException;
|
||||||
|
import org.jetbrains.k2js.facade.exceptions.TranslationInternalException;
|
||||||
import org.jetbrains.k2js.facade.exceptions.UnsupportedFeatureException;
|
import org.jetbrains.k2js.facade.exceptions.UnsupportedFeatureException;
|
||||||
import org.jetbrains.k2js.translate.context.StaticContext;
|
import org.jetbrains.k2js.translate.context.StaticContext;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
@@ -58,7 +58,7 @@ import static org.jetbrains.k2js.translate.utils.dangerous.DangerousData.collect
|
|||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
* <p/>
|
* <p/>
|
||||||
* This class provides a interface which all translators use to interact with each other.
|
* This class provides a interface which all translators use to interact with each other.
|
||||||
* Goal is to simlify interaction between translators.
|
* Goal is to simplify interaction between translators.
|
||||||
*/
|
*/
|
||||||
public final class Translation {
|
public final class Translation {
|
||||||
public enum EcmaVersion {
|
public enum EcmaVersion {
|
||||||
@@ -145,7 +145,9 @@ public final class Translation {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsProgram generateAst(@NotNull BindingContext bindingContext,
|
public static JsProgram generateAst(@NotNull BindingContext bindingContext,
|
||||||
@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters, EcmaVersion ecmaVersion) throws TranslationException {
|
@NotNull List<JetFile> files, @NotNull MainCallParameters mainCallParameters,
|
||||||
|
@NotNull EcmaVersion ecmaVersion)
|
||||||
|
throws TranslationException {
|
||||||
try {
|
try {
|
||||||
return doGenerateAst(bindingContext, files, mainCallParameters, ecmaVersion);
|
return doGenerateAst(bindingContext, files, mainCallParameters, ecmaVersion);
|
||||||
}
|
}
|
||||||
@@ -159,7 +161,8 @@ public final class Translation {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static JsProgram doGenerateAst(@NotNull BindingContext bindingContext, @NotNull List<JetFile> files,
|
private static JsProgram doGenerateAst(@NotNull BindingContext bindingContext, @NotNull List<JetFile> files,
|
||||||
@NotNull MainCallParameters mainCallParameters, EcmaVersion ecmaVersion) throws MainFunctionNotFoundException {
|
@NotNull MainCallParameters mainCallParameters,
|
||||||
|
@NotNull EcmaVersion ecmaVersion) throws MainFunctionNotFoundException {
|
||||||
//TODO: move some of the code somewhere
|
//TODO: move some of the code somewhere
|
||||||
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
|
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
|
||||||
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext, ecmaVersion);
|
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext, ecmaVersion);
|
||||||
|
|||||||
+8
-4
@@ -48,7 +48,7 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
|||||||
private final JetClassOrObject classDeclaration;
|
private final JetClassOrObject classDeclaration;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<JsStatement> initializerStatements = new ArrayList<JsStatement>();
|
private final List<JsStatement> initializerStatements = new ArrayList<JsStatement>();
|
||||||
|
@NotNull
|
||||||
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
||||||
|
|
||||||
public ClassInitializerTranslator(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
|
public ClassInitializerTranslator(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
|
||||||
@@ -135,13 +135,17 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
|||||||
if (propertyDescriptor == null) {
|
if (propertyDescriptor == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
JsNameRef initialValueForProperty = jsParameter.getName().makeRef();
|
||||||
|
addInitializerOrPropertyDefinition(initialValueForProperty, propertyDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
final JsNameRef nameRef = jsParameter.getName().makeRef();
|
private void addInitializerOrPropertyDefinition(@NotNull JsNameRef initialValue, @NotNull PropertyDescriptor propertyDescriptor) {
|
||||||
if (context().isEcma5()) {
|
if (context().isEcma5()) {
|
||||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, context(), nameRef));
|
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, initialValue, context()));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
JsStatement assignmentToBackingFieldExpression = assignmentToBackingField(context(), propertyDescriptor, nameRef).makeStmt();
|
JsStatement assignmentToBackingFieldExpression =
|
||||||
|
assignmentToBackingField(context(), propertyDescriptor, initialValue).makeStmt();
|
||||||
initializerStatements.add(assignmentToBackingFieldExpression);
|
initializerStatements.add(assignmentToBackingFieldExpression);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2012 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.initializer;
|
||||||
|
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||||
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
|
|
||||||
|
import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Pavel Talanov
|
||||||
|
*/
|
||||||
|
public final class Ecma3InitializerVisitor extends InitializerVisitor {
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
protected JsExpression generateInitializerForProperty(@NotNull PropertyDescriptor propertyDescriptor,
|
||||||
|
@NotNull JsExpression value, @NotNull TranslationContext context) {
|
||||||
|
return assignmentToBackingField(context, propertyDescriptor, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
-12
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.translate.initializer;
|
|||||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsObjectLiteral;
|
import com.google.dart.compiler.backend.js.ast.JsObjectLiteral;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsStatement;
|
import com.google.dart.compiler.backend.js.ast.JsStatement;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||||
@@ -27,27 +28,26 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
final class InitializerEcma5Visitor extends InitializerVisitor {
|
public final class Ecma5InitializerVisitor extends InitializerVisitor {
|
||||||
|
@NotNull
|
||||||
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
@Override
|
||||||
protected JsExpression defineMember(TranslationContext context, PropertyDescriptor propertyDescriptor, JsExpression value) {
|
@Nullable
|
||||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, context, value));
|
protected JsExpression generateInitializerForProperty(@NotNull PropertyDescriptor propertyDescriptor,
|
||||||
|
@NotNull JsExpression value, @NotNull TranslationContext context) {
|
||||||
|
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, value, context));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
protected List<JsStatement> createStatements(List<JetDeclaration> declarations, TranslationContext context) {
|
protected List<JsStatement> generateInitializerStatements(@NotNull List<JetDeclaration> declarations,
|
||||||
List<JsStatement> statements = super.createStatements(declarations, context);
|
@NotNull TranslationContext context) {
|
||||||
|
List<JsStatement> statements = super.generateInitializerStatements(declarations, context);
|
||||||
if (!propertiesDefinition.getPropertyInitializers().isEmpty()) {
|
if (!propertiesDefinition.getPropertyInitializers().isEmpty()) {
|
||||||
JsStatement statement = JsAstUtils.defineProperties(propertiesDefinition);
|
JsStatement statement = JsAstUtils.defineProperties(propertiesDefinition);
|
||||||
if (statements.isEmpty()) {
|
statements.add(0, statement);
|
||||||
statements.add(statement);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
statements.add(0, statement);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return statements;
|
return statements;
|
||||||
}
|
}
|
||||||
+24
-27
@@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.k2js.translate.initializer;
|
package org.jetbrains.k2js.translate.initializer;
|
||||||
|
|
||||||
import com.google.dart.compiler.backend.js.ast.*;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsInvocation;
|
||||||
|
import com.google.dart.compiler.backend.js.ast.JsStatement;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||||
@@ -26,25 +29,22 @@ import org.jetbrains.k2js.translate.context.TranslationContext;
|
|||||||
import org.jetbrains.k2js.translate.declaration.ClassTranslator;
|
import org.jetbrains.k2js.translate.declaration.ClassTranslator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.translate.general.TranslatorVisitor;
|
import org.jetbrains.k2js.translate.general.TranslatorVisitor;
|
||||||
|
import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.general.Translation.translateAsStatement;
|
import static org.jetbrains.k2js.translate.general.Translation.translateAsStatement;
|
||||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDeclarationsForNamespace;
|
import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
|
||||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getPropertyDescriptor;
|
|
||||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getPropertyDescriptorForObjectDeclaration;
|
|
||||||
import static org.jetbrains.k2js.translate.utils.PsiUtils.getObjectDeclarationForName;
|
import static org.jetbrains.k2js.translate.utils.PsiUtils.getObjectDeclarationForName;
|
||||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
public abstract class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||||
static InitializerVisitor create(TranslationContext context) {
|
static InitializerVisitor create(TranslationContext context) {
|
||||||
return context.isEcma5() ? new InitializerEcma5Visitor() : new InitializerVisitor();
|
return context.isEcma5() ? new Ecma5InitializerVisitor() : new Ecma3InitializerVisitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,19 +54,21 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
|||||||
if (initializer == null) {
|
if (initializer == null) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
return toStatements(defineMember(context, getPropertyDescriptor(context.bindingContext(), property),
|
JsExpression initalizerForProperty = generateInitializerForProperty(getPropertyDescriptor(context.bindingContext(), property),
|
||||||
Translation.translateAsExpression(initializer, context)));
|
Translation.translateAsExpression(initializer, context),
|
||||||
|
context);
|
||||||
|
return JsAstUtils.nullableExpressionToStatementList(initalizerForProperty);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: should return JsStatement?
|
||||||
@Nullable
|
@Nullable
|
||||||
protected JsExpression defineMember(TranslationContext context, PropertyDescriptor propertyDescriptor, JsExpression value) {
|
protected abstract JsExpression generateInitializerForProperty(@NotNull PropertyDescriptor descriptor,
|
||||||
return assignmentToBackingField(context, propertyDescriptor, value);
|
@NotNull JsExpression expression, @NotNull TranslationContext context);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
public List<JsStatement> visitAnonymousInitializer(@NotNull JetClassInitializer initializer,
|
public List<JsStatement> visitAnonymousInitializer(@NotNull JetClassInitializer initializer,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
return Arrays.asList(translateAsStatement(initializer.getBody(), context));
|
return Arrays.asList(translateAsStatement(initializer.getBody(), context));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,19 +86,14 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
|||||||
PropertyDescriptor propertyDescriptor = getPropertyDescriptorForObjectDeclaration(context.bindingContext(), objectName);
|
PropertyDescriptor propertyDescriptor = getPropertyDescriptorForObjectDeclaration(context.bindingContext(), objectName);
|
||||||
JetObjectDeclaration objectDeclaration = getObjectDeclarationForName(objectName);
|
JetObjectDeclaration objectDeclaration = getObjectDeclarationForName(objectName);
|
||||||
JsInvocation objectValue = ClassTranslator.generateClassCreationExpression(objectDeclaration, context);
|
JsInvocation objectValue = ClassTranslator.generateClassCreationExpression(objectDeclaration, context);
|
||||||
return toStatements(defineMember(context, propertyDescriptor, objectValue));
|
JsExpression initializerForProperty = generateInitializerForProperty(propertyDescriptor, objectValue, context);
|
||||||
|
return JsAstUtils.nullableExpressionToStatementList(initializerForProperty);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<JsStatement> toStatements(@Nullable JsExpression expression) {
|
@NotNull
|
||||||
return expression == null ? Collections.<JsStatement>emptyList() : Collections.<JsStatement>singletonList(expression.makeStmt());
|
protected List<JsStatement> generateInitializerStatements(@NotNull List<JetDeclaration> declarations,
|
||||||
}
|
@NotNull TranslationContext context) {
|
||||||
|
List<JsStatement> statements = Lists.newArrayList();
|
||||||
protected List<JsStatement> createStatements(List<JetDeclaration> declarations, TranslationContext context) {
|
|
||||||
if (declarations.isEmpty()) {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<JsStatement> statements = new ArrayList<JsStatement>(declarations.size());
|
|
||||||
for (JetDeclaration declaration : declarations) {
|
for (JetDeclaration declaration : declarations) {
|
||||||
statements.addAll(declaration.accept(this, context));
|
statements.addAll(declaration.accept(this, context));
|
||||||
}
|
}
|
||||||
@@ -105,11 +102,11 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public final List<JsStatement> traverseClass(@NotNull JetClassOrObject expression, @NotNull TranslationContext context) {
|
public final List<JsStatement> traverseClass(@NotNull JetClassOrObject expression, @NotNull TranslationContext context) {
|
||||||
return createStatements(expression.getDeclarations(), context);
|
return generateInitializerStatements(expression.getDeclarations(), context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public final List<JsStatement> traverseNamespace(@NotNull NamespaceDescriptor namespace, @NotNull TranslationContext context) {
|
public final List<JsStatement> traverseNamespace(@NotNull NamespaceDescriptor namespace, @NotNull TranslationContext context) {
|
||||||
return createStatements(getDeclarationsForNamespace(context.bindingContext(), namespace), context);
|
return generateInitializerStatements(getDeclarationsForNamespace(context.bindingContext(), namespace), context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,26 +232,43 @@ public final class CallTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public JsExpression construct(@Nullable JsExpression receiver) {
|
public JsExpression construct(@Nullable JsExpression receiver) {
|
||||||
JsExpression callee = callParameters.getFunctionReference();
|
JsExpression qualifiedCallee = getQualifiedCallee(receiver);
|
||||||
if (receiver != null) {
|
|
||||||
setQualifier(callee, receiver);
|
if (isEcma5PropertyAccess()) {
|
||||||
|
return ecma5PropertyAccess(qualifiedCallee);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (context().isEcma5()) {
|
return newInvocation(qualifiedCallee, arguments);
|
||||||
if (descriptor instanceof PropertyGetterDescriptor) {
|
|
||||||
return callee;
|
|
||||||
}
|
|
||||||
else if (descriptor instanceof PropertySetterDescriptor) {
|
|
||||||
assert arguments.size() == 1;
|
|
||||||
return assignment(callee, arguments.get(0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newInvocation(callee, arguments);
|
|
||||||
}
|
}
|
||||||
}, context());
|
}, context());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsExpression ecma5PropertyAccess(@NotNull JsExpression callee) {
|
||||||
|
if (descriptor instanceof PropertyGetterDescriptor) {
|
||||||
|
assert arguments.isEmpty();
|
||||||
|
return callee;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
assert descriptor instanceof PropertySetterDescriptor;
|
||||||
|
assert arguments.size() == 1;
|
||||||
|
return assignment(callee, arguments.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isEcma5PropertyAccess() {
|
||||||
|
return context().isEcma5() && descriptor instanceof PropertyAccessorDescriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private JsExpression getQualifiedCallee(@Nullable JsExpression receiver) {
|
||||||
|
JsExpression callee = callParameters.getFunctionReference();
|
||||||
|
if (receiver != null) {
|
||||||
|
setQualifier(callee, receiver);
|
||||||
|
}
|
||||||
|
return callee;
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private JsExpression getThisObjectOrQualifier() {
|
private JsExpression getThisObjectOrQualifier() {
|
||||||
JsExpression thisObject = callParameters.getThisObject();
|
JsExpression thisObject = callParameters.getThisObject();
|
||||||
|
|||||||
+16
-7
@@ -38,13 +38,13 @@ public final class ReferenceTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression,
|
public static JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
return getAccessTranslator(expression, context).translateAsGet();
|
return getAccessTranslator(expression, context).translateAsGet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsExpression translateAsFQReference(@NotNull DeclarationDescriptor referencedDescriptor,
|
public static JsExpression translateAsFQReference(@NotNull DeclarationDescriptor referencedDescriptor,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
JsExpression qualifier = context.getQualifierForDescriptor(referencedDescriptor);
|
JsExpression qualifier = context.getQualifierForDescriptor(referencedDescriptor);
|
||||||
if (qualifier == null) {
|
if (qualifier == null) {
|
||||||
return translateAsLocalNameReference(referencedDescriptor, context);
|
return translateAsLocalNameReference(referencedDescriptor, context);
|
||||||
@@ -55,7 +55,16 @@ public final class ReferenceTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsExpression translateAsLocalNameReference(@NotNull DeclarationDescriptor referencedDescriptor,
|
public static JsExpression translateAsLocalNameReference(@NotNull DeclarationDescriptor referencedDescriptor,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
|
DeclarationDescriptor effectiveDescriptor = getReferencedDescriptor(referencedDescriptor, context);
|
||||||
|
return context.getNameForDescriptor(effectiveDescriptor).makeRef();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//TODO: should not be doing this
|
||||||
|
@NotNull
|
||||||
|
private static DeclarationDescriptor getReferencedDescriptor(@NotNull DeclarationDescriptor referencedDescriptor,
|
||||||
|
@NotNull TranslationContext context) {
|
||||||
DeclarationDescriptor effectiveDescriptor;
|
DeclarationDescriptor effectiveDescriptor;
|
||||||
if (context.isEcma5() && referencedDescriptor instanceof PropertyAccessorDescriptor) {
|
if (context.isEcma5() && referencedDescriptor instanceof PropertyAccessorDescriptor) {
|
||||||
effectiveDescriptor = ((PropertyAccessorDescriptor) referencedDescriptor).getCorrespondingProperty();
|
effectiveDescriptor = ((PropertyAccessorDescriptor) referencedDescriptor).getCorrespondingProperty();
|
||||||
@@ -63,19 +72,19 @@ public final class ReferenceTranslator {
|
|||||||
else {
|
else {
|
||||||
effectiveDescriptor = referencedDescriptor;
|
effectiveDescriptor = referencedDescriptor;
|
||||||
}
|
}
|
||||||
return context.getNameForDescriptor(effectiveDescriptor).makeRef();
|
return effectiveDescriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
return getAccessTranslator(referenceExpression, null, context);
|
return getAccessTranslator(referenceExpression, null, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
||||||
@Nullable JsExpression receiver,
|
@Nullable JsExpression receiver,
|
||||||
@NotNull TranslationContext context) {
|
@NotNull TranslationContext context) {
|
||||||
if (isBackingFieldReference(referenceExpression)) {
|
if (isBackingFieldReference(referenceExpression)) {
|
||||||
return BackingFieldAccessTranslator.newInstance(referenceExpression, context);
|
return BackingFieldAccessTranslator.newInstance(referenceExpression, context);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ package org.jetbrains.k2js.translate.utils;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.dart.compiler.backend.js.ast.*;
|
import com.google.dart.compiler.backend.js.ast.*;
|
||||||
|
import com.google.dart.compiler.util.AstUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
|
||||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
|
|
||||||
@@ -31,14 +31,14 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
public final class JsAstUtils {
|
public final class JsAstUtils {
|
||||||
private static final JsNameRef VALUE = new JsNameRef("value");
|
private static final JsNameRef VALUE = new JsNameRef("value");
|
||||||
private static final JsPropertyInitializer WRITABLE = new JsPropertyInitializer(new JsNameRef("writable"), null) ;
|
private static final JsPropertyInitializer WRITABLE = new JsPropertyInitializer(new JsNameRef("writable"), null);
|
||||||
private static final JsNameRef DEFINE_PROPERTIES = new JsNameRef("defineProperties");
|
private static final JsNameRef DEFINE_PROPERTIES = new JsNameRef("defineProperties");
|
||||||
private static final JsNameRef CREATE = new JsNameRef("create");
|
private static final JsNameRef CREATE = new JsNameRef("create");
|
||||||
|
|
||||||
static {
|
static {
|
||||||
JsNameRef nameRef = new JsNameRef("Object");
|
JsNameRef globalObjectReference = new JsNameRef("Object");
|
||||||
DEFINE_PROPERTIES.setQualifier(nameRef);
|
DEFINE_PROPERTIES.setQualifier(globalObjectReference);
|
||||||
CREATE.setQualifier(nameRef);
|
CREATE.setQualifier(globalObjectReference);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsAstUtils() {
|
private JsAstUtils() {
|
||||||
@@ -287,36 +287,36 @@ public final class JsAstUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsStatement defineProperties(JsObjectLiteral propertiesDefinition) {
|
public static JsStatement defineProperties(@NotNull JsObjectLiteral propertiesDefinition) {
|
||||||
JsInvocation invoke = new JsInvocation();
|
return AstUtil.newInvocation(DEFINE_PROPERTIES, new JsThisRef(), propertiesDefinition).makeStmt();
|
||||||
invoke.setQualifier(DEFINE_PROPERTIES);
|
|
||||||
invoke.getArguments().add(new JsThisRef());
|
|
||||||
invoke.getArguments().add(propertiesDefinition);
|
|
||||||
return invoke.makeStmt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static JsPropertyInitializer propertyDescriptor(PropertyDescriptor ktDescriptor,
|
public static JsPropertyInitializer propertyDescriptor(@NotNull PropertyDescriptor descriptor,
|
||||||
TranslationContext context,
|
@NotNull JsExpression value, @NotNull TranslationContext context) {
|
||||||
JsExpression value) {
|
JsObjectLiteral jsPropertyDescriptor = new JsObjectLiteral();
|
||||||
return propertyDescriptor(ktDescriptor, context, value, ktDescriptor.isVar());
|
List<JsPropertyInitializer> meta = jsPropertyDescriptor.getPropertyInitializers();
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static JsPropertyInitializer propertyDescriptor(DeclarationDescriptor ktDescriptor,
|
|
||||||
TranslationContext context,
|
|
||||||
JsExpression value,
|
|
||||||
boolean writable) {
|
|
||||||
JsObjectLiteral descriptor = new JsObjectLiteral();
|
|
||||||
List<JsPropertyInitializer> meta = descriptor.getPropertyInitializers();
|
|
||||||
meta.add(new JsPropertyInitializer(VALUE, value));
|
meta.add(new JsPropertyInitializer(VALUE, value));
|
||||||
if (writable) {
|
if (descriptor.isVar()) {
|
||||||
if (WRITABLE.getValueExpr() == null) {
|
meta.add(getWritable(context));
|
||||||
WRITABLE.setValueExpr(context.program().getTrueLiteral());
|
|
||||||
}
|
|
||||||
meta.add(WRITABLE);
|
|
||||||
}
|
}
|
||||||
// todo accessors
|
// TODO: accessors
|
||||||
return new JsPropertyInitializer(context.getNameForDescriptor(ktDescriptor).makeRef(), descriptor);
|
return new JsPropertyInitializer(context.getNameForDescriptor(descriptor).makeRef(), jsPropertyDescriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static JsPropertyInitializer getWritable(@NotNull TranslationContext context) {
|
||||||
|
if (WRITABLE.getValueExpr() == null) {
|
||||||
|
WRITABLE.setValueExpr(context.program().getTrueLiteral());
|
||||||
|
}
|
||||||
|
return WRITABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<JsStatement> nullableExpressionToStatementList(@Nullable JsExpression initalizerForProperty) {
|
||||||
|
if (initalizerForProperty == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return Collections.<JsStatement>singletonList(initalizerForProperty.makeStmt());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user