Home | History | Annotate | Download | only in Scope
      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): coliver (at) mminternet.com, pschwartau (at) netscape.com
     20 * Date: 2001-07-03
     21 *
     22 * SUMMARY:  Testing scope with nested functions
     23 *
     24 * From correspondence with Christopher Oliver <coliver (at) mminternet.com>:
     25 *
     26 * > Running this test with Rhino produces the following exception:
     27 * >
     28 * > uncaught JavaScript exception: undefined: Cannot find default value for
     29 * > object. (line 3)
     30 * >
     31 * > This is due to a bug in org.mozilla.javascript.NativeCall which doesn't
     32 * > implement toString or valueOf or override getDefaultValue.
     33 * > However, even after I hacked in an implementation of getDefaultValue in
     34 * > NativeCall, Rhino still produces a different result then SpiderMonkey:
     35 * >
     36 * > [object Call]
     37 * > [object Object]
     38 * > [object Call]
     39 *
     40 * Note the results should be:
     41 *
     42 *   [object global]
     43 *   [object Object]
     44 *   [object global]
     45 *
     46 * This is what we are checking for in this testcase -
     47 */
     48 //-----------------------------------------------------------------------------
     49 var UBound = 0;
     50 var bug = '(none)';
     51 var summary = 'Testing scope with nested functions';
     52 var statprefix = 'Section ';
     53 var statsuffix = ' of test -';
     54 var self = this; // capture a reference to the global object;
     55 var cnGlobal = self.toString();
     56 var cnObject = (new Object).toString();
     57 var statusitems = [];
     58 var actualvalues = [];
     59 var expectedvalues = [];
     60 
     61 
     62 function a()
     63 {
     64   function b()
     65   {
     66     capture(this.toString());
     67   }
     68 
     69   this.c = function()
     70   {
     71     capture(this.toString());
     72     b();
     73   }
     74 
     75   b();
     76 }
     77 
     78 
     79 var obj = new a();  // captures actualvalues[0]
     80 obj.c();            // captures actualvalues[1], actualvalues[2]
     81 
     82 
     83 // The values we expect - see introduction above -
     84 expectedvalues[0] = cnGlobal;
     85 expectedvalues[1] = cnObject;
     86 expectedvalues[2] = cnGlobal;
     87 
     88 
     89 
     90 //-----------------------------------------------------------------------------
     91 test();
     92 //-----------------------------------------------------------------------------
     93 
     94 
     95 
     96 function capture(val)
     97 {
     98   actualvalues[UBound] = val;
     99   statusitems[UBound] = getStatus(UBound);
    100   UBound++;
    101 }
    102 
    103 
    104 function test()
    105 {
    106   enterFunc ('test');
    107   printBugNumber (bug);
    108   printStatus (summary);
    109 
    110   for (var i=0; i<UBound; i++)
    111   {
    112     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
    113   }
    114 
    115   exitFunc ('test');
    116 }
    117 
    118 
    119 function getStatus(i)
    120 {
    121   return statprefix + i + statsuffix;
    122 }
    123