| 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: 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 gTestfile = 'regress-185485.js'; |
| 38 | var UBound = 0; |
| 39 | var BUGNUMBER = 185485; |
| 40 | var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists'; |
| 41 | var status = ''; |
| 42 | var statusitems = []; |
| 43 | var actual = ''; |
| 44 | var actualvalues = []; |
| 45 | var expect= ''; |
| 46 | var expectedvalues = []; |
| 47 | |
| 48 | var x = { f:0, g:0 }; |
| 49 | |
| 50 | with (x) |
| 51 | { |
| 52 | f = 1; |
| 53 | } |
| 54 | status = inSection(1); |
| 55 | actual = x.f; |
| 56 | expect = 1; |
| 57 | addThis(); |
| 58 | |
| 59 | with (x) |
| 60 | { |
| 61 | var f = 2; |
| 62 | } |
| 63 | status = inSection(2); |
| 64 | actual = x.f; |
| 65 | expect = 2; |
| 66 | addThis(); |
| 67 | |
| 68 | /* |
| 69 | * Use of a function statement under the with-block should not affect |
| 70 | * the local property |f|, but define a function |f| in global scope - |
| 71 | */ |
| 72 | with (x) |
| 73 | { |
| 74 | function f() {} |
| 75 | } |
| 76 | status = inSection(3); |
| 77 | actual = x.f; |
| 78 | expect = 2; |
| 79 | addThis(); |
| 80 | |
| 81 | status = inSection(4); |
| 82 | actual = typeof this.f; |
| 83 | expect = 'function'; |
| 84 | addThis(); |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * Compare use of function expression instead of function statement. |
| 89 | * Note it is important that |x.g| already exists. Otherwise, this |
| 90 | * would newly define |g| in global scope - |
| 91 | */ |
| 92 | with (x) |
| 93 | { |
| 94 | var g = function() {} |
| 95 | } |
| 96 | status = inSection(5); |
| 97 | actual = x.g.toString(); |
| 98 | expect = (function () {}).toString(); |
| 99 | addThis(); |
| 100 | |
| 101 | |
| 102 | |
| 103 | //----------------------------------------------------------------------------- |
| 104 | test(); |
| 105 | //----------------------------------------------------------------------------- |
| 106 | |
| 107 | |
| 108 | |
| 109 | function addThis() |
| 110 | { |
| 111 | statusitems[UBound] = status; |
| 112 | actualvalues[UBound] = actual; |
| 113 | expectedvalues[UBound] = expect; |
| 114 | UBound++; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | function test() |
| 119 | { |
| 120 | enterFunc('test'); |
| 121 | printBugNumber(BUGNUMBER); |
| 122 | printStatus(summary); |
| 123 | |
| 124 | for (var i=0; i<UBound; i++) |
| 125 | { |
| 126 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 127 | } |
| 128 | |
| 129 | exitFunc ('test'); |
| 130 | } |