js/src/tests/js1_8/genexps/regress-349326.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_8/genexps/regress-349326.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +//-----------------------------------------------------------------------------
    1.10 +var BUGNUMBER = 349326;
    1.11 +var summary = 'closing generators';
    1.12 +var actual = 'PASS';
    1.13 +var expect = 'PASS';
    1.14 +
    1.15 +
    1.16 +//-----------------------------------------------------------------------------
    1.17 +test();
    1.18 +//-----------------------------------------------------------------------------
    1.19 +
    1.20 +function test()
    1.21 +{
    1.22 +  enterFunc ('test');
    1.23 +  printBugNumber(BUGNUMBER);
    1.24 +  printStatus (summary);
    1.25 + 
    1.26 +  let closed;
    1.27 +
    1.28 +  function gen()
    1.29 +  {
    1.30 +    try {
    1.31 +      yield 1;
    1.32 +      yield 2;
    1.33 +    } finally {
    1.34 +      closed = true;
    1.35 +    }
    1.36 +  }
    1.37 +
    1.38 +// Test that return closes the generator
    1.39 +  function test_return()
    1.40 +  {
    1.41 +    for (let i in gen()) {
    1.42 +      if (i != 1)
    1.43 +        throw "unexpected generator value";
    1.44 +      return 10;
    1.45 +    }
    1.46 +  }
    1.47 +
    1.48 +  closed = false;
    1.49 +  test_return();
    1.50 +  if (closed !== true)
    1.51 +    throw "return does not close generator";
    1.52 +
    1.53 +// test that break closes generator
    1.54 +
    1.55 +  closed = false;
    1.56 +  for (let i in gen()) {
    1.57 +    if (i != 1)
    1.58 +      throw "unexpected generator value";
    1.59 +    break;
    1.60 +  }
    1.61 +  if (closed !== true)
    1.62 +    throw "break does not close generator";
    1.63 +
    1.64 +label: {
    1.65 +    for (;;) {
    1.66 +      closed = false;
    1.67 +      for (let i in gen()) {
    1.68 +        if (i != 1)
    1.69 +          throw "unexpected generator value";
    1.70 +        break label;
    1.71 +      }
    1.72 +    }
    1.73 +  }
    1.74 +
    1.75 +  if (closed !== true)
    1.76 +    throw "break does not close generator";
    1.77 +
    1.78 +// test that an exception closes generator
    1.79 +
    1.80 +  function function_that_throws()
    1.81 +  {
    1.82 +    throw function_that_throws;
    1.83 +  }
    1.84 +
    1.85 +  try {
    1.86 +    closed = false;
    1.87 +    for (let i in gen()) {
    1.88 +      if (i != 1)
    1.89 +        throw "unexpected generator value";
    1.90 +      function_that_throws();
    1.91 +    }
    1.92 +  } catch (e) {
    1.93 +    if (e !== function_that_throws)
    1.94 +      throw e;
    1.95 +  }
    1.96 +
    1.97 +  if (closed !== true)
    1.98 +    throw "exception does not close generator";
    1.99 +
   1.100 +// Check consistency of finally execution in presence of generators
   1.101 +
   1.102 +  let gen2_was_closed = false;
   1.103 +  let gen3_was_closed = false;
   1.104 +  let finally_was_executed = false;
   1.105 +
   1.106 +  function gen2() {
   1.107 +    try {
   1.108 +      yield 2;
   1.109 +    } finally {
   1.110 +      if (gen2_was_closed || !finally_was_executed || !gen3_was_closed)
   1.111 +        throw "bad oder of gen2 finally execution"
   1.112 +          gen2_was_closed = true;
   1.113 +      throw gen2;
   1.114 +    }
   1.115 +  }
   1.116 +
   1.117 +  function gen3() {
   1.118 +    try {
   1.119 +      yield 3;
   1.120 +    } finally {
   1.121 +      if (gen2_was_closed || finally_was_executed || gen3_was_closed)
   1.122 +        throw "bad oder of gen3 finally execution"
   1.123 +          gen3_was_closed = true;
   1.124 +    }
   1.125 +  }
   1.126 +
   1.127 +label2: {
   1.128 +    try {
   1.129 +      for (let i in gen2()) {
   1.130 +        try {
   1.131 +          for (let j in gen3()) {
   1.132 +            break label2;
   1.133 +          }
   1.134 +        } finally {
   1.135 +          if (gen2_was_closed || finally_was_executed || !gen3_was_closed)
   1.136 +            throw "bad oder of try-finally execution";
   1.137 +          finally_was_executed = true;
   1.138 +        }
   1.139 +      }
   1.140 +      throw "gen2 finally should throw";
   1.141 +    } catch (e) {
   1.142 +      if (e != gen2) throw e;
   1.143 +    }
   1.144 +  }
   1.145 +
   1.146 +  print("OK");
   1.147 +
   1.148 +  reportCompare(expect, actual, summary);
   1.149 +
   1.150 +  exitFunc ('test');
   1.151 +}

mercurial