diff --git a/ets2panda/ir/statements/forOfStatement.cpp b/ets2panda/ir/statements/forOfStatement.cpp index 34147d4f7cad4068810abb8d0ee15b86fa33517d..62926980918b34847ac6a00cbb06d82a2c11a60a 100644 --- a/ets2panda/ir/statements/forOfStatement.cpp +++ b/ets2panda/ir/statements/forOfStatement.cpp @@ -229,6 +229,10 @@ bool ForOfStatement::CheckReturnTypeOfIteratorMethod(checker::ETSChecker *checke bool ForOfStatement::CheckIteratorInterfaceForObject(checker::ETSChecker *checker, checker::ETSObjectType *obj) { + if (obj->Name().Is(ITERATOR_INTERFACE_NAME)) { + return true; + } + for (auto *const it : obj->Interfaces()) { if (it->Name().Is(ITERATOR_INTERFACE_NAME)) { return true; diff --git a/ets2panda/test/runtime/ets/forOfCustomIterator3.ets b/ets2panda/test/runtime/ets/forOfCustomIterator3.ets new file mode 100644 index 0000000000000000000000000000000000000000..6d9e55bad5dc66752b95f5a0c7ac93b8b6379c5b --- /dev/null +++ b/ets2panda/test/runtime/ets/forOfCustomIterator3.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class A implements Iterable{ + data: string[] = ['a', 'b', 'c']; + $_iterator():Iterator { + return new CIterator(this); + } +} + +class CIterator implements Iterator { + index = 0; + base: A; + constructor (base: A) { + this.base = base; + } + next(): IteratorResult { + if (this.index >= this.base.data.length) { + return { + done: true, + value: undefined + } + } + return { + done: this.index >= this.base.data.length, + value: this.base.data[this.index++] + } + } +} + + +function fooIterable(a:Iterable){ + let res = ""; + for (let x of a) res += x; + arktest.assertEQ(res, "abc") +} + +function main(): void { + fooIterable(new A()) +}