js/src/tests/js1_5/Scope/regress-181834.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Scope/regress-181834.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     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 + *
    1.11 + * Date:    25 November 2002
    1.12 + * SUMMARY: Testing scope
    1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=181834
    1.14 + *
    1.15 + * This bug only bit in Rhino interpreted mode, when the
    1.16 + * 'compile functions with dynamic scope' feature was set.
    1.17 + *
    1.18 + */
    1.19 +//-----------------------------------------------------------------------------
    1.20 +var UBound = 0;
    1.21 +var BUGNUMBER = 181834;
    1.22 +var summary = 'Testing scope';
    1.23 +var status = '';
    1.24 +var statusitems = [];
    1.25 +var actual = '';
    1.26 +var actualvalues = [];
    1.27 +var expect= '';
    1.28 +var expectedvalues = [];
    1.29 +
    1.30 +
    1.31 +/*
    1.32 + * If N<=0, |outer_d| just gets incremented once,
    1.33 + * so the return value should be 1 in this case.
    1.34 + *
    1.35 + * If N>0, we end up calling inner() N+1 times:
    1.36 + * inner(N), inner(N-1), ... , inner(0).
    1.37 + *
    1.38 + * Each call to inner() increments |outer_d| by 1.
    1.39 + * The last call, inner(0), returns the final value
    1.40 + * of |outer_d|, which should be N+1.
    1.41 + */
    1.42 +function outer(N)
    1.43 +{
    1.44 +  var outer_d = 0;
    1.45 +  return inner(N);
    1.46 +
    1.47 +  function inner(level)
    1.48 +  {
    1.49 +    outer_d++;
    1.50 +
    1.51 +    if (level > 0)
    1.52 +      return inner(level - 1);
    1.53 +    else
    1.54 +      return outer_d;
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +
    1.59 +/*
    1.60 + * This only has meaning in Rhino -
    1.61 + */
    1.62 +setDynamicScope(true);
    1.63 +
    1.64 +/*
    1.65 + * Recompile the function |outer| via eval() in order to
    1.66 + * feel the effect of the dynamic scope mode we have set.
    1.67 + */
    1.68 +var s = outer.toString();
    1.69 +eval(s);
    1.70 +
    1.71 +status = inSection(1);
    1.72 +actual = outer(-5);
    1.73 +expect = 1;
    1.74 +addThis();
    1.75 +
    1.76 +status = inSection(2);
    1.77 +actual = outer(0);
    1.78 +expect = 1;
    1.79 +addThis();
    1.80 +
    1.81 +status = inSection(3);
    1.82 +actual = outer(5);
    1.83 +expect = 6;
    1.84 +addThis();
    1.85 +
    1.86 +
    1.87 +/*
    1.88 + * Sanity check: do same steps with the dynamic flag off
    1.89 + */
    1.90 +setDynamicScope(false);
    1.91 +
    1.92 +/*
    1.93 + * Recompile the function |outer| via eval() in order to
    1.94 + * feel the effect of the dynamic scope mode we have set.
    1.95 + */
    1.96 +eval(s);
    1.97 +
    1.98 +status = inSection(4);
    1.99 +actual = outer(-5);
   1.100 +expect = 1;
   1.101 +addThis();
   1.102 +
   1.103 +status = inSection(5);
   1.104 +actual = outer(0);
   1.105 +expect = 1;
   1.106 +addThis();
   1.107 +
   1.108 +status = inSection(6);
   1.109 +actual = outer(5);
   1.110 +expect = 6;
   1.111 +addThis();
   1.112 +
   1.113 +
   1.114 +
   1.115 +//-----------------------------------------------------------------------------
   1.116 +test();
   1.117 +//-----------------------------------------------------------------------------
   1.118 +
   1.119 +
   1.120 +
   1.121 +function setDynamicScope(flag)
   1.122 +{
   1.123 +  if (this.Packages)
   1.124 +  {
   1.125 +    var cx = this.Packages.org.mozilla.javascript.Context.getCurrentContext();
   1.126 +    cx.setCompileFunctionsWithDynamicScope(flag);
   1.127 +  }
   1.128 +}
   1.129 +
   1.130 +
   1.131 +function addThis()
   1.132 +{
   1.133 +  statusitems[UBound] = status;
   1.134 +  actualvalues[UBound] = actual;
   1.135 +  expectedvalues[UBound] = expect;
   1.136 +  UBound++;
   1.137 +}
   1.138 +
   1.139 +
   1.140 +function test()
   1.141 +{
   1.142 +  enterFunc('test');
   1.143 +  printBugNumber(BUGNUMBER);
   1.144 +  printStatus(summary);
   1.145 +
   1.146 +  for (var i=0; i<UBound; i++)
   1.147 +  {
   1.148 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.149 +  }
   1.150 +
   1.151 +  exitFunc ('test');
   1.152 +}

mercurial