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
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.
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.
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
32 // This code is based on:
33 // http://trac.webkit.org/export/89109/trunk/LayoutTests/fast/js/script-tests/keywords-and-reserved_words.js
38 eval("var " + x + ";");
46 function isStrictKeyword(x)
49 eval("'use strict'; var "+x+";");
57 function classifyIdentifier(x)
60 // All non-strict keywords are also keywords in strict code.
61 if (!isStrictKeyword(x)) {
67 // Check for strict mode future reserved words.
68 if (isStrictKeyword(x)) {
75 function testKeyword(word) {
77 assertEquals("keyword", classifyIdentifier(word));
79 // Simple use of a keyword
80 assertThrows("var " + word + " = 1;", SyntaxError);
82 assertThrows("typeof (" + word + ");", SyntaxError);
85 // object literal properties
86 eval("var x = { " + word + " : 42 };");
87 eval("var x = { get " + word + " () {} };");
88 eval("var x = { set " + word + " (value) {} };");
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) { } };");
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);
104 assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError);
107 // Not keywords - these are all just identifiers.
112 // The following are reserved in ES3 but not in ES5.
117 "double", "synchronized",
119 "float", "transient",
120 "goto", "volatile" ];
122 for (var i = 0; i < identifiers.length; i++) {
123 assertEquals ("identifier", classifyIdentifier(identifiers[i]));
129 "case", "instanceof",
131 "continue", "return",
132 "debugger", "switch",
141 // In ES5 "const" is a "future reserved word" but we treat it as a keyword.
144 for (var i = 0; i < keywords.length; i++) {
145 testKeyword(keywords[i]);
148 // 7.6.1.2 Future Reserved Words (without "const")
149 var future_reserved_words = [
157 for (var i = 0; i < future_reserved_words.length; i++) {
158 testKeyword(future_reserved_words[i]);
161 // 7.6.1.2 Future Reserved Words, in strict mode only.
162 var future_strict_reserved_words = [
173 for (var i = 0; i < future_strict_reserved_words.length; i++) {
174 assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i]));
177 // More strict mode specific tests can be found in mjsunit/strict-mode.js.