|
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 ComplType exceptionType() const { return Interrupted; } |
|
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 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 |
|
76 JSObject* createStackOverflowError(ExecState* exec) |
|
77 { |
|
78 return createRangeError(exec, "Maximum call stack size exceeded."); |
|
79 } |
|
80 |
|
81 JSObject* createStackOverflowError(JSGlobalObject* globalObject) |
|
82 { |
|
83 return createRangeError(globalObject, "Maximum call stack size exceeded."); |
|
84 } |
|
85 |
|
86 JSValue createUndefinedVariableError(ExecState* exec, const Identifier& ident, unsigned bytecodeOffset, CodeBlock* codeBlock) |
|
87 { |
|
88 int startOffset = 0; |
|
89 int endOffset = 0; |
|
90 int divotPoint = 0; |
|
91 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); |
|
92 UString message(makeString("Can't find variable: ", ident.ustring())); |
|
93 JSObject* exception = addErrorInfo(exec, createReferenceError(exec, message), line, codeBlock->ownerExecutable()->source(), divotPoint, startOffset, endOffset); |
|
94 return exception; |
|
95 } |
|
96 |
|
97 static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue value, UString error) |
|
98 { |
|
99 if (!expressionStop || expressionStart > codeBlock->source()->length()) |
|
100 return makeString(value.toString(exec), " is ", error); |
|
101 if (expressionStart < expressionStop) |
|
102 return makeString("Result of expression '", codeBlock->source()->getRange(expressionStart, expressionStop), "' [", value.toString(exec), "] is ", error, "."); |
|
103 |
|
104 // No range information, so give a few characters of context |
|
105 const UChar* data = codeBlock->source()->data(); |
|
106 int dataLength = codeBlock->source()->length(); |
|
107 int start = expressionStart; |
|
108 int stop = expressionStart; |
|
109 // Get up to 20 characters of context to the left and right of the divot, clamping to the line. |
|
110 // then strip whitespace. |
|
111 while (start > 0 && (expressionStart - start < 20) && data[start - 1] != '\n') |
|
112 start--; |
|
113 while (start < (expressionStart - 1) && isStrWhiteSpace(data[start])) |
|
114 start++; |
|
115 while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n') |
|
116 stop++; |
|
117 while (stop > expressionStart && isStrWhiteSpace(data[stop])) |
|
118 stop--; |
|
119 return makeString("Result of expression near '...", codeBlock->source()->getRange(start, stop), "...' [", value.toString(exec), "] is ", error, "."); |
|
120 } |
|
121 |
|
122 JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock) |
|
123 { |
|
124 int startOffset = 0; |
|
125 int endOffset = 0; |
|
126 int divotPoint = 0; |
|
127 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); |
|
128 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, makeString("not a valid argument for '", op, "'")); |
|
129 JSObject* exception = addErrorInfo(exec, createTypeError(exec, errorMessage), line, codeBlock->ownerExecutable()->source(), divotPoint, startOffset, endOffset); |
|
130 return exception; |
|
131 } |
|
132 |
|
133 JSObject* createNotAConstructorError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock) |
|
134 { |
|
135 int startOffset = 0; |
|
136 int endOffset = 0; |
|
137 int divotPoint = 0; |
|
138 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); |
|
139 |
|
140 // We're in a "new" expression, so we need to skip over the "new.." part |
|
141 int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new " |
|
142 const UChar* data = codeBlock->source()->data(); |
|
143 while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint])) |
|
144 startPoint++; |
|
145 |
|
146 UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor"); |
|
147 JSObject* exception = addErrorInfo(exec, createTypeError(exec, errorMessage), line, codeBlock->ownerExecutable()->source(), divotPoint, startOffset, endOffset); |
|
148 return exception; |
|
149 } |
|
150 |
|
151 JSValue createNotAFunctionError(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 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function"); |
|
158 JSObject* exception = addErrorInfo(exec, createTypeError(exec, errorMessage), line, codeBlock->ownerExecutable()->source(), divotPoint, startOffset, endOffset); |
|
159 return exception; |
|
160 } |
|
161 |
|
162 JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull) |
|
163 { |
|
164 return new (exec) JSNotAnObjectErrorStub(exec, isNull); |
|
165 } |
|
166 |
|
167 JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, unsigned bytecodeOffset, CodeBlock* codeBlock) |
|
168 { |
|
169 // Both op_create_this and op_instanceof require a use of op_get_by_id to get |
|
170 // the prototype property from an object. The exception messages for exceptions |
|
171 // thrown by these instances op_get_by_id need to reflect this. |
|
172 OpcodeID followingOpcodeID; |
|
173 if (codeBlock->getByIdExceptionInfoForBytecodeOffset(exec, bytecodeOffset, followingOpcodeID)) { |
|
174 ASSERT(followingOpcodeID == op_create_this || followingOpcodeID == op_instanceof); |
|
175 if (followingOpcodeID == op_create_this) |
|
176 return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock); |
|
177 return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock); |
|
178 } |
|
179 |
|
180 int startOffset = 0; |
|
181 int endOffset = 0; |
|
182 int divotPoint = 0; |
|
183 int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset); |
|
184 UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object"); |
|
185 JSObject* exception = addErrorInfo(exec, createTypeError(exec, errorMessage), line, codeBlock->ownerExecutable()->source(), divotPoint, startOffset, endOffset); |
|
186 return exception; |
|
187 } |
|
188 |
|
189 JSValue throwOutOfMemoryError(ExecState* exec) |
|
190 { |
|
191 return throwError(exec, createError(exec, "Out of memory")); |
|
192 } |
|
193 |
|
194 } // namespace JSC |