• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *gen1() {
23  yield 1
24  yield *[2,3,4]
25  yield 5
26}
27
28var g = gen1()
29check_result(g.next(), 1, false)
30check_result(g.next(), 2, false)
31check_result(g.next(), 3, false)
32check_result(g.next(), 4, false)
33check_result(g.next(), 5, false)
34check_result(g.next(), undefined, true)
35
36function *gen2() {
37  yield * true
38}
39
40try {
41  g = gen2()
42  g.next()
43  assert(false)
44} catch (e) {
45  assert(e instanceof TypeError)
46}
47
48var t0 = 0, t1 = 0
49
50function *gen3() {
51  function *f() {
52    try {
53      yield 5
54    } finally {
55      t0 = 1
56    }
57  }
58
59  try {
60    yield *f()
61  } finally {
62    t1 = 1
63  }
64}
65
66g = gen3()
67check_result(g.next(), 5, false)
68check_result(g.return(13), 13, true)
69assert(t0 === 1)
70assert(t1 === 1)
71
72t0 = -1
73t1 = 0
74
75function *gen4() {
76  function next(arg)
77  {
78    t0++;
79
80    if (t0 === 0)
81    {
82      assert(arg === undefined);
83      return { value:2, done:false }
84    }
85    if (t0 === 1)
86    {
87      assert(arg === -3);
88      return { value:3, done:false }
89    }
90    assert(arg === -4);
91    return { value:4, done:true }
92  }
93
94  var o = { [Symbol.iterator]() { return { next } } }
95  assert((yield *o) === 4)
96  return 5;
97}
98
99g = gen4()
100check_result(g.next(-2), 2, false)
101check_result(g.next(-3), 3, false)
102check_result(g.next(-4), 5, true)
103
104function *gen5() {
105  function *f() {
106    try {
107      yield 1
108      assert(false)
109    } catch (e) {
110      assert(e === 10)
111    }
112    return 2
113  }
114
115  assert((yield *f()) === 2)
116  yield 3
117}
118
119g = gen5()
120check_result(g.next(), 1, false)
121check_result(g.throw(10), 3, false)
122