Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / Exceptions / regress-232182.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Mozilla Foundation.
19  * Portions created by the Initial Developer are Copyright (C) 2005
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s): Michael Daumling <daumling@adobe.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 //-----------------------------------------------------------------------------
39
40 var BUGNUMBER = 232182;
41 var summary = 'Display non-ascii characters in JS exceptions';
42 var actual = '';
43 var expect = 'no error';
44
45 printBugNumber(BUGNUMBER);
46 printStatus (summary);
47
48 /*
49  * This test accesses an undefined Unicode symbol. If the code has not been
50  * compiled with JS_C_STRINGS_ARE_UTF8, the thrown error truncates Unicode
51  * characters to bytes. Accessing \u0440\u0441, therefore, results in a
52  * message talking about an undefined variable 'AB' (\x41\x42).
53  */
54 var utf8Enabled = false;
55 try
56 {
57   \u0441\u0442;
58 }
59 catch (e)
60 {
61   utf8Enabled = (e.message.charAt (0) == '\u0441');
62 }
63
64 // Run the tests only if UTF-8 is enabled
65
66 printStatus('UTF-8 is ' + (utf8Enabled?'':'not ') + 'enabled');
67
68 if (!utf8Enabled)
69 {
70   reportCompare('Not run', 'Not run', 'utf8 is not enabled');
71 }
72 else
73 {
74   status = summary + ': Throw Error with Unicode message';
75   expect = 'test \u0440\u0441';
76   try
77   {
78     throw Error (expect);
79   }
80   catch (e)
81   {
82     actual = e.message;
83   }
84   reportCompare(expect, actual, status);
85
86   var inShell = (typeof stringsAreUTF8 == "function");
87   if (!inShell)
88   {
89     inShell = (typeof stringsAreUtf8  == "function");
90     if (inShell)
91     {
92       this.stringsAreUTF8 = stringsAreUtf8;
93       this.testUTF8 = testUtf8;
94     }
95   }
96
97   if (inShell && stringsAreUTF8())
98   {
99     status = summary + ': UTF-8 test: bad UTF-08 sequence';
100     expect = 'Error';
101     actual = 'No error!';
102     try
103     {
104       testUTF8(1);
105     }
106     catch (e)
107     {
108       actual = 'Error';
109     }
110     reportCompare(expect, actual, status);
111
112     status = summary + ': UTF-8 character too big to fit into Unicode surrogate pairs';
113     expect = 'Error';
114     actual = 'No error!';
115     try
116     {
117       testUTF8(2);
118     }
119     catch (e)
120     {
121       actual = 'Error';
122     }
123     reportCompare(expect, actual, status);
124
125     status = summary + ': bad Unicode surrogate character';
126     expect = 'Error';
127     actual = 'No error!';
128     try
129     {
130       testUTF8(3);
131     }
132     catch (e)
133     {
134       actual = 'Error';
135     }
136     reportCompare(expect, actual, status);
137   }
138
139   if (inShell)
140   {
141     status = summary + ': conversion target buffer overrun';
142     expect = 'Error';
143     actual = 'No error!';
144     try
145     {
146       testUTF8(4);
147     }
148     catch (e)
149     {
150       actual = 'Error';
151     }
152     reportCompare(expect, actual, status);
153   }
154 }