1// Copyright JS Foundation and other contributors, http://js.foundation 2// 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 15function parse (txt) { 16 try { 17 eval (txt) 18 assert (false) 19 } catch (e) { 20 assert (e instanceof SyntaxError) 21 } 22} 23 24function checkError (obj) { 25 try { 26 for (var a of obj); 27 assert (false) 28 } catch (e) { 29 assert (e instanceof TypeError) 30 } 31} 32 33var arr = [1,2,3,4] 34 35var forOf = 36 "for var prop of obj" + 37 " obj [prop] += 4" 38parse (forOf) 39 40var forOf = 41 "for [var prop of obj]" + 42 " obj[prop] += 4;" 43parse (forOf) 44 45var forOf = 46 "for (var prop obj)" + 47 " obj[prop] += 4;" 48parse (forOf) 49 50var forOf = 51 "foreach (var prop of obj)" + 52 " obj[prop] += 4;" 53parse (forOf) 54 55var forOf = 56 "for (var a \"of\" []) {}" 57parse (forOf) 58 59checkError(5) 60 61var obj = {} 62Object.defineProperty(obj, Symbol.iterator, { get : function () { throw TypeError ('foo');}}); 63checkError (obj); 64 65var obj = { 66 [Symbol.iterator] : 5 67} 68 69checkError (obj); 70 71var obj = { 72 [Symbol.iterator] () { 73 return 5 74 } 75} 76 77checkError(obj); 78 79var obj = { 80 [Symbol.iterator] () { 81 return {} 82 } 83} 84checkError(obj); 85 86var obj = { 87 [Symbol.iterator] () { 88 return { 89 next() { 90 return 5; 91 } 92 } 93 } 94} 95checkError(obj); 96 97var array = [0, 1, 2, 3, 4, 5]; 98 99var i = 0; 100for (var a of array) { 101 assert (a === i++); 102} 103 104var obj = { 105 [Symbol.iterator]() { 106 return { 107 counter : 0, 108 next () { 109 if (this.counter == 10) { 110 return { done : true, value : undefined }; 111 } 112 return { done: false, value: this.counter++ }; 113 } 114 } 115 } 116} 117 118var i = 0; 119for (var a of obj) { 120 assert (a === i++); 121} 122