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 + ";"); |
| 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)) { |
| 62 return "ERROR"; |
| 63 } |
| 64 return "keyword"; |
| 65 } |
| 66 |
| 67 // Check for strict mode future reserved words. |
| 68 if (isStrictKeyword(x)) { |
| 69 return "strict"; |
| 70 } |
| 71 |
| 72 return "identifier"; |
| 73 } |
| 74 |
| 75 function testKeyword(word) { |
| 76 // Classify word |
| 77 assertEquals("keyword", classifyIdentifier(word)); |
| 78 |
| 79 // Simple use of a keyword |
| 80 assertThrows("var " + word + " = 1;", SyntaxError); |
| 81 if (word != "this") { |
| 82 assertThrows("typeof (" + word + ");", SyntaxError); |
| 83 } |
| 84 |
| 85 // object literal properties |
| 86 eval("var x = { " + word + " : 42 };"); |
| 87 eval("var x = { get " + word + " () {} };"); |
| 88 eval("var x = { set " + word + " (value) {} };"); |
| 89 |
| 90 // object literal with string literal property names |
| 91 eval("var x = { '" + word + "' : 42 };"); |
| 92 eval("var x = { get '" + word + "' () { } };"); |
| 93 eval("var x = { set '" + word + "' (value) { } };"); |
| 94 |
| 95 // Function names and arguments |
| 96 assertThrows("function " + word + " () { }", SyntaxError); |
| 97 assertThrows("function foo (" + word + ") {}", SyntaxError); |
| 98 assertThrows("function foo (a, " + word + ") { }", SyntaxError); |
| 99 assertThrows("function foo (" + word + ", a) { }", SyntaxError); |
| 100 assertThrows("function foo (a, " + word + ", b) { }", SyntaxError); |
| 101 assertThrows("var foo = function (" + word + ") { }", SyntaxError); |
| 102 |
| 103 // get/set |
| 104 // Getters don't have formal parameters, so particularly keywords |
| 105 // are disallowed. |
| 106 assertThrows("var x = { get foo(" + word + ") { } };", SyntaxError); |
| 107 assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError); |
| 108 } |
| 109 |
| 110 // Not keywords - these are all just identifiers. |
| 111 var identifiers = [ |
| 112 "x", "keyword", |
| 113 "id", "strict", |
| 114 "identifier", "use", |
| 115 // The following are reserved in ES3 but not in ES5. |
| 116 "abstract", "int", |
| 117 "boolean", "long", |
| 118 "byte", "native", |
| 119 "char", "short", |
| 120 "double", "synchronized", |
| 121 "final", "throws", |
| 122 "float", "transient", |
| 123 "goto", "volatile" ]; |
| 124 |
| 125 for (var i = 0; i < identifiers.length; i++) { |
| 126 assertEquals ("identifier", classifyIdentifier(identifiers[i])); |
| 127 } |
| 128 |
| 129 // 7.6.1.1 Keywords |
| 130 var keywords = [ |
| 131 "break", "in", |
| 132 "case", "instanceof", |
| 133 "catch", "new", |
| 134 "continue", "return", |
| 135 "debugger", "switch", |
| 136 "default", "this", |
| 137 "delete", "throw", |
| 138 "do", "try", |
| 139 "else", "typeof", |
| 140 "finally", "var", |
| 141 "for", "void", |
| 142 "function", "while", |
| 143 "if", "with", |
| 144 // In ES5 "const" is a "future reserved word" but we treat it as a keyword. |
| 145 "const" ]; |
| 146 |
| 147 for (var i = 0; i < keywords.length; i++) { |
| 148 testKeyword(keywords[i]); |
| 149 } |
| 150 |
| 151 // 7.6.1.2 Future Reserved Words (without "const") |
| 152 var future_reserved_words = [ |
| 153 "class", |
| 154 "enum", |
| 155 "export", |
| 156 "extends", |
| 157 "import", |
| 158 "super" ]; |
| 159 |
| 160 for (var i = 0; i < future_reserved_words.length; i++) { |
| 161 testKeyword(future_reserved_words[i]); |
| 162 } |
| 163 |
| 164 // 7.6.1.2 Future Reserved Words, in strict mode only. |
| 165 var future_strict_reserved_words = [ |
| 166 "implements", |
| 167 "interface", |
| 168 "let", |
| 169 "package", |
| 170 "private", |
| 171 "protected", |
| 172 "public", |
| 173 "static", |
| 174 "yield" ]; |
| 175 |
| 176 for (var i = 0; i < future_strict_reserved_words.length; i++) { |
| 177 assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i])); |
| 178 } |
| 179 |
| 180 // More strict mode specific tests can be found in mjsunit/strict-mode.js. |
OLD | NEW |