1 | /*
|
---|
2 | * Copyright (C) 2008 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 "ExecState.h"
|
---|
34 | #include "JSObject.h"
|
---|
35 | #include "JSNotAnObject.h"
|
---|
36 | #include "Machine.h"
|
---|
37 | #include "nodes.h"
|
---|
38 |
|
---|
39 | namespace JSC {
|
---|
40 |
|
---|
41 | static void substitute(UString& string, const UString& substring)
|
---|
42 | {
|
---|
43 | int position = string.find("%s");
|
---|
44 | ASSERT(position != -1);
|
---|
45 | UString newString = string.substr(0, position);
|
---|
46 | newString.append(substring);
|
---|
47 | newString.append(string.substr(position + 2));
|
---|
48 | string = newString;
|
---|
49 | }
|
---|
50 |
|
---|
51 | class InterruptedExecutionError : public JSObject {
|
---|
52 | public:
|
---|
53 | InterruptedExecutionError(JSGlobalData* globalData)
|
---|
54 | : JSObject(globalData->interruptedExecutionErrorStructure)
|
---|
55 | {
|
---|
56 | }
|
---|
57 |
|
---|
58 | virtual bool isWatchdogException() const { return true; }
|
---|
59 | };
|
---|
60 |
|
---|
61 | JSValue* createInterruptedExecutionException(JSGlobalData* globalData)
|
---|
62 | {
|
---|
63 | return new (globalData) InterruptedExecutionError(globalData);
|
---|
64 | }
|
---|
65 |
|
---|
66 | JSValue* createError(ExecState* exec, ErrorType e, const char* msg)
|
---|
67 | {
|
---|
68 | return Error::create(exec, e, msg, -1, -1, 0);
|
---|
69 | }
|
---|
70 |
|
---|
71 | JSValue* createError(ExecState* exec, ErrorType e, const char* msg, const Identifier& label)
|
---|
72 | {
|
---|
73 | UString message = msg;
|
---|
74 | substitute(message, label.ustring());
|
---|
75 | return Error::create(exec, e, message, -1, -1, 0);
|
---|
76 | }
|
---|
77 |
|
---|
78 | JSValue* createError(ExecState* exec, ErrorType e, const char* msg, JSValue* v)
|
---|
79 | {
|
---|
80 | UString message = msg;
|
---|
81 | substitute(message, v->toString(exec));
|
---|
82 | return Error::create(exec, e, message, -1, -1, 0);
|
---|
83 | }
|
---|
84 |
|
---|
85 | JSValue* createStackOverflowError(ExecState* exec)
|
---|
86 | {
|
---|
87 | return createError(exec, RangeError, "Maximum call stack size exceeded.");
|
---|
88 | }
|
---|
89 |
|
---|
90 | JSValue* createUndefinedVariableError(ExecState* exec, const Identifier& ident, const Instruction* vPC, CodeBlock* codeBlock)
|
---|
91 | {
|
---|
92 | int startOffset = 0;
|
---|
93 | int endOffset = 0;
|
---|
94 | int divotPoint = 0;
|
---|
95 | int line = codeBlock->expressionRangeForVPC(vPC, divotPoint, startOffset, endOffset);
|
---|
96 | UString message = "Can't find variable: ";
|
---|
97 | message.append(ident.ustring());
|
---|
98 | JSObject* exception = Error::create(exec, ReferenceError, message, line, codeBlock->ownerNode->sourceID(), codeBlock->ownerNode->sourceURL());
|
---|
99 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
---|
100 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
---|
101 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
---|
102 | return exception;
|
---|
103 | }
|
---|
104 |
|
---|
105 | bool isStrWhiteSpace(UChar c);
|
---|
106 |
|
---|
107 | static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue* value, UString error)
|
---|
108 | {
|
---|
109 | if (!expressionStop || expressionStart > codeBlock->source->length()) {
|
---|
110 | UString errorText = value->toString(exec);
|
---|
111 | errorText.append(" is ");
|
---|
112 | errorText.append(error);
|
---|
113 | return errorText;
|
---|
114 | }
|
---|
115 |
|
---|
116 | UString errorText = "Result of expression ";
|
---|
117 |
|
---|
118 | if (expressionStart < expressionStop) {
|
---|
119 | errorText.append('\'');
|
---|
120 | errorText.append(codeBlock->source->getRange(expressionStart, expressionStop));
|
---|
121 | errorText.append("' [");
|
---|
122 | errorText.append(value->toString(exec));
|
---|
123 | errorText.append("] is ");
|
---|
124 | } else {
|
---|
125 | // No range information, so give a few characters of context
|
---|
126 | const UChar* data = codeBlock->source->data();
|
---|
127 | int dataLength = codeBlock->source->length();
|
---|
128 | int start = expressionStart;
|
---|
129 | int stop = expressionStart;
|
---|
130 | // Get up to 20 characters of context to the left and right of the divot, clamping to the line.
|
---|
131 | // then strip whitespace.
|
---|
132 | while (start > 0 && (expressionStart - start < 20) && data[start - 1] != '\n')
|
---|
133 | start--;
|
---|
134 | while (start < (expressionStart - 1) && isStrWhiteSpace(data[start]))
|
---|
135 | start++;
|
---|
136 | while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n')
|
---|
137 | stop++;
|
---|
138 | while (stop > expressionStart && isStrWhiteSpace(data[stop]))
|
---|
139 | stop--;
|
---|
140 | errorText.append("near '...");
|
---|
141 | errorText.append(codeBlock->source->getRange(start, stop));
|
---|
142 | errorText.append("...' [");
|
---|
143 | errorText.append(value->toString(exec));
|
---|
144 | errorText.append("] is ");
|
---|
145 | }
|
---|
146 | errorText.append(error);
|
---|
147 | errorText.append(".");
|
---|
148 | return errorText;
|
---|
149 | }
|
---|
150 |
|
---|
151 | JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue* value, const Instruction* vPC, CodeBlock* codeBlock)
|
---|
152 | {
|
---|
153 | UString message = "not a valid argument for '";
|
---|
154 | message.append(op);
|
---|
155 | message.append("'");
|
---|
156 |
|
---|
157 | int startOffset = 0;
|
---|
158 | int endOffset = 0;
|
---|
159 | int divotPoint = 0;
|
---|
160 | int line = codeBlock->expressionRangeForVPC(vPC, divotPoint, startOffset, endOffset);
|
---|
161 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, message);
|
---|
162 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerNode->sourceID(), codeBlock->ownerNode->sourceURL());
|
---|
163 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
---|
164 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
---|
165 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
---|
166 | return exception;
|
---|
167 | }
|
---|
168 |
|
---|
169 | JSObject* createNotAConstructorError(ExecState* exec, JSValue* value, const Instruction* vPC, CodeBlock* codeBlock)
|
---|
170 | {
|
---|
171 | int startOffset = 0;
|
---|
172 | int endOffset = 0;
|
---|
173 | int divotPoint = 0;
|
---|
174 | int line = codeBlock->expressionRangeForVPC(vPC, divotPoint, startOffset, endOffset);
|
---|
175 |
|
---|
176 | // We're in a "new" expression, so we need to skip over the "new.." part
|
---|
177 | int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new "
|
---|
178 | const UChar* data = codeBlock->source->data();
|
---|
179 | while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint]))
|
---|
180 | startPoint++;
|
---|
181 |
|
---|
182 | UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor");
|
---|
183 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerNode->sourceID(), codeBlock->ownerNode->sourceURL());
|
---|
184 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
---|
185 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
---|
186 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
---|
187 | return exception;
|
---|
188 | }
|
---|
189 |
|
---|
190 | JSValue* createNotAFunctionError(ExecState* exec, JSValue* value, const Instruction* vPC, CodeBlock* codeBlock)
|
---|
191 | {
|
---|
192 | int startOffset = 0;
|
---|
193 | int endOffset = 0;
|
---|
194 | int divotPoint = 0;
|
---|
195 | int line = codeBlock->expressionRangeForVPC(vPC, divotPoint, startOffset, endOffset);
|
---|
196 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function");
|
---|
197 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerNode->sourceID(), codeBlock->ownerNode->sourceURL());
|
---|
198 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
---|
199 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
---|
200 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
---|
201 | return exception;
|
---|
202 | }
|
---|
203 |
|
---|
204 | JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull)
|
---|
205 | {
|
---|
206 | return new (exec) JSNotAnObjectErrorStub(exec, isNull);
|
---|
207 | }
|
---|
208 |
|
---|
209 | JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, const Instruction* vPC, CodeBlock* codeBlock)
|
---|
210 | {
|
---|
211 | if (vPC[8].u.opcode == exec->machine()->getOpcode(op_instanceof))
|
---|
212 | return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), vPC, codeBlock);
|
---|
213 | if (vPC[8].u.opcode == exec->machine()->getOpcode(op_construct))
|
---|
214 | return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), vPC, codeBlock);
|
---|
215 |
|
---|
216 | int startOffset = 0;
|
---|
217 | int endOffset = 0;
|
---|
218 | int divotPoint = 0;
|
---|
219 | int line = codeBlock->expressionRangeForVPC(vPC, divotPoint, startOffset, endOffset);
|
---|
220 | UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object");
|
---|
221 | JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerNode->sourceID(), codeBlock->ownerNode->sourceURL());
|
---|
222 | exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
|
---|
223 | exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
|
---|
224 | exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
|
---|
225 | return exception;
|
---|
226 | }
|
---|
227 |
|
---|
228 | } // namespace JSC
|
---|