Debian Codegraph @

rhino/testsrc/tests/js1_5/Scope/regress-181834.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 *
8 * Date: 25 November 2002
9 * SUMMARY: Testing scope
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=181834
11 *
12 * This bug only bit in Rhino interpreted mode, when the
13 * 'compile functions with dynamic scope' feature was set.
14 *
15 */
16 //-----------------------------------------------------------------------------
17 var gTestfile = 'regress-181834.js';
18 var UBound = 0;
19 var BUGNUMBER = 181834;
20 var summary = 'Testing scope';
21 var status = '';
22 var statusitems = [];
23 var actual = '';
24 var actualvalues = [];
25 var expect= '';
26 var expectedvalues = [];
27
28
29 /*
30 * If N<=0, |outer_d| just gets incremented once,
31 * so the return value should be 1 in this case.
32 *
33 * If N>0, we end up calling inner() N+1 times:
34 * inner(N), inner(N-1), ... , inner(0).
35 *
36 * Each call to inner() increments |outer_d| by 1.
37 * The last call, inner(0), returns the final value
38 * of |outer_d|, which should be N+1.
39 */
40 function outer(N)
41 {
42 var outer_d = 0;
43 return inner(N);
44
45 function inner(level)
46 {
47 outer_d++;
48
49 if (level > 0)
50 return inner(level - 1);
51 else
52 return outer_d;
53 }
54 }
55
56
57 /*
58 * This only has meaning in Rhino -
59 */
60 setDynamicScope(true);
61
62 /*
63 * Recompile the function |outer| via eval() in order to
64 * feel the effect of the dynamic scope mode we have set.
65 */
66 var s = outer.toString();
67 eval(s);
68
69 status = inSection(1);
70 actual = outer(-5);
71 expect = 1;
72 addThis();
73
74 status = inSection(2);
75 actual = outer(0);
76 expect = 1;
77 addThis();
78
79 status = inSection(3);
80 actual = outer(5);
81 expect = 6;
82 addThis();
83
84
85 /*
86 * Sanity check: do same steps with the dynamic flag off
87 */
88 setDynamicScope(false);
89
90 /*
91 * Recompile the function |outer| via eval() in order to
92 * feel the effect of the dynamic scope mode we have set.
93 */
94 eval(s);
95
96 status = inSection(4);
97 actual = outer(-5);
98 expect = 1;
99 addThis();
100
101 status = inSection(5);
102 actual = outer(0);
103 expect = 1;
104 addThis();
105
106 status = inSection(6);
107 actual = outer(5);
108 expect = 6;
109 addThis();
110
111
112
113 //-----------------------------------------------------------------------------
114 test();
115 //-----------------------------------------------------------------------------
116
117
118
119 function setDynamicScope(flag)
120 {
121 if (this.Packages)
122 {
123 var cx = this.Packages.org.mozilla.javascript.Context.getCurrentContext();
124 cx.setCompileFunctionsWithDynamicScope(flag);
125 }
126 }
127
128
129 function addThis()
130 {
131 statusitems[UBound] = status;
132 actualvalues[UBound] = actual;
133 expectedvalues[UBound] = expect;
134 UBound++;
135 }
136
137
138 function test()
139 {
140 enterFunc('test');
141 printBugNumber(BUGNUMBER);
142 printStatus(summary);
143
144 for (var i=0; i<UBound; i++)
145 {
146 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
147 }
148
149 exitFunc ('test');
150 }