diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index c03574d25f3..1e385a19b81 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -14,10 +14,13 @@
+
+
-
-
-
+
+
+
+
@@ -191,58 +194,13 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
@@ -251,7 +209,7 @@
-
+
@@ -266,10 +224,57 @@
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -288,60 +293,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -369,10 +320,64 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -402,10 +407,6 @@
@@ -508,6 +513,124 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -563,7 +686,7 @@
-
+
@@ -581,7 +704,37 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -603,7 +756,7 @@
-
+
@@ -621,33 +774,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -740,10 +867,6 @@
-
-
-
-
@@ -776,9 +899,9 @@
+
-
@@ -787,58 +910,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -918,6 +989,62 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1074,11 +1201,11 @@
-
-
-
-
-
+
+
+
+
+
localhost
@@ -1153,8 +1280,8 @@
-
-
+
+
@@ -1198,7 +1325,7 @@
-
+
@@ -1211,7 +1338,6 @@
-
@@ -1236,6 +1362,7 @@
+
@@ -1245,13 +1372,57 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1266,6 +1437,13 @@
+
+
+
+
+
+
+
@@ -1273,86 +1451,37 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
diff --git a/translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java b/translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java
index 9ba86743f4f..64954422c55 100644
--- a/translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java
+++ b/translator/src/org/jetbrains/k2js/translate/context/StandardClasses.java
@@ -72,7 +72,7 @@ public final class StandardClasses {
standardClasses.declareStandardTopLevelObject(arrayListFQName, "ArrayList");
standardClasses.declareStandardInnerDeclaration(arrayListFQName, "", "ArrayList");
declareMethods(standardClasses, arrayListFQName, "size", "add", "get",
- "isEmpty", "set", "remove", "addAll", "contains");
+ "isEmpty", "set", "remove", "addAll", "contains", "clear");
}
private static void declareIterator(@NotNull StandardClasses standardClasses,
diff --git a/translator/src/org/jetbrains/k2js/translate/expression/FunctionTranslator.java b/translator/src/org/jetbrains/k2js/translate/expression/FunctionTranslator.java
index 96bbeef8d70..c958d036c98 100644
--- a/translator/src/org/jetbrains/k2js/translate/expression/FunctionTranslator.java
+++ b/translator/src/org/jetbrains/k2js/translate/expression/FunctionTranslator.java
@@ -3,8 +3,8 @@ package org.jetbrains.k2js.translate.expression;
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.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
+import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.k2js.translate.context.TemporaryVariable;
import org.jetbrains.k2js.translate.context.TranslationContext;
@@ -15,7 +15,6 @@ import org.jetbrains.k2js.translate.utils.DescriptorUtils;
import java.util.ArrayList;
import java.util.List;
-import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForElement;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionDescriptor;
import static org.jetbrains.k2js.translate.utils.TranslationUtils.functionWithScope;
@@ -40,6 +39,8 @@ public final class FunctionTranslator extends AbstractTranslator {
private final JsFunction functionObject;
@NotNull
private final TranslationContext functionBodyContext;
+ @NotNull
+ private final FunctionDescriptor descriptor;
private FunctionTranslator(@NotNull JetDeclarationWithBody functionDeclaration,
@@ -48,6 +49,7 @@ public final class FunctionTranslator extends AbstractTranslator {
this.functionDeclaration = functionDeclaration;
this.functionObject = createFunctionObject();
this.functionBodyContext = functionBodyContext();
+ this.descriptor = getFunctionDescriptor(context.bindingContext(), functionDeclaration);
}
@NotNull
@@ -68,8 +70,8 @@ public final class FunctionTranslator extends AbstractTranslator {
@NotNull
private JsFunction generateFunctionObject() {
- functionObject.setParameters(translateParameters(functionDeclaration.getValueParameters(), functionBodyContext));
- functionObject.setBody(translateBody(functionBodyContext));
+ functionObject.setParameters(translateParameters());
+ functionObject.setBody(translateBody());
restoreContext();
return functionObject;
}
@@ -103,7 +105,7 @@ public final class FunctionTranslator extends AbstractTranslator {
}
@NotNull
- private JsBlock translateBody(@NotNull TranslationContext functionBodyContext) {
+ 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";
@@ -150,23 +152,19 @@ public final class FunctionTranslator extends AbstractTranslator {
}
@NotNull
- private List translateParameters(@NotNull List jetParameters,
- @NotNull TranslationContext functionBodyContext) {
+ private List translateParameters() {
List jsParameters = new ArrayList();
mayBeAddThisParameterForExtensionFunction(jsParameters);
- for (JetParameter jetParameter : jetParameters) {
- JsName parameterName = declareParameter(jetParameter, functionBodyContext);
+ for (ValueParameterDescriptor valueParameter : descriptor.getValueParameters()) {
+ JsName parameterName = declareParameter(valueParameter);
jsParameters.add(new JsParameter(parameterName));
}
return jsParameters;
}
@NotNull
- private JsName declareParameter(@NotNull JetParameter jetParameter,
- @NotNull TranslationContext functionBodyContext) {
- DeclarationDescriptor parameterDescriptor =
- getDescriptorForElement(functionBodyContext.bindingContext(), jetParameter);
- return context().declareLocalVariable(parameterDescriptor);
+ private JsName declareParameter(@NotNull ValueParameterDescriptor valueParameter) {
+ return context().declareLocalVariable(valueParameter);
}
private void mayBeAddThisParameterForExtensionFunction(@NotNull List jsParameters) {
diff --git a/translator/test/org/jetbrains/k2js/test/FunctionTest.java b/translator/test/org/jetbrains/k2js/test/FunctionTest.java
index 8b40489ec2c..6ebb655307f 100644
--- a/translator/test/org/jetbrains/k2js/test/FunctionTest.java
+++ b/translator/test/org/jetbrains/k2js/test/FunctionTest.java
@@ -59,5 +59,15 @@ public class FunctionTest extends AbstractExpressionTest {
testFunctionOutput("enclosingThis.jet", "Anonymous", "box", "OK");
}
+ @Test
+ public void implicitItParameter() throws Exception {
+ testFooBoxIsTrue("implicitItParameter.kt");
+ }
+
+ @Test
+ public void kt921() throws Exception {
+ checkOutput("KT-921.kt", "");
+ }
+
}
diff --git a/translator/testFiles/expression/function/cases/KT-921.kt b/translator/testFiles/expression/function/cases/KT-921.kt
new file mode 100644
index 00000000000..b36ed8b5784
--- /dev/null
+++ b/translator/testFiles/expression/function/cases/KT-921.kt
@@ -0,0 +1,67 @@
+import java.util.*
+
+class Lifetime() {
+ val attached = ArrayList< Function0 >()
+
+ public fun attach(action : ()->Unit)
+ {
+ attached.add(action)
+ }
+
+ fun close()
+ {
+ for(x in attached) x()
+ attached.clear()
+ }
+}
+
+public class Viewable()
+{
+ val items = ArrayList()
+
+ fun add(item:T)
+ {
+ items.add(item)
+ }
+
+ fun remove(item:T)
+ {
+ items.remove(item)
+ }
+
+ fun view(lifetime: Lifetime, viewer: (itemLifetime:Lifetime, item:T) -> Unit)
+ {
+ for(item in items)
+ viewer(lifetime, item)
+ }
+}
+
+fun lifetime(body: (Lifetime)->Unit)
+{
+ val l = Lifetime()
+ body(l)
+ l.close()
+}
+
+fun Dump(items:ArrayList)
+{
+ for(item in items)
+ System.out?.print(item.toString() + ", ")
+ System.out?.println()
+}
+
+fun main(args:Array)
+{
+ val v = Viewable()
+ val x = ArrayList()
+ v.add(1)
+ v.add(2)
+ lifetime(
+ {
+ v.view(it, {(itemLifetime, item)->
+ x.add(item)
+ Dump(x)
+ itemLifetime.attach { x.remove(item); Dump(x) }
+ })
+ })
+}
\ No newline at end of file
diff --git a/translator/testFiles/expression/function/cases/implicitItParameter.kt b/translator/testFiles/expression/function/cases/implicitItParameter.kt
new file mode 100644
index 00000000000..ecfbeb19844
--- /dev/null
+++ b/translator/testFiles/expression/function/cases/implicitItParameter.kt
@@ -0,0 +1,12 @@
+package foo
+
+fun test(f : (Int) -> Boolean, p : Int) = f(p)
+
+fun box() : Boolean {
+ if (!test({it + 1 == 2}, 1)) return false;
+
+ if (!test({it > 1}, 3)) return false;
+
+ return (test({((it < 1) == false)}, 1))
+
+}
\ No newline at end of file
diff --git a/translator/testFiles/kotlin_lib.js b/translator/testFiles/kotlin_lib.js
index a6ebd687d2b..f5b1c000bcb 100644
--- a/translator/testFiles/kotlin_lib.js
+++ b/translator/testFiles/kotlin_lib.js
@@ -331,6 +331,10 @@ Kotlin.ArrayList = Class.create({
}
this.$size--;
},
+ clear:function () {
+ this.array = [];
+ this.$size = 0;
+ },
contains:function (obj) {
for (var i = 0; i < this.$size; ++i) {
if (Kotlin.equals(this.array[i], obj)) {