]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/Error.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / Error.cpp
CommitLineData
9dae56ea
A
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"
6fe7ccc8
A
29#include "ExceptionHelpers.h"
30#include "FunctionPrototype.h"
31#include "JSArray.h"
ba379fdc 32#include "JSFunction.h"
9dae56ea
A
33#include "JSGlobalObject.h"
34#include "JSObject.h"
35#include "JSString.h"
36#include "NativeErrorConstructor.h"
81345200 37#include "JSCInlines.h"
14957cd0 38#include "SourceCode.h"
9dae56ea 39
6fe7ccc8
A
40#include <wtf/text/StringBuilder.h>
41
9dae56ea
A
42namespace JSC {
43
14957cd0 44static const char* linePropertyName = "line";
14957cd0
A
45static const char* sourceURLPropertyName = "sourceURL";
46
ed1e77d3 47JSObject* createError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
48{
49 ASSERT(!message.isEmpty());
ed1e77d3
A
50 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
51 return ErrorInstance::create(exec, globalObject->vm(), globalObject->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
52}
53
ed1e77d3 54JSObject* createEvalError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
55{
56 ASSERT(!message.isEmpty());
ed1e77d3
A
57 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
58 return ErrorInstance::create(exec, globalObject->vm(), globalObject->evalErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
59}
60
ed1e77d3 61JSObject* createRangeError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
62{
63 ASSERT(!message.isEmpty());
ed1e77d3
A
64 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
65 return ErrorInstance::create(exec, globalObject->vm(), globalObject->rangeErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
66}
67
ed1e77d3 68JSObject* createReferenceError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
69{
70 ASSERT(!message.isEmpty());
ed1e77d3
A
71 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
72 return ErrorInstance::create(exec, globalObject->vm(), globalObject->referenceErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
73}
74
ed1e77d3 75JSObject* createSyntaxError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
76{
77 ASSERT(!message.isEmpty());
ed1e77d3
A
78 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
79 return ErrorInstance::create(exec, globalObject->vm(), globalObject->syntaxErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
80}
81
ed1e77d3 82JSObject* createTypeError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender, RuntimeType type)
14957cd0
A
83{
84 ASSERT(!message.isEmpty());
ed1e77d3
A
85 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
86 return ErrorInstance::create(exec, globalObject->vm(), globalObject->typeErrorConstructor()->errorStructure(), message, appender, type, true);
6fe7ccc8
A
87}
88
ed1e77d3 89JSObject* createNotEnoughArgumentsError(ExecState* exec, ErrorInstance::SourceAppender appender)
6fe7ccc8 90{
ed1e77d3 91 return createTypeError(exec, ASCIILiteral("Not enough arguments"), appender, TypeNothing);
14957cd0
A
92}
93
ed1e77d3 94JSObject* createURIError(ExecState* exec, const String& message, ErrorInstance::SourceAppender appender)
14957cd0
A
95{
96 ASSERT(!message.isEmpty());
ed1e77d3
A
97 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
98 return ErrorInstance::create(exec, globalObject->vm(), globalObject->URIErrorConstructor()->errorStructure(), message, appender, TypeNothing, true);
14957cd0
A
99}
100
ed1e77d3 101JSObject* createOutOfMemoryError(ExecState* exec, ErrorInstance::SourceAppender appender)
14957cd0 102{
ed1e77d3 103 return createError(exec, ASCIILiteral("Out of memory"), appender);
14957cd0
A
104}
105
14957cd0 106
ed1e77d3
A
107class FindFirstCallerFrameWithCodeblockFunctor {
108public:
109 FindFirstCallerFrameWithCodeblockFunctor(CallFrame* startCallFrame)
110 : m_startCallFrame(startCallFrame)
111 , m_foundCallFrame(nullptr)
112 , m_foundStartCallFrame(false)
113 , m_index(0)
114 { }
9dae56ea 115
ed1e77d3
A
116 StackVisitor::Status operator()(StackVisitor& visitor)
117 {
118 if (!m_foundStartCallFrame && (visitor->callFrame() == m_startCallFrame))
119 m_foundStartCallFrame = true;
14957cd0 120
ed1e77d3
A
121 if (m_foundStartCallFrame) {
122 if (visitor->callFrame()->codeBlock()) {
123 m_foundCallFrame = visitor->callFrame();
124 return StackVisitor::Done;
125 }
126 m_index++;
127 }
14957cd0 128
ed1e77d3
A
129 return StackVisitor::Continue;
130 }
14957cd0 131
ed1e77d3
A
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)
6fe7ccc8 143{
ed1e77d3
A
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;
6fe7ccc8
A
184}
185
ed1e77d3 186void addErrorInfo(ExecState* exec, JSObject* obj, bool useCurrentFrame)
14957cd0 187{
ed1e77d3
A
188 CallFrame* callFrame = nullptr;
189 unsigned bytecodeOffset = 0;
190 addErrorInfoAndGetBytecodeOffset(exec, exec->vm(), obj, useCurrentFrame, callFrame, bytecodeOffset);
14957cd0
A
191}
192
6fe7ccc8 193JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source)
14957cd0 194{
93a37866
A
195 VM* vm = &callFrame->vm();
196 const String& sourceURL = source.provider()->url();
14957cd0
A
197
198 if (line != -1)
ed1e77d3 199 error->putDirect(*vm, Identifier::fromString(vm, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
9dae56ea 200 if (!sourceURL.isNull())
ed1e77d3 201 error->putDirect(*vm, Identifier::fromString(vm, sourceURLPropertyName), jsString(vm, sourceURL), ReadOnly | DontDelete);
9dae56ea
A
202 return error;
203}
204
9dae56ea 205
14957cd0 206bool hasErrorInfo(ExecState* exec, JSObject* error)
f9bf01c6 207{
ed1e77d3
A
208 return error->hasProperty(exec, Identifier::fromString(exec, linePropertyName))
209 || error->hasProperty(exec, Identifier::fromString(exec, sourceURLPropertyName));
f9bf01c6
A
210}
211
14957cd0 212JSObject* throwTypeError(ExecState* exec)
9dae56ea 213{
ed1e77d3 214 return exec->vm().throwException(exec, createTypeError(exec));
9dae56ea
A
215}
216
14957cd0 217JSObject* throwSyntaxError(ExecState* exec)
9dae56ea 218{
81345200 219 return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Syntax error")));
14957cd0
A
220}
221
ed1e77d3
A
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) };
6fe7ccc8
A
275
276void StrictModeTypeErrorFunction::destroy(JSCell* cell)
14957cd0 277{
93a37866 278 static_cast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();
9dae56ea
A
279}
280
281} // namespace JSC