scope-003.js (2506B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * Date: 2001-07-03 8 * 9 * SUMMARY: Testing scope with nested functions 10 * 11 * From correspondence with Christopher Oliver <coliver@mminternet.com>: 12 * 13 * > Running this test with Rhino produces the following exception: 14 * > 15 * > uncaught JavaScript exception: undefined: Cannot find default value for 16 * > object. (line 3) 17 * > 18 * > This is due to a bug in org.mozilla.javascript.NativeCall which doesn't 19 * > implement toString or valueOf or override getDefaultValue. 20 * > However, even after I hacked in an implementation of getDefaultValue in 21 * > NativeCall, Rhino still produces a different result then SpiderMonkey: 22 * > 23 * > [object Call] 24 * > [object Object] 25 * > [object Call] 26 * 27 * Note the results should be: 28 * 29 * [object global] 30 * [object Object] 31 * [object global] 32 * 33 * This is what we are checking for in this testcase - 34 */ 35 //----------------------------------------------------------------------------- 36 var UBound = 0; 37 var BUGNUMBER = '(none)'; 38 var summary = 'Testing scope with nested functions'; 39 var statprefix = 'Section '; 40 var statsuffix = ' of test -'; 41 var self = this; // capture a reference to the global object; 42 var cnGlobal = self.toString(); 43 var cnObject = (new Object).toString(); 44 var statusitems = []; 45 var actualvalues = []; 46 var expectedvalues = []; 47 48 49 function a() 50 { 51 function b() 52 { 53 capture(this.toString()); 54 } 55 56 this.c = function() 57 { 58 capture(this.toString()); 59 b(); 60 } 61 62 b(); 63 } 64 65 66 var obj = new a(); // captures actualvalues[0] 67 obj.c(); // captures actualvalues[1], actualvalues[2] 68 69 70 // The values we expect - see introduction above - 71 expectedvalues[0] = cnGlobal; 72 expectedvalues[1] = cnObject; 73 expectedvalues[2] = cnGlobal; 74 75 76 77 //----------------------------------------------------------------------------- 78 test(); 79 //----------------------------------------------------------------------------- 80 81 82 83 function capture(val) 84 { 85 actualvalues[UBound] = val; 86 statusitems[UBound] = getStatus(UBound); 87 UBound++; 88 } 89 90 91 function test() 92 { 93 printBugNumber(BUGNUMBER); 94 printStatus (summary); 95 96 for (var i=0; i<UBound; i++) 97 { 98 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); 99 } 100 } 101 102 103 function getStatus(i) 104 { 105 return statprefix + i + statsuffix; 106 }