Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Iterating over iterator.

  • Loading branch information...
commit 52a933e1798c0a9fbf699c0ae08894267e930e92 1 parent e184bf7
@pTalanov pTalanov authored
View
8 js/js.tests/test/org/jetbrains/k2js/test/semantics/MiscTest.java
@@ -187,4 +187,12 @@ public void testExclExclThrows() throws Exception {
public void testInheritFromJetIterator() throws Exception {
fooBoxTest();
}
+
+ public void testIterableExtension() throws Exception {
+ fooBoxTest();
+ }
+
+ public void testIterationOverIterator() throws Exception {
+ fooBoxTest();
+ }
}
View
20 js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/TopLevelFIF.java
@@ -16,12 +16,18 @@
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
+import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.k2js.translate.context.TranslationContext;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.BuiltInFunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
+import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
+import java.util.List;
+
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
/**
@@ -31,6 +37,19 @@
@NotNull
public static final CallStandardMethodIntrinsic EQUALS = new CallStandardMethodIntrinsic(new JsNameRef("equals", "Kotlin"), true, 1);
@NotNull
+ public static final FunctionIntrinsic RETURN_RECEIVER_INTRINSIC = new FunctionIntrinsic() {
+ @NotNull
+ @Override
+ public JsExpression apply(
+ @Nullable JsExpression receiver,
+ @NotNull List<JsExpression> arguments,
+ @NotNull TranslationContext context
+ ) {
+ assert receiver != null;
+ return receiver;
+ }
+ };
+ @NotNull
public static final FunctionIntrinsicFactory INSTANCE = new TopLevelFIF();
private TopLevelFIF() {
@@ -39,5 +58,6 @@ private TopLevelFIF() {
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), EQUALS);
add(pattern("String|Boolean|Char|Number.equals"), EQUALS);
add(pattern("arrayOfNulls"), new CallStandardMethodIntrinsic(new JsNameRef("nullArray", "Kotlin"), false, 1));
+ add(pattern("iterator"), RETURN_RECEIVER_INTRINSIC);
}
}
View
22 js/js.translator/testFiles/expression/misc/cases/iterableExtension.kt
@@ -0,0 +1,22 @@
+package foo
+
+import java.util.*
+import java.lang.*
+import jet.Iterator
+
+public inline fun <T, C: Collection<in T>> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
+ for (element in this) if (predicate(element)) result.add(element) else break
+ return result
+}
+
+fun box(): Any? {
+ val c = ArrayList<Int>()
+ for (i in 0..5) {
+ c.add(i)
+ }
+
+ val d = ArrayList<Int>()
+
+ c.iterator().takeWhileTo(d, {i -> i < 4 })
+ return d.size() == 4
+}
View
18 js/js.translator/testFiles/expression/misc/cases/iterationOverIterator.kt
@@ -0,0 +1,18 @@
+package foo
+
+
+import java.util.*
+
+fun box(): Boolean {
+ val c = ArrayList<Int>()
+ for (i in 0..5) {
+ c.add(i)
+ }
+
+ var s = ""
+ for (i in c.iterator()) {
+ s = s + i.toString()
+ }
+
+ return s == "012345"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.