diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 7ef651cbe4e..480cb9b447c 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -8,15 +8,13 @@
-
-
+
+
-
-
-
+
@@ -179,28 +177,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -215,10 +195,10 @@
-
+
-
+
@@ -228,10 +208,10 @@
-
-
+
+
-
+
@@ -240,34 +220,55 @@
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
@@ -294,22 +295,22 @@
@@ -1002,21 +1003,21 @@
-
+
-
+
-
+
-
+
@@ -1060,7 +1061,7 @@
-
+
@@ -1073,7 +1074,6 @@
-
@@ -1098,6 +1098,7 @@
+
@@ -1107,39 +1108,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1154,13 +1122,6 @@
-
-
-
-
-
-
-
@@ -1175,27 +1136,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -1210,16 +1150,9 @@
-
-
-
-
-
-
-
-
+
@@ -1228,6 +1161,72 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java
index d8ad67e3b39..e9b47cc676a 100644
--- a/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java
+++ b/translator/src/org/jetbrains/k2js/translate/ExpressionVisitor.java
@@ -381,7 +381,7 @@ public final class ExpressionVisitor extends TranslatorVisitor {
@NotNull
public JsNode visitFunctionLiteralExpression(@NotNull JetFunctionLiteralExpression expression,
@NotNull TranslationContext context) {
- return Translation.functionTranslator(context).translateAsLiteral(expression.getFunctionLiteral());
+ return Translation.functionTranslator(expression.getFunctionLiteral(), context).translateAsLiteral();
}
@Override
diff --git a/translator/src/org/jetbrains/k2js/translate/FunctionTranslator.java b/translator/src/org/jetbrains/k2js/translate/FunctionTranslator.java
index c622115d1ec..f4e27d15404 100644
--- a/translator/src/org/jetbrains/k2js/translate/FunctionTranslator.java
+++ b/translator/src/org/jetbrains/k2js/translate/FunctionTranslator.java
@@ -3,67 +3,70 @@ package org.jetbrains.k2js.translate;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.jet.lang.psi.JetExpression;
-import org.jetbrains.jet.lang.psi.JetFunction;
-import org.jetbrains.jet.lang.psi.JetFunctionLiteral;
-import org.jetbrains.jet.lang.psi.JetNamedFunction;
+import org.jetbrains.jet.lang.psi.*;
+import java.util.ArrayList;
import java.util.List;
/**
* @author Talanov Pavel
*/
-//TODO: think about adding state to translator
public final class FunctionTranslator extends AbstractTranslator {
@NotNull
- public static FunctionTranslator newInstance(@NotNull TranslationContext context) {
- return new FunctionTranslator(context);
+ private final JetFunction functionDeclaration;
+ @NotNull
+ private final JsFunction functionObject;
+
+ @NotNull
+ public static FunctionTranslator newInstance(@NotNull JetFunction function, @NotNull TranslationContext context) {
+ return new FunctionTranslator(function, context);
}
- private FunctionTranslator(@NotNull TranslationContext context) {
+ private FunctionTranslator(@NotNull JetFunction functionDeclaration, @NotNull TranslationContext context) {
super(context);
+ this.functionDeclaration = functionDeclaration;
+ this.functionObject = createFunctionObject();
}
@NotNull
- public JsPropertyInitializer translateAsMethod(@NotNull JetNamedFunction expression) {
- JsName functionName = context().getNameForElement(expression);
- JsFunction function = generateFunctionObject(expression);
+ public JsPropertyInitializer translateAsMethod() {
+ JsName functionName = context().getNameForElement(functionDeclaration);
+ JsFunction function = generateFunctionObject();
return new JsPropertyInitializer(functionName.makeRef(), function);
}
@NotNull
- public JsFunction translateAsLiteral(@NotNull JetFunctionLiteral expression) {
- return generateFunctionObject(expression);
+ public JsFunction translateAsLiteral() {
+ return generateFunctionObject();
}
@NotNull
- private JsFunction generateFunctionObject(@NotNull JetFunction jetFunction) {
- JsFunction result = createFunctionObject(jetFunction);
- result.setParameters(TranslationUtils.translateParameters
- (jetFunction.getValueParameters(), result.getScope()));
- result.setBody(translateBody(jetFunction, result.getScope()));
- return result;
+ private JsFunction generateFunctionObject() {
+ functionObject.setParameters(translateParameters(functionDeclaration.getValueParameters(),
+ functionObject.getScope()));
+ functionObject.setBody(translateBody());
+ return functionObject;
}
- private JsFunction createFunctionObject(JetFunction function) {
- if (function instanceof JetNamedFunction) {
+ private JsFunction createFunctionObject() {
+ if (functionDeclaration instanceof JetNamedFunction) {
return JsFunction.getAnonymousFunctionWithScope
- (context().getScopeForElement(function));
+ (context().getScopeForElement(functionDeclaration));
}
- if (function instanceof JetFunctionLiteral) {
+ if (functionDeclaration instanceof JetFunctionLiteral) {
return new JsFunction(context().enclosingScope());
}
- throw new AssertionError("Unsupported type of function.");
+ throw new AssertionError("Unsupported type of functionDeclaration.");
}
@NotNull
- private JsBlock translateBody(@NotNull JetFunction function, @NotNull JsScope functionScope) {
- JetExpression jetBodyExpression = function.getBodyExpression();
+ private JsBlock translateBody() {
+ JetExpression jetBodyExpression = functionDeclaration.getBodyExpression();
//TODO decide if there are cases where this assert is illegal
assert jetBodyExpression != null : "Function without body not supported";
- JsNode body = Translation.translateExpression(jetBodyExpression, functionBodyContext(function, functionScope));
- return wrapWithReturnIfNeeded(body, !function.hasBlockBody());
+ JsNode body = Translation.translateExpression(jetBodyExpression, functionBodyContext());
+ return wrapWithReturnIfNeeded(body, !functionDeclaration.hasBlockBody());
}
@NotNull
@@ -79,7 +82,7 @@ public final class FunctionTranslator extends AbstractTranslator {
addReturnToBlockStatement(bodyBlock);
return bodyBlock;
}
- throw new AssertionError("Invalid node as function body");
+ throw new AssertionError("Invalid node as functionDeclaration body");
}
private void addReturnToBlockStatement(@NotNull JsBlock bodyBlock) {
@@ -91,14 +94,24 @@ public final class FunctionTranslator extends AbstractTranslator {
}
@NotNull
- private TranslationContext functionBodyContext(@NotNull JetFunction function,
- @NotNull JsScope functionScope) {
- if (function instanceof JetNamedFunction) {
- return context().newFunctionDeclaration((JetNamedFunction) function);
+ private TranslationContext functionBodyContext() {
+ if (functionDeclaration instanceof JetNamedFunction) {
+ return context().newFunctionDeclaration((JetNamedFunction) functionDeclaration);
}
- if (function instanceof JetFunctionLiteral) {
- return context().newFunctionLiteral(functionScope);
+ if (functionDeclaration instanceof JetFunctionLiteral) {
+ return context().newEnclosingScope(functionObject.getScope());
}
- throw new AssertionError("Unsupported type of function.");
+ throw new AssertionError("Unsupported type of functionDeclaration.");
+ }
+
+ @NotNull
+ private List translateParameters(@NotNull List jetParameters,
+ @NotNull JsScope functionScope) {
+ List jsParameters = new ArrayList();
+ for (JetParameter jetParameter : jetParameters) {
+ JsName parameterName = functionScope.declareName(jetParameter.getName());
+ jsParameters.add(new JsParameter(parameterName));
+ }
+ return jsParameters;
}
}
diff --git a/translator/src/org/jetbrains/k2js/translate/Translation.java b/translator/src/org/jetbrains/k2js/translate/Translation.java
index 53146a35a00..2f0cf00fe1c 100644
--- a/translator/src/org/jetbrains/k2js/translate/Translation.java
+++ b/translator/src/org/jetbrains/k2js/translate/Translation.java
@@ -25,8 +25,9 @@ public final class Translation {
}
@NotNull
- static public FunctionTranslator functionTranslator(@NotNull TranslationContext context) {
- return FunctionTranslator.newInstance(context);
+ static public FunctionTranslator functionTranslator(@NotNull JetFunction function,
+ @NotNull TranslationContext context) {
+ return FunctionTranslator.newInstance(function, context);
}
@NotNull
diff --git a/translator/src/org/jetbrains/k2js/translate/TranslationContext.java b/translator/src/org/jetbrains/k2js/translate/TranslationContext.java
index 8a3cbd57582..2dc5b37d4c2 100644
--- a/translator/src/org/jetbrains/k2js/translate/TranslationContext.java
+++ b/translator/src/org/jetbrains/k2js/translate/TranslationContext.java
@@ -117,11 +117,12 @@ public final class TranslationContext {
return new TranslationContext(namespaceName, program, bindingContext, newScopes, declarations, block);
}
- @NotNull
- public TranslationContext newFunctionLiteral(@NotNull JsScope correspondingScope) {
- Scopes newScopes = new Scopes(correspondingScope, scopes.classScope, scopes.namespaceScope);
- return new TranslationContext(namespaceName, program, bindingContext, newScopes, declarations, block);
- }
+ //TODO: consider deleting
+// @NotNull
+// public TranslationContext newFunctionLiteral(@NotNull JsScope correspondingScope) {
+// Scopes newScopes = new Scopes(correspondingScope, scopes.classScope, scopes.namespaceScope);
+// return new TranslationContext(namespaceName, program, bindingContext, newScopes, declarations, block);
+// }
// Note: Should be used if and only if scope has no corresponding descriptor
@NotNull
diff --git a/translator/src/org/jetbrains/k2js/translate/TranslationUtils.java b/translator/src/org/jetbrains/k2js/translate/TranslationUtils.java
index 8b1fe14a1b4..46e9e782590 100644
--- a/translator/src/org/jetbrains/k2js/translate/TranslationUtils.java
+++ b/translator/src/org/jetbrains/k2js/translate/TranslationUtils.java
@@ -5,7 +5,10 @@ import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
-import org.jetbrains.jet.lang.psi.*;
+import org.jetbrains.jet.lang.psi.JetExpression;
+import org.jetbrains.jet.lang.psi.JetProperty;
+import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
+import org.jetbrains.jet.lang.psi.ValueArgument;
import java.util.ArrayList;
import java.util.List;
@@ -98,17 +101,4 @@ public final class TranslationUtils {
String backingFieldName = Namer.getKotlinBackingFieldName(propertyName);
return context.enclosingScope().findExistingName(backingFieldName);
}
-
- @NotNull
- static public List translateParameters(@NotNull List jetParameters,
- @NotNull JsScope scopeToDeclareParameters) {
- List jsParameters = new ArrayList();
- for (JetParameter jetParameter : jetParameters) {
- JsName parameterName = scopeToDeclareParameters.declareName(jetParameter.getName());
- jsParameters.add(new JsParameter(parameterName));
- }
- return jsParameters;
- }
-
-
}
diff --git a/translator/src/org/jetbrains/k2js/translate/declaration/DeclarationBodyVisitor.java b/translator/src/org/jetbrains/k2js/translate/declaration/DeclarationBodyVisitor.java
index 7c0647f6f79..bfd6abc3cf9 100644
--- a/translator/src/org/jetbrains/k2js/translate/declaration/DeclarationBodyVisitor.java
+++ b/translator/src/org/jetbrains/k2js/translate/declaration/DeclarationBodyVisitor.java
@@ -51,7 +51,7 @@ public final class DeclarationBodyVisitor extends TranslatorVisitor visitNamedFunction(@NotNull JetNamedFunction expression,
@NotNull TranslationContext context) {
- return Arrays.asList(Translation.functionTranslator(context).translateAsMethod(expression));
+ return Arrays.asList(Translation.functionTranslator(expression, context).translateAsMethod());
}
@Override