File indexing completed on 2024-04-28 15:28:38

0001 var b = new Boolean();
0002 b.x = 11;
0003 
0004 with (b) {
0005   f = function(a) { return a*x; } // remember scope chain
0006 }
0007 
0008 shouldBe("f(2)", "22");
0009 
0010 var OBJECT = new MyObject( "hello" );
0011 function MyObject(value) {
0012     this.value = value;
0013     this.toString = new Function( "return this.value+''" );
0014     return this;
0015 }
0016 shouldBe("OBJECT.toString()", "'hello'");
0017 var s;
0018 with (OBJECT) {
0019     s = toString();
0020 }
0021 shouldBe("s", "'hello'");
0022 
0023 
0024 // Make sure that for ... in reevaluates the scoping every time!
0025 P = { foo : 1, bar : 2, baz : 3 }
0026 
0027 function testForIn() {
0028    for (g in P) {
0029         eval("var g;") //Change the scope of g half-ways through the loop
0030    }
0031 }
0032 
0033 testForIn();
0034 shouldBe("g", "'foo'"); //Before the eval, g was in outer scope, but not after!
0035 
0036 // Check to make sure we cleanup the scope stack right
0037 o = { x : 4 }
0038 p = {};
0039 
0040 x = 42;
0041 
0042 label:with (o) {
0043     break label;
0044 }
0045 
0046 with(p) { // We need 'with' here to prevent bypassing of the scope stack by the optimizer
0047     y = x;
0048 }
0049 
0050 shouldBe('y', '42');
0051 
0052 // And a nastier one, also with a catch scope
0053 o = { x : 4 }
0054 p = {};
0055 
0056 x = 42;
0057 
0058 label:try {
0059     throw "a";
0060 } catch (x) {
0061     with (o) {
0062         break label;
0063     }
0064 }
0065 
0066 with(p) { // We need 'with' here to prevent bypassing of the scope stack by the optimizer
0067     y = x;
0068 }
0069 
0070 shouldBe('y', '42');
0071 
0072 //# 108538 -- bind var assignments are proper assignments, not statically
0073 function testVarInCatch()
0074 {
0075    try {
0076       throw "any exception";
0077    } catch (x) { 
0078       var x = 2; // The catches scope is on of the chain, so
0079                  // this should assign there
0080       if (x != 2)
0081         return "FAIL";
0082    }
0083 
0084    // Now we should touch the normal local, which should be unchanged.
0085    if (x == undefined)
0086       return "PASS";
0087    else
0088       return "FAIL #2";
0089 }
0090 
0091 shouldBe('testVarInCatch()', '"PASS"');
0092 
0093 function testVarInWith()
0094 {
0095    o = { x: 42 };
0096    with (o) {
0097       if (x != 42)
0098         return "FAIL"; // Should lookup from 'o'.
0099       var x = 4; // Since o is in scope, should set there, not global.
0100       if (x != 4)
0101         return "FAIL #2";
0102       if (o.x != 4)
0103         return "FAIL #3";
0104    }
0105    if (x != undefined)
0106       return "FAIL #4";
0107 
0108    return "PASS";
0109 }
0110 
0111 shouldBe('testVarInWith()', '"PASS"');
0112 
0113 // Local function injection with eval
0114 function testFunInject()
0115 {
0116     try {
0117         eval("function injected() { return 'PASS'; }");
0118         return injected();
0119     } catch (e) {
0120         return "Threw!";
0121     }
0122 }
0123 
0124 shouldBe('testFunInject()', '"PASS"');
0125 
0126 debug("Done.");