| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 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: 16 Dec 2002 |
| 9 |
* SUMMARY: Testing |with (x) {function f() {}}| when |x.f| already exists |
| 10 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 |
| 11 |
* |
| 12 |
* The idea is this: if |x| does not already have a property named |f|, |
| 13 |
* a |with| statement cannot be used to define one. See, for example, |
| 14 |
* |
| 15 |
* http://bugzilla.mozilla.org/show_bug.cgi?id=159849#c11 |
| 16 |
* http://bugzilla.mozilla.org/show_bug.cgi?id=184107 |
| 17 |
* |
| 18 |
* |
| 19 |
* However, if |x| already has a property |f|, a |with| statement can be |
| 20 |
* used to modify the value it contains: |
| 21 |
* |
| 22 |
* with (x) {f = 1;} |
| 23 |
* |
| 24 |
* This should work even if we use a |var| statement, like this: |
| 25 |
* |
| 26 |
* with (x) {var f = 1;} |
| 27 |
* |
| 28 |
* However, it should NOT work if we use a |function| statement, like this: |
| 29 |
* |
| 30 |
* with (x) {function f() {}} |
| 31 |
* |
| 32 |
* Instead, this should newly define a function f in global scope. |
| 33 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 |
| 34 |
* |
| 35 |
*/ |
| 36 |
//----------------------------------------------------------------------------- |
| 37 |
var UBound = 0; |
| 38 |
var BUGNUMBER = 185485; |
| 39 |
var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists'; |
| 40 |
var status = ''; |
| 41 |
var statusitems = []; |
| 42 |
var actual = ''; |
| 43 |
var actualvalues = []; |
| 44 |
var expect= ''; |
| 45 |
var expectedvalues = []; |
| 46 |
|
| 47 |
var x = { f:0, g:0 }; |
| 48 |
|
| 49 |
with (x) |
| 50 |
{ |
| 51 |
f = 1; |
| 52 |
} |
| 53 |
status = inSection(1); |
| 54 |
actual = x.f; |
| 55 |
expect = 1; |
| 56 |
addThis(); |
| 57 |
|
| 58 |
with (x) |
| 59 |
{ |
| 60 |
var f = 2; |
| 61 |
} |
| 62 |
status = inSection(2); |
| 63 |
actual = x.f; |
| 64 |
expect = 2; |
| 65 |
addThis(); |
| 66 |
|
| 67 |
/* |
| 68 |
* Use of a function statement under the with-block should not affect |
| 69 |
* the local property |f|, but define a function |f| in global scope - |
| 70 |
*/ |
| 71 |
with (x) |
| 72 |
{ |
| 73 |
function f() {} |
| 74 |
} |
| 75 |
status = inSection(3); |
| 76 |
actual = x.f; |
| 77 |
expect = 2; |
| 78 |
addThis(); |
| 79 |
|
| 80 |
status = inSection(4); |
| 81 |
actual = typeof this.f; |
| 82 |
expect = 'function'; |
| 83 |
addThis(); |
| 84 |
|
| 85 |
|
| 86 |
/* |
| 87 |
* Compare use of function expression instead of function statement. |
| 88 |
* Note it is important that |x.g| already exists. Otherwise, this |
| 89 |
* would newly define |g| in global scope - |
| 90 |
*/ |
| 91 |
with (x) |
| 92 |
{ |
| 93 |
var g = function() {} |
| 94 |
} |
| 95 |
status = inSection(5); |
| 96 |
actual = x.g.toString(); |
| 97 |
expect = (function() {}).toString(); |
| 98 |
addThis(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
//----------------------------------------------------------------------------- |
| 103 |
test(); |
| 104 |
//----------------------------------------------------------------------------- |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
function addThis() |
| 109 |
{ |
| 110 |
statusitems[UBound] = status; |
| 111 |
actualvalues[UBound] = actual; |
| 112 |
expectedvalues[UBound] = expect; |
| 113 |
UBound++; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
function test() |
| 118 |
{ |
| 119 |
printBugNumber(BUGNUMBER); |
| 120 |
printStatus(summary); |
| 121 |
|
| 122 |
for (var i=0; i<UBound; i++) |
| 123 |
{ |
| 124 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 125 |
} |
| 126 |
} |