1 /* ***** BEGIN LICENSE BLOCK ***** 2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1 3 * 4 * The contents of this file are subject to the Netscape Public License 5 * Version 1.1 (the "License"); you may not use this file except in 6 * compliance with the License. You may obtain a copy of the License at 7 * http://www.mozilla.org/NPL/ 8 * 9 * Software distributed under the License is distributed on an "AS IS" basis, 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 11 * for the specific language governing rights and limitations under the 12 * License. 13 * 14 * The Original Code is JavaScript Engine testing utilities. 15 * 16 * The Initial Developer of the Original Code is Netscape Communications Corp. 17 * Portions created by the Initial Developer are Copyright (C) 2002 18 * the Initial Developer. All Rights Reserved. 19 * 20 * Contributor(s): igor (at) icesoft.no, pschwartau (at) netscape.com 21 * 22 * Alternatively, the contents of this file may be used under the terms of 23 * either the GNU General Public License Version 2 or later (the "GPL"), or 24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 25 * in which case the provisions of the GPL or the LGPL are applicable instead 26 * of those above. If you wish to allow use of your version of this file only 27 * under the terms of either the GPL or the LGPL, and not to allow others to 28 * use your version of this file under the terms of the NPL, indicate your 29 * decision by deleting the provisions above and replace them with the notice 30 * and other provisions required by the GPL or the LGPL. If you do not delete 31 * the provisions above, a recipient may use your version of this file under 32 * the terms of any one of the NPL, the GPL or the LGPL. 33 * 34 * ***** END LICENSE BLOCK ***** 35 * 36 * 37 * Date: 16 Dec 2002 38 * SUMMARY: Testing |with (x) {function f() {}}| when |x.f| already exists 39 * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 40 * 41 * The idea is this: if |x| does not already have a property named |f|, 42 * a |with| statement cannot be used to define one. See, for example, 43 * 44 * http://bugzilla.mozilla.org/show_bug.cgi?id=159849#c11 45 * http://bugzilla.mozilla.org/show_bug.cgi?id=184107 46 * 47 * 48 * However, if |x| already has a property |f|, a |with| statement can be 49 * used to modify the value it contains: 50 * 51 * with (x) {f = 1;} 52 * 53 * This should work even if we use a |var| statement, like this: 54 * 55 * with (x) {var f = 1;} 56 * 57 * However, it should NOT work if we use a |function| statement, like this: 58 * 59 * with (x) {function f() {}} 60 * 61 * Instead, this should newly define a function f in global scope. 62 * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 63 * 64 */ 65 //----------------------------------------------------------------------------- 66 var UBound = 0; 67 var bug = 185485; 68 var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists'; 69 var status = ''; 70 var statusitems = []; 71 var actual = ''; 72 var actualvalues = []; 73 var expect= ''; 74 var expectedvalues = []; 75 76 var x = { f:0, g:0 }; 77 78 with (x) 79 { 80 f = 1; 81 } 82 status = inSection(1); 83 actual = x.f; 84 expect = 1; 85 addThis(); 86 87 with (x) 88 { 89 var f = 2; 90 } 91 status = inSection(2); 92 actual = x.f; 93 expect = 2; 94 addThis(); 95 96 /* 97 * Use of a function statement under the with-block should not affect 98 * the local property |f|, but define a function |f| in global scope - 99 */ 100 with (x) 101 { 102 function f() {} 103 } 104 status = inSection(3); 105 actual = x.f; 106 expect = 2; 107 addThis(); 108 109 status = inSection(4); 110 actual = typeof this.f; 111 expect = 'function'; 112 addThis(); 113 114 115 /* 116 * Compare use of function expression instead of function statement. 117 * Note it is important that |x.g| already exists. Otherwise, this 118 * would newly define |g| in global scope - 119 */ 120 with (x) 121 { 122 var g = function() {} 123 } 124 status = inSection(5); 125 actual = x.g.toString(); 126 expect = (function () {}).toString(); 127 addThis(); 128 129 130 131 //----------------------------------------------------------------------------- 132 test(); 133 //----------------------------------------------------------------------------- 134 135 136 137 function addThis() 138 { 139 statusitems[UBound] = status; 140 actualvalues[UBound] = actual; 141 expectedvalues[UBound] = expect; 142 UBound++; 143 } 144 145 146 function test() 147 { 148 enterFunc('test'); 149 printBugNumber(bug); 150 printStatus(summary); 151 152 for (var i=0; i<UBound; i++) 153 { 154 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 155 } 156 157 exitFunc ('test'); 158 } 159