Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 // Test proper handling of keywords, future reserved words and | |
| 29 // future reserved words in strict mode as specific by 7.6.1 and 7.6.2 | |
| 30 // in ECMA-262. | |
| 31 | |
| 32 // This code is based on: | |
| 33 // http://trac.webkit.org/export/89109/trunk/LayoutTests/fast/js/script-tests/ke ywords-and-reserved_words.js | |
| 34 | |
| 35 function isKeyword(x) | |
| 36 { | |
| 37 try { | |
| 38 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.
| |
| 39 } catch(e) { | |
| 40 return true; | |
| 41 } | |
| 42 | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 function isStrictKeyword(x) | |
| 47 { | |
| 48 try { | |
| 49 eval("'use strict'; var "+x+";"); | |
| 50 } catch(e) { | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 function classifyIdentifier(x) | |
| 58 { | |
| 59 if (isKeyword(x)) { | |
| 60 // All non-strict keywords are also keywords in strict code. | |
| 61 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.
| |
| 62 return "ERROR"; | |
|
Lasse Reichstein
2011/06/22 20:29:33
and indent by 2.
Steven
2011/06/24 11:34:59
Done.
| |
| 63 return "keyword"; | |
| 64 } | |
| 65 | |
| 66 // Check for strict mode future reserved words. | |
| 67 if (isStrictKeyword(x)) | |
|
Lasse Reichstein
2011/06/22 20:29:33
ditto here.
Steven
2011/06/24 11:34:59
Done.
| |
| 68 return "strict"; | |
| 69 | |
| 70 return "identifier"; | |
| 71 } | |
| 72 | |
| 73 function testKeyword(word) { | |
| 74 // Classify word | |
| 75 assertEquals ("keyword", classifyIdentifier(word)); | |
|
Lasse Reichstein
2011/06/22 20:29:33
Extra space after "assertEquals".
Steven
2011/06/24 11:34:59
Done.
| |
| 76 | |
| 77 // Simple use of a keyword | |
| 78 assertThrows("var " + word + " = 1;", SyntaxError); | |
| 79 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.
| |
| 80 assertThrows("typeof (" + word + ");", SyntaxError); | |
| 81 | |
| 82 // object literal properties | |
| 83 eval("var x = { " + word + " : 42 };"); | |
| 84 eval("var x = { get " + word + " () {} };"); | |
| 85 eval("var x = { set " + word + " (value) {} };"); | |
| 86 | |
| 87 // object literal with string literal property names | |
| 88 eval("var x = { '" + word + "' : 42 };"); | |
| 89 eval("var x = { get '" + word + "' () { } };"); | |
| 90 eval("var x = { set '" + word + "' (value) { } };"); | |
| 91 | |
| 92 // Function names and arguments | |
| 93 assertThrows("function " + word + " () { }", SyntaxError); | |
| 94 assertThrows("function foo (" + word + ") {}", SyntaxError); | |
| 95 assertThrows("function foo (a, " + word + ") { }", SyntaxError); | |
| 96 assertThrows("function foo (" + word + ", a) { }", SyntaxError); | |
| 97 assertThrows("function foo (a, " + word + ", b) { }", SyntaxError); | |
| 98 assertThrows("var foo = function (" + word + ") { }", SyntaxError); | |
| 99 | |
| 100 // get/set | |
| 101 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 :).
| |
| 102 assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError); | |
| 103 } | |
| 104 | |
| 105 // Not keywords - these are all just identifiers. | |
| 106 var identifiers = [ | |
| 107 "x", "final", | |
| 108 "id", "float", | |
| 109 "identifier", "goto", | |
| 110 "keyword", "int", | |
| 111 "strict", "long", | |
| 112 "use", "native", | |
| 113 "abstract", "short", | |
| 114 "boolean", "synchronized", | |
| 115 "byte", "throws", | |
| 116 "char", "transient", | |
| 117 "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
| |
| 118 | |
| 119 for (var i = 0; i < identifiers.length; i++) { | |
| 120 assertEquals ("identifier", classifyIdentifier(identifiers[i])); | |
| 121 } | |
| 122 | |
| 123 // 7.6.1.1 Keywords | |
| 124 var keywords = [ | |
| 125 "break", "in", | |
| 126 "case", "instanceof", | |
| 127 "catch", "new", | |
| 128 "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.
| |
| 129 "debugger", "switch", | |
| 130 "default", "this", | |
| 131 "delete", "throw", | |
| 132 "do", "try", | |
| 133 "else", "typeof", | |
| 134 "finally", "var", | |
| 135 "for", "void", | |
| 136 "function", "while", | |
| 137 "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
| |
| 138 | |
| 139 for (var i = 0; i < keywords.length; i++) { | |
| 140 testKeyword(keywords[i]); | |
| 141 } | |
| 142 | |
| 143 // 7.6.1.2 Future Reserved Words | |
| 144 var future_reserved_words = [ | |
| 145 "class", | |
| 146 "enum", | |
| 147 "export", | |
| 148 "extends", | |
| 149 "import", | |
| 150 "super" ]; | |
| 151 | |
| 152 for (var i = 0; i < future_reserved_words.length; i++) { | |
| 153 testKeyword(future_reserved_words[i]); | |
| 154 } | |
| 155 | |
| 156 // 7.6.1.2 Future Reserved Words, in strict mode only. | |
| 157 var future_strict_reserved_words = [ | |
| 158 "implements", | |
| 159 "interface", | |
| 160 "let", | |
| 161 "package", | |
| 162 "private", | |
| 163 "protected", | |
| 164 "public", | |
| 165 "static", | |
| 166 "yield" ]; | |
| 167 | |
| 168 for (var i = 0; i < future_strict_reserved_words.length; i++) { | |
| 169 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.
| |
| 170 } | |
| OLD | NEW |