1/* 2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16class CIterator implements Iterator<string> { 17 18 private ind_: int = 0; 19 private base_: C; 20 21 constructor (base: C) { 22 this.base_ = base; 23 } 24 25 override next(): IteratorResult<string> { 26 return { 27 done: this.base_[this.ind_].equals("none"), 28 value: this.base_[this.ind_++] 29 } 30 } 31} 32 33class C { 34 private y: string[] = ["one", "two", "three"]; 35 36 $_get(ind: int) : string { 37 if (ind >= 0 && ind < 3) return this.y[ind]; 38 return "none"; 39 } 40 41 $_iterator(): CIterator { 42 return new CIterator(this); 43 } 44 45 $_set(ind: string, val: string): void { 46 if (ind == "1") this.y[0] = val; 47 if (ind == "2") this.y[1] = val; 48 if (ind == "3") this.y[2] = val; 49 } 50 51} 52 53function foo(c: C): void { 54 c["2"] = 'ДВА!'; 55} 56 57function main(): void { 58 59 let c = new C(); 60 let i: int = 0; 61 62 for (let it of c) { 63 it += ": in for"; 64 65 if (i == 0) { 66 assert(it == "one: in for"); 67 } else if (i == 1) { 68 assert(it == "two: in for") 69 } else if (i == 2) { 70 assert(it == "three: in for") 71 } else { 72 assert(false); 73 } 74 75 ++i; 76 } 77 78 foo(c); 79 80 i = 0; 81 let it: string = "init"; 82 for (it of c) { 83 it += ": after foo"; 84 85 if (i == 0) { 86 assert(it == "one: after foo"); 87 } else if (i == 1) { 88 assert(it == "ДВА!: after foo") 89 } else if (i == 2) { 90 assert(it == "three: after foo") 91 } else { 92 assert(false); 93 } 94 95 ++i; 96 } 97} 98