]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/Function/regress-58274.js
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is JavaScript Engine testing utilities.
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2002
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s): rogerl@netscape.com, pschwartau@netscape.com
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the NPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the NPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK *****
38 * SUMMARY: Testing functions with double-byte names
39 * See http://bugzilla.mozilla.org/show_bug.cgi?id=58274
41 * Here is a sample of the problem:
43 * js> function f\u02B1 () {}
45 * js> f\u02B1.toSource();
48 * js> f\u02B1.toSource().toSource();
49 * (new String("function f\xB1() {}"))
52 * See how the high-byte information (the 02) has been lost?
53 * The same thing was happening with the toString() method:
55 * js> f\u02B1.toString();
60 * js> f\u02B1.toString().toSource();
61 * (new String("\nfunction f\xB1() {\n}\n"))
64 //-----------------------------------------------------------------------------
67 var summary
= 'Testing functions with double-byte names';
68 var ERR
= 'UNEXPECTED ERROR! \n';
69 var ERR_MALFORMED_NAME
= ERR
+ 'Could not find function name in: \n\n';
73 var actualvalues
= [];
75 var expectedvalues
= [];
80 sEval
= "function f\u02B2() {return 42;}";
82 sName
= getFunctionName(f
\u02B2);
84 // Test function call -
85 status
= inSection(1);
90 // Test both characters of function name -
91 status
= inSection(2);
96 status
= inSection(3);
103 sEval
= "function f\u02B2\u0AAA () {return 84;}";
105 sName
= getFunctionName(f
\u02B2\u0AAA);
107 // Test function call -
108 status
= inSection(4);
109 actual
= f
\u02B2\u0AAA();
113 // Test all three characters of function name -
114 status
= inSection(5);
119 status
= inSection(6);
124 status
= inSection(7);
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
139 * Goal: test that f.toString() contains the proper function name.
141 * Note, however, f.toString() is implementation-independent. For example,
142 * it may begin with '\nfunction' instead of 'function'. Therefore we use
143 * a regexp to make sure we extract the name properly.
145 * Here we assume that f has been defined by means of a function statement,
146 * and not a function expression (where it wouldn't have to have a name).
148 * Rhino uses a Unicode representation for f.toString(); whereas
149 * SpiderMonkey uses an ASCII representation, putting escape sequences
150 * for non-ASCII characters. For example, if a function is called f\u02B1,
151 * then in Rhino the toString() method will present a 2-character Unicode
152 * string for its name, whereas SpiderMonkey will present a 7-character
153 * ASCII string for its name: the string literal 'f\u02B1'.
155 * So we force the lexer to condense the string before using it.
156 * This will give uniform results in Rhino and SpiderMonkey.
158 function getFunctionName(f
)
160 var s
= condenseStr(f
.toString());
161 var re
= /\s*function\s+(\S+)\s*\(/;
162 var arr
= s
.match(re
);
164 if (!(arr
&& arr
[1]))
165 return ERR_MALFORMED_NAME
+ s
;
171 * This function is the opposite of functions like escape(), which take
172 * Unicode characters and return escape sequences for them. Here, we force
173 * the lexer to turn escape sequences back into single characters.
175 * Note we can't simply do |eval(str)|, since in practice |str| will be an
176 * identifier somewhere in the program (e.g. a function name); thus |eval(str)|
177 * would return the object that the identifier represents: not what we want.
179 * So we surround |str| lexicographically with quotes to force the lexer to
180 * evaluate it as a string. Have to strip out any linefeeds first, however -
182 function condenseStr(str
)
185 * You won't be able to do the next step if |str| has
186 * any carriage returns or linefeeds in it. For example:
188 * js> eval("'" + '\nHello' + "'");
189 * 1: SyntaxError: unterminated string literal:
193 * So replace them with the empty string -
195 str
= str
.replace(/[\r\n]/g, '')
196 return eval("'" + str
+ "'");
202 statusitems
[UBound
] = status
;
203 actualvalues
[UBound
] = actual
;
204 expectedvalues
[UBound
] = expect
;
213 printStatus(summary
);
215 for (var i
=0; i
<UBound
; i
++)
217 reportCompare(expectedvalues
[i
], actualvalues
[i
], statusitems
[i
]);