summaryrefslogtreecommitdiffstats
path: root/js/src/tests/js1_8/genexps/regress-349326.js
blob: eea4e98a7b9cd8239eaaf3a0dc437ab00fef4266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//-----------------------------------------------------------------------------
var BUGNUMBER = 349326;
var summary = 'closing generators';
var actual = 'PASS';
var expect = 'PASS';


//-----------------------------------------------------------------------------
test();
//-----------------------------------------------------------------------------

function test()
{
  enterFunc ('test');
  printBugNumber(BUGNUMBER);
  printStatus (summary);
 
  let closed;

  function gen()
  {
    try {
      yield 1;
      yield 2;
    } finally {
      closed = true;
    }
  }

// Test that return closes the generator
  function test_return()
  {
    for (let i in gen()) {
      if (i != 1)
        throw "unexpected generator value";
      return 10;
    }
  }

  closed = false;
  test_return();
  if (closed !== true)
    throw "return does not close generator";

// test that break closes generator

  closed = false;
  for (let i in gen()) {
    if (i != 1)
      throw "unexpected generator value";
    break;
  }
  if (closed !== true)
    throw "break does not close generator";

label: {
    for (;;) {
      closed = false;
      for (let i in gen()) {
        if (i != 1)
          throw "unexpected generator value";
        break label;
      }
    }
  }

  if (closed !== true)
    throw "break does not close generator";

// test that an exception closes generator

  function function_that_throws()
  {
    throw function_that_throws;
  }

  try {
    closed = false;
    for (let i in gen()) {
      if (i != 1)
        throw "unexpected generator value";
      function_that_throws();
    }
  } catch (e) {
    if (e !== function_that_throws)
      throw e;
  }

  if (closed !== true)
    throw "exception does not close generator";

// Check consistency of finally execution in presence of generators

  let gen2_was_closed = false;
  let gen3_was_closed = false;
  let finally_was_executed = false;

  function gen2() {
    try {
      yield 2;
    } finally {
      if (gen2_was_closed || !finally_was_executed || !gen3_was_closed)
        throw "bad oder of gen2 finally execution"
          gen2_was_closed = true;
      throw gen2;
    }
  }

  function gen3() {
    try {
      yield 3;
    } finally {
      if (gen2_was_closed || finally_was_executed || gen3_was_closed)
        throw "bad oder of gen3 finally execution"
          gen3_was_closed = true;
    }
  }

label2: {
    try {
      for (let i in gen2()) {
        try {
          for (let j in gen3()) {
            break label2;
          }
        } finally {
          if (gen2_was_closed || finally_was_executed || !gen3_was_closed)
            throw "bad oder of try-finally execution";
          finally_was_executed = true;
        }
      }
      throw "gen2 finally should throw";
    } catch (e) {
      if (e != gen2) throw e;
    }
  }

  print("OK");

  reportCompare(expect, actual, summary);

  exitFunc ('test');
}