|
1 /* |
|
2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /** |
|
25 * NASHORN-207 : implement strict mode |
|
26 * |
|
27 * @test |
|
28 * @run |
|
29 */ |
|
30 |
|
31 'use strict'; |
|
32 |
|
33 // cannot delete a variable |
|
34 try { |
|
35 eval("var fooVar = 3; delete fooVar;"); |
|
36 fail("#1 should have thrown SyntaxError"); |
|
37 } catch (e) { |
|
38 if (! (e instanceof SyntaxError)) { |
|
39 fail("#2 SyntaxError expected but got " + e); |
|
40 } |
|
41 } |
|
42 |
|
43 // cannot delete function parameter variable |
|
44 try { |
|
45 eval("function func(a) { delete a; }; func(2);"); |
|
46 fail("#3 should have thrown SyntaxError"); |
|
47 } catch(e) { |
|
48 if (! (e instanceof SyntaxError)) { |
|
49 fail("#4 SyntaxError expected but got " + e); |
|
50 } |
|
51 } |
|
52 |
|
53 // assignment can't be used to define as new variable |
|
54 try { |
|
55 bar = 3; |
|
56 fail("#5 should have thrown ReferenceError"); |
|
57 } catch (e) { |
|
58 if (! (e instanceof ReferenceError)) { |
|
59 fail("#6 ReferenceError expected but got " + e); |
|
60 } |
|
61 } |
|
62 |
|
63 // repeated property definition is not allowed in an object literal |
|
64 try { |
|
65 eval("var obj = { foo: 33, foo: 44 }"); |
|
66 fail("#7 should have thrown SyntaxError"); |
|
67 } catch(e) { |
|
68 if (! (e instanceof SyntaxError)) { |
|
69 fail("#8 SyntaxError expected but got " + e); |
|
70 } |
|
71 } |
|
72 |
|
73 // can't assign to "eval" |
|
74 try { |
|
75 eval("var eval = 3"); |
|
76 fail("#9 should have thrown SyntaxError"); |
|
77 } catch(e) { |
|
78 if (! (e instanceof SyntaxError)) { |
|
79 fail("#10 SyntaxError expected but got " + e); |
|
80 } |
|
81 } |
|
82 |
|
83 // can't assign to 'arguments' |
|
84 try { |
|
85 eval("function func() { arguments = 34; }"); |
|
86 fail("#11 should have thrown SyntaxError"); |
|
87 } catch(e) { |
|
88 if (! (e instanceof SyntaxError)) { |
|
89 fail("#12 SyntaxError expected but got " + e); |
|
90 } |
|
91 } |
|
92 |
|
93 // can't delete non-configurable properties |
|
94 try { |
|
95 delete Object.prototype; |
|
96 fail("#13 should have thrown TypeError"); |
|
97 } catch (e) { |
|
98 if (! (e instanceof TypeError)) { |
|
99 fail("#14 TypeError expected but got " + e); |
|
100 } |
|
101 } |
|
102 |
|
103 // cannot re-use function parameter name |
|
104 try { |
|
105 eval("function func(a, a) {}"); |
|
106 fail("#15 should have thrown SyntaxError"); |
|
107 } catch (e) { |
|
108 if (! (e instanceof SyntaxError)) { |
|
109 fail("#16 SyntaxError expected but got " + e); |
|
110 } |
|
111 } |
|
112 |
|
113 // strict eval creates own scope and caller's scope is untouched! |
|
114 eval("var myVar = 4;"); |
|
115 if (typeof myVar !== 'undefined') { |
|
116 fail("#17 eval var 'myVar' created in global scope"); |
|
117 } |
|
118 |
|
119 eval("function myNewFunc() {}"); |
|
120 if (typeof myNewFunc !== 'undefined') { |
|
121 fail("#18 eval function 'myNewFunc' created in global scope"); |
|
122 } |
|
123 |
|
124 // no octal in strict mode |
|
125 try { |
|
126 eval("var x = 010;"); |
|
127 fail("#19 should have thrown SyntaxError"); |
|
128 } catch (e) { |
|
129 if (! (e instanceof SyntaxError)) { |
|
130 fail("#20 SyntaxError expected but got " + e); |
|
131 } |
|
132 } |