2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / NativeError.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\r
3  * The contents of this file are subject to the Netscape Public\r
4  * License Version 1.1 (the "License"); you may not use this file\r
5  * except in compliance with the License. You may obtain a copy of\r
6  * the License at http://www.mozilla.org/NPL/\r
7  *\r
8  * Software distributed under the License is distributed on an "AS\r
9  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr\r
10  * implied. See the License for the specific language governing\r
11  * rights and limitations under the License.\r
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\r
16  * The Initial Developer of the Original Code is Netscape\r
17  * Communications Corporation.  Portions created by Netscape are\r
18  * Copyright (C) 1997-1999 Netscape Communications Corporation. All\r
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Igor Bukanov\r
23  * Roger Lawrence\r
24  * \r
25  * Alternatively, the contents of this file may be used under the\r
26  * terms of the GNU Public License (the "GPL"), in which case the\r
27  * provisions of the GPL are applicable instead of those above.\r
28  * If you wish to allow use of your version of this file only\r
29  * under the terms of the GPL and not to allow others to use your\r
30  * version of this file under the NPL, indicate your decision by\r
31  * deleting the provisions above and replace them with the notice\r
32  * and other provisions required by the GPL.  If you do not delete\r
33  * the provisions above, a recipient may use your version of this\r
34  * file under either the NPL or the GPL.\r
35  */\r
36 \r
37 \r
38 package org.mozilla.javascript;\r
39 \r
40 /**\r
41  *\r
42  * The class of error objects\r
43  *\r
44  *  ECMA 15.11\r
45  */\r
46 public class NativeError extends IdScriptable {\r
47 \r
48     public static void init(Context cx, Scriptable scope, boolean sealed) {\r
49         NativeError obj = new NativeError();\r
50         obj.prototypeFlag = true;\r
51         obj.messageValue = "";\r
52         obj.nameValue = "Error";\r
53         obj.addAsPrototype(MAX_PROTOTYPE_ID, cx, scope, sealed);\r
54     }\r
55     \r
56     protected int getIdDefaultAttributes(int id) {\r
57         if (id == Id_message || id == Id_name) { return EMPTY; }\r
58         return super.getIdDefaultAttributes(id);\r
59     }\r
60 \r
61     protected boolean hasIdValue(int id) {\r
62         if (id == Id_message) { return messageValue != NOT_FOUND; }\r
63         if (id == Id_name) { return nameValue != NOT_FOUND; }\r
64         return super.hasIdValue(id);\r
65     }\r
66 \r
67     protected Object getIdValue(int id) {\r
68         if (id == Id_message) { return messageValue; }\r
69         if (id == Id_name) { return nameValue; }\r
70         return super.getIdValue(id);\r
71     }\r
72 \r
73     protected void setIdValue(int id, Object value) {\r
74         if (id == Id_message) { messageValue = value; return; }\r
75         if (id == Id_name) { nameValue = value; return; }\r
76         super.setIdValue(id, value);\r
77     }\r
78 \r
79     protected void deleteIdValue(int id) {\r
80         if (id == Id_message) { messageValue = NOT_FOUND; return; }\r
81         if (id == Id_name) { nameValue = NOT_FOUND; return; }\r
82         super.deleteIdValue(id);\r
83     }\r
84 \r
85     public int methodArity(int methodId) {\r
86         if (prototypeFlag) {\r
87             if (methodId == Id_constructor) return 1;\r
88             if (methodId == Id_toString) return 0;\r
89         }\r
90         return super.methodArity(methodId);\r
91     }\r
92 \r
93     public Object execMethod\r
94         (int methodId, IdFunction f,\r
95          Context cx, Scriptable scope, Scriptable thisObj, Object[] args)\r
96         throws JavaScriptException\r
97     {\r
98         if (prototypeFlag) {\r
99             if (methodId == Id_constructor) {\r
100                 return jsConstructor(cx, args, f, thisObj == null);\r
101             }\r
102             else if (methodId == Id_toString) {\r
103                 return realThis(thisObj, f).toString();\r
104             }\r
105         }\r
106         return super.execMethod(methodId, f, cx, scope, thisObj, args);\r
107     }\r
108 \r
109     private NativeError realThis(Scriptable thisObj, IdFunction f) {\r
110         while (!(thisObj instanceof NativeError)) {\r
111             thisObj = nextInstanceCheck(thisObj, f, true);\r
112         }\r
113         return (NativeError)thisObj;\r
114     }\r
115 \r
116     private static Object jsConstructor(Context cx, Object[] args, \r
117                                         Function funObj, boolean inNewExpr)\r
118     {\r
119         NativeError result = new NativeError();\r
120         if (args.length >= 1) \r
121             result.messageValue = ScriptRuntime.toString(args[0]);\r
122         result.setPrototype(getClassPrototype(funObj, "Error"));\r
123         return result;\r
124     }\r
125     \r
126     public String getClassName() { \r
127         return "Error"; \r
128     }\r
129 \r
130     public String toString() {\r
131         return getName() + ": " + getMessage();\r
132     }\r
133     \r
134     public String getName() {\r
135         Object val = nameValue;\r
136         return ScriptRuntime.toString(val != NOT_FOUND ? val \r
137                                                        : Undefined.instance);\r
138     }\r
139     \r
140     public String getMessage() {\r
141         Object val = messageValue;\r
142         return ScriptRuntime.toString(val != NOT_FOUND ? val \r
143                                                        : Undefined.instance);\r
144     }    \r
145 \r
146     protected int maxInstanceId() { return MAX_INSTANCE_ID; }\r
147 \r
148     protected String getIdName(int id) {\r
149         if (id == Id_message) { return "message"; }\r
150         if (id == Id_name) { return "name"; }\r
151         if (prototypeFlag) {\r
152             if (id == Id_constructor) return "constructor";\r
153             if (id == Id_toString) return "toString";\r
154         }\r
155         return null;        \r
156     }\r
157     \r
158 // #string_id_map#\r
159 \r
160     private static final int\r
161         Id_message               = 1,\r
162         Id_name                  = 2,\r
163         \r
164         MAX_INSTANCE_ID          = 2;\r
165 \r
166     protected int mapNameToId(String s) {\r
167         int id;\r
168 // #generated# Last update: 2001-05-19 21:55:23 CEST\r
169         L0: { id = 0; String X = null;\r
170             int s_length = s.length();\r
171             if (s_length==4) { X="name";id=Id_name; }\r
172             else if (s_length==7) { X="message";id=Id_message; }\r
173             if (X!=null && X!=s && !X.equals(s)) id = 0;\r
174         }\r
175 // #/generated#\r
176 // #/string_id_map#\r
177 \r
178         if (id != 0 || !prototypeFlag) { return id; }\r
179 \r
180 // #string_id_map#\r
181 // #generated# Last update: 2001-05-19 21:55:23 CEST\r
182         L0: { id = 0; String X = null;\r
183             int s_length = s.length();\r
184             if (s_length==8) { X="toString";id=Id_toString; }\r
185             else if (s_length==11) { X="constructor";id=Id_constructor; }\r
186             if (X!=null && X!=s && !X.equals(s)) id = 0;\r
187         }\r
188 // #/generated#\r
189         return id;\r
190     }\r
191 \r
192     private static final int\r
193         Id_constructor    = MAX_INSTANCE_ID + 1,\r
194         Id_toString       = MAX_INSTANCE_ID + 2,\r
195         \r
196         MAX_PROTOTYPE_ID  = MAX_INSTANCE_ID + 2;\r
197 \r
198 // #/string_id_map#\r
199     \r
200     private Object messageValue = NOT_FOUND;\r
201     private Object nameValue = NOT_FOUND;\r
202 \r
203     private boolean prototypeFlag;\r
204 }\r