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 {
|
||||
// The value of this constant is generated by the build script
|
||||
// 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");
|
||||
addPathToSourcesDir(args, srcDir);
|
||||
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);
|
||||
if (data.first != null) {
|
||||
args.add("-libzip");
|
||||
@@ -146,7 +151,6 @@ public final class K2JSCompiler implements TranslatingCompiler {
|
||||
args.add("-target");
|
||||
args.add(data.second);
|
||||
}
|
||||
return ArrayUtil.toStringArray(args);
|
||||
}
|
||||
|
||||
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 static StaticContext generateStaticContext(@NotNull JetStandardLibrary library,
|
||||
@NotNull BindingContext bindingContext, Translation.EcmaVersion ecmaVersion) {
|
||||
@NotNull BindingContext bindingContext,
|
||||
@NotNull Translation.EcmaVersion ecmaVersion) {
|
||||
JsProgram program = new JsProgram("main");
|
||||
JsRootScope jsRootScope = program.getRootScope();
|
||||
Namer namer = Namer.newInstance(jsRootScope);
|
||||
NamingScope scope = NamingScope.rootScope(jsRootScope);
|
||||
Intrinsics intrinsics = Intrinsics.standardLibraryIntrinsics(library);
|
||||
StandardClasses standardClasses =
|
||||
StandardClasses.bindImplementations(namer.getKotlinScope());
|
||||
StandardClasses standardClasses = StandardClasses.bindImplementations(namer.getKotlinScope());
|
||||
return new StaticContext(program, bindingContext, namer, intrinsics, standardClasses, scope, ecmaVersion);
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ public final class StaticContext {
|
||||
if (!(descriptor instanceof NamespaceDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
String nameForNamespace = getNameForNamespace((NamespaceDescriptor)descriptor);
|
||||
String nameForNamespace = getNameForNamespace((NamespaceDescriptor) descriptor);
|
||||
return getRootScope().declareUnobfuscatableName(nameForNamespace);
|
||||
}
|
||||
};
|
||||
@@ -200,7 +200,7 @@ public final class StaticContext {
|
||||
return null;
|
||||
}
|
||||
boolean isGetter = descriptor instanceof PropertyGetterDescriptor;
|
||||
String propertyName = ((PropertyAccessorDescriptor)descriptor).getCorrespondingProperty().getName();
|
||||
String propertyName = ((PropertyAccessorDescriptor) descriptor).getCorrespondingProperty().getName();
|
||||
String accessorName = Namer.getNameForAccessor(propertyName, isGetter);
|
||||
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
||||
return enclosingScope.declareObfuscatableName(accessorName);
|
||||
@@ -228,12 +228,13 @@ public final class StaticContext {
|
||||
return null;
|
||||
}
|
||||
|
||||
//TODO: move somewhere
|
||||
NamingScope enclosingScope = getEnclosingScope(descriptor);
|
||||
if (isEcma5()) {
|
||||
String name = descriptor.getName();
|
||||
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -258,7 +259,7 @@ public final class StaticContext {
|
||||
if (!descriptor.getName().equals("toString")) {
|
||||
return null;
|
||||
}
|
||||
if (((FunctionDescriptor)descriptor).getValueParameters().isEmpty()) {
|
||||
if (((FunctionDescriptor) descriptor).getValueParameters().isEmpty()) {
|
||||
return getEnclosingScope(descriptor).declareUnobfuscatableName("toString");
|
||||
}
|
||||
return null;
|
||||
@@ -272,7 +273,7 @@ public final class StaticContext {
|
||||
if (!(descriptor instanceof FunctionDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor)descriptor);
|
||||
FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor) descriptor);
|
||||
if (overriddenDescriptor == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -307,7 +308,7 @@ public final class StaticContext {
|
||||
if (!(descriptor instanceof ClassDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
if (getSuperclass((ClassDescriptor)descriptor) == null) {
|
||||
if (getSuperclass((ClassDescriptor) descriptor) == null) {
|
||||
return getRootScope().innerScope("Scope for class " + descriptor.getName());
|
||||
}
|
||||
return null;
|
||||
@@ -319,7 +320,7 @@ public final class StaticContext {
|
||||
if (!(descriptor instanceof ClassDescriptor)) {
|
||||
return null;
|
||||
}
|
||||
ClassDescriptor superclass = getSuperclass((ClassDescriptor)descriptor);
|
||||
ClassDescriptor superclass = getSuperclass((ClassDescriptor) descriptor);
|
||||
if (superclass == 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.k2js.facade.MainCallParameters;
|
||||
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.TranslationInternalException;
|
||||
import org.jetbrains.k2js.facade.exceptions.UnsupportedFeatureException;
|
||||
import org.jetbrains.k2js.translate.context.StaticContext;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
@@ -58,7 +58,7 @@ import static org.jetbrains.k2js.translate.utils.dangerous.DangerousData.collect
|
||||
* @author Pavel Talanov
|
||||
* <p/>
|
||||
* 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 enum EcmaVersion {
|
||||
@@ -145,7 +145,9 @@ public final class Translation {
|
||||
|
||||
@NotNull
|
||||
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 {
|
||||
return doGenerateAst(bindingContext, files, mainCallParameters, ecmaVersion);
|
||||
}
|
||||
@@ -159,7 +161,8 @@ public final class Translation {
|
||||
|
||||
@NotNull
|
||||
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
|
||||
JetStandardLibrary standardLibrary = JetStandardLibrary.getInstance();
|
||||
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext, ecmaVersion);
|
||||
|
||||
+8
-4
@@ -48,7 +48,7 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
||||
private final JetClassOrObject classDeclaration;
|
||||
@NotNull
|
||||
private final List<JsStatement> initializerStatements = new ArrayList<JsStatement>();
|
||||
|
||||
@NotNull
|
||||
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
||||
|
||||
public ClassInitializerTranslator(@NotNull JetClassOrObject classDeclaration, @NotNull TranslationContext context) {
|
||||
@@ -135,13 +135,17 @@ public final class ClassInitializerTranslator extends AbstractTranslator {
|
||||
if (propertyDescriptor == null) {
|
||||
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()) {
|
||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, context(), nameRef));
|
||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, initialValue, context()));
|
||||
}
|
||||
else {
|
||||
JsStatement assignmentToBackingFieldExpression = assignmentToBackingField(context(), propertyDescriptor, nameRef).makeStmt();
|
||||
JsStatement assignmentToBackingFieldExpression =
|
||||
assignmentToBackingField(context(), propertyDescriptor, initialValue).makeStmt();
|
||||
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.JsObjectLiteral;
|
||||
import com.google.dart.compiler.backend.js.ast.JsStatement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
||||
@@ -27,27 +28,26 @@ import org.jetbrains.k2js.translate.utils.JsAstUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
final class InitializerEcma5Visitor extends InitializerVisitor {
|
||||
public final class Ecma5InitializerVisitor extends InitializerVisitor {
|
||||
@NotNull
|
||||
private final JsObjectLiteral propertiesDefinition = new JsObjectLiteral();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected JsExpression defineMember(TranslationContext context, PropertyDescriptor propertyDescriptor, JsExpression value) {
|
||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, context, value));
|
||||
@Nullable
|
||||
protected JsExpression generateInitializerForProperty(@NotNull PropertyDescriptor propertyDescriptor,
|
||||
@NotNull JsExpression value, @NotNull TranslationContext context) {
|
||||
propertiesDefinition.getPropertyInitializers().add(JsAstUtils.propertyDescriptor(propertyDescriptor, value, context));
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected List<JsStatement> createStatements(List<JetDeclaration> declarations, TranslationContext context) {
|
||||
List<JsStatement> statements = super.createStatements(declarations, context);
|
||||
protected List<JsStatement> generateInitializerStatements(@NotNull List<JetDeclaration> declarations,
|
||||
@NotNull TranslationContext context) {
|
||||
List<JsStatement> statements = super.generateInitializerStatements(declarations, context);
|
||||
if (!propertiesDefinition.getPropertyInitializers().isEmpty()) {
|
||||
JsStatement statement = JsAstUtils.defineProperties(propertiesDefinition);
|
||||
if (statements.isEmpty()) {
|
||||
statements.add(statement);
|
||||
}
|
||||
else {
|
||||
statements.add(0, statement);
|
||||
}
|
||||
statements.add(0, statement);
|
||||
}
|
||||
return statements;
|
||||
}
|
||||
+24
-27
@@ -16,7 +16,10 @@
|
||||
|
||||
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.Nullable;
|
||||
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.general.Translation;
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
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.getPropertyDescriptor;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getPropertyDescriptorForObjectDeclaration;
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.*;
|
||||
import static org.jetbrains.k2js.translate.utils.PsiUtils.getObjectDeclarationForName;
|
||||
import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||
public abstract class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||
static InitializerVisitor create(TranslationContext context) {
|
||||
return context.isEcma5() ? new InitializerEcma5Visitor() : new InitializerVisitor();
|
||||
return context.isEcma5() ? new Ecma5InitializerVisitor() : new Ecma3InitializerVisitor();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -54,19 +54,21 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||
if (initializer == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return toStatements(defineMember(context, getPropertyDescriptor(context.bindingContext(), property),
|
||||
Translation.translateAsExpression(initializer, context)));
|
||||
JsExpression initalizerForProperty = generateInitializerForProperty(getPropertyDescriptor(context.bindingContext(), property),
|
||||
Translation.translateAsExpression(initializer, context),
|
||||
context);
|
||||
return JsAstUtils.nullableExpressionToStatementList(initalizerForProperty);
|
||||
}
|
||||
|
||||
//TODO: should return JsStatement?
|
||||
@Nullable
|
||||
protected JsExpression defineMember(TranslationContext context, PropertyDescriptor propertyDescriptor, JsExpression value) {
|
||||
return assignmentToBackingField(context, propertyDescriptor, value);
|
||||
}
|
||||
protected abstract JsExpression generateInitializerForProperty(@NotNull PropertyDescriptor descriptor,
|
||||
@NotNull JsExpression expression, @NotNull TranslationContext context);
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public List<JsStatement> visitAnonymousInitializer(@NotNull JetClassInitializer initializer,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context) {
|
||||
return Arrays.asList(translateAsStatement(initializer.getBody(), context));
|
||||
}
|
||||
|
||||
@@ -84,19 +86,14 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||
PropertyDescriptor propertyDescriptor = getPropertyDescriptorForObjectDeclaration(context.bindingContext(), objectName);
|
||||
JetObjectDeclaration objectDeclaration = getObjectDeclarationForName(objectName);
|
||||
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) {
|
||||
return expression == null ? Collections.<JsStatement>emptyList() : Collections.<JsStatement>singletonList(expression.makeStmt());
|
||||
}
|
||||
|
||||
protected List<JsStatement> createStatements(List<JetDeclaration> declarations, TranslationContext context) {
|
||||
if (declarations.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<JsStatement> statements = new ArrayList<JsStatement>(declarations.size());
|
||||
@NotNull
|
||||
protected List<JsStatement> generateInitializerStatements(@NotNull List<JetDeclaration> declarations,
|
||||
@NotNull TranslationContext context) {
|
||||
List<JsStatement> statements = Lists.newArrayList();
|
||||
for (JetDeclaration declaration : declarations) {
|
||||
statements.addAll(declaration.accept(this, context));
|
||||
}
|
||||
@@ -105,11 +102,11 @@ class InitializerVisitor extends TranslatorVisitor<List<JsStatement>> {
|
||||
|
||||
@NotNull
|
||||
public final List<JsStatement> traverseClass(@NotNull JetClassOrObject expression, @NotNull TranslationContext context) {
|
||||
return createStatements(expression.getDeclarations(), context);
|
||||
return generateInitializerStatements(expression.getDeclarations(), context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
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
|
||||
@Override
|
||||
public JsExpression construct(@Nullable JsExpression receiver) {
|
||||
JsExpression callee = callParameters.getFunctionReference();
|
||||
if (receiver != null) {
|
||||
setQualifier(callee, receiver);
|
||||
JsExpression qualifiedCallee = getQualifiedCallee(receiver);
|
||||
|
||||
if (isEcma5PropertyAccess()) {
|
||||
return ecma5PropertyAccess(qualifiedCallee);
|
||||
}
|
||||
|
||||
if (context().isEcma5()) {
|
||||
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);
|
||||
return newInvocation(qualifiedCallee, arguments);
|
||||
}
|
||||
}, 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
|
||||
private JsExpression getThisObjectOrQualifier() {
|
||||
JsExpression thisObject = callParameters.getThisObject();
|
||||
|
||||
+16
-7
@@ -38,13 +38,13 @@ public final class ReferenceTranslator {
|
||||
|
||||
@NotNull
|
||||
public static JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context) {
|
||||
return getAccessTranslator(expression, context).translateAsGet();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsExpression translateAsFQReference(@NotNull DeclarationDescriptor referencedDescriptor,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context) {
|
||||
JsExpression qualifier = context.getQualifierForDescriptor(referencedDescriptor);
|
||||
if (qualifier == null) {
|
||||
return translateAsLocalNameReference(referencedDescriptor, context);
|
||||
@@ -55,7 +55,16 @@ public final class ReferenceTranslator {
|
||||
|
||||
@NotNull
|
||||
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;
|
||||
if (context.isEcma5() && referencedDescriptor instanceof PropertyAccessorDescriptor) {
|
||||
effectiveDescriptor = ((PropertyAccessorDescriptor) referencedDescriptor).getCorrespondingProperty();
|
||||
@@ -63,19 +72,19 @@ public final class ReferenceTranslator {
|
||||
else {
|
||||
effectiveDescriptor = referencedDescriptor;
|
||||
}
|
||||
return context.getNameForDescriptor(effectiveDescriptor).makeRef();
|
||||
return effectiveDescriptor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
||||
@NotNull TranslationContext context) {
|
||||
@NotNull TranslationContext context) {
|
||||
return getAccessTranslator(referenceExpression, null, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static AccessTranslator getAccessTranslator(@NotNull JetSimpleNameExpression referenceExpression,
|
||||
@Nullable JsExpression receiver,
|
||||
@NotNull TranslationContext context) {
|
||||
@Nullable JsExpression receiver,
|
||||
@NotNull TranslationContext context) {
|
||||
if (isBackingFieldReference(referenceExpression)) {
|
||||
return BackingFieldAccessTranslator.newInstance(referenceExpression, context);
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@ package org.jetbrains.k2js.translate.utils;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.dart.compiler.backend.js.ast.*;
|
||||
import com.google.dart.compiler.util.AstUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
|
||||
@@ -31,14 +31,14 @@ import java.util.*;
|
||||
*/
|
||||
public final class JsAstUtils {
|
||||
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 CREATE = new JsNameRef("create");
|
||||
|
||||
static {
|
||||
JsNameRef nameRef = new JsNameRef("Object");
|
||||
DEFINE_PROPERTIES.setQualifier(nameRef);
|
||||
CREATE.setQualifier(nameRef);
|
||||
JsNameRef globalObjectReference = new JsNameRef("Object");
|
||||
DEFINE_PROPERTIES.setQualifier(globalObjectReference);
|
||||
CREATE.setQualifier(globalObjectReference);
|
||||
}
|
||||
|
||||
private JsAstUtils() {
|
||||
@@ -287,36 +287,36 @@ public final class JsAstUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsStatement defineProperties(JsObjectLiteral propertiesDefinition) {
|
||||
JsInvocation invoke = new JsInvocation();
|
||||
invoke.setQualifier(DEFINE_PROPERTIES);
|
||||
invoke.getArguments().add(new JsThisRef());
|
||||
invoke.getArguments().add(propertiesDefinition);
|
||||
return invoke.makeStmt();
|
||||
public static JsStatement defineProperties(@NotNull JsObjectLiteral propertiesDefinition) {
|
||||
return AstUtil.newInvocation(DEFINE_PROPERTIES, new JsThisRef(), propertiesDefinition).makeStmt();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsPropertyInitializer propertyDescriptor(PropertyDescriptor ktDescriptor,
|
||||
TranslationContext context,
|
||||
JsExpression value) {
|
||||
return propertyDescriptor(ktDescriptor, context, value, ktDescriptor.isVar());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsPropertyInitializer propertyDescriptor(DeclarationDescriptor ktDescriptor,
|
||||
TranslationContext context,
|
||||
JsExpression value,
|
||||
boolean writable) {
|
||||
JsObjectLiteral descriptor = new JsObjectLiteral();
|
||||
List<JsPropertyInitializer> meta = descriptor.getPropertyInitializers();
|
||||
public static JsPropertyInitializer propertyDescriptor(@NotNull PropertyDescriptor descriptor,
|
||||
@NotNull JsExpression value, @NotNull TranslationContext context) {
|
||||
JsObjectLiteral jsPropertyDescriptor = new JsObjectLiteral();
|
||||
List<JsPropertyInitializer> meta = jsPropertyDescriptor.getPropertyInitializers();
|
||||
meta.add(new JsPropertyInitializer(VALUE, value));
|
||||
if (writable) {
|
||||
if (WRITABLE.getValueExpr() == null) {
|
||||
WRITABLE.setValueExpr(context.program().getTrueLiteral());
|
||||
}
|
||||
meta.add(WRITABLE);
|
||||
if (descriptor.isVar()) {
|
||||
meta.add(getWritable(context));
|
||||
}
|
||||
// todo accessors
|
||||
return new JsPropertyInitializer(context.getNameForDescriptor(ktDescriptor).makeRef(), descriptor);
|
||||
// TODO: accessors
|
||||
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