Index: test/mjsunit/keywords-and-reserved_words.js |
diff --git a/test/mjsunit/keywords-and-reserved_words.js b/test/mjsunit/keywords-and-reserved_words.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f32539eff5038100386e8f888cfbca84aeec4a39 |
--- /dev/null |
+++ b/test/mjsunit/keywords-and-reserved_words.js |
@@ -0,0 +1,170 @@ |
+// Copyright 2011 the V8 project authors. All rights reserved. |
+// Redistribution and use in source and binary forms, with or without |
+// modification, are permitted provided that the following conditions are |
+// met: |
+// |
+// * Redistributions of source code must retain the above copyright |
+// notice, this list of conditions and the following disclaimer. |
+// * Redistributions in binary form must reproduce the above |
+// copyright notice, this list of conditions and the following |
+// disclaimer in the documentation and/or other materials provided |
+// with the distribution. |
+// * Neither the name of Google Inc. nor the names of its |
+// contributors may be used to endorse or promote products derived |
+// from this software without specific prior written permission. |
+// |
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+// Test proper handling of keywords, future reserved words and |
+// future reserved words in strict mode as specific by 7.6.1 and 7.6.2 |
+// in ECMA-262. |
+ |
+// This code is based on: |
+// http://trac.webkit.org/export/89109/trunk/LayoutTests/fast/js/script-tests/keywords-and-reserved_words.js |
+ |
+function isKeyword(x) |
+{ |
+ try { |
+ eval("var "+x+";"); |
Lasse Reichstein
2011/06/22 20:29:33
Spaces around '+'.
Even though I think readabilit
Steven
2011/06/24 11:34:59
Done.
|
+ } catch(e) { |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+function isStrictKeyword(x) |
+{ |
+ try { |
+ eval("'use strict'; var "+x+";"); |
+ } catch(e) { |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+function classifyIdentifier(x) |
+{ |
+ if (isKeyword(x)) { |
+ // All non-strict keywords are also keywords in strict code. |
+ if (!isStrictKeyword(x)) |
Lasse Reichstein
2011/06/22 20:29:33
Use '{' and '}' when the if statement is not on on
Steven
2011/06/24 11:34:59
Done.
|
+ return "ERROR"; |
Lasse Reichstein
2011/06/22 20:29:33
and indent by 2.
Steven
2011/06/24 11:34:59
Done.
|
+ return "keyword"; |
+ } |
+ |
+ // Check for strict mode future reserved words. |
+ if (isStrictKeyword(x)) |
Lasse Reichstein
2011/06/22 20:29:33
ditto here.
Steven
2011/06/24 11:34:59
Done.
|
+ return "strict"; |
+ |
+ return "identifier"; |
+} |
+ |
+function testKeyword(word) { |
+ // Classify word |
+ assertEquals ("keyword", classifyIdentifier(word)); |
Lasse Reichstein
2011/06/22 20:29:33
Extra space after "assertEquals".
Steven
2011/06/24 11:34:59
Done.
|
+ |
+ // Simple use of a keyword |
+ assertThrows("var " + word + " = 1;", SyntaxError); |
+ if(word != "this") |
Lasse Reichstein
2011/06/22 20:29:33
Space after "if", '{' and '}' around body.
Steven
2011/06/24 11:34:59
Done.
|
+ assertThrows("typeof (" + word + ");", SyntaxError); |
+ |
+ // object literal properties |
+ eval("var x = { " + word + " : 42 };"); |
+ eval("var x = { get " + word + " () {} };"); |
+ eval("var x = { set " + word + " (value) {} };"); |
+ |
+ // object literal with string literal property names |
+ eval("var x = { '" + word + "' : 42 };"); |
+ eval("var x = { get '" + word + "' () { } };"); |
+ eval("var x = { set '" + word + "' (value) { } };"); |
+ |
+ // Function names and arguments |
+ assertThrows("function " + word + " () { }", SyntaxError); |
+ assertThrows("function foo (" + word + ") {}", SyntaxError); |
+ assertThrows("function foo (a, " + word + ") { }", SyntaxError); |
+ assertThrows("function foo (" + word + ", a) { }", SyntaxError); |
+ assertThrows("function foo (a, " + word + ", b) { }", SyntaxError); |
+ assertThrows("var foo = function (" + word + ") { }", SyntaxError); |
+ |
+ // get/set |
+ assertThrows("var x = { get foo(" + word + ") { } };", SyntaxError); |
Lasse Reichstein
2011/06/22 20:29:33
This is a syntax error for any word. Literal gette
Steven
2011/06/24 11:34:59
This was copied from strict-mode.js. Before I dele
Lasse Reichstein
2011/06/24 12:38:04
Most likely a bad cut-n-paste :).
|
+ assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError); |
+} |
+ |
+// Not keywords - these are all just identifiers. |
+var identifiers = [ |
+ "x", "final", |
+ "id", "float", |
+ "identifier", "goto", |
+ "keyword", "int", |
+ "strict", "long", |
+ "use", "native", |
+ "abstract", "short", |
+ "boolean", "synchronized", |
+ "byte", "throws", |
+ "char", "transient", |
+ "double", "volatile" ]; |
Lasse Reichstein
2011/06/22 20:29:33
Great that you check the ES3 keywords that are not
Steven
2011/06/24 11:34:59
Done. (That's from the WebKit test. Can't take the
|
+ |
+for (var i = 0; i < identifiers.length; i++) { |
+ assertEquals ("identifier", classifyIdentifier(identifiers[i])); |
+} |
+ |
+// 7.6.1.1 Keywords |
+var keywords = [ |
+ "break", "in", |
+ "case", "instanceof", |
+ "catch", "new", |
+ "continue", "return", |
Lasse Reichstein
2011/06/22 20:29:33
"const".
It's an ES5 future reserved word, so it s
Steven
2011/06/24 11:34:59
Done.
|
+ "debugger", "switch", |
+ "default", "this", |
+ "delete", "throw", |
+ "do", "try", |
+ "else", "typeof", |
+ "finally", "var", |
+ "for", "void", |
+ "function", "while", |
+ "if", "with" ]; |
Lasse Reichstein
2011/06/22 20:29:33
Currently we consider \u0069f as a valid identif
Steven
2011/06/24 11:34:59
This one is nasty. 'var \u0069f;' works but 'eval(
Lasse Reichstein
2011/06/24 12:38:04
You have string-escaping to account for, so it sho
|
+ |
+for (var i = 0; i < keywords.length; i++) { |
+ testKeyword(keywords[i]); |
+} |
+ |
+// 7.6.1.2 Future Reserved Words |
+var future_reserved_words = [ |
+ "class", |
+ "enum", |
+ "export", |
+ "extends", |
+ "import", |
+ "super" ]; |
+ |
+for (var i = 0; i < future_reserved_words.length; i++) { |
+ testKeyword(future_reserved_words[i]); |
+} |
+ |
+// 7.6.1.2 Future Reserved Words, in strict mode only. |
+var future_strict_reserved_words = [ |
+ "implements", |
+ "interface", |
+ "let", |
+ "package", |
+ "private", |
+ "protected", |
+ "public", |
+ "static", |
+ "yield" ]; |
+ |
+for (var i = 0; i < future_strict_reserved_words.length; i++) { |
+ assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i])); |
Lasse Reichstein
2011/06/22 20:29:33
Make a note that the strict mode tests are in stri
Steven
2011/06/24 11:34:59
Done.
|
+} |