| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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 <[email protected]>: |
| 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 gTestfile = 'scope-003.js'; |
| 37 | var UBound = 0; |
| 38 | var BUGNUMBER = '(none)'; |
| 39 | var summary = 'Testing scope with nested functions'; |
| 40 | var statprefix = 'Section '; |
| 41 | var statsuffix = ' of test -'; |
| 42 | var self = this; // capture a reference to the global object; |
| 43 | var cnGlobal = self.toString(); |
| 44 | var cnObject = (new Object).toString(); |
| 45 | var statusitems = []; |
| 46 | var actualvalues = []; |
| 47 | var expectedvalues = []; |
| 48 | |
| 49 | |
| 50 | function a() |
| 51 | { |
| 52 | function b() |
| 53 | { |
| 54 | capture(this.toString()); |
| 55 | } |
| 56 | |
| 57 | this.c = function() |
| 58 | { |
| 59 | capture(this.toString()); |
| 60 | b(); |
| 61 | } |
| 62 | |
| 63 | b(); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | var obj = new a(); // captures actualvalues[0] |
| 68 | obj.c(); // captures actualvalues[1], actualvalues[2] |
| 69 | |
| 70 | |
| 71 | // The values we expect - see introduction above - |
| 72 | expectedvalues[0] = cnGlobal; |
| 73 | expectedvalues[1] = cnObject; |
| 74 | expectedvalues[2] = cnGlobal; |
| 75 | |
| 76 | |
| 77 | |
| 78 | //----------------------------------------------------------------------------- |
| 79 | test(); |
| 80 | //----------------------------------------------------------------------------- |
| 81 | |
| 82 | |
| 83 | |
| 84 | function capture(val) |
| 85 | { |
| 86 | actualvalues[UBound] = val; |
| 87 | statusitems[UBound] = getStatus(UBound); |
| 88 | UBound++; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | function test() |
| 93 | { |
| 94 | enterFunc ('test'); |
| 95 | printBugNumber(BUGNUMBER); |
| 96 | printStatus (summary); |
| 97 | |
| 98 | for (var i=0; i<UBound; i++) |
| 99 | { |
| 100 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 101 | } |
| 102 | |
| 103 | exitFunc ('test'); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | function getStatus(i) |
| 108 | { |
| 109 | return statprefix + i + statsuffix; |
| 110 | } |