1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 ECMA Section: 12.10 The with statement
45 with ( Expression ) Statement
47 The with statement adds a computed object to the front of the scope chain
48 of the current execution context, then executes a statement with this
49 augmented scope chain, then restores the scope chain.
53 The production WithStatement : with ( Expression ) Statement is evaluated
55 1. Evaluate Expression.
56 2. Call GetValue(Result(1)).
57 3. Call ToObject(Result(2)).
58 4. Add Result(3) to the front of the scope chain.
59 5. Evaluate Statement using the augmented scope chain from step 4.
60 6. Remove Result(3) from the front of the scope chain.
64 Note that no matter how control leaves the embedded Statement, whether
65 normally or by some form of abrupt completion, the scope chain is always
66 restored to its former state.
68 Author: christine@netscape.com
69 Date: 12 november 1997
72 var SECTION = "12.10-1";
73 var VERSION = "ECMA_1";
75 var TITLE = "The with statement";
77 writeHeaderToLog( SECTION + " "+ TITLE);
80 // although the scope chain changes, the this value is immutable for a given
83 new TestCase( SECTION,
84 "with( new Number() ) { this +'' }",
86 eval("with( new Number() ) { this +'' }") );
88 // the object's functions and properties should override those of the
93 "var MYOB = new WithObject(true); with (MYOB) { parseInt() }",
95 eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") );
99 "var MYOB = new WithObject(false); with (MYOB) { NaN }",
101 eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") );
105 "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }",
107 eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") );
111 "var MYOB = new WithObject(false); with (MYOB) { }; Infinity",
112 Number.POSITIVE_INFINITY,
113 eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") );
118 "var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }",
119 Number.POSITIVE_INFINITY,
120 eval("var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }") );
122 // let us leave the with block via a break.
126 "var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity",
127 Number.POSITIVE_INFINITY,
128 eval("var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity") );
133 function WithObject( value ) {
135 this.prop2 = new Boolean(true);
136 this.prop3 = "a string";
139 // now we will override global functions
141 this.parseInt = new Function( "return this.value" );
143 this.Infinity = value;
144 this.unescape = new Function( "return this.value" );
145 this.escape = new Function( "return this.value" );
146 this.eval = new Function( "return this.value" );
147 this.parseFloat = new Function( "return this.value" );
148 this.isNaN = new Function( "return this.value" );
149 this.isFinite = new Function( "return this.value" );