js/src/tests/ecma_3/Function/scope-002.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 /*
     7  * Date: 28 May 2001
     8  *
     9  * SUMMARY:  Functions are scoped statically, not dynamically
    10  *
    11  * See ECMA Section 10.1.4 Scope Chain and Identifier Resolution
    12  * (This section defines the scope chain of an execution context)
    13  *
    14  * See ECMA Section 12.10 The with Statement
    15  *
    16  * See ECMA Section 13 Function Definition
    17  * (This section defines the scope chain of a function object as that
    18  *  of the running execution context when the function was declared)
    19  *
    20  * Like scope-001.js, but using assignment var f = function expression
    21  * instead of a function declaration: function f() {} etc.
    22  */
    23 //-----------------------------------------------------------------------------
    24 var UBound = 0;
    25 var BUGNUMBER = '(none)';
    26 var summary = 'Testing that functions are scoped statically, not dynamically';
    27 var self = this;  // capture a reference to the global object
    28 var status = '';
    29 var statusitems = [ ];
    30 var actual = '';
    31 var actualvalues = [ ];
    32 var expect= '';
    33 var expectedvalues = [ ];
    36 /*
    37  * In this section the expected value is 1, not 2.
    38  *
    39  * Why? f captures its scope chain from when it's declared, and imposes that chain
    40  * when it's executed. In other words, f's scope chain is from when it was compiled.
    41  * Since f is a top-level function, this is the global object only. Hence 'a' resolves to 1.
    42  */
    43 status = 'Section A of test';
    44 var a = 1;
    45 var f = function () {return a;};
    46 var obj = {a:2};
    47 with (obj)
    48 {
    49   actual = f();
    50 }
    51 expect = 1;
    52 addThis();
    55 /*
    56  * In this section the expected value is 2, not 1. That is because here
    57  * f's associated scope chain now includes 'obj' before the global object.
    58  */
    59 status = 'Section B of test';
    60 var a = 1;
    61 var obj = {a:2};
    62 with (obj)
    63 {
    64   var f = function () {return a;};
    65   actual = f();
    66 }
    67 expect = 2;
    68 addThis();
    71 /*
    72  * Like Section B , except that we call f outside the with block.
    73  * By the principles explained above, we still expect 2 -
    74  */
    75 status = 'Section C of test';
    76 var a = 1;
    77 var obj = {a:2};
    78 with (obj)
    79 {
    80   var f = function () {return a;};
    81 }
    82 actual = f();
    83 expect = 2;
    84 addThis();
    87 /*
    88  * Like Section C, but with one more level of indirection -
    89  */
    90 status = 'Section D of test';
    91 var a = 1;
    92 var obj = {a:2, obj:{a:3}};
    93 with (obj)
    94 {
    95   with (obj)
    96   {
    97     var f = function () {return a;};
    98   }
    99 }
   100 actual = f();
   101 expect = 3;
   102 addThis();
   105 /*
   106  * Like Section C, but here we actually delete obj before calling f.
   107  * We still expect 2 -
   108  */
   109 status = 'Section E of test';
   110 var a = 1;
   111 var obj = {a:2};
   112 with (obj)
   113 {
   114   var f = function () {return a;};
   115 }
   116 delete obj;
   117 actual = f();
   118 expect = 2;
   119 addThis();
   122 /*
   123  * Like Section E. Here we redefine obj and call f under with (obj) -
   124  * We still expect 2 -
   125  */
   126 status = 'Section F of test';
   127 var a = 1;
   128 var obj = {a:2};
   129 with (obj)
   130 {
   131   var f = function () {return a;};
   132 }
   133 delete obj;
   134 var obj = {a:3};
   135 with (obj)
   136 {
   137   actual = f();
   138 }
   139 expect = 2;  // NOT 3 !!!
   140 addThis();
   143 /*
   144  * Explicitly verify that f exists at global level, even though
   145  * it was defined under the with(obj) block -
   146  */
   147 status = 'Section G of test';
   148 var a = 1;
   149 var obj = {a:2};
   150 with (obj)
   151 {
   152   var f = function () {return a;};
   153 }
   154 actual = String([obj.hasOwnProperty('f'), self.hasOwnProperty('f')]);
   155 expect = String([false, true]);
   156 addThis();
   159 /*
   160  * Explicitly verify that f exists at global level, even though
   161  * it was defined under the with(obj) block -
   162  */
   163 status = 'Section H of test';
   164 var a = 1;
   165 var obj = {a:2};
   166 with (obj)
   167 {
   168   var f = function () {return a;};
   169 }
   170 actual = String(['f' in obj, 'f' in self]);
   171 expect = String([false, true]);
   172 addThis();
   176 //-------------------------------------------------------------------------------------------------
   177 test();
   178 //-------------------------------------------------------------------------------------------------
   181 function addThis()
   182 {
   183   statusitems[UBound] = status;
   184   actualvalues[UBound] = actual;
   185   expectedvalues[UBound] = expect;
   186   UBound++;
   187   resetTestVars();
   188 }
   191 function resetTestVars()
   192 {
   193   delete a;
   194   delete obj;
   195   delete f;
   196 }
   199 function test()
   200 {
   201   enterFunc ('test');
   202   printBugNumber(BUGNUMBER);
   203   printStatus (summary);
   205   for (var i = 0; i < UBound; i++)
   206   {
   207     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
   208   }
   210   exitFunc ('test');
   211 }

mercurial