|
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 UBound = 0; |
|
18 var BUGNUMBER = 181834; |
|
19 var summary = 'Testing scope'; |
|
20 var status = ''; |
|
21 var statusitems = []; |
|
22 var actual = ''; |
|
23 var actualvalues = []; |
|
24 var expect= ''; |
|
25 var expectedvalues = []; |
|
26 |
|
27 |
|
28 /* |
|
29 * If N<=0, |outer_d| just gets incremented once, |
|
30 * so the return value should be 1 in this case. |
|
31 * |
|
32 * If N>0, we end up calling inner() N+1 times: |
|
33 * inner(N), inner(N-1), ... , inner(0). |
|
34 * |
|
35 * Each call to inner() increments |outer_d| by 1. |
|
36 * The last call, inner(0), returns the final value |
|
37 * of |outer_d|, which should be N+1. |
|
38 */ |
|
39 function outer(N) |
|
40 { |
|
41 var outer_d = 0; |
|
42 return inner(N); |
|
43 |
|
44 function inner(level) |
|
45 { |
|
46 outer_d++; |
|
47 |
|
48 if (level > 0) |
|
49 return inner(level - 1); |
|
50 else |
|
51 return outer_d; |
|
52 } |
|
53 } |
|
54 |
|
55 |
|
56 /* |
|
57 * This only has meaning in Rhino - |
|
58 */ |
|
59 setDynamicScope(true); |
|
60 |
|
61 /* |
|
62 * Recompile the function |outer| via eval() in order to |
|
63 * feel the effect of the dynamic scope mode we have set. |
|
64 */ |
|
65 var s = outer.toString(); |
|
66 eval(s); |
|
67 |
|
68 status = inSection(1); |
|
69 actual = outer(-5); |
|
70 expect = 1; |
|
71 addThis(); |
|
72 |
|
73 status = inSection(2); |
|
74 actual = outer(0); |
|
75 expect = 1; |
|
76 addThis(); |
|
77 |
|
78 status = inSection(3); |
|
79 actual = outer(5); |
|
80 expect = 6; |
|
81 addThis(); |
|
82 |
|
83 |
|
84 /* |
|
85 * Sanity check: do same steps with the dynamic flag off |
|
86 */ |
|
87 setDynamicScope(false); |
|
88 |
|
89 /* |
|
90 * Recompile the function |outer| via eval() in order to |
|
91 * feel the effect of the dynamic scope mode we have set. |
|
92 */ |
|
93 eval(s); |
|
94 |
|
95 status = inSection(4); |
|
96 actual = outer(-5); |
|
97 expect = 1; |
|
98 addThis(); |
|
99 |
|
100 status = inSection(5); |
|
101 actual = outer(0); |
|
102 expect = 1; |
|
103 addThis(); |
|
104 |
|
105 status = inSection(6); |
|
106 actual = outer(5); |
|
107 expect = 6; |
|
108 addThis(); |
|
109 |
|
110 |
|
111 |
|
112 //----------------------------------------------------------------------------- |
|
113 test(); |
|
114 //----------------------------------------------------------------------------- |
|
115 |
|
116 |
|
117 |
|
118 function setDynamicScope(flag) |
|
119 { |
|
120 if (this.Packages) |
|
121 { |
|
122 var cx = this.Packages.org.mozilla.javascript.Context.getCurrentContext(); |
|
123 cx.setCompileFunctionsWithDynamicScope(flag); |
|
124 } |
|
125 } |
|
126 |
|
127 |
|
128 function addThis() |
|
129 { |
|
130 statusitems[UBound] = status; |
|
131 actualvalues[UBound] = actual; |
|
132 expectedvalues[UBound] = expect; |
|
133 UBound++; |
|
134 } |
|
135 |
|
136 |
|
137 function test() |
|
138 { |
|
139 enterFunc('test'); |
|
140 printBugNumber(BUGNUMBER); |
|
141 printStatus(summary); |
|
142 |
|
143 for (var i=0; i<UBound; i++) |
|
144 { |
|
145 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
|
146 } |
|
147 |
|
148 exitFunc ('test'); |
|
149 } |