]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Error.cpp
JavaScriptCore-521.tar.gz
[apple/javascriptcore.git] / runtime / Error.cpp
1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
5 * Copyright (C) 2007 Eric Seidel (eric@webkit.org)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24 #include "config.h"
25 #include "Error.h"
26
27 #include "ConstructData.h"
28 #include "ErrorConstructor.h"
29 #include "JSGlobalObject.h"
30 #include "JSObject.h"
31 #include "JSString.h"
32 #include "NativeErrorConstructor.h"
33
34 namespace JSC {
35
36 const char* expressionBeginOffsetPropertyName = "expressionBeginOffset";
37 const char* expressionCaretOffsetPropertyName = "expressionCaretOffset";
38 const char* expressionEndOffsetPropertyName = "expressionEndOffset";
39
40 JSObject* Error::create(ExecState* exec, ErrorType type, const UString& message, int lineNumber, intptr_t sourceID, const UString& sourceURL)
41 {
42 JSObject* constructor;
43 const char* name;
44 switch (type) {
45 case EvalError:
46 constructor = exec->lexicalGlobalObject()->evalErrorConstructor();
47 name = "Evaluation error";
48 break;
49 case RangeError:
50 constructor = exec->lexicalGlobalObject()->rangeErrorConstructor();
51 name = "Range error";
52 break;
53 case ReferenceError:
54 constructor = exec->lexicalGlobalObject()->referenceErrorConstructor();
55 name = "Reference error";
56 break;
57 case SyntaxError:
58 constructor = exec->lexicalGlobalObject()->syntaxErrorConstructor();
59 name = "Syntax error";
60 break;
61 case TypeError:
62 constructor = exec->lexicalGlobalObject()->typeErrorConstructor();
63 name = "Type error";
64 break;
65 case URIError:
66 constructor = exec->lexicalGlobalObject()->URIErrorConstructor();
67 name = "URI error";
68 break;
69 default:
70 constructor = exec->lexicalGlobalObject()->errorConstructor();
71 name = "Error";
72 break;
73 }
74
75 ArgList args;
76 if (message.isEmpty())
77 args.append(jsString(exec, name));
78 else
79 args.append(jsString(exec, message));
80 ConstructData constructData;
81 ConstructType constructType = constructor->getConstructData(constructData);
82 JSObject* error = construct(exec, constructor, constructType, constructData, args);
83
84 if (lineNumber != -1)
85 error->putWithAttributes(exec, Identifier(exec, "line"), jsNumber(exec, lineNumber), ReadOnly | DontDelete);
86 if (sourceID != -1)
87 error->putWithAttributes(exec, Identifier(exec, "sourceId"), jsNumber(exec, sourceID), ReadOnly | DontDelete);
88 if (!sourceURL.isNull())
89 error->putWithAttributes(exec, Identifier(exec, "sourceURL"), jsString(exec, sourceURL), ReadOnly | DontDelete);
90
91 return error;
92 }
93
94 JSObject* Error::create(ExecState* exec, ErrorType type, const char* message)
95 {
96 return create(exec, type, message, -1, -1, NULL);
97 }
98
99 JSObject* throwError(ExecState* exec, ErrorType type)
100 {
101 JSObject* error = Error::create(exec, type, UString(), -1, -1, NULL);
102 exec->setException(error);
103 return error;
104 }
105
106 JSObject* throwError(ExecState* exec, ErrorType type, const UString& message)
107 {
108 JSObject* error = Error::create(exec, type, message, -1, -1, NULL);
109 exec->setException(error);
110 return error;
111 }
112
113 JSObject* throwError(ExecState* exec, ErrorType type, const char* message)
114 {
115 JSObject* error = Error::create(exec, type, message, -1, -1, NULL);
116 exec->setException(error);
117 return error;
118 }
119
120 JSObject* throwError(ExecState* exec, ErrorType type, const UString& message, int line, intptr_t sourceID, const UString& sourceURL)
121 {
122 JSObject* error = Error::create(exec, type, message, line, sourceID, sourceURL);
123 exec->setException(error);
124 return error;
125 }
126
127 } // namespace JSC