michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 16 Dec 2002 michael@0: * SUMMARY: Testing |with (x) {function f() {}}| when |x.f| already exists michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 michael@0: * michael@0: * The idea is this: if |x| does not already have a property named |f|, michael@0: * a |with| statement cannot be used to define one. See, for example, michael@0: * michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=159849#c11 michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=184107 michael@0: * michael@0: * michael@0: * However, if |x| already has a property |f|, a |with| statement can be michael@0: * used to modify the value it contains: michael@0: * michael@0: * with (x) {f = 1;} michael@0: * michael@0: * This should work even if we use a |var| statement, like this: michael@0: * michael@0: * with (x) {var f = 1;} michael@0: * michael@0: * However, it should NOT work if we use a |function| statement, like this: michael@0: * michael@0: * with (x) {function f() {}} michael@0: * michael@0: * Instead, this should newly define a function f in global scope. michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485 michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 185485; michael@0: var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: michael@0: var x = { f:0, g:0 }; michael@0: michael@0: with (x) michael@0: { michael@0: f = 1; michael@0: } michael@0: status = inSection(1); michael@0: actual = x.f; michael@0: expect = 1; michael@0: addThis(); michael@0: michael@0: with (x) michael@0: { michael@0: var f = 2; michael@0: } michael@0: status = inSection(2); michael@0: actual = x.f; michael@0: expect = 2; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Use of a function statement under the with-block should not affect michael@0: * the local property |f|, but define a function |f| in global scope - michael@0: */ michael@0: with (x) michael@0: { michael@0: function f() {} michael@0: } michael@0: status = inSection(3); michael@0: actual = x.f; michael@0: expect = 2; michael@0: addThis(); michael@0: michael@0: status = inSection(4); michael@0: actual = typeof this.f; michael@0: expect = 'function'; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Compare use of function expression instead of function statement. michael@0: * Note it is important that |x.g| already exists. Otherwise, this michael@0: * would newly define |g| in global scope - michael@0: */ michael@0: with (x) michael@0: { michael@0: var g = function() {} michael@0: } michael@0: status = inSection(5); michael@0: actual = x.g.toString(); michael@0: expect = (function () {}).toString(); michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: for (var i=0; i