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_syntax_error (code) 19{ 20 try { 21 eval (code) 22 assert (false) 23 } catch (e) { 24 assert (e instanceof SyntaxError) 25 } 26} 27 28check_syntax_error ("({ * })") 29check_syntax_error ("({ *, b:4 })") 30check_syntax_error ("({ *a:4 })") 31check_syntax_error ("({ *['a']:4 })") 32check_syntax_error ("({ *a(yield) {} })") 33check_syntax_error ("({ get *a() {} })") 34check_syntax_error ("({ set *b(v) {} })") 35 36check_syntax_error ("class C { * }") 37check_syntax_error ("class C { static * }") 38check_syntax_error ("class C { *() {} }") 39check_syntax_error ("class C { static * () {} }") 40check_syntax_error ("class C { *['a'] {} }") 41 42function check_result(result, value, done) 43{ 44 assert(result.value === value) 45 assert(result.done === done) 46} 47 48function postfix(a) { return a + "b" } 49 50var o = { 51 * a () { 52 yield 1 53 return 2 54 }, 55 *2(x) { 56 yield x + 1 57 return x + 2 58 }, 59 *[postfix("a")]() { 60 var o = { get yield() { return 3 + 2 } } 61 62 yield o.yield 63 return 6 64 }, 65 *yield() { 66 var o = { yield:7 } 67 68 yield o.yield 69 return 8 70 } 71} 72 73var f = o.a() 74check_result(f.next(), 1, false) 75check_result(f.next(), 2, true) 76 77var f = o[2](2) 78check_result(f.next(), 3, false) 79check_result(f.next(), 4, true) 80 81var f = o.ab() 82check_result(f.next(), 5, false) 83check_result(f.next(), 6, true) 84 85var f = o.yield() 86check_result(f.next(), 7, false) 87check_result(f.next(), 8, true) 88 89class C { 90 * a () { 91 yield 1 92 return 2 93 } 94 95 *3(x) { 96 yield x + 1 97 return x + 2 98 } 99 100 *[postfix("a")]() { 101 var o = { get yield() { return 3 + 2 } } 102 103 yield o.yield 104 return 6 105 } 106 107 static *yield() { 108 var o = { yield:7 } 109 110 yield o.yield 111 return 8 112 } 113 114 static * [postfix("b") ] (v = 9) { 115 return v 116 } 117} 118 119var c = new C 120 121var f = c.a() 122check_result(f.next(), 1, false) 123check_result(f.next(), 2, true) 124 125var f = c[3](2) 126check_result(f.next(), 3, false) 127check_result(f.next(), 4, true) 128 129var f = c.ab() 130check_result(f.next(), 5, false) 131check_result(f.next(), 6, true) 132 133var f = C.yield() 134check_result(f.next(), 7, false) 135check_result(f.next(), 8, true) 136 137var f = C.bb() 138check_result(f.next(), 9, true) 139