|
| 1 | +/* |
| 2 | + * Copyright 2026 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package javaemul.internal; |
| 17 | + |
| 18 | +import java.util.Iterator; |
| 19 | +import jsinterop.annotations.JsMethod; |
| 20 | +import jsinterop.annotations.JsPackage; |
| 21 | +import jsinterop.annotations.JsType; |
| 22 | + |
| 23 | +/** A helper class to create JavaScript iterable objects. */ |
| 24 | +public final class JsIterableHelper { |
| 25 | + |
| 26 | + /** Abstraction for JavaScript Iterable. */ |
| 27 | + @JsType(isNative = true, name = "Iterable", namespace = JsPackage.GLOBAL) |
| 28 | + public interface JsIterable<T> { |
| 29 | + @JsMethod(name = "[Symbol.iterator]") |
| 30 | + JsIterator<T> _private_jsIterator__(); |
| 31 | + } |
| 32 | + |
| 33 | + /** Abstraction for JavaScript Iterator. */ |
| 34 | + @JsType(isNative = true, name = "Iterator", namespace = JsPackage.GLOBAL) |
| 35 | + public interface JsIterator<T> { |
| 36 | + @JsMethod(name = "next") |
| 37 | + IIterableResult<T> _private_jsNext__(); |
| 38 | + } |
| 39 | + |
| 40 | + /** Abstraction for JavaScript IIterableResult. */ |
| 41 | + @JsType(isNative = true, namespace = JsPackage.GLOBAL) |
| 42 | + public interface IIterableResult<T> {} |
| 43 | + |
| 44 | + @JsMethod |
| 45 | + public static native <T> IIterableResult<T> makeResult(T value, boolean done); |
| 46 | + |
| 47 | + public static <T> JsIterable<T> asJsIterable(Iterable<T> iterable) { |
| 48 | + return new JsIterableAdapter<T>(iterable); |
| 49 | + } |
| 50 | + |
| 51 | + private static final class JsIterableAdapter<T> implements JsIterable<T> { |
| 52 | + private final Iterable<T> delegate; |
| 53 | + |
| 54 | + private JsIterableAdapter(Iterable<T> delegate) { |
| 55 | + this.delegate = delegate; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public final JsIterableIteratorAdapter<T> _private_jsIterator__() { |
| 60 | + return new JsIterableIteratorAdapter<>(delegate.iterator()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Adapts a Java {@link Iterator} to also implement the {@link JsIterable} API. This ultimately |
| 66 | + * makes this type an {@code IteratorIterable} for JS. |
| 67 | + * |
| 68 | + * <p>In general JS expects all Iterators to themselves be iterable, see: |
| 69 | + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol:~:text=Such%20object%20is,not%20iterators.) |
| 70 | + */ |
| 71 | + private static final class JsIterableIteratorAdapter<T> implements JsIterator<T>, JsIterable<T> { |
| 72 | + private final JsIterator<T> delegate; |
| 73 | + |
| 74 | + private JsIterableIteratorAdapter(JsIterator<T> delegate) { |
| 75 | + this.delegate = delegate; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public final IIterableResult<T> _private_jsNext__() { |
| 80 | + return delegate._private_jsNext__(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public final JsIterator<T> _private_jsIterator__() { |
| 85 | + return this; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private JsIterableHelper() {} |
| 90 | +} |
0 commit comments