]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/Error.cpp
JavaScriptCore-1097.3.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 "ExceptionHelpers.h"
30 #include "FunctionPrototype.h"
31 #include "JSArray.h"
32 #include "JSFunction.h"
33 #include "JSGlobalObject.h"
34 #include "JSObject.h"
35 #include "JSString.h"
36 #include "NativeErrorConstructor.h"
37 #include "SourceCode.h"
38
39 #include <wtf/text/StringBuilder.h>
40
41 namespace JSC {
42
43 static const char* linePropertyName = "line";
44 static const char* sourceURLPropertyName = "sourceURL";
45
46 JSObject* createError(JSGlobalObject* globalObject, const UString& message)
47 {
48 ASSERT(!message.isEmpty());
49 return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message);
50 }
51
52 JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
53 {
54 ASSERT(!message.isEmpty());
55 return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
56 }
57
58 JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
59 {
60 ASSERT(!message.isEmpty());
61 return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
62 }
63
64 JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
65 {
66 ASSERT(!message.isEmpty());
67 return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
68 }
69
70 JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
71 {
72 ASSERT(!message.isEmpty());
73 return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
74 }
75
76 JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
77 {
78 ASSERT(!message.isEmpty());
79 return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
80 }
81
82 JSObject* createNotEnoughArgumentsError(JSGlobalObject* globalObject)
83 {
84 return createTypeError(globalObject, "Not enough arguments");
85 }
86
87 JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
88 {
89 ASSERT(!message.isEmpty());
90 return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
91 }
92
93 JSObject* createError(ExecState* exec, const UString& message)
94 {
95 return createError(exec->lexicalGlobalObject(), message);
96 }
97
98 JSObject* createEvalError(ExecState* exec, const UString& message)
99 {
100 return createEvalError(exec->lexicalGlobalObject(), message);
101 }
102
103 JSObject* createRangeError(ExecState* exec, const UString& message)
104 {
105 return createRangeError(exec->lexicalGlobalObject(), message);
106 }
107
108 JSObject* createReferenceError(ExecState* exec, const UString& message)
109 {
110 return createReferenceError(exec->lexicalGlobalObject(), message);
111 }
112
113 JSObject* createSyntaxError(ExecState* exec, const UString& message)
114 {
115 return createSyntaxError(exec->lexicalGlobalObject(), message);
116 }
117
118 JSObject* createTypeError(ExecState* exec, const UString& message)
119 {
120 return createTypeError(exec->lexicalGlobalObject(), message);
121 }
122
123 JSObject* createNotEnoughArgumentsError(ExecState* exec)
124 {
125 return createNotEnoughArgumentsError(exec->lexicalGlobalObject());
126 }
127
128 JSObject* createURIError(ExecState* exec, const UString& message)
129 {
130 return createURIError(exec->lexicalGlobalObject(), message);
131 }
132
133 JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source)
134 {
135 JSGlobalData* globalData = &callFrame->globalData();
136 const UString& sourceURL = source.provider()->url();
137
138 if (line != -1)
139 error->putDirect(*globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
140 if (!sourceURL.isNull())
141 error->putDirect(*globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
142
143 globalData->interpreter->addStackTraceIfNecessary(callFrame, error);
144
145 return error;
146 }
147
148
149 bool hasErrorInfo(ExecState* exec, JSObject* error)
150 {
151 return error->hasProperty(exec, Identifier(exec, linePropertyName))
152 || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
153 }
154
155 JSValue throwError(ExecState* exec, JSValue error)
156 {
157 if (error.isObject())
158 return throwError(exec, asObject(error));
159 exec->globalData().exception = error;
160 return error;
161 }
162
163 JSObject* throwError(ExecState* exec, JSObject* error)
164 {
165 Interpreter::addStackTraceIfNecessary(exec, error);
166 exec->globalData().exception = error;
167 return error;
168 }
169
170 JSObject* throwTypeError(ExecState* exec)
171 {
172 return throwError(exec, createTypeError(exec, "Type error"));
173 }
174
175 JSObject* throwSyntaxError(ExecState* exec)
176 {
177 return throwError(exec, createSyntaxError(exec, "Syntax error"));
178 }
179
180 ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction);
181
182 const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) };
183
184 void StrictModeTypeErrorFunction::destroy(JSCell* cell)
185 {
186 jsCast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();
187 }
188
189 } // namespace JSC