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 16function check_result(result, value, done) 17{ 18 assert(result.value === value) 19 assert(result.done === done) 20} 21 22function check_throw(str, expected) 23{ 24 try { 25 eval(str) 26 assert(false); 27 } catch (e) { 28 assert(e === expected); 29 } 30} 31 32function * gen1(a) { 33 return "a: " + (yield a.p) 34} 35 36var f = gen1({}) 37check_throw("f.throw(4)", 4) 38check_result(f.next(), undefined, true) 39 40f = gen1({ p:"x" }) 41check_result(f.next(), "x", false) 42check_throw("f.throw(10)", 10) 43check_result(f.next(), undefined, true) 44 45f = gen1({ p:"b" }) 46check_result(f.next(), "b", false) 47check_result(f.next(), "a: undefined", true) 48check_result(f.next(), undefined, true) 49 50function*gen2() { 51 try { 52 for (let i in { x:1, y:2 }) 53 { 54 assert((yield i) === "33") 55 } 56 assert(false) 57 } finally { 58 yield "z" 59 } 60} 61 62f = gen2() 63check_throw("f.throw('throw')", "throw") 64check_result(f.next(), undefined, true) 65 66f = gen2() 67check_result(f.next(), "x", false) 68check_result(f.throw("throw"), "z", false) 69check_throw("f.next()", "throw") 70check_result(f.next(), undefined, true) 71 72function* gen3() { 73 try { 74 return 8 75 } finally { 76 yield 1 77 } 78} 79 80f = gen3() 81check_result(f.next(), 1, false) 82check_throw("f.throw(2)", 2) 83