| 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: 10 Jan 2002 |
| 9 | * SUMMARY: Reassignment to a const is NOT an error per ECMA |
| 10 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=103602 |
| 11 | * |
| 12 | * ------- Additional Comment #4 From Brendan Eich 2002-01-10 15:30 ------- |
| 13 | * |
| 14 | * That's per ECMA (don't blame me, I fought for what Netscape always did: |
| 15 | * throw an error [could be a catchable exception since 1.3]). |
| 16 | * Readonly properties, when set by assignment, are not changed, but no error |
| 17 | * or exception is thrown. The value of the assignment expression is the value |
| 18 | * of the r.h.s. |
| 19 | * |
| 20 | * If you want a *strict* warning, pls change the summary of this bug |
| 21 | * to say so. |
| 22 | */ |
| 23 | //----------------------------------------------------------------------------- |
| 24 | var gTestfile = 'regress-103602.js'; |
| 25 | var UBound = 0; |
| 26 | var BUGNUMBER = 103602; |
| 27 | var summary = 'Reassignment to a const is NOT an error per ECMA'; |
| 28 | var status = ''; |
| 29 | var statusitems = []; |
| 30 | var actual = ''; |
| 31 | var actualvalues = []; |
| 32 | var expect= ''; |
| 33 | var expectedvalues = []; |
| 34 | var cnFAIL_1 = 'Redeclaration of a const FAILED to cause an error'; |
| 35 | var cnFAIL_2 = 'Reassigning to a const caused an ERROR! It should not!!!'; |
| 36 | var sEval = ''; |
| 37 | |
| 38 | /* |
| 39 | * Not every implementation supports const (a non-ECMA extension) |
| 40 | * For example, Rhino does not; it would generate a complile-time error. |
| 41 | * So we have to hide this so it will be detected at run-time instead. |
| 42 | */ |
| 43 | try |
| 44 | { |
| 45 | sEval = 'const one = 1'; |
| 46 | eval(sEval); |
| 47 | } |
| 48 | catch(e) |
| 49 | { |
| 50 | quit(); // if const is not supported, this testcase is over - |
| 51 | } |
| 52 | |
| 53 | |
| 54 | status = inSection(1); |
| 55 | try |
| 56 | { |
| 57 | /* |
| 58 | * Redeclaration of const should be a compile-time error. |
| 59 | * Hide so it will be detected at run-time. |
| 60 | */ |
| 61 | sEval = 'const one = 2;'; |
| 62 | eval(sEval); |
| 63 | |
| 64 | expect = ''; // we shouldn't reach this line |
| 65 | actual = cnFAIL_1; |
| 66 | addThis(); |
| 67 | } |
| 68 | catch(e) |
| 69 | { |
| 70 | // good - we should be here. |
| 71 | actual = expect; |
| 72 | addThis(); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | status = inSection(2); |
| 77 | try |
| 78 | { |
| 79 | /* |
| 80 | * Reassignment to a const should be NOT be an error, per ECMA. |
| 81 | */ |
| 82 | one = 2; |
| 83 | actual = expect; // good: no error was generated |
| 84 | addThis(); |
| 85 | |
| 86 | // although no error, the assignment should have been ignored - |
| 87 | status = inSection(3); |
| 88 | actual = one; |
| 89 | expect = 1; |
| 90 | addThis(); |
| 91 | |
| 92 | // the value of the EXPRESSION, however, is the value of the r.h.s. - |
| 93 | status = inSection(4); |
| 94 | actual = (one = 2); |
| 95 | expect = 2; |
| 96 | addThis(); |
| 97 | } |
| 98 | |
| 99 | catch(e) |
| 100 | { |
| 101 | // BAD - we shouldn't be here |
| 102 | expect = ''; |
| 103 | actual = cnFAIL_2; |
| 104 | addThis(); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | //----------------------------------------------------------------------------- |
| 110 | test(); |
| 111 | //----------------------------------------------------------------------------- |
| 112 | |
| 113 | |
| 114 | function addThis() |
| 115 | { |
| 116 | statusitems[UBound] = status; |
| 117 | actualvalues[UBound] = actual; |
| 118 | expectedvalues[UBound] = expect; |
| 119 | UBound++; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | function test() |
| 124 | { |
| 125 | enterFunc ('test'); |
| 126 | printBugNumber(BUGNUMBER); |
| 127 | printStatus (summary); |
| 128 | |
| 129 | for (var i = 0; i < UBound; i++) |
| 130 | { |
| 131 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 132 | } |
| 133 | |
| 134 | exitFunc ('test'); |
| 135 | } |