michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 15 Feb 2001 michael@0: * michael@0: * SUMMARY: self.eval(str) inside a function michael@0: * NOTE: 'self' is just a variable used to capture the global JS object. michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=68498 michael@0: * See http://bugzilla.mozilla.org/showattachment.cgi?attach_id=25251 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=69441 (!!!) michael@0: * michael@0: * Brendan: michael@0: * michael@0: * "ECMA-262 Edition 3, 10.1.3 requires a FunctionDeclaration parsed as part michael@0: * of a Program by eval to create a property of eval's caller's variable object. michael@0: * This test evals in the body of a with statement, whose scope chain *is* michael@0: * relevant to the effect of parsing the FunctionDeclaration." michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 68498; michael@0: var summary = 'Testing self.eval(str) inside a function'; michael@0: var statprefix = '; currently at expect['; michael@0: var statsuffix = '] within test -'; michael@0: var sToEval=''; michael@0: var actual=[ ]; michael@0: var expect=[ ]; michael@0: michael@0: michael@0: // Capture a reference to the global object - michael@0: var self = this; michael@0: michael@0: // You shouldn't see this global variable's value in any printout - michael@0: var x = 'outer'; michael@0: michael@0: // This function is the heart of the test - michael@0: function f(o,s,x) {with(o) eval(s); return z;}; michael@0: michael@0: // Run-time statements to pass to the eval inside f michael@0: sToEval += 'actual[0] = typeof g;' michael@0: sToEval += 'function g(){actual[1]=(typeof w == "undefined" || w); return x};' michael@0: sToEval += 'actual[2] = w;' michael@0: sToEval += 'actual[3] = typeof g;' michael@0: sToEval += 'var z=g();' michael@0: michael@0: // Set the actual-results array. The next line will set actual[0] - actual[4] in one shot michael@0: actual[4] = f({w:44}, sToEval, 'inner'); michael@0: actual[5] = 'z' in self && z; michael@0: michael@0: michael@0: /* Set the expected-results array. michael@0: * michael@0: * Sample issue: why do we set expect[4] = 'inner'? Look at actual[4]... michael@0: * 1. The return value of f equals z, which is not defined at compile-time michael@0: * 2. At run-time (via with(o) eval(s) inside f), z is defined as the return value of g michael@0: * 3. At run-time (via with(o) eval(s) inside f), g is defined to return x michael@0: * 4. In the scope of with(o), x is undefined michael@0: * 5. Farther up the scope chain, x can be located as an argument of f michael@0: * 6. The value of this argument at run-time is 'inner' michael@0: * 7. Even farther up the scope chain, the name x can be found as a global variable michael@0: * 8. The value of this global variable is 'outer', but we should NOT have gone michael@0: * this far up the scope chain to find x...therefore we expect 'inner' michael@0: */ michael@0: expect[0] = 'function'; michael@0: expect[1] = 44; michael@0: expect[2] = 44; michael@0: expect[3] = 'function'; michael@0: expect[4] = 'inner'; michael@0: expect[5] = false; michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------------------------ michael@0: test(); michael@0: //------------------------------------------------------------------------------------------------- michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: for (var i in expect) michael@0: { michael@0: reportCompare(expect[i], actual[i], getStatus(i)); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: } michael@0: michael@0: michael@0: function getStatus(i) michael@0: { michael@0: return (summary + statprefix + i + statsuffix); michael@0: }