001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * Roger Lawrence
027: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript;
041:
042: /**
043: *
044: * The class of error objects
045: *
046: * ECMA 15.11
047: */
048: final class NativeError extends IdScriptableObject {
049: static final long serialVersionUID = -5338413581437645187L;
050:
051: private static final Object ERROR_TAG = new Object();
052:
053: static void init(Scriptable scope, boolean sealed) {
054: NativeError obj = new NativeError();
055: ScriptableObject.putProperty(obj, "name", "Error");
056: ScriptableObject.putProperty(obj, "message", "");
057: ScriptableObject.putProperty(obj, "fileName", "");
058: ScriptableObject.putProperty(obj, "lineNumber", new Integer(0));
059: obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
060: }
061:
062: static NativeError make(Context cx, Scriptable scope,
063: IdFunctionObject ctorObj, Object[] args) {
064: Scriptable proto = (Scriptable) (ctorObj.get("prototype",
065: ctorObj));
066:
067: NativeError obj = new NativeError();
068: obj.setPrototype(proto);
069: obj.setParentScope(scope);
070:
071: int arglen = args.length;
072: if (arglen >= 1) {
073: ScriptableObject.putProperty(obj, "message", ScriptRuntime
074: .toString(args[0]));
075: if (arglen >= 2) {
076: ScriptableObject.putProperty(obj, "fileName", args[1]);
077: if (arglen >= 3) {
078: int line = ScriptRuntime.toInt32(args[2]);
079: ScriptableObject.putProperty(obj, "lineNumber",
080: new Integer(line));
081: }
082: }
083: }
084: if (arglen < 3
085: && cx
086: .hasFeature(Context.FEATURE_LOCATION_INFORMATION_IN_ERROR)) {
087: // Fill in fileName and lineNumber automatically when not specified
088: // explicitly, see Bugzilla issue #342807
089: int[] linep = new int[1];
090: String fileName = Context.getSourcePositionFromStack(linep);
091: ScriptableObject.putProperty(obj, "lineNumber",
092: new Integer(linep[0]));
093: if (arglen < 2) {
094: ScriptableObject.putProperty(obj, "fileName", fileName);
095: }
096: }
097: return obj;
098: }
099:
100: public String getClassName() {
101: return "Error";
102: }
103:
104: public String toString() {
105: return js_toString(this );
106: }
107:
108: protected void initPrototypeId(int id) {
109: String s;
110: int arity;
111: switch (id) {
112: case Id_constructor:
113: arity = 1;
114: s = "constructor";
115: break;
116: case Id_toString:
117: arity = 0;
118: s = "toString";
119: break;
120: case Id_toSource:
121: arity = 0;
122: s = "toSource";
123: break;
124: default:
125: throw new IllegalArgumentException(String.valueOf(id));
126: }
127: initPrototypeMethod(ERROR_TAG, id, s, arity);
128: }
129:
130: public Object execIdCall(IdFunctionObject f, Context cx,
131: Scriptable scope, Scriptable this Obj, Object[] args) {
132: if (!f.hasTag(ERROR_TAG)) {
133: return super .execIdCall(f, cx, scope, this Obj, args);
134: }
135: int id = f.methodId();
136: switch (id) {
137: case Id_constructor:
138: return make(cx, scope, f, args);
139:
140: case Id_toString:
141: return js_toString(this Obj);
142:
143: case Id_toSource:
144: return js_toSource(cx, scope, this Obj);
145: }
146: throw new IllegalArgumentException(String.valueOf(id));
147: }
148:
149: private static String js_toString(Scriptable this Obj) {
150: return getString(this Obj, "name") + ": "
151: + getString(this Obj, "message");
152: }
153:
154: private static String js_toSource(Context cx, Scriptable scope,
155: Scriptable this Obj) {
156: // Emulation of SpiderMonkey behavior
157: Object name = ScriptableObject.getProperty(this Obj, "name");
158: Object message = ScriptableObject.getProperty(this Obj,
159: "message");
160: Object fileName = ScriptableObject.getProperty(this Obj,
161: "fileName");
162: Object lineNumber = ScriptableObject.getProperty(this Obj,
163: "lineNumber");
164:
165: StringBuffer sb = new StringBuffer();
166: sb.append("(new ");
167: if (name == NOT_FOUND) {
168: name = Undefined.instance;
169: }
170: sb.append(ScriptRuntime.toString(name));
171: sb.append("(");
172: if (message != NOT_FOUND || fileName != NOT_FOUND
173: || lineNumber != NOT_FOUND) {
174: if (message == NOT_FOUND) {
175: message = "";
176: }
177: sb.append(ScriptRuntime.uneval(cx, scope, message));
178: if (fileName != NOT_FOUND || lineNumber != NOT_FOUND) {
179: sb.append(", ");
180: if (fileName == NOT_FOUND) {
181: fileName = "";
182: }
183: sb.append(ScriptRuntime.uneval(cx, scope, fileName));
184: if (lineNumber != NOT_FOUND) {
185: int line = ScriptRuntime.toInt32(lineNumber);
186: if (line != 0) {
187: sb.append(", ");
188: sb.append(ScriptRuntime.toString(line));
189: }
190: }
191: }
192: }
193: sb.append("))");
194: return sb.toString();
195: }
196:
197: private static String getString(Scriptable obj, String id) {
198: Object value = ScriptableObject.getProperty(obj, id);
199: if (value == NOT_FOUND)
200: return "";
201: return ScriptRuntime.toString(value);
202: }
203:
204: protected int findPrototypeId(String s) {
205: int id;
206: // #string_id_map#
207: // #generated# Last update: 2007-05-09 08:15:45 EDT
208: L0: {
209: id = 0;
210: String X = null;
211: int c;
212: int s_length = s.length();
213: if (s_length == 8) {
214: c = s.charAt(3);
215: if (c == 'o') {
216: X = "toSource";
217: id = Id_toSource;
218: } else if (c == 't') {
219: X = "toString";
220: id = Id_toString;
221: }
222: } else if (s_length == 11) {
223: X = "constructor";
224: id = Id_constructor;
225: }
226: if (X != null && X != s && !X.equals(s))
227: id = 0;
228: break L0;
229: }
230: // #/generated#
231: return id;
232: }
233:
234: private static final int Id_constructor = 1, Id_toString = 2,
235: Id_toSource = 3,
236:
237: MAX_PROTOTYPE_ID = 3;
238:
239: // #/string_id_map#
240: }
|