0
|
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 |
|
|
49 |
virtual bool isWatchdogException() const { return true; }
|
|
50 |
|
|
51 |
virtual UString toString(ExecState*) const { return "JavaScript execution exceeded timeout."; }
|
|
52 |
};
|
|
53 |
|
|
54 |
JSValue createInterruptedExecutionException(JSGlobalData* globalData)
|
|
55 |
{
|
|
56 |
return new (globalData) InterruptedExecutionError(globalData);
|
|
57 |
}
|
|
58 |
|
|
59 |
static JSValue createError(ExecState* exec, ErrorType e, const char* msg)
|
|
60 |
{
|
|
61 |
return Error::create(exec, e, msg, -1, -1, 0);
|
|
62 |
}
|
|
63 |
|
|
64 |
JSValue createStackOverflowError(ExecState* exec)
|
|
65 |
{
|
|
66 |
return createError(exec, RangeError, "Maximum call stack size exceeded.");
|
|
67 |
}
|
|
68 |
|
|
69 |
JSValue createTypeError(ExecState* exec, const char* message)
|
|
70 |
{
|
|
71 |
return createError(exec, TypeError, message);
|
|
72 |
}
|
|
73 |
|
|
74 |
JSValue createUndefinedVariableError(ExecState* exec, const Identifier& ident, unsigned bytecodeOffset, CodeBlock* codeBlock)
|
|
75 |
{
|
|
76 |
int startOffset = 0;
|
|
77 |
int endOffset = 0;
|
|
78 |
int divotPoint = 0;
|
|
79 |
int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
|
|
80 |
UString message = "Can't find variable: ";
|
|
81 |
message.append(ident.ustring());
|
|
82 |
JSObject* exception = Error::create(exec, ReferenceError, message, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
|
|
83 |
exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
|
84 |
exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
|
85 |
exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
|
86 |
return exception;
|
|
87 |
}
|
|
88 |
|
|
89 |
static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue value, UString error)
|
|
90 |
{
|
|
91 |
if (!expressionStop || expressionStart > codeBlock->source()->length()) {
|
|
92 |
UString errorText = value.toString(exec);
|
|
93 |
errorText.append(" is ");
|
|
94 |
errorText.append(error);
|
|
95 |
return errorText;
|
|
96 |
}
|
|
97 |
|
|
98 |
UString errorText = "Result of expression ";
|
|
99 |
|
|
100 |
if (expressionStart < expressionStop) {
|
|
101 |
errorText.append('\'');
|
|
102 |
errorText.append(codeBlock->source()->getRange(expressionStart, expressionStop));
|
|
103 |
errorText.append("' [");
|
|
104 |
errorText.append(value.toString(exec));
|
|
105 |
errorText.append("] is ");
|
|
106 |
} else {
|
|
107 |
// No range information, so give a few characters of context
|
|
108 |
const UChar* data = codeBlock->source()->data();
|
|
109 |
int dataLength = codeBlock->source()->length();
|
|
110 |
int start = expressionStart;
|
|
111 |
int stop = expressionStart;
|
|
112 |
// Get up to 20 characters of context to the left and right of the divot, clamping to the line.
|
|
113 |
// then strip whitespace.
|
|
114 |
while (start > 0 && (expressionStart - start < 20) && data[start - 1] != '\n')
|
|
115 |
start--;
|
|
116 |
while (start < (expressionStart - 1) && isStrWhiteSpace(data[start]))
|
|
117 |
start++;
|
|
118 |
while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n')
|
|
119 |
stop++;
|
|
120 |
while (stop > expressionStart && isStrWhiteSpace(data[stop]))
|
|
121 |
stop--;
|
|
122 |
errorText.append("near '...");
|
|
123 |
errorText.append(codeBlock->source()->getRange(start, stop));
|
|
124 |
errorText.append("...' [");
|
|
125 |
errorText.append(value.toString(exec));
|
|
126 |
errorText.append("] is ");
|
|
127 |
}
|
|
128 |
errorText.append(error);
|
|
129 |
errorText.append(".");
|
|
130 |
return errorText;
|
|
131 |
}
|
|
132 |
|
|
133 |
JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
|
|
134 |
{
|
|
135 |
UString message = "not a valid argument for '";
|
|
136 |
message.append(op);
|
|
137 |
message.append("'");
|
|
138 |
|
|
139 |
int startOffset = 0;
|
|
140 |
int endOffset = 0;
|
|
141 |
int divotPoint = 0;
|
|
142 |
int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
|
|
143 |
UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, message);
|
|
144 |
JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
|
|
145 |
exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
|
146 |
exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
|
147 |
exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
|
148 |
return exception;
|
|
149 |
}
|
|
150 |
|
|
151 |
JSObject* createNotAConstructorError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
|
|
152 |
{
|
|
153 |
int startOffset = 0;
|
|
154 |
int endOffset = 0;
|
|
155 |
int divotPoint = 0;
|
|
156 |
int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
|
|
157 |
|
|
158 |
// We're in a "new" expression, so we need to skip over the "new.." part
|
|
159 |
int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new "
|
|
160 |
const UChar* data = codeBlock->source()->data();
|
|
161 |
while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint]))
|
|
162 |
startPoint++;
|
|
163 |
|
|
164 |
UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor");
|
|
165 |
JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
|
|
166 |
exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
|
167 |
exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
|
168 |
exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
|
169 |
return exception;
|
|
170 |
}
|
|
171 |
|
|
172 |
JSValue createNotAFunctionError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
|
|
173 |
{
|
|
174 |
int startOffset = 0;
|
|
175 |
int endOffset = 0;
|
|
176 |
int divotPoint = 0;
|
|
177 |
int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
|
|
178 |
UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function");
|
|
179 |
JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
|
|
180 |
exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
|
181 |
exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
|
182 |
exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
|
183 |
return exception;
|
|
184 |
}
|
|
185 |
|
|
186 |
JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull)
|
|
187 |
{
|
|
188 |
return new (exec) JSNotAnObjectErrorStub(exec, isNull);
|
|
189 |
}
|
|
190 |
|
|
191 |
JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, unsigned bytecodeOffset, CodeBlock* codeBlock)
|
|
192 |
{
|
|
193 |
// Both op_construct and op_instanceof require a use of op_get_by_id to get
|
|
194 |
// the prototype property from an object. The exception messages for exceptions
|
|
195 |
// thrown by these instances op_get_by_id need to reflect this.
|
|
196 |
OpcodeID followingOpcodeID;
|
|
197 |
if (codeBlock->getByIdExceptionInfoForBytecodeOffset(exec, bytecodeOffset, followingOpcodeID)) {
|
|
198 |
ASSERT(followingOpcodeID == op_construct || followingOpcodeID == op_instanceof);
|
|
199 |
if (followingOpcodeID == op_construct)
|
|
200 |
return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
|
|
201 |
return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
|
|
202 |
}
|
|
203 |
|
|
204 |
int startOffset = 0;
|
|
205 |
int endOffset = 0;
|
|
206 |
int divotPoint = 0;
|
|
207 |
int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
|
|
208 |
UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object");
|
|
209 |
JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
|
|
210 |
exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
|
211 |
exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
|
212 |
exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
|
213 |
return exception;
|
|
214 |
}
|
|
215 |
|
|
216 |
} // namespace JSC
|