]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - runtime/Error.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / Error.cpp
... / ...
CommitLineData
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 "JSCInlines.h"
38#include "SourceCode.h"
39
40#include <wtf/text/StringBuilder.h>
41
42namespace JSC {
43
44static const char* linePropertyName = "line";
45static const char* sourceURLPropertyName = "sourceURL";
46
47JSObject* createError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
48{
49 ASSERT(!message.isEmpty());
50 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
51 return ErrorInstance::create(exec, globalObject->vm(), globalObject->errorStructure(), message, appender, TypeNothing, true);
52}
53
54JSObject* createEvalError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
55{
56 ASSERT(!message.isEmpty());
57 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
58 return ErrorInstance::create(exec, globalObject->vm(), globalObject->evalErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
59}
60
61JSObject* createRangeError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
62{
63 ASSERT(!message.isEmpty());
64 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
65 return ErrorInstance::create(exec, globalObject->vm(), globalObject->rangeErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
66}
67
68JSObject* createReferenceError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
69{
70 ASSERT(!message.isEmpty());
71 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
72 return ErrorInstance::create(exec, globalObject->vm(), globalObject->referenceErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
73}
74
75JSObject* createSyntaxError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
76{
77 ASSERT(!message.isEmpty());
78 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
79 return ErrorInstance::create(exec, globalObject->vm(), globalObject->syntaxErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
80}
81
82JSObject* createTypeError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender, RuntimeType type)
83{
84 ASSERT(!message.isEmpty());
85 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
86 return ErrorInstance::create(exec, globalObject->vm(), globalObject->typeErrorConstructor()->errorStructure(), message, appender, type, true);
87}
88
89JSObject* createNotEnoughArgumentsError(ExecState* exec, ErrorInstance::SourceAppender appender)
90{
91 return createTypeError(exec, ASCIILiteral("Not enough arguments"), appender, TypeNothing);
92}
93
94JSObject* createURIError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
95{
96 ASSERT(!message.isEmpty());
97 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
98 return ErrorInstance::create(exec, globalObject->vm(), globalObject->URIErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
99}
100
101JSObject* createOutOfMemoryError(ExecState* exec, ErrorInstance::SourceAppender appender)
102{
103 return createError(exec, ASCIILiteral("Out of memory"), appender);
104}
105
106
107class FindFirstCallerFrameWithCodeblockFunctor {
108public:
109 FindFirstCallerFrameWithCodeblockFunctor(CallFrame* startCallFrame)
110 : m_startCallFrame(startCallFrame)
111 , m_foundCallFrame(nullptr)
112 , m_foundStartCallFrame(false)
113 , m_index(0)
114 { }
115
116 StackVisitor::Status operator()(StackVisitor& visitor)
117 {
118 if (!m_foundStartCallFrame && (visitor->callFrame() == m_startCallFrame))
119 m_foundStartCallFrame = true;
120
121 if (m_foundStartCallFrame) {
122 if (visitor->callFrame()->codeBlock()) {
123 m_foundCallFrame = visitor->callFrame();
124 return StackVisitor::Done;
125 }
126 m_index++;
127 }
128
129 return StackVisitor::Continue;
130 }
131
132 CallFrame* foundCallFrame() const { return m_foundCallFrame; }
133 unsigned index() const { return m_index; }
134
135private:
136 CallFrame* m_startCallFrame;
137 CallFrame* m_foundCallFrame;
138 bool m_foundStartCallFrame;
139 unsigned m_index;
140};
141
142bool addErrorInfoAndGetBytecodeOffset(ExecState* exec, VM& vm, JSObject* obj, bool useCurrentFrame, CallFrame*& callFrame, unsigned &bytecodeOffset)
143{
144 Vector<StackFrame> stackTrace = Vector<StackFrame>();
145
146 if (exec && stackTrace.isEmpty())
147 vm.interpreter->getStackTrace(stackTrace);
148
149 if (!stackTrace.isEmpty()) {
150
151 ASSERT(exec == vm.topCallFrame || exec == exec->lexicalGlobalObject()->globalExec() || exec == exec->vmEntryGlobalObject()->globalExec());
152
153 StackFrame* stackFrame;
154 for (unsigned i = 0 ; i < stackTrace.size(); ++i) {
155 stackFrame = &stackTrace.at(i);
156 if (stackFrame->bytecodeOffset)
157 break;
158 }
159
160 if (bytecodeOffset) {
161 FindFirstCallerFrameWithCodeblockFunctor functor(exec);
162 vm.topCallFrame->iterate(functor);
163 callFrame = functor.foundCallFrame();
164 unsigned stackIndex = functor.index();
165 bytecodeOffset = stackTrace.at(stackIndex).bytecodeOffset;
166 }
167
168 unsigned line;
169 unsigned column;
170 stackFrame->computeLineAndColumn(line, column);
171 obj->putDirect(vm, vm.propertyNames->line, jsNumber(line), ReadOnly | DontDelete);
172 obj->putDirect(vm, vm.propertyNames->column, jsNumber(column), ReadOnly | DontDelete);
173
174 if (!stackFrame->sourceURL.isEmpty())
175 obj->putDirect(vm, vm.propertyNames->sourceURL, jsString(&vm, stackFrame->sourceURL), ReadOnly | DontDelete);
176
177 if (!useCurrentFrame)
178 stackTrace.remove(0);
179 obj->putDirect(vm, vm.propertyNames->stack, vm.interpreter->stackTraceAsString(vm.topCallFrame, stackTrace), DontEnum);
180
181 return true;
182 }
183 return false;
184}
185
186void addErrorInfo(ExecState* exec, JSObject* obj, bool useCurrentFrame)
187{
188 CallFrame* callFrame = nullptr;
189 unsigned bytecodeOffset = 0;
190 addErrorInfoAndGetBytecodeOffset(exec, exec->vm(), obj, useCurrentFrame, callFrame, bytecodeOffset);
191}
192
193JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source)
194{
195 VM* vm = &callFrame->vm();
196 const String& sourceURL = source.provider()->url();
197
198 if (line != -1)
199 error->putDirect(*vm, Identifier::fromString(vm, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
200 if (!sourceURL.isNull())
201 error->putDirect(*vm, Identifier::fromString(vm, sourceURLPropertyName), jsString(vm, sourceURL), ReadOnly | DontDelete);
202 return error;
203}
204
205
206bool hasErrorInfo(ExecState* exec, JSObject* error)
207{
208 return error->hasProperty(exec, Identifier::fromString(exec, linePropertyName))
209 || error->hasProperty(exec, Identifier::fromString(exec, sourceURLPropertyName));
210}
211
212JSObject* throwTypeError(ExecState* exec)
213{
214 return exec->vm().throwException(exec, createTypeError(exec));
215}
216
217JSObject* throwSyntaxError(ExecState* exec)
218{
219 return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Syntax error")));
220}
221
222
223JSObject* createError(ExecState* exec, const String& message)
224{
225 return createError(exec, message, nullptr);
226}
227
228JSObject* createEvalError(ExecState* exec, const String& message)
229{
230 return createEvalError(exec, message, nullptr);
231}
232
233JSObject* createRangeError(ExecState* exec, const String& message)
234{
235 return createRangeError(exec, message, nullptr);
236}
237
238JSObject* createReferenceError(ExecState* exec, const String& message)
239{
240 return createReferenceError(exec, message, nullptr);
241}
242
243JSObject* createSyntaxError(ExecState* exec, const String& message)
244{
245 return createSyntaxError(exec, message, nullptr);
246}
247
248JSObject* createTypeError(ExecState* exec)
249{
250 return createTypeError(exec, ASCIILiteral("Type error"));
251}
252
253JSObject* createTypeError(ExecState* exec, const String& message)
254{
255 return createTypeError(exec, message, nullptr, TypeNothing);
256}
257
258JSObject* createNotEnoughArgumentsError(ExecState* exec)
259{
260 return createNotEnoughArgumentsError(exec, nullptr);
261}
262
263JSObject* createURIError(ExecState* exec, const String& message)
264{
265 return createURIError(exec, message, nullptr);
266}
267
268JSObject* createOutOfMemoryError(ExecState* exec)
269{
270 return createOutOfMemoryError(exec, nullptr);
271}
272
273
274const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) };
275
276void StrictModeTypeErrorFunction::destroy(JSCell* cell)
277{
278 static_cast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();
279}
280
281} // namespace JSC