1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\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
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
13 * The Original Code is Rhino code, released
\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
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
38 package org.mozilla.javascript;
\r
42 * The class of error objects
\r
46 public class NativeError extends IdScriptable {
\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
56 protected int getIdDefaultAttributes(int id) {
\r
57 if (id == Id_message || id == Id_name) { return EMPTY; }
\r
58 return super.getIdDefaultAttributes(id);
\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
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
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
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
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
90 return super.methodArity(methodId);
\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
98 if (prototypeFlag) {
\r
99 if (methodId == Id_constructor) {
\r
100 return jsConstructor(cx, args, f, thisObj == null);
\r
102 else if (methodId == Id_toString) {
\r
103 return realThis(thisObj, f).toString();
\r
106 return super.execMethod(methodId, f, cx, scope, thisObj, args);
\r
109 private NativeError realThis(Scriptable thisObj, IdFunction f) {
\r
110 while (!(thisObj instanceof NativeError)) {
\r
111 thisObj = nextInstanceCheck(thisObj, f, true);
\r
113 return (NativeError)thisObj;
\r
116 private static Object jsConstructor(Context cx, Object[] args,
\r
117 Function funObj, boolean inNewExpr)
\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
126 public String getClassName() {
\r
130 public String toString() {
\r
131 return getName() + ": " + getMessage();
\r
134 public String getName() {
\r
135 Object val = nameValue;
\r
136 return ScriptRuntime.toString(val != NOT_FOUND ? val
\r
137 : Undefined.instance);
\r
140 public String getMessage() {
\r
141 Object val = messageValue;
\r
142 return ScriptRuntime.toString(val != NOT_FOUND ? val
\r
143 : Undefined.instance);
\r
146 protected int maxInstanceId() { return MAX_INSTANCE_ID; }
\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
160 private static final int
\r
164 MAX_INSTANCE_ID = 2;
\r
166 protected int mapNameToId(String s) {
\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
176 // #/string_id_map#
\r
178 if (id != 0 || !prototypeFlag) { return id; }
\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
192 private static final int
\r
193 Id_constructor = MAX_INSTANCE_ID + 1,
\r
194 Id_toString = MAX_INSTANCE_ID + 2,
\r
196 MAX_PROTOTYPE_ID = MAX_INSTANCE_ID + 2;
\r
198 // #/string_id_map#
\r
200 private Object messageValue = NOT_FOUND;
\r
201 private Object nameValue = NOT_FOUND;
\r
203 private boolean prototypeFlag;
\r