]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Error.cpp
db1d8ccb0b60a26c44da665cf13efa2bcdc0f322
[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 "JSFunction.h"
30 #include "JSGlobalObject.h"
31 #include "JSObject.h"
32 #include "JSString.h"
33 #include "NativeErrorConstructor.h"
34
35 namespace JSC {
36
37 const char* expressionBeginOffsetPropertyName = "expressionBeginOffset";
38 const char* expressionCaretOffsetPropertyName = "expressionCaretOffset";
39 const char* expressionEndOffsetPropertyName = "expressionEndOffset";
40
41 JSObject* Error::create(ExecState* exec, ErrorType type, const UString& message, int lineNumber, intptr_t sourceID, const UString& sourceURL)
42 {
43 JSObject* constructor;
44 const char* name;
45 switch (type) {
46 case EvalError:
47 constructor = exec->lexicalGlobalObject()->evalErrorConstructor();
48 name = "Evaluation error";
49 break;
50 case RangeError:
51 constructor = exec->lexicalGlobalObject()->rangeErrorConstructor();
52 name = "Range error";
53 break;
54 case ReferenceError:
55 constructor = exec->lexicalGlobalObject()->referenceErrorConstructor();
56 name = "Reference error";
57 break;
58 case SyntaxError:
59 constructor = exec->lexicalGlobalObject()->syntaxErrorConstructor();
60 name = "Syntax error";
61 break;
62 case TypeError:
63 constructor = exec->lexicalGlobalObject()->typeErrorConstructor();
64 name = "Type error";
65 break;
66 case URIError:
67 constructor = exec->lexicalGlobalObject()->URIErrorConstructor();
68 name = "URI error";
69 break;
70 default:
71 constructor = exec->lexicalGlobalObject()->errorConstructor();
72 name = "Error";
73 break;
74 }
75
76 MarkedArgumentBuffer args;
77 if (message.isEmpty())
78 args.append(jsString(exec, name));
79 else
80 args.append(jsString(exec, message));
81 ConstructData constructData;
82 ConstructType constructType = constructor->getConstructData(constructData);
83 JSObject* error = construct(exec, constructor, constructType, constructData, args);
84
85 if (lineNumber != -1)
86 error->putWithAttributes(exec, Identifier(exec, "line"), jsNumber(exec, lineNumber), ReadOnly | DontDelete);
87 if (sourceID != -1)
88 error->putWithAttributes(exec, Identifier(exec, "sourceId"), jsNumber(exec, sourceID), ReadOnly | DontDelete);
89 if (!sourceURL.isNull())
90 error->putWithAttributes(exec, Identifier(exec, "sourceURL"), jsString(exec, sourceURL), ReadOnly | DontDelete);
91
92 return error;
93 }
94
95 JSObject* Error::create(ExecState* exec, ErrorType type, const char* message)
96 {
97 return create(exec, type, message, -1, -1, NULL);
98 }
99
100 JSObject* throwError(ExecState* exec, ErrorType type)
101 {
102 JSObject* error = Error::create(exec, type, UString(), -1, -1, NULL);
103 exec->setException(error);
104 return error;
105 }
106
107 JSObject* throwError(ExecState* exec, ErrorType type, const UString& message)
108 {
109 JSObject* error = Error::create(exec, type, message, -1, -1, NULL);
110 exec->setException(error);
111 return error;
112 }
113
114 JSObject* throwError(ExecState* exec, ErrorType type, const char* message)
115 {
116 JSObject* error = Error::create(exec, type, message, -1, -1, NULL);
117 exec->setException(error);
118 return error;
119 }
120
121 JSObject* throwError(ExecState* exec, ErrorType type, const UString& message, int line, intptr_t sourceID, const UString& sourceURL)
122 {
123 JSObject* error = Error::create(exec, type, message, line, sourceID, sourceURL);
124 exec->setException(error);
125 return error;
126 }
127
128 } // namespace JSC