js/src/tests/ecma/Statements/12.10-1.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/Statements/12.10-1.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          12.10-1.js
    1.12 +   ECMA Section:       12.10 The with statement
    1.13 +   Description:
    1.14 +   WithStatement :
    1.15 +   with ( Expression ) Statement
    1.16 +
    1.17 +   The with statement adds a computed object to the front of the scope chain
    1.18 +   of the current execution context, then executes a statement with this
    1.19 +   augmented scope chain, then restores the scope chain.
    1.20 +
    1.21 +   Semantics
    1.22 +
    1.23 +   The production WithStatement : with ( Expression ) Statement is evaluated
    1.24 +   as follows:
    1.25 +   1.  Evaluate Expression.
    1.26 +   2.  Call GetValue(Result(1)).
    1.27 +   3.  Call ToObject(Result(2)).
    1.28 +   4.  Add Result(3) to the front of the scope chain.
    1.29 +   5.  Evaluate Statement using the augmented scope chain from step 4.
    1.30 +   6.  Remove Result(3) from the front of the scope chain.
    1.31 +   7.  Return Result(5).
    1.32 +
    1.33 +   Discussion
    1.34 +   Note that no matter how control leaves the embedded Statement, whether
    1.35 +   normally or by some form of abrupt completion, the scope chain is always
    1.36 +   restored to its former state.
    1.37 +
    1.38 +   Author:             christine@netscape.com
    1.39 +   Date:               12 november 1997
    1.40 +*/
    1.41 +
    1.42 +var SECTION = "12.10-1";
    1.43 +var VERSION = "ECMA_1";
    1.44 +startTest();
    1.45 +var TITLE   = "The with statement";
    1.46 +
    1.47 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.48 +
    1.49 +
    1.50 +// although the scope chain changes, the this value is immutable for a given
    1.51 +// execution context.
    1.52 +
    1.53 +new TestCase( SECTION,
    1.54 +	      "with( new Number() ) { this +'' }",
    1.55 +	      GLOBAL,
    1.56 +	      eval("with( new Number() ) { this +'' }") );
    1.57 +
    1.58 +// the object's functions and properties should override those of the
    1.59 +// global object.
    1.60 +
    1.61 +new TestCase(
    1.62 +  SECTION,
    1.63 +  "var MYOB = new WithObject(true); with (MYOB) { parseInt() }",
    1.64 +  true,
    1.65 +  eval("var MYOB = new WithObject(true); with (MYOB) { parseInt() }") );
    1.66 +
    1.67 +new TestCase(
    1.68 +  SECTION,
    1.69 +  "var MYOB = new WithObject(false); with (MYOB) { NaN }",
    1.70 +  false,
    1.71 +  eval("var MYOB = new WithObject(false); with (MYOB) { NaN }") );
    1.72 +
    1.73 +new TestCase(
    1.74 +  SECTION,
    1.75 +  "var MYOB = new WithObject(NaN); with (MYOB) { Infinity }",
    1.76 +  Number.NaN,
    1.77 +  eval("var MYOB = new WithObject(NaN); with (MYOB) { Infinity }") );
    1.78 +
    1.79 +new TestCase(
    1.80 +  SECTION,
    1.81 +  "var MYOB = new WithObject(false); with (MYOB) { }; Infinity",
    1.82 +  Number.POSITIVE_INFINITY,
    1.83 +  eval("var MYOB = new WithObject(false); with (MYOB) { }; Infinity") );
    1.84 +
    1.85 +
    1.86 +new TestCase(
    1.87 +  SECTION,
    1.88 +  "var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }",
    1.89 +  Number.POSITIVE_INFINITY,
    1.90 +  eval("var MYOB = new WithObject(0); with (MYOB) { delete Infinity; Infinity }") );
    1.91 +
    1.92 +// let us leave the with block via a break.
    1.93 +
    1.94 +new TestCase(
    1.95 +  SECTION,
    1.96 +  "var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity",
    1.97 +  Number.POSITIVE_INFINITY,
    1.98 +  eval("var MYOB = new WithObject(0); while (true) { with (MYOB) { Infinity; break; } } Infinity") );
    1.99 +
   1.100 +
   1.101 +test();
   1.102 +
   1.103 +function WithObject( value ) {
   1.104 +  this.prop1 = 1;
   1.105 +  this.prop2 = new Boolean(true);
   1.106 +  this.prop3 = "a string";
   1.107 +  this.value = value;
   1.108 +
   1.109 +  // now we will override global functions
   1.110 +
   1.111 +  this.parseInt = new Function( "return this.value" );
   1.112 +  this.NaN = value;
   1.113 +  this.Infinity = value;
   1.114 +  this.unescape = new Function( "return this.value" );
   1.115 +  this.escape   = new Function( "return this.value" );
   1.116 +  this.eval     = new Function( "return this.value" );
   1.117 +  this.parseFloat = new Function( "return this.value" );
   1.118 +  this.isNaN      = new Function( "return this.value" );
   1.119 +  this.isFinite   = new Function( "return this.value" );
   1.120 +}

mercurial