1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is JavaScript Engine testing utilities.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corp.
19
* Portions created by the Initial Developer are Copyright (C) 2002
20
* the Initial Developer. All Rights Reserved.
23
* rogerl@netscape.com, pschwartau@netscape.com
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
42
* SUMMARY: Testing functions with double-byte names
43
* See http://bugzilla.mozilla.org/show_bug.cgi?id=58274
45
* Here is a sample of the problem:
47
* js> function f\u02B1 () {}
49
* js> f\u02B1.toSource();
52
* js> f\u02B1.toSource().toSource();
53
* (new String("function f\xB1() {}"))
56
* See how the high-byte information (the 02) has been lost?
57
* The same thing was happening with the toString() method:
59
* js> f\u02B1.toString();
64
* js> f\u02B1.toString().toSource();
65
* (new String("\nfunction f\xB1() {\n}\n"))
68
//-----------------------------------------------------------------------------
69
var gTestfile = 'regress-58274.js';
71
var BUGNUMBER = 58274;
72
var summary = 'Testing functions with double-byte names';
73
var ERR = 'UNEXPECTED ERROR! \n';
74
var ERR_MALFORMED_NAME = ERR + 'Could not find function name in: \n\n';
78
var actualvalues = [];
80
var expectedvalues = [];
85
sEval = "function f\u02B2() {return 42;}";
87
sName = getFunctionName(f\u02B2);
89
// Test function call -
90
status = inSection(1);
95
// Test both characters of function name -
96
status = inSection(2);
101
status = inSection(3);
108
sEval = "function f\u02B2\u0AAA () {return 84;}";
110
sName = getFunctionName(f\u02B2\u0AAA);
112
// Test function call -
113
status = inSection(4);
114
actual = f\u02B2\u0AAA();
118
// Test all three characters of function name -
119
status = inSection(5);
124
status = inSection(6);
129
status = inSection(7);
137
//-----------------------------------------------------------------------------
139
//-----------------------------------------------------------------------------
144
* Goal: test that f.toString() contains the proper function name.
146
* Note, however, f.toString() is implementation-independent. For example,
147
* it may begin with '\nfunction' instead of 'function'. Therefore we use
148
* a regexp to make sure we extract the name properly.
150
* Here we assume that f has been defined by means of a function statement,
151
* and not a function expression (where it wouldn't have to have a name).
153
* Rhino uses a Unicode representation for f.toString(); whereas
154
* SpiderMonkey uses an ASCII representation, putting escape sequences
155
* for non-ASCII characters. For example, if a function is called f\u02B1,
156
* then in Rhino the toString() method will present a 2-character Unicode
157
* string for its name, whereas SpiderMonkey will present a 7-character
158
* ASCII string for its name: the string literal 'f\u02B1'.
160
* So we force the lexer to condense the string before using it.
161
* This will give uniform results in Rhino and SpiderMonkey.
163
function getFunctionName(f)
165
var s = condenseStr(f.toString());
166
var re = /\s*function\s+(\S+)\s*\(/;
167
var arr = s.match(re);
169
if (!(arr && arr[1]))
170
return ERR_MALFORMED_NAME + s;
176
* This function is the opposite of functions like escape(), which take
177
* Unicode characters and return escape sequences for them. Here, we force
178
* the lexer to turn escape sequences back into single characters.
180
* Note we can't simply do |eval(str)|, since in practice |str| will be an
181
* identifier somewhere in the program (e.g. a function name); thus |eval(str)|
182
* would return the object that the identifier represents: not what we want.
184
* So we surround |str| lexicographically with quotes to force the lexer to
185
* evaluate it as a string. Have to strip out any linefeeds first, however -
187
function condenseStr(str)
190
* You won't be able to do the next step if |str| has
191
* any carriage returns or linefeeds in it. For example:
193
* js> eval("'" + '\nHello' + "'");
194
* 1: SyntaxError: unterminated string literal:
198
* So replace them with the empty string -
200
str = str.replace(/[\r\n]/g, '')
201
return eval("'" + str + "'");
207
statusitems[UBound] = status;
208
actualvalues[UBound] = actual;
209
expectedvalues[UBound] = expect;
217
printBugNumber(BUGNUMBER);
218
printStatus(summary);
220
for (var i=0; i<UBound; i++)
222
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);