strict-identifiers.pyt 8.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
# 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.

# Templatated tests with eval/arguments/future reserved words.

# ----------------------------------------------------------------------
# Constants and utility functions

reserved_words = [
  'class',
  'const', # Has other error message than other reserved words.
  'enum',
  'export',
  'extends',
  'import',
  'super'
  ]

strict_reserved_words = [
  'implements',
  'interface',
  'let',
  'package',
  'private',
  'protected',
  'public',
  'static',
  'yield'
  ]

assign_ops = {
  "=": "assign",
  "+=": "addeq",
  "-=": "subeq",
  "*=": "muleq",
  "/=": "diveq",
  "%=": "modeq",
  "&=": "andeq",
  "|=": "oreq",
  "^=": "xoreq",
  "<<=": "shleq",
  ">>=": "asreq",
  ">>>=": "lsreq"
  }


# A template that performs the same strict-mode test in different
# scopes (global scope, function scope, and nested function scope).
def StrictTemplate(name, source):
  def MakeTests(replacement, expectation):
    Template(name, '"use strict";\n' + source)(replacement, expectation)
    Template(name + '-infunc',
             'function foo() {\n "use strict";\n' + source +'\n}\n')(
              replacement, expectation)
    Template(name + '-infunc2',
             'function foo() {\n "use strict";\n  function bar() {\n' +
             source +'\n }\n}\n')(replacement, expectation)
  return MakeTests

# ----------------------------------------------------------------------
# Test templates

arg_name_own = Template("argument-name-own-$id", """
  function foo($id) {
    "use strict";
  }
""")

arg_name_nested = Template("argument-name-nested-$id", """
  function foo() {
    "use strict";
    function bar($id) { }
  }
""")

func_name_own = Template("function-name-own-$id", """
  function $id(foo) {
    "use strict";
  }
""")

func_name_nested = Template("function-name-nested-$id", """
  function foo() {
    "use strict";
    function $id(bar) { }
  }
""")

catch_var = StrictTemplate("catch-$id", """
    try { } catch ($id) { }
""")

declare_var = StrictTemplate("var-$id", """
  var $id = 42;
""")

assign_var = StrictTemplate("assign-$id-$opname", """
  var x = $id $op 42;
""")

prefix_var = StrictTemplate("prefix-$opname-$id", """
  var x = $op$id;
""")

postfix_var = StrictTemplate("postfix-$opname-$id", """
  var x = $id$op;
""")

read_var = StrictTemplate("read-reserved-$id", """
  var x = $id;
""")

137 138 139 140
setter_arg = StrictTemplate("setter-param-$id", """
  var x = {set foo($id) { }};
""")

141 142 143 144 145 146 147 148 149
label_normal = Template("label-normal-$id", """
  $id: '';
""")

label_strict = StrictTemplate("label-strict-$id", """
  $id: '';
""")

break_normal = Template("break-normal-$id", """
150
  $id: for (;false;) {
151 152 153 154 155
    break $id;
  }
""")

break_strict = StrictTemplate("break-strict-$id", """
156
  $id: for (;false;) {
157 158 159 160 161
    break $id;
  }
""")

continue_normal = Template("continue-normal-$id", """
162
  $id: for (;false;) {
163 164 165 166 167
    continue $id;
  }
""")

continue_strict = StrictTemplate("continue-strict-$id", """
168
  $id: for (;false;) {
169 170 171 172
    continue $id;
  }
""")

173 174 175 176 177 178 179 180 181 182
non_strict_use = Template("nonstrict-$id", """
  var $id = 42;
  $id++;
  $id--;
  ++$id;
  --$id;
  $id += 10;
  $id -= 10;
  try {} catch ($id) { }
  function $id($id) { }
183 184
  var x = {$id: 42};
  x = {get $id() {}, set $id(value) {}};
185 186 187 188 189 190 191 192 193 194
  function foo() { "use strict;" }
  var $id = 42;
  $id++;
  $id--;
  ++$id;
  --$id;
  $id += 10;
  $id -= 10;
  try {} catch ($id) { }
  function $id($id) { }
195 196
  x = {$id: 42};
  x = {get $id() {}, set $id(value) {}};
197
  $id: '';
198 199
""")

200 201 202 203 204 205 206 207 208 209 210 211 212 213
identifier_name_source = """
  var x = {$id: 42};
  x = {get $id() {}, set $id(value) {}};
  x.$id = 42;
  function foo() { "use strict;" }
  x = {$id: 42};
  x = {get $id() {}, set $id(value) {}};
  x.$id = 42;
"""

identifier_name = Template("identifier_name-$id", identifier_name_source)
identifier_name_strict = StrictTemplate("identifier_name_strict-$id",
                                        identifier_name_source)

214 215 216 217 218 219 220 221 222
# ----------------------------------------------------------------------
# Run tests

# eval and arguments have specific exceptions for different uses.
for id in ["eval", "arguments"]:
  arg_name_own({"id": id}, "strict_param_name")
  arg_name_nested({"id": id}, "strict_param_name")
  func_name_own({"id": id}, "strict_function_name")
  func_name_nested({"id": id}, "strict_function_name")
223
  setter_arg({"id": id}, "strict_param_name")
224 225 226 227 228 229 230 231 232
  for op in assign_ops.keys():
    assign_var({"id": id, "op":op, "opname": assign_ops[op]},
               "strict_lhs_assignment")
  catch_var({"id": id}, "strict_catch_variable")
  declare_var({"id": id}, "strict_var_name")
  prefix_var({"id": id, "op":"++", "opname":"inc"}, "strict_lhs_prefix")
  prefix_var({"id": id, "op":"--", "opname":"dec"}, "strict_lhs_prefix")
  postfix_var({"id": id, "op":"++", "opname":"inc"}, "strict_lhs_postfix")
  postfix_var({"id": id, "op":"--", "opname":"dec"}, "strict_lhs_postfix")
233 234 235 236 237 238
  label_normal({"id": id}, None)
  label_strict({"id": id}, None)
  break_normal({"id": id}, None)
  break_strict({"id": id}, None)
  continue_normal({"id": id}, None)
  continue_strict({"id": id}, None)
239 240 241 242 243 244
  non_strict_use({"id": id}, None)


# Reserved words just throw the same exception in all cases
# (with "const" being special, as usual).
for reserved_word in reserved_words + strict_reserved_words:
245 246
  if (reserved_word in strict_reserved_words):
    message = "strict_reserved_word"
247
    label_message = None
248 249
  elif (reserved_word == "const"):
    message = "unexpected_token"
250
    label_message = message
251 252
  else:
    message = "reserved_word"
253
    label_message = message
254 255
  arg_name_own({"id":reserved_word}, message)
  arg_name_nested({"id":reserved_word}, message)
256
  setter_arg({"id": reserved_word}, message)
257 258 259 260 261 262 263 264 265 266 267
  func_name_own({"id":reserved_word}, message)
  func_name_nested({"id":reserved_word}, message)
  for op in assign_ops.keys():
    assign_var({"id":reserved_word, "op":op, "opname": assign_ops[op]}, message)
  catch_var({"id":reserved_word}, message)
  declare_var({"id":reserved_word}, message)
  prefix_var({"id":reserved_word, "op":"++", "opname":"inc"}, message)
  prefix_var({"id":reserved_word, "op":"--", "opname":"dec"}, message)
  postfix_var({"id":reserved_word, "op":"++", "opname":"inc"}, message)
  postfix_var({"id":reserved_word, "op":"--", "opname":"dec"}, message)
  read_var({"id": reserved_word}, message)
268 269
  identifier_name({"id": reserved_word}, None);
  identifier_name_strict({"id": reserved_word}, None);
270 271 272 273 274 275 276 277 278 279 280 281 282
  label_normal({"id": reserved_word}, label_message)
  break_normal({"id": reserved_word}, label_message)
  continue_normal({"id": reserved_word}, label_message)
  if (reserved_word == "const"):
    # The error message for this case is different because
    # ParseLabelledStatementOrExpression will try to parse this as an expression
    # first, effectively disallowing the use in ParseVariableDeclarations, i.e.
    # the preparser never sees that 'const' was intended to be a label.
    label_strict({"id": reserved_word}, "strict_const")
  else:
    label_strict({"id": reserved_word}, message)
  break_strict({"id": reserved_word}, message)
  continue_strict({"id": reserved_word}, message)
283 284


285 286 287
# Future reserved words in strict mode behave like normal identifiers
# in a non strict context.
for reserved_word in strict_reserved_words:
288
  non_strict_use({"id": reserved_word}, None)