]>
Commit | Line | Data |
---|---|---|
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 | ||
42 | namespace JSC { | |
43 | ||
44 | static const char* linePropertyName = "line"; | |
45 | static const char* sourceURLPropertyName = "sourceURL"; | |
46 | ||
47 | JSObject* 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 | ||
54 | JSObject* 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 | ||
61 | JSObject* 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 | ||
68 | JSObject* 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 | ||
75 | JSObject* 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 | ||
82 | JSObject* 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 | ||
89 | JSObject* createNotEnoughArgumentsError(ExecState* exec, ErrorInstance::SourceAppender appender) | |
90 | { | |
91 | return createTypeError(exec, ASCIILiteral("Not enough arguments"), appender, TypeNothing); | |
92 | } | |
93 | ||
94 | JSObject* 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 | ||
101 | JSObject* createOutOfMemoryError(ExecState* exec, ErrorInstance::SourceAppender appender) | |
102 | { | |
103 | return createError(exec, ASCIILiteral("Out of memory"), appender); | |
104 | } | |
105 | ||
106 | ||
107 | class FindFirstCallerFrameWithCodeblockFunctor { | |
108 | public: | |
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 | ||
135 | private: | |
136 | CallFrame* m_startCallFrame; | |
137 | CallFrame* m_foundCallFrame; | |
138 | bool m_foundStartCallFrame; | |
139 | unsigned m_index; | |
140 | }; | |
141 | ||
142 | bool 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 | ||
186 | void 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 | ||
193 | JSObject* 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 | ||
206 | bool 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 | ||
212 | JSObject* throwTypeError(ExecState* exec) | |
213 | { | |
214 | return exec->vm().throwException(exec, createTypeError(exec)); | |
215 | } | |
216 | ||
217 | JSObject* throwSyntaxError(ExecState* exec) | |
218 | { | |
219 | return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Syntax error"))); | |
220 | } | |
221 | ||
222 | ||
223 | JSObject* createError(ExecState* exec, const String& message) | |
224 | { | |
225 | return createError(exec, message, nullptr); | |
226 | } | |
227 | ||
228 | JSObject* createEvalError(ExecState* exec, const String& message) | |
229 | { | |
230 | return createEvalError(exec, message, nullptr); | |
231 | } | |
232 | ||
233 | JSObject* createRangeError(ExecState* exec, const String& message) | |
234 | { | |
235 | return createRangeError(exec, message, nullptr); | |
236 | } | |
237 | ||
238 | JSObject* createReferenceError(ExecState* exec, const String& message) | |
239 | { | |
240 | return createReferenceError(exec, message, nullptr); | |
241 | } | |
242 | ||
243 | JSObject* createSyntaxError(ExecState* exec, const String& message) | |
244 | { | |
245 | return createSyntaxError(exec, message, nullptr); | |
246 | } | |
247 | ||
248 | JSObject* createTypeError(ExecState* exec) | |
249 | { | |
250 | return createTypeError(exec, ASCIILiteral("Type error")); | |
251 | } | |
252 | ||
253 | JSObject* createTypeError(ExecState* exec, const String& message) | |
254 | { | |
255 | return createTypeError(exec, message, nullptr, TypeNothing); | |
256 | } | |
257 | ||
258 | JSObject* createNotEnoughArgumentsError(ExecState* exec) | |
259 | { | |
260 | return createNotEnoughArgumentsError(exec, nullptr); | |
261 | } | |
262 | ||
263 | JSObject* createURIError(ExecState* exec, const String& message) | |
264 | { | |
265 | return createURIError(exec, message, nullptr); | |
266 | } | |
267 | ||
268 | JSObject* createOutOfMemoryError(ExecState* exec) | |
269 | { | |
270 | return createOutOfMemoryError(exec, nullptr); | |
271 | } | |
272 | ||
273 | ||
274 | const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) }; | |
275 | ||
276 | void StrictModeTypeErrorFunction::destroy(JSCell* cell) | |
277 | { | |
278 | static_cast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction(); | |
279 | } | |
280 | ||
281 | } // namespace JSC |