initial import
[vuplus_webkit] / Source / JavaScriptCore / tests / mozilla / js1_5 / Expressions / shell.js
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  IS"
8 * 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): pschwartau@netscape.com
20 * Date: 07 February 2001
21 *
22 * Functionality common to Array testing -
23 */
24 //-------------------------------------------------------------------------------------------------
25 var CHAR_LBRACKET = '[';
26 var CHAR_RBRACKET = ']';
27 var CHAR_QT_DBL = '"';
28 var CHAR_QT = "'";
29 var CHAR_NL = '\n';
30 var CHAR_COMMA = ',';
31 var CHAR_SPACE = ' ';
32 var TYPE_STRING = typeof 'abc';
33
34
35 /*
36  * If available, arr.toSource() gives more detail than arr.toString()
37  *
38  * var arr = Array(1,2,'3');
39  *
40  * arr.toSource()
41  * [1, 2, "3"]
42  *
43  * arr.toString()
44  * 1,2,3
45  *
46  * But toSource() doesn't exist in Rhino, so use our own imitation, below -
47  *
48  */
49 function formatArray(arr)
50 {
51   try
52   {
53     return arr.toSource();
54   }
55   catch(e)
56   {
57     return toSource(arr);
58   }
59 }
60
61
62
63 /*
64  * Imitate SpiderMonkey's arr.toSource() method:
65  *
66  * a) Double-quote each array element that is of string type
67  * b) Represent |undefined| and |null| by empty strings
68  * c) Delimit elements by a comma + single space
69  * d) Do not add delimiter at the end UNLESS the last element is |undefined|
70  * e) Add square brackets to the beginning and end of the string
71  */
72 function toSource(arr)
73 {
74   var delim = CHAR_COMMA + CHAR_SPACE;
75   var elt = '';
76   var ret = '';
77   var len = arr.length;
78
79   for (i=0; i<len; i++)
80   {
81     elt = arr[i];
82
83     switch(true)
84     {
85       case (typeof elt === TYPE_STRING) :
86         ret += doubleQuote(elt);
87         break;
88
89       case (elt === undefined || elt === null) :
90         break; // add nothing but the delimiter, below -
91
92       default:
93         ret += elt.toString();
94     }
95
96     if ((i < len-1) || (elt === undefined))
97       ret += delim;
98   }
99
100   return  CHAR_LBRACKET + ret + CHAR_RBRACKET;
101 }
102
103
104 function doubleQuote(text)
105 {
106   return CHAR_QT_DBL + text + CHAR_QT_DBL;
107 }
108
109
110 function singleQuote(text)
111 {
112   return CHAR_QT + text + CHAR_QT;
113 }