summaryrefslogtreecommitdiffstats
path: root/js/src/tests/js1_5/Scope/regress-181834.js
blob: 311ae2e9af39fed89ff73f2e7a7c0c3f25f8e8f1 (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
149
/* -*- 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/. */

/*
 *
 * Date:    25 November 2002
 * SUMMARY: Testing scope
 * See http://bugzilla.mozilla.org/show_bug.cgi?id=181834
 *
 * This bug only bit in Rhino interpreted mode, when the
 * 'compile functions with dynamic scope' feature was set.
 *
 */
//-----------------------------------------------------------------------------
var UBound = 0;
var BUGNUMBER = 181834;
var summary = 'Testing scope';
var status = '';
var statusitems = [];
var actual = '';
var actualvalues = [];
var expect= '';
var expectedvalues = [];


/*
 * If N<=0, |outer_d| just gets incremented once,
 * so the return value should be 1 in this case.
 *
 * If N>0, we end up calling inner() N+1 times:
 * inner(N), inner(N-1), ... , inner(0).
 *
 * Each call to inner() increments |outer_d| by 1.
 * The last call, inner(0), returns the final value
 * of |outer_d|, which should be N+1.
 */
function outer(N)
{
  var outer_d = 0;
  return inner(N);

  function inner(level)
  {
    outer_d++;

    if (level > 0)
      return inner(level - 1);
    else
      return outer_d;
  }
}


/*
 * This only has meaning in Rhino -
 */
setDynamicScope(true);

/*
 * Recompile the function |outer| via eval() in order to
 * feel the effect of the dynamic scope mode we have set.
 */
var s = outer.toString();
eval(s);

status = inSection(1);
actual = outer(-5);
expect = 1;
addThis();

status = inSection(2);
actual = outer(0);
expect = 1;
addThis();

status = inSection(3);
actual = outer(5);
expect = 6;
addThis();


/*
 * Sanity check: do same steps with the dynamic flag off
 */
setDynamicScope(false);

/*
 * Recompile the function |outer| via eval() in order to
 * feel the effect of the dynamic scope mode we have set.
 */
eval(s);

status = inSection(4);
actual = outer(-5);
expect = 1;
addThis();

status = inSection(5);
actual = outer(0);
expect = 1;
addThis();

status = inSection(6);
actual = outer(5);
expect = 6;
addThis();



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



function setDynamicScope(flag)
{
  if (this.Packages)
  {
    var cx = this.Packages.org.mozilla.javascript.Context.getCurrentContext();
    cx.setCompileFunctionsWithDynamicScope(flag);
  }
}


function addThis()
{
  statusitems[UBound] = status;
  actualvalues[UBound] = actual;
  expectedvalues[UBound] = expect;
  UBound++;
}


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

  for (var i=0; i<UBound; i++)
  {
    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
  }

  exitFunc ('test');
}