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