Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: test/mjsunit/strict-mode.js

Issue 6606002: Merge revision 6500-6600 from bleeding_edge to the isolates branch. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/isolates/
Patch Set: '' Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 26 matching lines...) Expand all
37 }"); 37 }");
38 assertThrows("\ 38 assertThrows("\
39 function outer() {\ 39 function outer() {\
40 'use strict';\ 40 'use strict';\
41 function inner() {\n" 41 function inner() {\n"
42 + code + 42 + code +
43 "\n}\ 43 "\n}\
44 }", exception); 44 }", exception);
45 } 45 }
46 46
47 function CheckFunctionConstructorStrictMode() {
48 var args = [];
49 for (var i = 0; i < arguments.length; i ++) {
50 args[i] = arguments[i];
51 }
52 // Create non-strict function. No exception.
53 args[arguments.length] = "";
54 assertDoesNotThrow(function() {
55 Function.apply(this, args);
56 });
57 // Create strict mode function. Exception expected.
58 args[arguments.length] = "'use strict';";
59 assertThrows(function() {
60 Function.apply(this, args);
61 }, SyntaxError);
62 }
63
47 // Incorrect 'use strict' directive. 64 // Incorrect 'use strict' directive.
48 function UseStrictEscape() { 65 (function UseStrictEscape() {
49 "use\\x20strict"; 66 "use\\x20strict";
50 with ({}) {}; 67 with ({}) {};
51 } 68 })();
52 69
53 // 'use strict' in non-directive position. 70 // 'use strict' in non-directive position.
54 function UseStrictNonDirective() { 71 (function UseStrictNonDirective() {
55 void(0); 72 void(0);
56 "use strict"; 73 "use strict";
57 with ({}) {}; 74 with ({}) {};
58 } 75 })();
59 76
60 // Multiple directives, including "use strict". 77 // Multiple directives, including "use strict".
61 assertThrows('\ 78 assertThrows('\
62 "directive 1";\ 79 "directive 1";\
63 "another directive";\ 80 "another directive";\
64 "use strict";\ 81 "use strict";\
65 "directive after strict";\ 82 "directive after strict";\
66 "and one more";\ 83 "and one more";\
67 with({}) {}', SyntaxError); 84 with({}) {}', SyntaxError);
68 85
69 // 'with' disallowed in strict mode. 86 // 'with' disallowed in strict mode.
70 CheckStrictMode("with({}) {}", SyntaxError); 87 CheckStrictMode("with({}) {}", SyntaxError);
71 88
72 // Function named 'eval'. 89 // Function named 'eval'.
73 CheckStrictMode("function eval() {}", SyntaxError) 90 CheckStrictMode("function eval() {}", SyntaxError);
74 91
75 // Function named 'arguments'. 92 // Function named 'arguments'.
76 CheckStrictMode("function arguments() {}", SyntaxError) 93 CheckStrictMode("function arguments() {}", SyntaxError);
77 94
78 // Function parameter named 'eval'. 95 // Function parameter named 'eval'.
79 CheckStrictMode("function foo(a, b, eval, c, d) {}", SyntaxError) 96 CheckStrictMode("function foo(a, b, eval, c, d) {}", SyntaxError);
80 97
81 // Function parameter named 'arguments'. 98 // Function parameter named 'arguments'.
82 CheckStrictMode("function foo(a, b, arguments, c, d) {}", SyntaxError) 99 CheckStrictMode("function foo(a, b, arguments, c, d) {}", SyntaxError);
83 100
84 // Property accessor parameter named 'eval'. 101 // Property accessor parameter named 'eval'.
85 CheckStrictMode("var o = { set foo(eval) {} }", SyntaxError) 102 CheckStrictMode("var o = { set foo(eval) {} }", SyntaxError);
86 103
87 // Property accessor parameter named 'arguments'. 104 // Property accessor parameter named 'arguments'.
88 CheckStrictMode("var o = { set foo(arguments) {} }", SyntaxError) 105 CheckStrictMode("var o = { set foo(arguments) {} }", SyntaxError);
89 106
90 // Duplicate function parameter name. 107 // Duplicate function parameter name.
91 CheckStrictMode("function foo(a, b, c, d, b) {}", SyntaxError) 108 CheckStrictMode("function foo(a, b, c, d, b) {}", SyntaxError);
109
110 // Function constructor: eval parameter name.
111 CheckFunctionConstructorStrictMode("eval");
112
113 // Function constructor: arguments parameter name.
114 CheckFunctionConstructorStrictMode("arguments");
115
116 // Function constructor: duplicate parameter name.
117 CheckFunctionConstructorStrictMode("a", "b", "c", "b");
118 CheckFunctionConstructorStrictMode("a,b,c,b");
92 119
93 // catch(eval) 120 // catch(eval)
94 CheckStrictMode("try{}catch(eval){};", SyntaxError) 121 CheckStrictMode("try{}catch(eval){};", SyntaxError);
95 122
96 // catch(arguments) 123 // catch(arguments)
97 CheckStrictMode("try{}catch(arguments){};", SyntaxError) 124 CheckStrictMode("try{}catch(arguments){};", SyntaxError);
98 125
99 // var eval 126 // var eval
100 CheckStrictMode("var eval;", SyntaxError) 127 CheckStrictMode("var eval;", SyntaxError);
101 128
102 // var arguments 129 // var arguments
103 CheckStrictMode("var arguments;", SyntaxError) 130 CheckStrictMode("var arguments;", SyntaxError);
104 131
105 // Strict mode applies to the function in which the directive is used.. 132 // Strict mode applies to the function in which the directive is used..
106 assertThrows('\ 133 assertThrows('\
107 function foo(eval) {\ 134 function foo(eval) {\
108 "use strict";\ 135 "use strict";\
109 }', SyntaxError); 136 }', SyntaxError);
110 137
111 // Strict mode doesn't affect the outer stop of strict code. 138 // Strict mode doesn't affect the outer stop of strict code.
112 function NotStrict(eval) { 139 (function NotStrict(eval) {
113 function Strict() { 140 function Strict() {
114 "use strict"; 141 "use strict";
115 } 142 }
116 with ({}) {}; 143 with ({}) {};
117 } 144 })();
118 145
119 // Octal literal 146 // Octal literal
120 CheckStrictMode("var x = 012"); 147 CheckStrictMode("var x = 012");
121 CheckStrictMode("012"); 148 CheckStrictMode("012");
122 CheckStrictMode("'Hello octal\\032'"); 149 CheckStrictMode("'Hello octal\\032'");
123 CheckStrictMode("function octal() { return 012; }"); 150 CheckStrictMode("function octal() { return 012; }");
124 CheckStrictMode("function octal() { return '\\032'; }"); 151 CheckStrictMode("function octal() { return '\\032'; }");
125 152
153 (function ValidEscape() {
154 "use strict";
155 var x = '\0';
156 var y = "\0";
157 })();
158
126 // Octal before "use strict" 159 // Octal before "use strict"
127 assertThrows('\ 160 assertThrows('\
128 function strict() {\ 161 function strict() {\
129 "octal\\032directive";\ 162 "octal\\032directive";\
130 "use strict";\ 163 "use strict";\
131 }', SyntaxError); 164 }', SyntaxError);
132 165
133 // Duplicate data properties. 166 // Duplicate data properties.
134 CheckStrictMode("var x = { dupe : 1, nondupe: 3, dupe : 2 };", SyntaxError) 167 CheckStrictMode("var x = { dupe : 1, nondupe: 3, dupe : 2 };", SyntaxError);
135 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, '1234' : 3 };", SyntaxError) 168 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, '1234' : 3 };", SyntaxError);
136 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, 1234 : 3 };", SyntaxError) 169 CheckStrictMode("var x = { '1234' : 1, '2345' : 2, 1234 : 3 };", SyntaxError);
137 CheckStrictMode("var x = { 3.14 : 1, 2.71 : 2, 3.14 : 3 };", SyntaxError) 170 CheckStrictMode("var x = { 3.14 : 1, 2.71 : 2, 3.14 : 3 };", SyntaxError);
138 CheckStrictMode("var x = { 3.14 : 1, '3.14' : 2 };", SyntaxError) 171 CheckStrictMode("var x = { 3.14 : 1, '3.14' : 2 };", SyntaxError);
139 CheckStrictMode("var x = { 123: 1, 123.00000000000000000000000000000000000000000 000000000000000000000000001 : 2 }", SyntaxError) 172 CheckStrictMode("var x = { 123: 1, 123.00000000000000000000000000000000000000000 000000000000000000000000001 : 2 }", SyntaxError);
140 173
141 // Non-conflicting data properties. 174 // Non-conflicting data properties.
142 function StrictModeNonDuplicate() { 175 (function StrictModeNonDuplicate() {
143 "use strict"; 176 "use strict";
144 var x = { 123 : 1, "0123" : 2 }; 177 var x = { 123 : 1, "0123" : 2 };
145 var x = { 123: 1, '123.0000000000000000000000000000000000000000000000000000000 0000000000001' : 2 } 178 var x = { 123: 1, '123.0000000000000000000000000000000000000000000000000000000 0000000000001' : 2 }
146 } 179 })();
147
148 //CheckStrictMode("", SyntaxError)
149 180
150 // Two getters (non-strict) 181 // Two getters (non-strict)
151 assertThrows("var x = { get foo() { }, get foo() { } };", SyntaxError) 182 assertThrows("var x = { get foo() { }, get foo() { } };", SyntaxError);
152 assertThrows("var x = { get foo(){}, get 'foo'(){}};", SyntaxError) 183 assertThrows("var x = { get foo(){}, get 'foo'(){}};", SyntaxError);
153 assertThrows("var x = { get 12(){}, get '12'(){}};", SyntaxError) 184 assertThrows("var x = { get 12(){}, get '12'(){}};", SyntaxError);
154 185
155 // Two setters (non-strict) 186 // Two setters (non-strict)
156 assertThrows("var x = { set foo(v) { }, set foo(v) { } };", SyntaxError) 187 assertThrows("var x = { set foo(v) { }, set foo(v) { } };", SyntaxError);
157 assertThrows("var x = { set foo(v) { }, set 'foo'(v) { } };", SyntaxError) 188 assertThrows("var x = { set foo(v) { }, set 'foo'(v) { } };", SyntaxError);
158 assertThrows("var x = { set 13(v) { }, set '13'(v) { } };", SyntaxError) 189 assertThrows("var x = { set 13(v) { }, set '13'(v) { } };", SyntaxError);
159 190
160 // Setter and data (non-strict) 191 // Setter and data (non-strict)
161 assertThrows("var x = { foo: 'data', set foo(v) { } };", SyntaxError) 192 assertThrows("var x = { foo: 'data', set foo(v) { } };", SyntaxError);
162 assertThrows("var x = { set foo(v) { }, foo: 'data' };", SyntaxError) 193 assertThrows("var x = { set foo(v) { }, foo: 'data' };", SyntaxError);
163 assertThrows("var x = { foo: 'data', set 'foo'(v) { } };", SyntaxError) 194 assertThrows("var x = { foo: 'data', set 'foo'(v) { } };", SyntaxError);
164 assertThrows("var x = { set foo(v) { }, 'foo': 'data' };", SyntaxError) 195 assertThrows("var x = { set foo(v) { }, 'foo': 'data' };", SyntaxError);
165 assertThrows("var x = { 'foo': 'data', set foo(v) { } };", SyntaxError) 196 assertThrows("var x = { 'foo': 'data', set foo(v) { } };", SyntaxError);
166 assertThrows("var x = { set 'foo'(v) { }, foo: 'data' };", SyntaxError) 197 assertThrows("var x = { set 'foo'(v) { }, foo: 'data' };", SyntaxError);
167 assertThrows("var x = { 'foo': 'data', set 'foo'(v) { } };", SyntaxError) 198 assertThrows("var x = { 'foo': 'data', set 'foo'(v) { } };", SyntaxError);
168 assertThrows("var x = { set 'foo'(v) { }, 'foo': 'data' };", SyntaxError) 199 assertThrows("var x = { set 'foo'(v) { }, 'foo': 'data' };", SyntaxError);
169 assertThrows("var x = { 12: 1, set '12'(v){}};", SyntaxError); 200 assertThrows("var x = { 12: 1, set '12'(v){}};", SyntaxError);
170 assertThrows("var x = { 12: 1, set 12(v){}};", SyntaxError); 201 assertThrows("var x = { 12: 1, set 12(v){}};", SyntaxError);
171 assertThrows("var x = { '12': 1, set '12'(v){}};", SyntaxError); 202 assertThrows("var x = { '12': 1, set '12'(v){}};", SyntaxError);
172 assertThrows("var x = { '12': 1, set 12(v){}};", SyntaxError); 203 assertThrows("var x = { '12': 1, set 12(v){}};", SyntaxError);
173 204
174 // Getter and data (non-strict) 205 // Getter and data (non-strict)
175 assertThrows("var x = { foo: 'data', get foo() { } };", SyntaxError) 206 assertThrows("var x = { foo: 'data', get foo() { } };", SyntaxError);
176 assertThrows("var x = { get foo() { }, foo: 'data' };", SyntaxError) 207 assertThrows("var x = { get foo() { }, foo: 'data' };", SyntaxError);
177 assertThrows("var x = { 'foo': 'data', get foo() { } };", SyntaxError) 208 assertThrows("var x = { 'foo': 'data', get foo() { } };", SyntaxError);
178 assertThrows("var x = { get 'foo'() { }, 'foo': 'data' };", SyntaxError) 209 assertThrows("var x = { get 'foo'() { }, 'foo': 'data' };", SyntaxError);
179 assertThrows("var x = { '12': 1, get '12'(){}};", SyntaxError); 210 assertThrows("var x = { '12': 1, get '12'(){}};", SyntaxError);
180 assertThrows("var x = { '12': 1, get 12(){}};", SyntaxError); 211 assertThrows("var x = { '12': 1, get 12(){}};", SyntaxError);
212
213 // Assignment to eval or arguments
214 CheckStrictMode("function strict() { eval = undefined; }", SyntaxError);
215 CheckStrictMode("function strict() { arguments = undefined; }", SyntaxError);
216 CheckStrictMode("function strict() { print(eval = undefined); }", SyntaxError);
217 CheckStrictMode("function strict() { print(arguments = undefined); }", SyntaxErr or);
218 CheckStrictMode("function strict() { var x = eval = undefined; }", SyntaxError);
219 CheckStrictMode("function strict() { var x = arguments = undefined; }", SyntaxEr ror);
220
221 // Compound assignment to eval or arguments
222 CheckStrictMode("function strict() { eval *= undefined; }", SyntaxError);
223 CheckStrictMode("function strict() { arguments /= undefined; }", SyntaxError);
224 CheckStrictMode("function strict() { print(eval %= undefined); }", SyntaxError);
225 CheckStrictMode("function strict() { print(arguments %= undefined); }", SyntaxEr ror);
226 CheckStrictMode("function strict() { var x = eval += undefined; }", SyntaxError) ;
227 CheckStrictMode("function strict() { var x = arguments -= undefined; }", SyntaxE rror);
228 CheckStrictMode("function strict() { eval <<= undefined; }", SyntaxError);
229 CheckStrictMode("function strict() { arguments >>= undefined; }", SyntaxError);
230 CheckStrictMode("function strict() { print(eval >>>= undefined); }", SyntaxError );
231 CheckStrictMode("function strict() { print(arguments &= undefined); }", SyntaxEr ror);
232 CheckStrictMode("function strict() { var x = eval ^= undefined; }", SyntaxError) ;
233 CheckStrictMode("function strict() { var x = arguments |= undefined; }", SyntaxE rror);
234
235 // Postfix increment with eval or arguments
236 CheckStrictMode("function strict() { eval++; }", SyntaxError);
237 CheckStrictMode("function strict() { arguments++; }", SyntaxError);
238 CheckStrictMode("function strict() { print(eval++); }", SyntaxError);
239 CheckStrictMode("function strict() { print(arguments++); }", SyntaxError);
240 CheckStrictMode("function strict() { var x = eval++; }", SyntaxError);
241 CheckStrictMode("function strict() { var x = arguments++; }", SyntaxError);
242
243 // Postfix decrement with eval or arguments
244 CheckStrictMode("function strict() { eval--; }", SyntaxError);
245 CheckStrictMode("function strict() { arguments--; }", SyntaxError);
246 CheckStrictMode("function strict() { print(eval--); }", SyntaxError);
247 CheckStrictMode("function strict() { print(arguments--); }", SyntaxError);
248 CheckStrictMode("function strict() { var x = eval--; }", SyntaxError);
249 CheckStrictMode("function strict() { var x = arguments--; }", SyntaxError);
250
251 // Prefix increment with eval or arguments
252 CheckStrictMode("function strict() { ++eval; }", SyntaxError);
253 CheckStrictMode("function strict() { ++arguments; }", SyntaxError);
254 CheckStrictMode("function strict() { print(++eval); }", SyntaxError);
255 CheckStrictMode("function strict() { print(++arguments); }", SyntaxError);
256 CheckStrictMode("function strict() { var x = ++eval; }", SyntaxError);
257 CheckStrictMode("function strict() { var x = ++arguments; }", SyntaxError);
258
259 // Prefix decrement with eval or arguments
260 CheckStrictMode("function strict() { --eval; }", SyntaxError);
261 CheckStrictMode("function strict() { --arguments; }", SyntaxError);
262 CheckStrictMode("function strict() { print(--eval); }", SyntaxError);
263 CheckStrictMode("function strict() { print(--arguments); }", SyntaxError);
264 CheckStrictMode("function strict() { var x = --eval; }", SyntaxError);
265 CheckStrictMode("function strict() { var x = --arguments; }", SyntaxError);
266
267 // Prefix unary operators other than delete, ++, -- are valid in strict mode
268 (function StrictModeUnaryOperators() {
269 "use strict";
270 var x = [void eval, typeof eval, +eval, -eval, ~eval, !eval];
271 var y = [void arguments, typeof arguments,
272 +arguments, -arguments, ~arguments, !arguments];
273 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698