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