]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/ExceptionHelpers.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / ExceptionHelpers.cpp
CommitLineData
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.
81345200 13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
9dae56ea
A
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"
81345200 34#include "ErrorHandlingScope.h"
ed1e77d3 35#include "Exception.h"
9dae56ea 36#include "JSGlobalObjectFunctions.h"
9dae56ea
A
37#include "JSNotAnObject.h"
38#include "Interpreter.h"
39#include "Nodes.h"
81345200 40#include "JSCInlines.h"
ed1e77d3
A
41#include "RuntimeType.h"
42#include <wtf/text/StringBuilder.h>
43#include <wtf/text/StringView.h>
9dae56ea
A
44
45namespace JSC {
46
81345200 47STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(TerminatedExecutionError);
4e4e5a6f 48
ed1e77d3 49const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
4e4e5a6f 50
6fe7ccc8
A
51JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
52{
53 if (hint == PreferString)
93a37866 54 return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
81345200 55 return JSValue(PNaN);
6fe7ccc8 56}
4e4e5a6f 57
93a37866 58JSObject* createTerminatedExecutionException(VM* vm)
4e4e5a6f 59{
93a37866 60 return TerminatedExecutionError::create(*vm);
6fe7ccc8
A
61}
62
ed1e77d3 63bool isTerminatedExecutionException(Exception* exception)
6fe7ccc8 64{
ed1e77d3 65 return exception->value().inherits(TerminatedExecutionError::info());
4e4e5a6f
A
66}
67
14957cd0 68JSObject* createStackOverflowError(ExecState* exec)
9dae56ea 69{
93a37866 70 return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
9dae56ea
A
71}
72
14957cd0 73JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
f9bf01c6 74{
ed1e77d3 75 if (exec->propertyNames().isPrivateName(ident)) {
81345200
A
76 String message(makeString("Can't find private variable: @", exec->propertyNames().getPublicName(ident).string()));
77 return createReferenceError(exec, message);
78 }
93a37866 79 String message(makeString("Can't find variable: ", ident.string()));
14957cd0 80 return createReferenceError(exec, message);
9dae56ea
A
81}
82
81345200
A
83JSString* errorDescriptionForValue(ExecState* exec, JSValue v)
84{
81345200 85 if (v.isString())
ed1e77d3 86 return jsNontrivialString(exec, makeString('"', asString(v)->value(exec), '"'));
81345200
A
87 if (v.isObject()) {
88 CallData callData;
89 JSObject* object = asObject(v);
90 if (object->methodTable()->getCallData(object, callData) != CallTypeNone)
ed1e77d3
A
91 return exec->vm().smallStrings.functionString();
92 return jsString(exec, JSObject::calculatedClassName(object));
81345200 93 }
ed1e77d3 94 return v.toString(exec);
81345200
A
95}
96
ed1e77d3
A
97static String defaultApproximateSourceError(const String& originalMessage, const String& sourceText)
98{
99 return makeString(originalMessage, " (near '...", sourceText, "...')");
100}
101
102static String defaultSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
103{
104 if (occurrence == ErrorInstance::FoundApproximateSource)
105 return defaultApproximateSourceError(originalMessage, sourceText);
106
107 ASSERT(occurrence == ErrorInstance::FoundExactSource);
108 return makeString(originalMessage, " (evaluating '", sourceText, "')");
109}
110
111static String functionCallBase(const String& sourceText)
112{
113 // This function retrieves the 'foo.bar' substring from 'foo.bar(baz)'.
114 // FIXME: This function has simple processing of /* */ style comments.
115 // It doesn't properly handle embedded comments of string literals that contain
116 // parenthesis or comment constructs, e.g. foo.bar("/abc\)*/").
117 // https://bugs.webkit.org/show_bug.cgi?id=146304
118
119 unsigned sourceLength = sourceText.length();
120 unsigned idx = sourceLength - 1;
121 if (sourceLength < 2 || sourceText[idx] != ')') {
122 // For function calls that have many new lines in between their open parenthesis
123 // and their closing parenthesis, the text range passed into the message appender
124 // will not inlcude the text in between these parentheses, it will just be the desired
125 // text that precedes the parentheses.
126 return sourceText;
127 }
128
129 unsigned parenStack = 1;
130 bool isInMultiLineComment = false;
131 idx -= 1;
132 // Note that we're scanning text right to left instead of the more common left to right,
133 // so syntax detection is backwards.
134 while (parenStack > 0) {
135 UChar curChar = sourceText[idx];
136 if (isInMultiLineComment) {
137 if (idx > 0 && curChar == '*' && sourceText[idx - 1] == '/') {
138 isInMultiLineComment = false;
139 idx -= 1;
140 }
141 } else if (curChar == '(')
142 parenStack -= 1;
143 else if (curChar == ')')
144 parenStack += 1;
145 else if (idx > 0 && curChar == '/' && sourceText[idx - 1] == '*') {
146 isInMultiLineComment = true;
147 idx -= 1;
148 }
149
150 if (!idx)
151 break;
152
153 idx -= 1;
154 }
155
156 return sourceText.left(idx + 1);
157}
158
159static String notAFunctionSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType type, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
9dae56ea 160{
ed1e77d3
A
161 ASSERT(type != TypeFunction);
162
163 if (occurrence == ErrorInstance::FoundApproximateSource)
164 return defaultApproximateSourceError(originalMessage, sourceText);
165
166 ASSERT(occurrence == ErrorInstance::FoundExactSource);
167 auto notAFunctionIndex = originalMessage.reverseFind("is not a function");
168 RELEASE_ASSERT(notAFunctionIndex != notFound);
169 StringView displayValue;
170 if (originalMessage.is8Bit())
171 displayValue = StringView(originalMessage.characters8(), notAFunctionIndex - 1);
172 else
173 displayValue = StringView(originalMessage.characters16(), notAFunctionIndex - 1);
174
175 String base = functionCallBase(sourceText);
176 StringBuilder builder;
177 builder.append(base);
178 builder.appendLiteral(" is not a function. (In '");
179 builder.append(sourceText);
180 builder.appendLiteral("', '");
181 builder.append(base);
182 builder.appendLiteral("' is ");
183 if (type == TypeObject)
184 builder.appendLiteral("an instance of ");
185 builder.append(displayValue);
186 builder.appendLiteral(")");
187
188 return builder.toString();
189}
190
191static String invalidParameterInSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType type, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
192{
193 ASSERT_UNUSED(type, type != TypeObject);
194
195 if (occurrence == ErrorInstance::FoundApproximateSource)
196 return defaultApproximateSourceError(originalMessage, sourceText);
197
198 ASSERT(occurrence == ErrorInstance::FoundExactSource);
199 auto inIndex = sourceText.reverseFind("in");
200 RELEASE_ASSERT(inIndex != notFound);
201 if (sourceText.find("in") != inIndex)
202 return makeString(originalMessage, " (evaluating '", sourceText, "')");
203
204 static const unsigned inLength = 2;
205 String rightHandSide = sourceText.substring(inIndex + inLength).simplifyWhiteSpace();
206 return makeString(rightHandSide, " is not an Object. (evaluating '", sourceText, "')");
207}
208
209static String invalidParameterInstanceofSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
210{
211 if (occurrence == ErrorInstance::FoundApproximateSource)
212 return defaultApproximateSourceError(originalMessage, sourceText);
213
214 ASSERT(occurrence == ErrorInstance::FoundExactSource);
215 auto instanceofIndex = sourceText.reverseFind("instanceof");
216 RELEASE_ASSERT(instanceofIndex != notFound);
217 if (sourceText.find("instanceof") != instanceofIndex)
218 return makeString(originalMessage, " (evaluating '", sourceText, "')");
219
220 static const unsigned instanceofLength = 10;
221 String rightHandSide = sourceText.substring(instanceofIndex + instanceofLength).simplifyWhiteSpace();
222 return makeString(rightHandSide, " is not a function. (evaluating '", sourceText, "')");
223}
224
225JSObject* createError(ExecState* exec, JSValue value, const String& message, ErrorInstance::SourceAppender appender)
226{
227 String errorMessage = makeString(errorDescriptionForValue(exec, value)->value(exec), ' ', message);
228 JSObject* exception = createTypeError(exec, errorMessage, appender, runtimeTypeForValue(value));
229 ASSERT(exception->isErrorInstance());
230 return exception;
231}
232
233JSObject* createInvalidFunctionApplyParameterError(ExecState* exec, JSValue value)
234{
235 JSObject* exception = createTypeError(exec, makeString("second argument to Function.prototype.apply must be an Array-like object"), defaultSourceAppender, runtimeTypeForValue(value));
14957cd0 236 ASSERT(exception->isErrorInstance());
14957cd0 237 return exception;
9dae56ea
A
238}
239
ed1e77d3 240JSObject* createInvalidInParameterError(ExecState* exec, JSValue value)
81345200 241{
ed1e77d3
A
242 return createError(exec, value, makeString("is not an Object."), invalidParameterInSourceAppender);
243}
244
245JSObject* createInvalidInstanceofParameterError(ExecState* exec, JSValue value)
246{
247 return createError(exec, value, makeString("is not a function."), invalidParameterInstanceofSourceAppender);
81345200
A
248}
249
14957cd0 250JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
9dae56ea 251{
ed1e77d3 252 return createError(exec, value, ASCIILiteral("is not a constructor"), defaultSourceAppender);
9dae56ea
A
253}
254
14957cd0 255JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
9dae56ea 256{
ed1e77d3 257 return createError(exec, value, ASCIILiteral("is not a function"), notAFunctionSourceAppender);
9dae56ea
A
258}
259
14957cd0 260JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
9dae56ea 261{
ed1e77d3 262 return createError(exec, value, ASCIILiteral("is not an object"), defaultSourceAppender);
9dae56ea
A
263}
264
93a37866 265JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
9dae56ea 266{
ed1e77d3 267 return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, '\''));
14957cd0 268}
9dae56ea 269
14957cd0
A
270JSObject* throwOutOfMemoryError(ExecState* exec)
271{
ed1e77d3 272 return exec->vm().throwException(exec, createOutOfMemoryError(exec));
9dae56ea
A
273}
274
14957cd0 275JSObject* throwStackOverflowError(ExecState* exec)
4e4e5a6f 276{
81345200
A
277 VM& vm = exec->vm();
278 ErrorHandlingScope errorScope(vm);
279 return vm.throwException(exec, createStackOverflowError(exec));
4e4e5a6f
A
280}
281
93a37866
A
282JSObject* throwTerminatedExecutionException(ExecState* exec)
283{
81345200
A
284 VM& vm = exec->vm();
285 ErrorHandlingScope errorScope(vm);
286 return vm.throwException(exec, createTerminatedExecutionException(&vm));
93a37866
A
287}
288
9dae56ea 289} // namespace JSC