Home | History | Annotate | Download | only in Statements
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          12.10-1.js
     24     ECMA Section:       12.10 The with statement
     25     Description:
     26     WithStatement :
     27     with ( Expression ) Statement
     28 
     29     The with statement adds a computed object to the front of the scope chain
     30     of the current execution context, then executes a statement with this
     31     augmented scope chain, then restores the scope chain.
     32 
     33     Semantics
     34 
     35     The production WithStatement : with ( Expression ) Statement is evaluated
     36     as follows:
     37     1.  Evaluate Expression.
     38     2.  Call GetValue(Result(1)).
     39     3.  Call ToObject(Result(2)).
     40     4.  Add Result(3) to the front of the scope chain.
     41     5.  Evaluate Statement using the augmented scope chain from step 4.
     42     6.  Remove Result(3) from the front of the scope chain.
     43     7.  Return Result(5).
     44 
     45     Discussion
     46     Note that no matter how control leaves the embedded Statement, whether
     47     normally or by some form of abrupt completion, the scope chain is always
     48     restored to its former state.
     49 
     50     Author:             christine (at) netscape.com
     51     Date:               12 november 1997
     52 */
     53 
     54     var SECTION = "12.10-1";
     55     var VERSION = "ECMA_1";
     56     startTest();
     57     var TITLE   = "The with statment";
     58 
     59     writeHeaderToLog( SECTION + " "+ TITLE);
     60 
     61     var testcases = getTestCases();
     62 
     63     test();
     64 
     65 function test() {
     66     for ( tc=0; tc < testcases.length; tc++ ) {
     67         testcases[tc].passed = writeTestCaseResult(
     68                             testcases[tc].expect,
     69                             testcases[tc].actual,
     70                             testcases[tc].description +" = "+
     71                             testcases[tc].actual );
     72 
     73         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     74     }
     75     stopTest();
     76     return ( testcases );
     77 }
     78 function getTestCases() {
     79     var array = new Array();
     80     var item = 0;
     81 
     82     // although the scope chain changes, the this value is immutable for a given
     83     // execution context.
     84 
     85     array[item++] = new TestCase( SECTION,
     86                                 "with( new Number() ) { this +'' }",
     87                                 "[object global]",
     88                                 eval("with( new Number() ) { this +'' }") );
     89 
     90     // the object's functions and properties should override those of the
     91     // global object.
     92 
     93     array[item++] = new TestCase(
     94                         SECTION,
     95                         "var MYOB = new WithObject(true); with (MYOB) { parseInt() }",
     96                         true,
     97                         eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") );
     98 
     99     array[item++] = new TestCase(
    100                         SECTION,
    101                         "var MYOB = new WithObject(false); with (MYOB) { NaN }",
    102                         false,
    103                         eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") );
    104 
    105     array[item++] = new TestCase(
    106                         SECTION,
    107                         "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }",
    108                         Number.NaN,
    109                         eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") );
    110 
    111     array[item++] = new TestCase(
    112                         SECTION,
    113                         "var MYOB = new WithObject(false); with (MYOB) { }; Infinity",
    114                         Number.POSITIVE_INFINITY,
    115                         eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") );
    116 
    117 
    118     array[item++] = new TestCase(
    119                         SECTION,
    120                         "var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }",
    121                         Number.POSITIVE_INFINITY,
    122                         eval("var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }") );
    123 
    124     // let us leave the with block via a break.
    125 
    126     array[item++] = new TestCase(
    127                         SECTION,
    128                         "var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity",
    129                         Number.POSITIVE_INFINITY,
    130                         eval("var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity") );
    131 
    132     return ( array );
    133 }
    134 function WithObject( value ) {
    135     this.prop1 = 1;
    136     this.prop2 = new Boolean(true);
    137     this.prop3 = "a string";
    138     this.value = value;
    139 
    140     // now we will override global functions
    141 
    142     this.parseInt = new Function( "return this.value" );
    143     this.NaN = value;
    144     this.Infinity = value;
    145     this.unescape = new Function( "return this.value" );
    146     this.escape   = new Function( "return this.value" );
    147     this.eval     = new Function( "return this.value" );
    148     this.parseFloat = new Function( "return this.value" );
    149     this.isNaN      = new Function( "return this.value" );
    150     this.isFinite   = new Function( "return this.value" );
    151 }
    152