Home | History | Annotate | Download | only in mjsunit
      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/keywords-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   // setter parameter
    104   assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError);
    105 }
    106 
    107 // Not keywords - these are all just identifiers.
    108 var identifiers = [
    109   "x",            "keyword",
    110   "id",           "strict",
    111   "identifier",   "use",
    112   // The following are reserved in ES3 but not in ES5.
    113   "abstract",     "int",
    114   "boolean",      "long",
    115   "byte",         "native",
    116   "char",         "short",
    117   "double",       "synchronized",
    118   "final",        "throws",
    119   "float",        "transient",
    120   "goto",         "volatile" ];
    121 
    122 for (var i = 0; i < identifiers.length; i++) {
    123   assertEquals ("identifier", classifyIdentifier(identifiers[i]));
    124 }
    125 
    126 // 7.6.1.1 Keywords
    127 var keywords = [
    128   "break",        "in",
    129   "case",         "instanceof",
    130   "catch",        "new",
    131   "continue",     "return",
    132   "debugger",     "switch",
    133   "default",      "this",
    134   "delete",       "throw",
    135   "do",           "try",
    136   "else",         "typeof",
    137   "finally",      "var",
    138   "for",          "void",
    139   "function",     "while",
    140   "if",           "with",
    141   // In ES5 "const" is a "future reserved word" but we treat it as a keyword.
    142   "const" ];
    143 
    144 for (var i = 0; i < keywords.length; i++) {
    145   testKeyword(keywords[i]);
    146 }
    147 
    148 // 7.6.1.2 Future Reserved Words (without "const")
    149 var future_reserved_words = [
    150   "class",
    151   "enum",
    152   "export",
    153   "extends",
    154   "import",
    155   "super" ];
    156 
    157 for (var i = 0; i < future_reserved_words.length; i++) {
    158   testKeyword(future_reserved_words[i]);
    159 }
    160 
    161 // 7.6.1.2 Future Reserved Words, in strict mode only.
    162 var future_strict_reserved_words = [
    163   "implements",
    164   "interface",
    165   "let",
    166   "package",
    167   "private",
    168   "protected",
    169   "public",
    170   "static",
    171   "yield" ];
    172 
    173 for (var i = 0; i < future_strict_reserved_words.length; i++) {
    174   assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i]));
    175 }
    176 
    177 // More strict mode specific tests can be found in mjsunit/strict-mode.js.
    178