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 (at) netscape.com, pschwartau (at) 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 //------------------------------------------------------------------------------------------------- 26 var UBound = 0; 27 var bug = 77578; 28 var summary = 'Testing eval scope inside a function'; 29 var cnEquals = '='; 30 var status = ''; 31 var statusitems = []; 32 var actual = ''; 33 var actualvalues = []; 34 var expect= ''; 35 var expectedvalues = []; 36 37 38 // various versions of JavaScript - 39 var JS_VER = [100, 110, 120, 130, 140, 150]; 40 41 // Note contrast with local variables i,j,k defined below - 42 var i = 999; 43 var j = 999; 44 var k = 999; 45 46 47 //-------------------------------------------------- 48 test(); 49 //-------------------------------------------------- 50 51 52 function 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 83 function 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 102 function 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 123 function 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 148 function captureThis(varName) 149 { 150 statusitems[UBound] = status; 151 actualvalues[UBound] = varName + cnEquals + actual; 152 expectedvalues[UBound] = varName + cnEquals + expect; 153 UBound++; 154 } 155