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.utils.dangerous;
018
019 import com.google.dart.compiler.backend.js.ast.JsExpression;
020 import com.google.dart.compiler.backend.js.ast.JsName;
021 import com.google.dart.compiler.backend.js.ast.JsNode;
022 import com.google.dart.compiler.backend.js.ast.JsVars;
023 import gnu.trove.THashMap;
024 import org.jetbrains.annotations.NotNull;
025 import org.jetbrains.jet.lang.psi.JetExpression;
026 import org.jetbrains.k2js.translate.context.TranslationContext;
027 import org.jetbrains.k2js.translate.general.AbstractTranslator;
028 import org.jetbrains.k2js.translate.general.Translation;
029
030 import java.util.ArrayList;
031 import java.util.List;
032 import java.util.Map;
033
034 public final class DangerousTranslator extends AbstractTranslator {
035 @NotNull
036 private final DangerousData data;
037
038 @NotNull
039 public static JsNode translate(@NotNull DangerousData data, @NotNull TranslationContext context) {
040 assert data.exists();
041 return new DangerousTranslator(data, context).translate();
042 }
043
044 private DangerousTranslator(@NotNull DangerousData data, @NotNull TranslationContext context) {
045 super(context);
046 this.data = data;
047 }
048
049 @NotNull
050 private JsNode translate() {
051 Map<JetExpression, JsName> aliasesForExpressions =
052 translateAllExpressionsAndCreateAliasesForThem(data.getNodesToBeGeneratedBefore());
053 TranslationContext contextWithAliases = context().innerContextWithAliasesForExpressions(aliasesForExpressions);
054 return Translation.doTranslateExpression(data.getRootNode(), contextWithAliases);
055 }
056
057 @NotNull
058 private Map<JetExpression, JsName> translateAllExpressionsAndCreateAliasesForThem(@NotNull List<JetExpression> expressions) {
059 Map<JetExpression, JsName> aliasesForExpressions = new THashMap<JetExpression, JsName>(expressions.size());
060 List<JsVars.JsVar> vars = new ArrayList<JsVars.JsVar>(expressions.size());
061 for (JetExpression expression : expressions) {
062 JsExpression translatedExpression = Translation.translateAsExpression(expression, context());
063 JsVars.JsVar aliasForExpression = context().dynamicContext().createTemporaryVar(translatedExpression);
064 vars.add(aliasForExpression);
065 aliasesForExpressions.put(expression, aliasForExpression.getName());
066 }
067 context().addStatementToCurrentBlock(new JsVars(vars, true));
068 return aliasesForExpressions;
069 }
070 }