Debian Codegraph @

rhino/testsrc/tests/ecma/Statements/12.10-1.js

raw symbols SCIP
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/. */
5
6 gTestfile = '12.10-1.js';
7
8 /**
9 File Name: 12.10-1.js
10 ECMA Section: 12.10 The with statement
11 Description:
12 WithStatement :
13 with ( Expression ) Statement
14
15 The with statement adds a computed object to the front of the scope chain
16 of the current execution context, then executes a statement with this
17 augmented scope chain, then restores the scope chain.
18
19 Semantics
20
21 The production WithStatement : with ( Expression ) Statement is evaluated
22 as follows:
23 1. Evaluate Expression.
24 2. Call GetValue(Result(1)).
25 3. Call ToObject(Result(2)).
26 4. Add Result(3) to the front of the scope chain.
27 5. Evaluate Statement using the augmented scope chain from step 4.
28 6. Remove Result(3) from the front of the scope chain.
29 7. Return Result(5).
30
31 Discussion
32 Note that no matter how control leaves the embedded Statement, whether
33 normally or by some form of abrupt completion, the scope chain is always
34 restored to its former state.
35
36 Author: [email protected]
37 Date: 12 november 1997
38 */
39
40 var SECTION = "12.10-1";
41 var VERSION = "ECMA_1";
42 startTest();
43 var TITLE = "The with statement";
44
45 writeHeaderToLog( SECTION + " "+ TITLE);
46
47
48 // although the scope chain changes, the this value is immutable for a given
49 // execution context.
50
51 new TestCase( SECTION,
52 "with( new Number() ) { this +'' }",
53 GLOBAL,
54 eval("with( new Number() ) { this +'' }") );
55
56 // the object's functions and properties should override those of the
57 // global object.
58
59 new TestCase(
60 SECTION,
61 "var MYOB = new WithObject(true); with (MYOB) { parseInt() }",
62 true,
63 eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") );
64
65 new TestCase(
66 SECTION,
67 "var MYOB = new WithObject(false); with (MYOB) { NaN }",
68 false,
69 eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") );
70
71 new TestCase(
72 SECTION,
73 "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }",
74 Number.NaN,
75 eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") );
76
77 new TestCase(
78 SECTION,
79 "var MYOB = new WithObject(false); with (MYOB) { }; Infinity",
80 Number.POSITIVE_INFINITY,
81 eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") );
82
83
84 new TestCase(
85 SECTION,
86 "var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }",
87 Number.POSITIVE_INFINITY,
88 eval("var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }") );
89
90 // let us leave the with block via a break.
91
92 new TestCase(
93 SECTION,
94 "var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity",
95 Number.POSITIVE_INFINITY,
96 eval("var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity") );
97
98
99 test();
100
101 function WithObject( value ) {
102 this.prop1 = 1;
103 this.prop2 = new Boolean(true);
104 this.prop3 = "a string";
105 this.value = value;
106
107 // now we will override global functions
108
109 this.parseInt = new Function( "return this.value" );
110 this.NaN = value;
111 this.Infinity = value;
112 this.unescape = new Function( "return this.value" );
113 this.escape = new Function( "return this.value" );
114 this.eval = new Function( "return this.value" );
115 this.parseFloat = new Function( "return this.value" );
116 this.isNaN = new Function( "return this.value" );
117 this.isFinite = new Function( "return this.value" );
118 }