js/src/tests/js1_5/Scope/regress-185485.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_5/Scope/regress-185485.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     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 + * Date:    16 Dec 2002
    1.12 + * SUMMARY: Testing |with (x) {function f() {}}| when |x.f| already exists
    1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485
    1.14 + *
    1.15 + * The idea is this: if |x| does not already have a property named |f|,
    1.16 + * a |with| statement cannot be used to define one. See, for example,
    1.17 + *
    1.18 + *       http://bugzilla.mozilla.org/show_bug.cgi?id=159849#c11
    1.19 + *       http://bugzilla.mozilla.org/show_bug.cgi?id=184107
    1.20 + *
    1.21 + *
    1.22 + * However, if |x| already has a property |f|, a |with| statement can be
    1.23 + * used to modify the value it contains:
    1.24 + *
    1.25 + *                 with (x) {f = 1;}
    1.26 + *
    1.27 + * This should work even if we use a |var| statement, like this:
    1.28 + *
    1.29 + *                 with (x) {var f = 1;}
    1.30 + *
    1.31 + * However, it should NOT work if we use a |function| statement, like this:
    1.32 + *
    1.33 + *                 with (x) {function f() {}}
    1.34 + *
    1.35 + * Instead, this should newly define a function f in global scope.
    1.36 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=185485
    1.37 + *
    1.38 + */
    1.39 +//-----------------------------------------------------------------------------
    1.40 +var UBound = 0;
    1.41 +var BUGNUMBER = 185485;
    1.42 +var summary = 'Testing |with (x) {function f() {}}| when |x.f| already exists';
    1.43 +var status = '';
    1.44 +var statusitems = [];
    1.45 +var actual = '';
    1.46 +var actualvalues = [];
    1.47 +var expect= '';
    1.48 +var expectedvalues = [];
    1.49 +
    1.50 +var x = { f:0, g:0 };
    1.51 +
    1.52 +with (x)
    1.53 +{
    1.54 +  f = 1;
    1.55 +}
    1.56 +status = inSection(1);
    1.57 +actual = x.f;
    1.58 +expect = 1;
    1.59 +addThis();
    1.60 +
    1.61 +with (x)
    1.62 +{
    1.63 +  var f = 2;
    1.64 +}
    1.65 +status = inSection(2);
    1.66 +actual = x.f;
    1.67 +expect = 2;
    1.68 +addThis();
    1.69 +
    1.70 +/*
    1.71 + * Use of a function statement under the with-block should not affect
    1.72 + * the local property |f|, but define a function |f| in global scope -
    1.73 + */
    1.74 +with (x)
    1.75 +{
    1.76 +  function f() {}
    1.77 +}
    1.78 +status = inSection(3);
    1.79 +actual = x.f;
    1.80 +expect = 2;
    1.81 +addThis();
    1.82 +
    1.83 +status = inSection(4);
    1.84 +actual = typeof this.f;
    1.85 +expect = 'function';
    1.86 +addThis();
    1.87 +
    1.88 +
    1.89 +/*
    1.90 + * Compare use of function expression instead of function statement.
    1.91 + * Note it is important that |x.g| already exists. Otherwise, this
    1.92 + * would newly define |g| in global scope -
    1.93 + */
    1.94 +with (x)
    1.95 +{
    1.96 +  var g = function() {}
    1.97 +}
    1.98 +status = inSection(5);
    1.99 +actual = x.g.toString();
   1.100 +expect = (function () {}).toString();
   1.101 +addThis();
   1.102 +
   1.103 +
   1.104 +
   1.105 +//-----------------------------------------------------------------------------
   1.106 +test();
   1.107 +//-----------------------------------------------------------------------------
   1.108 +
   1.109 +
   1.110 +
   1.111 +function addThis()
   1.112 +{
   1.113 +  statusitems[UBound] = status;
   1.114 +  actualvalues[UBound] = actual;
   1.115 +  expectedvalues[UBound] = expect;
   1.116 +  UBound++;
   1.117 +}
   1.118 +
   1.119 +
   1.120 +function test()
   1.121 +{
   1.122 +  enterFunc('test');
   1.123 +  printBugNumber(BUGNUMBER);
   1.124 +  printStatus(summary);
   1.125 +
   1.126 +  for (var i=0; i<UBound; i++)
   1.127 +  {
   1.128 +    reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   1.129 +  }
   1.130 +
   1.131 +  exitFunc ('test');
   1.132 +}

mercurial