Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Date: 28 May 2001 |
michael@0 | 8 | * |
michael@0 | 9 | * SUMMARY: Functions are scoped statically, not dynamically |
michael@0 | 10 | * |
michael@0 | 11 | * See ECMA Section 10.1.4 Scope Chain and Identifier Resolution |
michael@0 | 12 | * (This section defines the scope chain of an execution context) |
michael@0 | 13 | * |
michael@0 | 14 | * See ECMA Section 12.10 The with Statement |
michael@0 | 15 | * |
michael@0 | 16 | * See ECMA Section 13 Function Definition |
michael@0 | 17 | * (This section defines the scope chain of a function object as that |
michael@0 | 18 | * of the running execution context when the function was declared) |
michael@0 | 19 | */ |
michael@0 | 20 | //----------------------------------------------------------------------------- |
michael@0 | 21 | var UBound = 0; |
michael@0 | 22 | var BUGNUMBER = '(none)'; |
michael@0 | 23 | var summary = 'Testing that functions are scoped statically, not dynamically'; |
michael@0 | 24 | var self = this; // capture a reference to the global object |
michael@0 | 25 | var status = ''; |
michael@0 | 26 | var statusitems = [ ]; |
michael@0 | 27 | var actual = ''; |
michael@0 | 28 | var actualvalues = [ ]; |
michael@0 | 29 | var expect= ''; |
michael@0 | 30 | var expectedvalues = [ ]; |
michael@0 | 31 | |
michael@0 | 32 | /* |
michael@0 | 33 | * In this section the expected value is 1, not 2. |
michael@0 | 34 | * |
michael@0 | 35 | * Why? f captures its scope chain from when it's declared, and imposes that chain |
michael@0 | 36 | * when it's executed. In other words, f's scope chain is from when it was compiled. |
michael@0 | 37 | * Since f is a top-level function, this is the global object only. Hence 'a' resolves to 1. |
michael@0 | 38 | */ |
michael@0 | 39 | status = 'Section A of test'; |
michael@0 | 40 | var a = 1; |
michael@0 | 41 | function f() |
michael@0 | 42 | { |
michael@0 | 43 | return a; |
michael@0 | 44 | } |
michael@0 | 45 | var obj = {a:2}; |
michael@0 | 46 | with (obj) |
michael@0 | 47 | { |
michael@0 | 48 | actual = f(); |
michael@0 | 49 | } |
michael@0 | 50 | expect = 1; |
michael@0 | 51 | addThis(); |
michael@0 | 52 | |
michael@0 | 53 | |
michael@0 | 54 | /* |
michael@0 | 55 | * In this section the expected value is 2, not 1. That is because here |
michael@0 | 56 | * f's associated scope chain now includes 'obj' before the global object. |
michael@0 | 57 | */ |
michael@0 | 58 | status = 'Section B of test'; |
michael@0 | 59 | var a = 1; |
michael@0 | 60 | var obj = {a:2}; |
michael@0 | 61 | with (obj) |
michael@0 | 62 | { |
michael@0 | 63 | function f() |
michael@0 | 64 | { |
michael@0 | 65 | return a; |
michael@0 | 66 | } |
michael@0 | 67 | actual = f(); |
michael@0 | 68 | } |
michael@0 | 69 | expect = 2; |
michael@0 | 70 | addThis(); |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | /* |
michael@0 | 74 | * Like Section B , except that we call f outside the with block. |
michael@0 | 75 | * By the principles explained above, we still expect 2 - |
michael@0 | 76 | */ |
michael@0 | 77 | status = 'Section C of test'; |
michael@0 | 78 | var a = 1; |
michael@0 | 79 | var obj = {a:2}; |
michael@0 | 80 | with (obj) |
michael@0 | 81 | { |
michael@0 | 82 | function f() |
michael@0 | 83 | { |
michael@0 | 84 | return a; |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | actual = f(); |
michael@0 | 88 | expect = 2; |
michael@0 | 89 | addThis(); |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | /* |
michael@0 | 93 | * Like Section C, but with one more level of indirection - |
michael@0 | 94 | */ |
michael@0 | 95 | status = 'Section D of test'; |
michael@0 | 96 | var a = 1; |
michael@0 | 97 | var obj = {a:2, obj:{a:3}}; |
michael@0 | 98 | with (obj) |
michael@0 | 99 | { |
michael@0 | 100 | with (obj) |
michael@0 | 101 | { |
michael@0 | 102 | function f() |
michael@0 | 103 | { |
michael@0 | 104 | return a; |
michael@0 | 105 | } |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | actual = f(); |
michael@0 | 109 | expect = 3; |
michael@0 | 110 | addThis(); |
michael@0 | 111 | |
michael@0 | 112 | |
michael@0 | 113 | /* |
michael@0 | 114 | * Like Section C, but here we actually delete obj before calling f. |
michael@0 | 115 | * We still expect 2 - |
michael@0 | 116 | */ |
michael@0 | 117 | status = 'Section E of test'; |
michael@0 | 118 | var a = 1; |
michael@0 | 119 | var obj = {a:2}; |
michael@0 | 120 | with (obj) |
michael@0 | 121 | { |
michael@0 | 122 | function f() |
michael@0 | 123 | { |
michael@0 | 124 | return a; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | delete obj; |
michael@0 | 128 | actual = f(); |
michael@0 | 129 | expect = 2; |
michael@0 | 130 | addThis(); |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | /* |
michael@0 | 134 | * Like Section E. Here we redefine obj and call f under with (obj) - |
michael@0 | 135 | * We still expect 2 - |
michael@0 | 136 | */ |
michael@0 | 137 | status = 'Section F of test'; |
michael@0 | 138 | var a = 1; |
michael@0 | 139 | var obj = {a:2}; |
michael@0 | 140 | with (obj) |
michael@0 | 141 | { |
michael@0 | 142 | function f() |
michael@0 | 143 | { |
michael@0 | 144 | return a; |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | delete obj; |
michael@0 | 148 | var obj = {a:3}; |
michael@0 | 149 | with (obj) |
michael@0 | 150 | { |
michael@0 | 151 | actual = f(); |
michael@0 | 152 | } |
michael@0 | 153 | expect = 2; // NOT 3 !!! |
michael@0 | 154 | addThis(); |
michael@0 | 155 | |
michael@0 | 156 | |
michael@0 | 157 | /* |
michael@0 | 158 | * Explicitly verify that f exists at global level, even though |
michael@0 | 159 | * it was defined under the with(obj) block - |
michael@0 | 160 | */ |
michael@0 | 161 | status = 'Section G of test'; |
michael@0 | 162 | var a = 1; |
michael@0 | 163 | var obj = {a:2}; |
michael@0 | 164 | with (obj) |
michael@0 | 165 | { |
michael@0 | 166 | function f() |
michael@0 | 167 | { |
michael@0 | 168 | return a; |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | actual = String([obj.hasOwnProperty('f'), self.hasOwnProperty('f')]); |
michael@0 | 172 | expect = String([false, true]); |
michael@0 | 173 | addThis(); |
michael@0 | 174 | |
michael@0 | 175 | |
michael@0 | 176 | /* |
michael@0 | 177 | * Explicitly verify that f exists at global level, even though |
michael@0 | 178 | * it was defined under the with(obj) block - |
michael@0 | 179 | */ |
michael@0 | 180 | status = 'Section H of test'; |
michael@0 | 181 | var a = 1; |
michael@0 | 182 | var obj = {a:2}; |
michael@0 | 183 | with (obj) |
michael@0 | 184 | { |
michael@0 | 185 | function f() |
michael@0 | 186 | { |
michael@0 | 187 | return a; |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | actual = String(['f' in obj, 'f' in self]); |
michael@0 | 191 | expect = String([false, true]); |
michael@0 | 192 | addThis(); |
michael@0 | 193 | |
michael@0 | 194 | |
michael@0 | 195 | |
michael@0 | 196 | //------------------------------------------------------------------------------------------------- |
michael@0 | 197 | test(); |
michael@0 | 198 | //------------------------------------------------------------------------------------------------- |
michael@0 | 199 | |
michael@0 | 200 | |
michael@0 | 201 | function addThis() |
michael@0 | 202 | { |
michael@0 | 203 | statusitems[UBound] = status; |
michael@0 | 204 | actualvalues[UBound] = actual; |
michael@0 | 205 | expectedvalues[UBound] = expect; |
michael@0 | 206 | UBound++; |
michael@0 | 207 | resetTestVars(); |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | |
michael@0 | 211 | function resetTestVars() |
michael@0 | 212 | { |
michael@0 | 213 | delete a; |
michael@0 | 214 | delete obj; |
michael@0 | 215 | delete f; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | |
michael@0 | 219 | function test() |
michael@0 | 220 | { |
michael@0 | 221 | enterFunc ('test'); |
michael@0 | 222 | printBugNumber(BUGNUMBER); |
michael@0 | 223 | printStatus (summary); |
michael@0 | 224 | |
michael@0 | 225 | for (var i = 0; i < UBound; i++) |
michael@0 | 226 | { |
michael@0 | 227 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | exitFunc ('test'); |
michael@0 | 231 | } |