]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
14 | * its contributors may be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "config.h" | |
30 | #include "ExceptionHelpers.h" | |
31 | ||
32 | #include "CodeBlock.h" | |
33 | #include "CallFrame.h" | |
34 | #include "JSGlobalObjectFunctions.h" | |
35 | #include "JSObject.h" | |
36 | #include "JSNotAnObject.h" | |
37 | #include "Interpreter.h" | |
38 | #include "Nodes.h" | |
39 | ||
40 | namespace JSC { | |
41 | ||
42 | class InterruptedExecutionError : public JSObject { | |
43 | public: | |
44 | InterruptedExecutionError(JSGlobalData* globalData) | |
45 | : JSObject(globalData->interruptedExecutionErrorStructure) | |
46 | { | |
47 | } | |
48 | ||
4e4e5a6f | 49 | virtual ComplType exceptionType() const { return Interrupted; } |
9dae56ea A |
50 | |
51 | virtual UString toString(ExecState*) const { return "JavaScript execution exceeded timeout."; } | |
52 | }; | |
53 | ||
ba379fdc | 54 | JSValue createInterruptedExecutionException(JSGlobalData* globalData) |
9dae56ea A |
55 | { |
56 | return new (globalData) InterruptedExecutionError(globalData); | |
57 | } | |
58 | ||
4e4e5a6f A |
59 | class TerminatedExecutionError : public JSObject { |
60 | public: | |
61 | TerminatedExecutionError(JSGlobalData* globalData) | |
62 | : JSObject(globalData->terminatedExecutionErrorStructure) | |
63 | { | |
64 | } | |
65 | ||
66 | virtual ComplType exceptionType() const { return Terminated; } | |
67 | ||
68 | virtual UString toString(ExecState*) const { return "JavaScript execution terminated."; } | |
69 | }; | |
70 | ||
71 | JSValue createTerminatedExecutionException(JSGlobalData* globalData) | |
72 | { | |
73 | return new (globalData) TerminatedExecutionError(globalData); | |
74 | } | |
75 | ||
ba379fdc | 76 | static JSValue createError(ExecState* exec, ErrorType e, const char* msg) |
9dae56ea | 77 | { |
4e4e5a6f | 78 | return Error::create(exec, e, msg, -1, -1, UString()); |
9dae56ea A |
79 | } |
80 | ||
ba379fdc | 81 | JSValue createStackOverflowError(ExecState* exec) |
9dae56ea A |
82 | { |
83 | return createError(exec, RangeError, "Maximum call stack size exceeded."); | |
84 | } | |
85 | ||
f9bf01c6 A |
86 | JSValue createTypeError(ExecState* exec, const char* message) |
87 | { | |
88 | return createError(exec, TypeError, message); | |
89 | } | |
90 | ||
ba379fdc | 91 | JSValue createUndefinedVariableError(ExecState* exec, const Identifier& ident, unsigned bytecodeOffset, CodeBlock* codeBlock) |
9dae56ea A |
92 | { |
93 | int startOffset = 0; | |
94 | int endOffset = 0; | |
95 | int divotPoint = 0; | |
96 | int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); | |
f9bf01c6 | 97 | JSObject* exception = Error::create(exec, ReferenceError, makeString("Can't find variable: ", ident.ustring()), line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL()); |
9dae56ea A |
98 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete); |
99 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete); | |
100 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete); | |
101 | return exception; | |
102 | } | |
103 | ||
ba379fdc | 104 | static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue value, UString error) |
9dae56ea | 105 | { |
f9bf01c6 A |
106 | if (!expressionStop || expressionStart > codeBlock->source()->length()) |
107 | return makeString(value.toString(exec), " is ", error); | |
108 | if (expressionStart < expressionStop) | |
109 | return makeString("Result of expression '", codeBlock->source()->getRange(expressionStart, expressionStop), "' [", value.toString(exec), "] is ", error, "."); | |
9dae56ea | 110 | |
f9bf01c6 A |
111 | // No range information, so give a few characters of context |
112 | const UChar* data = codeBlock->source()->data(); | |
113 | int dataLength = codeBlock->source()->length(); | |
114 | int start = expressionStart; | |
115 | int stop = expressionStart; | |
116 | // Get up to 20 characters of context to the left and right of the divot, clamping to the line. | |
117 | // then strip whitespace. | |
118 | while (start > 0 && (expressionStart - start < 20) && data[start - 1] != '\n') | |
119 | start--; | |
120 | while (start < (expressionStart - 1) && isStrWhiteSpace(data[start])) | |
121 | start++; | |
122 | while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n') | |
123 | stop++; | |
124 | while (stop > expressionStart && isStrWhiteSpace(data[stop])) | |
125 | stop--; | |
126 | return makeString("Result of expression near '...", codeBlock->source()->getRange(start, stop), "...' [", value.toString(exec), "] is ", error, "."); | |
9dae56ea A |
127 | } |
128 | ||
ba379fdc | 129 | JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock) |
9dae56ea | 130 | { |
9dae56ea A |
131 | int startOffset = 0; |
132 | int endOffset = 0; | |
133 | int divotPoint = 0; | |
134 | int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); | |
f9bf01c6 A |
135 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, makeString("not a valid argument for '", op, "'")); |
136 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL()); | |
9dae56ea A |
137 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete); |
138 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete); | |
139 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete); | |
140 | return exception; | |
141 | } | |
142 | ||
ba379fdc | 143 | JSObject* createNotAConstructorError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock) |
9dae56ea A |
144 | { |
145 | int startOffset = 0; | |
146 | int endOffset = 0; | |
147 | int divotPoint = 0; | |
148 | int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); | |
149 | ||
150 | // We're in a "new" expression, so we need to skip over the "new.." part | |
151 | int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new " | |
152 | const UChar* data = codeBlock->source()->data(); | |
153 | while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint])) | |
154 | startPoint++; | |
155 | ||
156 | UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor"); | |
f9bf01c6 | 157 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL()); |
9dae56ea A |
158 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete); |
159 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete); | |
160 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete); | |
161 | return exception; | |
162 | } | |
163 | ||
ba379fdc | 164 | JSValue createNotAFunctionError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock) |
9dae56ea A |
165 | { |
166 | int startOffset = 0; | |
167 | int endOffset = 0; | |
168 | int divotPoint = 0; | |
169 | int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); | |
170 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function"); | |
f9bf01c6 | 171 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL()); |
9dae56ea A |
172 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete); |
173 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete); | |
174 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete); | |
175 | return exception; | |
176 | } | |
177 | ||
178 | JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull) | |
179 | { | |
180 | return new (exec) JSNotAnObjectErrorStub(exec, isNull); | |
181 | } | |
182 | ||
183 | JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, unsigned bytecodeOffset, CodeBlock* codeBlock) | |
184 | { | |
185 | // Both op_construct and op_instanceof require a use of op_get_by_id to get | |
186 | // the prototype property from an object. The exception messages for exceptions | |
187 | // thrown by these instances op_get_by_id need to reflect this. | |
188 | OpcodeID followingOpcodeID; | |
189 | if (codeBlock->getByIdExceptionInfoForBytecodeOffset(exec, bytecodeOffset, followingOpcodeID)) { | |
190 | ASSERT(followingOpcodeID == op_construct || followingOpcodeID == op_instanceof); | |
191 | if (followingOpcodeID == op_construct) | |
192 | return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock); | |
193 | return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock); | |
194 | } | |
195 | ||
196 | int startOffset = 0; | |
197 | int endOffset = 0; | |
198 | int divotPoint = 0; | |
199 | int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); | |
200 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object"); | |
f9bf01c6 | 201 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL()); |
9dae56ea A |
202 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete); |
203 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete); | |
204 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete); | |
205 | return exception; | |
206 | } | |
207 | ||
4e4e5a6f A |
208 | JSValue throwOutOfMemoryError(ExecState* exec) |
209 | { | |
210 | return throwError(exec, GeneralError, "Out of memory"); | |
211 | } | |
212 | ||
9dae56ea | 213 | } // namespace JSC |