darin | cd553de | 2003-10-13 02:05:37 +0000 | [diff] [blame] | 1 | /* ***** BEGIN LICENSE BLOCK ***** |
| 2 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 | * |
| 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/ |
| 8 | * |
| 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 |
| 12 | * License. |
| 13 | * |
| 14 | * The Original Code is JavaScript Engine testing utilities. |
| 15 | * |
| 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. |
| 19 | * |
| 20 | * Contributor(s): rogerl@netscape.com, pschwartau@netscape.com |
| 21 | * |
| 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. |
| 33 | * |
| 34 | * ***** END LICENSE BLOCK ***** |
| 35 | * |
| 36 | * |
| 37 | * Date: 15 July 2002 |
| 38 | * SUMMARY: Testing functions with double-byte names |
| 39 | * See http://bugzilla.mozilla.org/show_bug.cgi?id=58274 |
| 40 | * |
| 41 | * Here is a sample of the problem: |
| 42 | * |
| 43 | * js> function f\u02B1 () {} |
| 44 | * |
| 45 | * js> f\u02B1.toSource(); |
| 46 | * function f¦() {} |
| 47 | * |
| 48 | * js> f\u02B1.toSource().toSource(); |
| 49 | * (new String("function f\xB1() {}")) |
| 50 | * |
| 51 | * |
| 52 | * See how the high-byte information (the 02) has been lost? |
| 53 | * The same thing was happening with the toString() method: |
| 54 | * |
| 55 | * js> f\u02B1.toString(); |
| 56 | * |
| 57 | * function f¦() { |
| 58 | * } |
| 59 | * |
| 60 | * js> f\u02B1.toString().toSource(); |
| 61 | * (new String("\nfunction f\xB1() {\n}\n")) |
| 62 | * |
| 63 | */ |
| 64 | //----------------------------------------------------------------------------- |
| 65 | var UBound = 0; |
| 66 | var bug = 58274; |
| 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'; |
| 70 | var status = ''; |
| 71 | var statusitems = []; |
| 72 | var actual = ''; |
| 73 | var actualvalues = []; |
| 74 | var expect= ''; |
| 75 | var expectedvalues = []; |
| 76 | var sEval; |
| 77 | var sName; |
| 78 | |
| 79 | |
| 80 | sEval = "function f\u02B2() {return 42;}"; |
| 81 | eval(sEval); |
| 82 | sName = getFunctionName(f\u02B2); |
| 83 | |
| 84 | // Test function call - |
| 85 | status = inSection(1); |
| 86 | actual = f\u02B2(); |
| 87 | expect = 42; |
| 88 | addThis(); |
| 89 | |
| 90 | // Test both characters of function name - |
| 91 | status = inSection(2); |
| 92 | actual = sName[0]; |
| 93 | expect = sEval[9]; |
| 94 | addThis(); |
| 95 | |
| 96 | status = inSection(3); |
| 97 | actual = sName[1]; |
| 98 | expect = sEval[10]; |
| 99 | addThis(); |
| 100 | |
| 101 | |
| 102 | |
| 103 | sEval = "function f\u02B2\u0AAA () {return 84;}"; |
| 104 | eval(sEval); |
| 105 | sName = getFunctionName(f\u02B2\u0AAA); |
| 106 | |
| 107 | // Test function call - |
| 108 | status = inSection(4); |
| 109 | actual = f\u02B2\u0AAA(); |
| 110 | expect = 84; |
| 111 | addThis(); |
| 112 | |
| 113 | // Test all three characters of function name - |
| 114 | status = inSection(5); |
| 115 | actual = sName[0]; |
| 116 | expect = sEval[9]; |
| 117 | addThis(); |
| 118 | |
| 119 | status = inSection(6); |
| 120 | actual = sName[1]; |
| 121 | expect = sEval[10]; |
| 122 | addThis(); |
| 123 | |
| 124 | status = inSection(7); |
| 125 | actual = sName[2]; |
| 126 | expect = sEval[11]; |
| 127 | addThis(); |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | //----------------------------------------------------------------------------- |
| 133 | test(); |
| 134 | //----------------------------------------------------------------------------- |
| 135 | |
| 136 | |
| 137 | |
| 138 | /* |
| 139 | * Goal: test that f.toString() contains the proper function name. |
| 140 | * |
| 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. |
| 144 | * |
| 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). |
| 147 | * |
| 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'. |
| 154 | * |
| 155 | * So we force the lexer to condense the string before using it. |
| 156 | * This will give uniform results in Rhino and SpiderMonkey. |
| 157 | */ |
| 158 | function getFunctionName(f) |
| 159 | { |
| 160 | var s = condenseStr(f.toString()); |
| 161 | var re = /\s*function\s+(\S+)\s*\(/; |
| 162 | var arr = s.match(re); |
| 163 | |
| 164 | if (!(arr && arr[1])) |
| 165 | return ERR_MALFORMED_NAME + s; |
| 166 | return arr[1]; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /* |
| 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. |
| 174 | * |
| 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. |
| 178 | * |
| 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 - |
| 181 | */ |
| 182 | function condenseStr(str) |
| 183 | { |
| 184 | /* |
| 185 | * You won't be able to do the next step if |str| has |
| 186 | * any carriage returns or linefeeds in it. For example: |
| 187 | * |
| 188 | * js> eval("'" + '\nHello' + "'"); |
| 189 | * 1: SyntaxError: unterminated string literal: |
| 190 | * 1: ' |
| 191 | * 1: ^ |
| 192 | * |
| 193 | * So replace them with the empty string - |
| 194 | */ |
| 195 | str = str.replace(/[\r\n]/g, '') |
| 196 | return eval("'" + str + "'"); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | function addThis() |
| 201 | { |
| 202 | statusitems[UBound] = status; |
| 203 | actualvalues[UBound] = actual; |
| 204 | expectedvalues[UBound] = expect; |
| 205 | UBound++; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | function test() |
| 210 | { |
| 211 | enterFunc('test'); |
| 212 | printBugNumber(bug); |
| 213 | printStatus(summary); |
| 214 | |
| 215 | for (var i=0; i<UBound; i++) |
| 216 | { |
| 217 | reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 218 | } |
| 219 | |
| 220 | exitFunc ('test'); |
| 221 | } |