001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.k2js.translate.initializer;
018
019 import com.google.dart.compiler.backend.js.ast.*;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.descriptors.*;
023 import org.jetbrains.jet.lang.psi.JetExpression;
024 import org.jetbrains.jet.lang.psi.JetObjectDeclaration;
025 import org.jetbrains.jet.lang.psi.JetProperty;
026 import org.jetbrains.k2js.translate.context.Namer;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028 import org.jetbrains.k2js.translate.declaration.ClassTranslator;
029 import org.jetbrains.k2js.translate.general.Translation;
030 import org.jetbrains.k2js.translate.utils.JsAstUtils;
031
032 import java.util.List;
033
034 import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor;
035 import static org.jetbrains.k2js.translate.utils.JsAstUtils.assignment;
036 import static org.jetbrains.k2js.translate.utils.TranslationUtils.assignmentToBackingField;
037
038 public final class InitializerUtils {
039 private InitializerUtils() {
040 }
041
042 @NotNull
043 public static JsStatement generateInitializerForProperty(@NotNull TranslationContext context,
044 @NotNull PropertyDescriptor descriptor,
045 @NotNull JsExpression value) {
046 if (context.isEcma5()) {
047 return JsAstUtils.definePropertyDataDescriptor(descriptor, value, context).makeStmt();
048 }
049 else {
050 return assignmentToBackingField(context, descriptor, value).makeStmt();
051 }
052 }
053
054 @Nullable
055 public static JsStatement generateInitializerForDelegate(@NotNull TranslationContext context, @NotNull JetProperty property) {
056 JetExpression delegate = property.getDelegateExpression();
057 if (delegate != null) {
058 JsExpression value = Translation.translateAsExpression(delegate, context);
059 String name = property.getName();
060 assert name != null: "Delegate property must have name";
061 return JsAstUtils.defineSimpleProperty(Namer.getDelegateName(name), value, context);
062 }
063 return null;
064 }
065
066 public static void generate(
067 @NotNull JetObjectDeclaration declaration,
068 @NotNull List<JsStatement> initializers,
069 @NotNull TranslationContext context
070 ) {
071 ClassDescriptor descriptor = getClassDescriptor(context.bindingContext(), declaration);
072 JsExpression value = ClassTranslator.generateObjectLiteral(declaration, descriptor, context);
073 initializers.add(create(descriptor, !(descriptor.getContainingDeclaration() instanceof NamespaceDescriptor), value, context));
074 }
075
076 private static JsStatement create(DeclarationDescriptor descriptor, boolean enumerable, JsExpression value, TranslationContext context) {
077 JsExpression expression;
078 if (context.isEcma5()) {
079 expression = JsAstUtils.defineProperty(descriptor.getName().asString(), JsAstUtils.createDataDescriptor(value, false, enumerable), context);
080 }
081 else {
082 expression = assignment(new JsNameRef(descriptor.getName().asString(), JsLiteral.THIS), value);
083 }
084 return expression.makeStmt();
085 }
086
087
088 public static JsExpression toDataDescriptor(JsExpression value, TranslationContext context) {
089 return context.isEcma5() ? JsAstUtils.createDataDescriptor(value) : value;
090 }
091
092 public static JsPropertyInitializer createPropertyInitializer(Named named, JsExpression value, TranslationContext context) {
093 return new JsPropertyInitializer(context.nameToLiteral(named), toDataDescriptor(value, context));
094 }
095 }