js/src/tests/js1_8/genexps/regress-634472.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_8/genexps/regress-634472.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +//-----------------------------------------------------------------------------
    1.11 +var BUGNUMBER = 634472;
    1.12 +var summary = 'contextual restrictions for yield and arguments';
    1.13 +var actual = '';
    1.14 +var expect = '';
    1.15 +
    1.16 +
    1.17 +function error(str) {
    1.18 +  let base;
    1.19 +  try {
    1.20 +    // the following line must not be broken up into multiple lines
    1.21 +    base = (function(){try{eval('throw new Error()')}catch(e){return e.lineNumber}})(); eval(str);
    1.22 +    return null;
    1.23 +  } catch (e) {
    1.24 +    e.lineNumber = e.lineNumber - base + 1;
    1.25 +    return e;
    1.26 +  }
    1.27 +}
    1.28 +
    1.29 +const JSMSG_GENEXP_YIELD         = error("(function(){((yield) for (x in []))})").message;
    1.30 +const JSMSG_TOP_YIELD            = error("yield").message;
    1.31 +const JSMSG_YIELD_PAREN          = error("(function(){yield, 1})").message;
    1.32 +const JSMSG_YIELD_FOR            = error("(function(){yield for})").message;
    1.33 +const JSMSG_BAD_GENERATOR_SYNTAX = error("(1, x for (x in []))").message;
    1.34 +
    1.35 +const cases = [
    1.36 +  // yield expressions
    1.37 +  { expr: "yield",        top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "simple yield" },
    1.38 +  { expr: "yield 1",      top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg" },
    1.39 +  { expr: "1, yield",     top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "simple yield at end of list" },
    1.40 +  { expr: "1, yield 2",   top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg at end of list" },
    1.41 +  { expr: "yield, 1",     top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN, gen: JSMSG_YIELD_PAREN,  desc: "simple yield in list" },
    1.42 +  { expr: "yield 1, 2",   top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN, gen: JSMSG_YIELD_PAREN,  desc: "yield w/ arg in list" },
    1.43 +  { expr: "(yield)",      top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "simple yield, parenthesized" },
    1.44 +  { expr: "(yield 1)",    top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg, parenthesized" },
    1.45 +  { expr: "(1, yield)",   top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "simple yield at end of list, parenthesized" },
    1.46 +  { expr: "(1, yield 2)", top: JSMSG_TOP_YIELD, fun: null,              gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized" },
    1.47 +  { expr: "(yield, 1)",   top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN, gen: JSMSG_YIELD_PAREN,  desc: "simple yield in list, parenthesized" },
    1.48 +  { expr: "(yield 1, 2)", top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN, gen: JSMSG_YIELD_PAREN,  desc: "yield w/ arg in list, parenthesized" },
    1.49 +
    1.50 +  // deeply nested yield expressions
    1.51 +  { expr: "((((yield))))",   top: JSMSG_TOP_YIELD, fun: null, gen: JSMSG_GENEXP_YIELD, desc: "deeply nested yield" },
    1.52 +  { expr: "((((yield 1))))", top: JSMSG_TOP_YIELD, fun: null, gen: JSMSG_GENEXP_YIELD, desc: "deeply nested yield w/ arg" },
    1.53 +
    1.54 +  // arguments
    1.55 +  { expr: "arguments",    top: null, fun: null, gen: null, desc: "arguments in list" },
    1.56 +  { expr: "1, arguments", top: null, fun: null, gen: null, desc: "arguments in list" },
    1.57 +
    1.58 +  // yield in generator expressions
    1.59 +  { expr: "(yield for (x in []))",           top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_FOR,    gen: JSMSG_YIELD_FOR,      desc: "simple yield in genexp" },
    1.60 +  { expr: "(yield 1 for (x in []))",         top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg in genexp" },
    1.61 +  { expr: "(yield, 1 for (x in []))",        top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "simple yield in list in genexp" },
    1.62 +  { expr: "(yield 1, 2 for (x in []))",      top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "yield w/ arg in list in genexp" },
    1.63 +  { expr: "(1, yield for (x in []))",        top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_FOR,    gen: JSMSG_YIELD_FOR,      desc: "simple yield at end of list in genexp" },
    1.64 +  { expr: "(1, yield 2 for (x in []))",      top: JSMSG_TOP_YIELD, fun: { simple: JSMSG_GENEXP_YIELD, call: JSMSG_GENEXP_YIELD },
    1.65 +                                                                                            gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg at end of list in genexp" },
    1.66 +  { expr: "((yield) for (x in []))",         top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "simple yield, parenthesized in genexp" },
    1.67 +  { expr: "((yield 1) for (x in []))",       top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg, parenthesized in genexp" },
    1.68 +  { expr: "(1, (yield) for (x in []))",      top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "simple yield, parenthesized in list in genexp" },
    1.69 +  { expr: "(1, (yield 2) for (x in []))",    top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg, parenthesized in list in genexp" },
    1.70 +  { expr: "((1, yield) for (x in []))",      top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "simple yield at end of list, parenthesized in genexp" },
    1.71 +  { expr: "((1, yield 2) for (x in []))",    top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized in genexp" },
    1.72 +  { expr: "(1, (2, yield) for (x in []))",   top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "simple yield at end of list, parenthesized in list in genexp" },
    1.73 +  { expr: "(1, (2, yield 3) for (x in []))", top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "yield w/ arg at end of list, parenthesized in list in genexp" },
    1.74 +  { expr: "((yield, 1) for (x in []))",      top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "simple yield in list, parenthesized in genexp" },
    1.75 +  { expr: "((yield 1, 2) for (x in []))",    top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "yield w/ arg in list, parenthesized in genexp" },
    1.76 +  { expr: "(1, (yield, 2) for (x in []))",   top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "simple yield in list, parenthesized in list in genexp" },
    1.77 +  { expr: "(1, (yield 2, 3) for (x in []))", top: JSMSG_TOP_YIELD, fun: JSMSG_YIELD_PAREN,  gen: JSMSG_YIELD_PAREN,  desc: "yield w/ arg in list, parenthesized in list in genexp" },
    1.78 +
    1.79 +  // deeply nested yield in generator expressions
    1.80 +  { expr: "((((1, yield 2))) for (x in []))",               top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "deeply nested yield in genexp" },
    1.81 +  { expr: "((((1, yield 2)) for (x in [])) for (y in []))", top: JSMSG_TOP_YIELD, fun: JSMSG_GENEXP_YIELD, gen: JSMSG_GENEXP_YIELD, desc: "deeply nested yield in multiple genexps" },
    1.82 +
    1.83 +  // arguments in generator expressions
    1.84 +  { expr: "(arguments for (x in []))",         top: null,                       fun: null,                       gen: null,                       desc: "simple arguments in genexp" },
    1.85 +  { expr: "(1, arguments for (x in []))",      top: JSMSG_BAD_GENERATOR_SYNTAX, fun: JSMSG_BAD_GENERATOR_SYNTAX, gen: JSMSG_BAD_GENERATOR_SYNTAX, desc: "arguments in list in genexp" },
    1.86 +  { expr: "((arguments) for (x in []))",       top: null,                       fun: null,                       gen: null,                       desc: "arguments, parenthesized in genexp" },
    1.87 +  { expr: "(1, (arguments) for (x in []))",    top: JSMSG_BAD_GENERATOR_SYNTAX, fun: JSMSG_BAD_GENERATOR_SYNTAX, gen: JSMSG_BAD_GENERATOR_SYNTAX, desc: "arguments, parenthesized in list in genexp" },
    1.88 +  { expr: "((1, arguments) for (x in []))",    top: null,                       fun: null,                       gen: null,                       desc: "arguments in list, parenthesized in genexp" },
    1.89 +  { expr: "(1, (2, arguments) for (x in []))", top: JSMSG_BAD_GENERATOR_SYNTAX, fun: JSMSG_BAD_GENERATOR_SYNTAX, gen: JSMSG_BAD_GENERATOR_SYNTAX, desc: "arguments in list, parenthesized in list in genexp" },
    1.90 +
    1.91 +  // deeply nested arguments in generator expressions
    1.92 +  { expr: "((((1, arguments))) for (x in []))",               top: null, fun: null, gen: null, desc: "deeply nested arguments in genexp" },
    1.93 +  { expr: "((((1, arguments)) for (x in [])) for (y in []))", top: null, fun: null, gen: null, desc: "deeply nested arguments in multiple genexps" },
    1.94 +
    1.95 +  // legal yield/arguments in nested function
    1.96 +  { expr: "((function() { yield }) for (x in []))",           top: null, fun: null, gen: null, desc: "legal yield in nested function" },
    1.97 +  { expr: "((function() { arguments }) for (x in []))",       top: null, fun: null, gen: null, desc: "legal arguments in nested function" },
    1.98 +  { expr: "((function() arguments) for (x in []))",           top: null, fun: null, gen: null, desc: "legal arguments in nested expression-closure" }
    1.99 +];
   1.100 +
   1.101 +//-----------------------------------------------------------------------------
   1.102 +test();
   1.103 +//-----------------------------------------------------------------------------
   1.104 +
   1.105 +function splitKeyword(str) {
   1.106 +  return str.
   1.107 +         replace(/yield for/, '\nyield for\n').
   1.108 +         replace(/yield ([0-9])/, '\nyield $1\n').
   1.109 +         replace(/yield([^ ]|$)/, '\nyield\n$1').
   1.110 +         replace(/arguments/, '\narguments\n');
   1.111 +}
   1.112 +
   1.113 +function expectError1(err, ctx, msg) {
   1.114 +  reportCompare('object', typeof err,     'exn for: ' + msg);
   1.115 +  reportCompare(ctx,      err.message,    'exn message for: ' + msg);
   1.116 +  if (ctx !== JSMSG_BAD_GENERATOR_SYNTAX)
   1.117 +      reportCompare(2,    err.lineNumber, 'exn token for: ' + msg);
   1.118 +}
   1.119 +
   1.120 +function expectError(expr, call, wrapCtx, expect, msg) {
   1.121 +  let exps = (typeof expect === "string")
   1.122 +           ? { simple: expect, call: expect }
   1.123 +           : expect;
   1.124 +  expectError1(error(wrapCtx(expr)), exps.simple, msg);
   1.125 +  if (call)
   1.126 +    expectError1(error(wrapCtx(call)), exps.call, 'call argument in ' + msg);
   1.127 +}
   1.128 +
   1.129 +function expectSuccess(err, msg) {
   1.130 +  reportCompare(null, err, 'parse: ' + msg);
   1.131 +}
   1.132 +
   1.133 +function atTop(str) { return str }
   1.134 +function inFun(str) { return '(function(){' + str + '})' }
   1.135 +function inGen(str) { return '(y for (y in ' + str + '))' }
   1.136 +
   1.137 +function test()
   1.138 +{
   1.139 +  enterFunc ('test');
   1.140 +  printBugNumber(BUGNUMBER);
   1.141 +  printStatus (summary);
   1.142 +
   1.143 +  for (let i = 0, len = cases.length; i < len; i++) {
   1.144 +    let {expr, top, fun, gen, desc} = cases[i];
   1.145 +
   1.146 +    let call = (expr[0] === "(") ? ("print" + expr) : null;
   1.147 +
   1.148 +    expr = splitKeyword(expr);
   1.149 +    if (call)
   1.150 +      call = splitKeyword(call);
   1.151 +
   1.152 +    if (top)
   1.153 +      expectError(expr, call, atTop, top, 'top-level context, ' + desc);
   1.154 +    else
   1.155 +      expectSuccess(error(expr), 'top-level context, ' + desc);
   1.156 +
   1.157 +    if (fun)
   1.158 +      expectError(expr, call, inFun, fun, 'function context, ' + desc);
   1.159 +    else
   1.160 +      expectSuccess(error(inFun(expr)), 'function context, ' + desc);
   1.161 +
   1.162 +    if (gen)
   1.163 +      expectError(expr, call, inGen, gen, 'genexp context, ' + desc);
   1.164 +    else
   1.165 +      expectSuccess(error(inGen(expr)), 'genexp context, ' + desc);
   1.166 +  }
   1.167 +
   1.168 +  exitFunc ('test');
   1.169 +}

mercurial