1/* 2* The contents of this file are subject to the Netscape Public 3* License Version 1.1 (the "License"); you may not use this file 4* except in compliance with the License. You may obtain a copy of 5* the License at http://www.mozilla.org/NPL/ 6* 7* Software distributed under the License is distributed on an 8* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9* or implied. See the License for the specific language governing 10* rights and limitations under the License. 11* 12* The Original Code is mozilla.org code. 13* 14* The Initial Developer of the Original Code is Netscape 15* Communications Corporation. Portions created by Netscape are 16* Copyright (C) 1998 Netscape Communications Corporation. 17* All Rights Reserved. 18* 19* Contributor(s): jrgm@netscape.com, pschwartau@netscape.com 20* Date: 2001-07-11 21* 22* SUMMARY: Testing eval scope inside a function. 23* See http://bugzilla.mozilla.org/show_bug.cgi?id=77578 24*/ 25//------------------------------------------------------------------------------------------------- 26var UBound = 0; 27var bug = 77578; 28var summary = 'Testing eval scope inside a function'; 29var cnEquals = '='; 30var status = ''; 31var statusitems = []; 32var actual = ''; 33var actualvalues = []; 34var expect= ''; 35var expectedvalues = []; 36 37 38// various versions of JavaScript - 39var JS_VER = [100, 110, 120, 130, 140, 150]; 40 41// Note contrast with local variables i,j,k defined below - 42var i = 999; 43var j = 999; 44var k = 999; 45 46 47//-------------------------------------------------- 48test(); 49//-------------------------------------------------- 50 51 52function test() 53{ 54 enterFunc ('test'); 55 printBugNumber (bug); 56 printStatus (summary); 57 58 // Run tests A,B,C on each version of JS and store results 59 for (var n=0; n!=JS_VER.length; n++) 60 { 61 testA(JS_VER[n]); 62 } 63 for (var n=0; n!=JS_VER.length; n++) 64 { 65 testB(JS_VER[n]); 66 } 67 for (var n=0; n!=JS_VER.length; n++) 68 { 69 testC(JS_VER[n]); 70 } 71 72 73 // Compare actual values to expected values - 74 for (var i=0; i<UBound; i++) 75 { 76 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 77 } 78 79 exitFunc ('test'); 80} 81 82 83function testA(ver) 84{ 85 // Set the version of JS to test - 86 version(ver); 87 88 // eval the test, so it compiles AFTER version() has executed - 89 var sTestScript = ""; 90 91 // Define a local variable i 92 sTestScript += "status = 'Section A of test; JS ' + ver/100;"; 93 sTestScript += "var i=1;"; 94 sTestScript += "actual = eval('i');"; 95 sTestScript += "expect = 1;"; 96 sTestScript += "captureThis('i');"; 97 98 eval(sTestScript); 99} 100 101 102function testB(ver) 103{ 104 // Set the version of JS to test - 105 version(ver); 106 107 // eval the test, so it compiles AFTER version() has executed - 108 var sTestScript = ""; 109 110 // Define a local for-loop iterator j 111 sTestScript += "status = 'Section B of test; JS ' + ver/100;"; 112 sTestScript += "for(var j=1; j<2; j++)"; 113 sTestScript += "{"; 114 sTestScript += " actual = eval('j');"; 115 sTestScript += "};"; 116 sTestScript += "expect = 1;"; 117 sTestScript += "captureThis('j');"; 118 119 eval(sTestScript); 120} 121 122 123function testC(ver) 124{ 125 // Set the version of JS to test - 126 version(ver); 127 128 // eval the test, so it compiles AFTER version() has executed - 129 var sTestScript = ""; 130 131 // Define a local variable k in a try-catch block - 132 sTestScript += "status = 'Section C of test; JS ' + ver/100;"; 133 sTestScript += "try"; 134 sTestScript += "{"; 135 sTestScript += " var k=1;"; 136 sTestScript += " actual = eval('k');"; 137 sTestScript += "}"; 138 sTestScript += "catch(e)"; 139 sTestScript += "{"; 140 sTestScript += "};"; 141 sTestScript += "expect = 1;"; 142 sTestScript += "captureThis('k');"; 143 144 eval(sTestScript); 145} 146 147 148function captureThis(varName) 149{ 150 statusitems[UBound] = status; 151 actualvalues[UBound] = varName + cnEquals + actual; 152 expectedvalues[UBound] = varName + cnEquals + expect; 153 UBound++; 154} 155