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: 10 Jan 2002 michael@0: * SUMMARY: Reassignment to a const is NOT an error per ECMA michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=103602 michael@0: * michael@0: * ------- Additional Comment #4 From Brendan Eich 2002-01-10 15:30 ------- michael@0: * michael@0: * That's per ECMA (don't blame me, I fought for what Netscape always did: michael@0: * throw an error [could be a catchable exception since 1.3]). michael@0: * Readonly properties, when set by assignment, are not changed, but no error michael@0: * or exception is thrown. The value of the assignment expression is the value michael@0: * of the r.h.s. michael@0: * michael@0: * If you want a *strict* warning, pls change the summary of this bug michael@0: * to say so. michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 103602; michael@0: var summary = 'Reassignment to a const is NOT an error per ECMA'; 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: var cnFAIL_1 = 'Redeclaration of a const FAILED to cause an error'; michael@0: var cnFAIL_2 = 'Reassigning to a const caused an ERROR! It should not!!!'; michael@0: var sEval = ''; michael@0: michael@0: /* michael@0: * Not every implementation supports const (a non-ECMA extension) michael@0: * For example, Rhino does not; it would generate a complile-time error. michael@0: * So we have to hide this so it will be detected at run-time instead. michael@0: */ michael@0: try michael@0: { michael@0: sEval = 'const one = 1'; michael@0: eval(sEval); michael@0: } michael@0: catch(e) michael@0: { michael@0: quit(); // if const is not supported, this testcase is over - michael@0: } michael@0: michael@0: michael@0: status = inSection(1); michael@0: try michael@0: { michael@0: /* michael@0: * Redeclaration of const should be a compile-time error. michael@0: * Hide so it will be detected at run-time. michael@0: */ michael@0: sEval = 'const one = 2;'; michael@0: eval(sEval); michael@0: michael@0: expect = ''; // we shouldn't reach this line michael@0: actual = cnFAIL_1; michael@0: addThis(); michael@0: } michael@0: catch(e) michael@0: { michael@0: // good - we should be here. michael@0: actual = expect; michael@0: addThis(); michael@0: } michael@0: michael@0: michael@0: status = inSection(2); michael@0: try michael@0: { michael@0: /* michael@0: * Reassignment to a const should be NOT be an error, per ECMA. michael@0: */ michael@0: one = 2; michael@0: actual = expect; // good: no error was generated michael@0: addThis(); michael@0: michael@0: // although no error, the assignment should have been ignored - michael@0: status = inSection(3); michael@0: actual = one; michael@0: expect = 1; michael@0: addThis(); michael@0: michael@0: // the value of the EXPRESSION, however, is the value of the r.h.s. - michael@0: status = inSection(4); michael@0: actual = (one = 2); michael@0: expect = 2; michael@0: addThis(); michael@0: } michael@0: michael@0: catch(e) michael@0: { michael@0: // BAD - we shouldn't be here michael@0: expect = ''; michael@0: actual = cnFAIL_2; michael@0: addThis(); michael@0: } michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); 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 < UBound; i++) michael@0: { michael@0: reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: }