Home | History | Annotate | Download | only in Array
      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 "AS
      8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      9 * 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): igor (at) icesoft.no, pschwartau (at) netscape.com
     20 * Date: 24 September 2001
     21 *
     22 * SUMMARY: Try assigning arr.length = new Number(n)
     23 * From correspondence with Igor Bukanov <igor (at) icesoft.no>
     24 * See http://bugzilla.mozilla.org/show_bug.cgi?id=101488
     25 *
     26 * Without the "new" keyword, assigning arr.length = Number(n) worked.
     27 * But with it, Rhino was giving an error "Inappropriate array length"
     28 * and SpiderMonkey was exiting without giving any error or return value -
     29 *
     30 * Comments on the Rhino code by igor (at) icesoft.no:
     31 *
     32 * jsSet_length requires that the new length value should be an instance
     33 * of Number. But according to Ecma 15.4.5.1, item 12-13, an error should
     34 * be thrown only if ToUint32(length_value) != ToNumber(length_value)
     35 */
     36 //-----------------------------------------------------------------------------
     37 var UBound = 0;
     38 var bug = 101488;
     39 var summary = 'Try assigning arr.length = new Number(n)';
     40 var status = '';
     41 var statusitems = [];
     42 var actual = '';
     43 var actualvalues = [];
     44 var expect= '';
     45 var expectedvalues = [];
     46 var arr = [];
     47 
     48 
     49 status = inSection(1);
     50 arr = Array();
     51 tryThis('arr.length = new Number(1);');
     52 actual = arr.length;
     53 expect = 1;
     54 addThis();
     55 
     56 status = inSection(2);
     57 arr = Array(5);
     58 tryThis('arr.length = new Number(1);');
     59 actual = arr.length;
     60 expect = 1;
     61 addThis();
     62 
     63 status = inSection(3);
     64 arr = Array();
     65 tryThis('arr.length = new Number(17);');
     66 actual = arr.length;
     67 expect = 17;
     68 addThis();
     69 
     70 status = inSection(4);
     71 arr = Array(5);
     72 tryThis('arr.length = new Number(17);');
     73 actual = arr.length;
     74 expect = 17;
     75 addThis();
     76 
     77 
     78 /*
     79  * Also try the above with the "new" keyword before Array().
     80  * Array() and new Array() should be equivalent, by ECMA 15.4.1.1
     81  */
     82 status = inSection(5);
     83 arr = new Array();
     84 tryThis('arr.length = new Number(1);');
     85 actual = arr.length;
     86 expect = 1;
     87 addThis();
     88 
     89 status = inSection(6);
     90 arr = new Array(5);
     91 tryThis('arr.length = new Number(1);');
     92 actual = arr.length;
     93 expect = 1;
     94 addThis();
     95 
     96 arr = new Array();
     97 tryThis('arr.length = new Number(17);');
     98 actual = arr.length;
     99 expect = 17;
    100 addThis();
    101 
    102 status = inSection(7);
    103 arr = new Array(5);
    104 tryThis('arr.length = new Number(17);');
    105 actual = arr.length;
    106 expect = 17;
    107 addThis();
    108 
    109 
    110 
    111 //-----------------------------------------------------------------------------
    112 test();
    113 //-----------------------------------------------------------------------------
    114 
    115 
    116 
    117 function tryThis(s)
    118 {
    119   try
    120   {
    121     eval(s);
    122   }
    123   catch(e)
    124   {
    125     // keep going
    126   }
    127 }
    128 
    129 
    130 function addThis()
    131 {
    132   statusitems[UBound] = status;
    133   actualvalues[UBound] = actual;
    134   expectedvalues[UBound] = expect;
    135   UBound++;
    136 }
    137 
    138 
    139 function test()
    140 {
    141   enterFunc ('test');
    142   printBugNumber (bug);
    143   printStatus (summary);
    144 
    145   for (var i=0; i<UBound; i++)
    146   {
    147     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
    148   }
    149 
    150   exitFunc ('test');
    151 }
    152