Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | File Name: 12.10-1.js |
michael@0 | 9 | ECMA Section: 12.10 The with statement |
michael@0 | 10 | Description: |
michael@0 | 11 | WithStatement : |
michael@0 | 12 | with ( Expression ) Statement |
michael@0 | 13 | |
michael@0 | 14 | The with statement adds a computed object to the front of the scope chain |
michael@0 | 15 | of the current execution context, then executes a statement with this |
michael@0 | 16 | augmented scope chain, then restores the scope chain. |
michael@0 | 17 | |
michael@0 | 18 | Semantics |
michael@0 | 19 | |
michael@0 | 20 | The production WithStatement : with ( Expression ) Statement is evaluated |
michael@0 | 21 | as follows: |
michael@0 | 22 | 1. Evaluate Expression. |
michael@0 | 23 | 2. Call GetValue(Result(1)). |
michael@0 | 24 | 3. Call ToObject(Result(2)). |
michael@0 | 25 | 4. Add Result(3) to the front of the scope chain. |
michael@0 | 26 | 5. Evaluate Statement using the augmented scope chain from step 4. |
michael@0 | 27 | 6. Remove Result(3) from the front of the scope chain. |
michael@0 | 28 | 7. Return Result(5). |
michael@0 | 29 | |
michael@0 | 30 | Discussion |
michael@0 | 31 | Note that no matter how control leaves the embedded Statement, whether |
michael@0 | 32 | normally or by some form of abrupt completion, the scope chain is always |
michael@0 | 33 | restored to its former state. |
michael@0 | 34 | |
michael@0 | 35 | Author: christine@netscape.com |
michael@0 | 36 | Date: 12 november 1997 |
michael@0 | 37 | */ |
michael@0 | 38 | |
michael@0 | 39 | var SECTION = "12.10-1"; |
michael@0 | 40 | var VERSION = "ECMA_1"; |
michael@0 | 41 | startTest(); |
michael@0 | 42 | var TITLE = "The with statement"; |
michael@0 | 43 | |
michael@0 | 44 | writeHeaderToLog( SECTION + " "+ TITLE); |
michael@0 | 45 | |
michael@0 | 46 | |
michael@0 | 47 | // although the scope chain changes, the this value is immutable for a given |
michael@0 | 48 | // execution context. |
michael@0 | 49 | |
michael@0 | 50 | new TestCase( SECTION, |
michael@0 | 51 | "with( new Number() ) { this +'' }", |
michael@0 | 52 | GLOBAL, |
michael@0 | 53 | eval("with( new Number() ) { this +'' }") ); |
michael@0 | 54 | |
michael@0 | 55 | // the object's functions and properties should override those of the |
michael@0 | 56 | // global object. |
michael@0 | 57 | |
michael@0 | 58 | new TestCase( |
michael@0 | 59 | SECTION, |
michael@0 | 60 | "var MYOB = new WithObject(true); with (MYOB) { parseInt() }", |
michael@0 | 61 | true, |
michael@0 | 62 | eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") ); |
michael@0 | 63 | |
michael@0 | 64 | new TestCase( |
michael@0 | 65 | SECTION, |
michael@0 | 66 | "var MYOB = new WithObject(false); with (MYOB) { NaN }", |
michael@0 | 67 | false, |
michael@0 | 68 | eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") ); |
michael@0 | 69 | |
michael@0 | 70 | new TestCase( |
michael@0 | 71 | SECTION, |
michael@0 | 72 | "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }", |
michael@0 | 73 | Number.NaN, |
michael@0 | 74 | eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") ); |
michael@0 | 75 | |
michael@0 | 76 | new TestCase( |
michael@0 | 77 | SECTION, |
michael@0 | 78 | "var MYOB = new WithObject(false); with (MYOB) { }; Infinity", |
michael@0 | 79 | Number.POSITIVE_INFINITY, |
michael@0 | 80 | eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") ); |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | new TestCase( |
michael@0 | 84 | SECTION, |
michael@0 | 85 | "var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }", |
michael@0 | 86 | Number.POSITIVE_INFINITY, |
michael@0 | 87 | eval("var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }") ); |
michael@0 | 88 | |
michael@0 | 89 | // let us leave the with block via a break. |
michael@0 | 90 | |
michael@0 | 91 | new TestCase( |
michael@0 | 92 | SECTION, |
michael@0 | 93 | "var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity", |
michael@0 | 94 | Number.POSITIVE_INFINITY, |
michael@0 | 95 | eval("var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity") ); |
michael@0 | 96 | |
michael@0 | 97 | |
michael@0 | 98 | test(); |
michael@0 | 99 | |
michael@0 | 100 | function WithObject( value ) { |
michael@0 | 101 | this.prop1 = 1; |
michael@0 | 102 | this.prop2 = new Boolean(true); |
michael@0 | 103 | this.prop3 = "a string"; |
michael@0 | 104 | this.value = value; |
michael@0 | 105 | |
michael@0 | 106 | // now we will override global functions |
michael@0 | 107 | |
michael@0 | 108 | this.parseInt = new Function( "return this.value" ); |
michael@0 | 109 | this.NaN = value; |
michael@0 | 110 | this.Infinity = value; |
michael@0 | 111 | this.unescape = new Function( "return this.value" ); |
michael@0 | 112 | this.escape = new Function( "return this.value" ); |
michael@0 | 113 | this.eval = new Function( "return this.value" ); |
michael@0 | 114 | this.parseFloat = new Function( "return this.value" ); |
michael@0 | 115 | this.isNaN = new Function( "return this.value" ); |
michael@0 | 116 | this.isFinite = new Function( "return this.value" ); |
michael@0 | 117 | } |