Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Statements / 12.10-1.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
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.
22  *
23  * Contributor(s):
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          12.10-1.js
42    ECMA Section:       12.10 The with statement
43    Description:
44    WithStatement :
45    with ( Expression ) Statement
46
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.
50
51    Semantics
52
53    The production WithStatement : with ( Expression ) Statement is evaluated
54    as follows:
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.
61    7.  Return Result(5).
62
63    Discussion
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.
67
68    Author:             christine@netscape.com
69    Date:               12 november 1997
70 */
71
72 var SECTION = "12.10-1";
73 var VERSION = "ECMA_1";
74 startTest();
75 var TITLE   = "The with statement";
76
77 writeHeaderToLog( SECTION + " "+ TITLE);
78
79
80 // although the scope chain changes, the this value is immutable for a given
81 // execution context.
82
83 new TestCase( SECTION,
84               "with( new Number() ) { this +'' }",
85               GLOBAL,
86               eval("with( new Number() ) { this +'' }") );
87
88 // the object's functions and properties should override those of the
89 // global object.
90
91 new TestCase(
92   SECTION,
93   "var MYOB = new WithObject(true); with (MYOB) { parseInt() }",
94   true,
95   eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") );
96
97 new TestCase(
98   SECTION,
99   "var MYOB = new WithObject(false); with (MYOB) { NaN }",
100   false,
101   eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") );
102
103 new TestCase(
104   SECTION,
105   "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }",
106   Number.NaN,
107   eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") );
108
109 new TestCase(
110   SECTION,
111   "var MYOB = new WithObject(false); with (MYOB) { }; Infinity",
112   Number.POSITIVE_INFINITY,
113   eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") );
114
115
116 new TestCase(
117   SECTION,
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 }") );
121
122 // let us leave the with block via a break.
123
124 new TestCase(
125   SECTION,
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") );
129
130
131 test();
132
133 function WithObject( value ) {
134   this.prop1 = 1;
135   this.prop2 = new Boolean(true);
136   this.prop3 = "a string";
137   this.value = value;
138
139   // now we will override global functions
140
141   this.parseInt = new Function( "return this.value" );
142   this.NaN = 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" );
150 }