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 */ 15 16/* This file checks core generator operations. */ 17 18function check_result(result, value, done) 19{ 20 assert(result.value === value) 21 assert(result.done === done) 22} 23 24function * gen1(a = (t = 8)) { 25 var o = { p: 2 } 26 var x = 3.25 27 28 assert((o.p + (yield 10)) === 23) 29 assert((o.p + (yield 11)) === 24) 30 return x 31} 32 33/* Cannot be invoked with new. */ 34try { 35 new gen1 36 assert(false) 37} catch (e) { 38 assert(e instanceof TypeError) 39} 40 41/* Fully read values. */ 42var t = 0 43var g = gen1() 44assert(t === 8) 45 46check_result(g.next(20), 10, false) 47check_result(g.next(21), 11, false) 48check_result(g.next(22), 3.25, true) 49check_result(g.next(23), undefined, true) 50 51/* Partly read values (gc needs to free a suspended generator). */ 52t = 0 53g = gen1() 54assert(t === 8) 55 56check_result(g.next(20), 10, false) 57 58function * gen2() { 59 for (i in { x:0, y:1, z:2 }) 60 { 61 let a = eval("'s'") 62 63 var b = yield a + i 64 assert (b === ++t) 65 } 66} 67 68/* Fully read values. */ 69t = 0 70f = gen2() 71 72check_result(f.next(0), "sx", false) 73check_result(f.next(1), "sy", false) 74check_result(f.next(2), "sz", false) 75check_result(f.next(3), undefined, true) 76check_result(f.next(4), undefined, true) 77 78/* Partly read values (gc needs to free a suspended generator). */ 79f = gen2() 80 81t = 0 82check_result(f.next(0), "sx", false) 83 84function *gen3() { 85 function f(yield) { 86 return -yield * 2 87 } 88 89 var g = (v) => { 90 assert(v === 6) 91 } 92 93 g(yield yield f(++t)) 94 95 return 77 96} 97 98/* Fully read values. */ 99t = 0 100f = gen3() 101 102check_result(f.next(0), -2, false) 103check_result(f.next(88), 88, false) 104check_result(f.next(6), 77, true) 105 106/* Partly read values (gc needs to free a suspended generator). */ 107t = 0 108f = gen3() 109 110check_result(f.next(0), -2, false) 111 112function 113 /* generator: */ * 114 /* name: */ gen4() { 115 116 let a = eval("5") 117 with ({a}) 118 { 119 let a = eval("6") 120 121 for (let a = 10; a < 11; a++) 122 { 123 let a = eval("7") 124 yield (a) 125 } 126 127 yield a, !assert(a === 6) 128 } 129 assert((yield a) === undefined) 130} 131 132/* Fully read values. */ 133f = gen4() 134 135check_result(f.next(), 7, false) 136check_result(f.next(), 6, false) 137check_result(f.next(), 5, false) 138check_result(f.next(), undefined, true) 139 140/* Partly read values (gc needs to free a suspended generator). */ 141f = gen4() 142 143check_result(f.next(), 7, false) 144 145function*gen5(a,b,c,d) { 146 yield a 147 yield b 148 yield c 149 yield d 150} 151 152/* Fully read values. */ 153t = [] 154for(let i of gen5(1,3,5,7)) { 155 t.push(i) 156} 157 158assert(t.length === 4) 159assert(t[0] === 1) 160assert(t[1] === 3) 161assert(t[2] === 5) 162assert(t[3] === 7) 163 164/* Partly read values (gc needs to free a suspended generator). */ 165t = [] 166for(let i of gen5(1,3,5,7)) { 167 t.push(i) 168 if (i === 3) { 169 break 170 } 171} 172 173assert(t.length === 2) 174assert(t[0] === 1) 175assert(t[1] === 3) 176 177/* Recursive generator call. */ 178function* gen6(a,b,c,d) { 179 yield f.next() 180} 181 182f = gen6() 183 184try { 185 f.next() 186 assert(false) 187} catch (e) { 188 assert(e instanceof TypeError) 189} 190 191/* Parameterless yield. */ 192function* gen7() { 193 yield 194} 195 196f = gen7() 197check_result(f.next(), undefined, false) 198check_result(f.next(), undefined, true) 199 200