]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - runtime/ExceptionHelpers.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / ExceptionHelpers.cpp
... / ...
CommitLineData
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 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 "ErrorHandlingScope.h"
35#include "Exception.h"
36#include "JSGlobalObjectFunctions.h"
37#include "JSNotAnObject.h"
38#include "Interpreter.h"
39#include "Nodes.h"
40#include "JSCInlines.h"
41#include "RuntimeType.h"
42#include <wtf/text/StringBuilder.h>
43#include <wtf/text/StringView.h>
44
45namespace JSC {
46
47STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(TerminatedExecutionError);
48
49const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
50
51JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
52{
53 if (hint == PreferString)
54 return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
55 return JSValue(PNaN);
56}
57
58JSObject* createTerminatedExecutionException(VM* vm)
59{
60 return TerminatedExecutionError::create(*vm);
61}
62
63bool isTerminatedExecutionException(Exception* exception)
64{
65 return exception->value().inherits(TerminatedExecutionError::info());
66}
67
68JSObject* createStackOverflowError(ExecState* exec)
69{
70 return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
71}
72
73JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
74{
75 if (exec->propertyNames().isPrivateName(ident)) {
76 String message(makeString("Can't find private variable: @", exec->propertyNames().getPublicName(ident).string()));
77 return createReferenceError(exec, message);
78 }
79 String message(makeString("Can't find variable: ", ident.string()));
80 return createReferenceError(exec, message);
81}
82
83JSString* errorDescriptionForValue(ExecState* exec, JSValue v)
84{
85 if (v.isString())
86 return jsNontrivialString(exec, makeString('"', asString(v)->value(exec), '"'));
87 if (v.isObject()) {
88 CallData callData;
89 JSObject* object = asObject(v);
90 if (object->methodTable()->getCallData(object, callData) != CallTypeNone)
91 return exec->vm().smallStrings.functionString();
92 return jsString(exec, JSObject::calculatedClassName(object));
93 }
94 return v.toString(exec);
95}
96
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)
160{
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));
236 ASSERT(exception->isErrorInstance());
237 return exception;
238}
239
240JSObject* createInvalidInParameterError(ExecState* exec, JSValue value)
241{
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);
248}
249
250JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
251{
252 return createError(exec, value, ASCIILiteral("is not a constructor"), defaultSourceAppender);
253}
254
255JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
256{
257 return createError(exec, value, ASCIILiteral("is not a function"), notAFunctionSourceAppender);
258}
259
260JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
261{
262 return createError(exec, value, ASCIILiteral("is not an object"), defaultSourceAppender);
263}
264
265JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
266{
267 return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, '\''));
268}
269
270JSObject* throwOutOfMemoryError(ExecState* exec)
271{
272 return exec->vm().throwException(exec, createOutOfMemoryError(exec));
273}
274
275JSObject* throwStackOverflowError(ExecState* exec)
276{
277 VM& vm = exec->vm();
278 ErrorHandlingScope errorScope(vm);
279 return vm.throwException(exec, createStackOverflowError(exec));
280}
281
282JSObject* throwTerminatedExecutionException(ExecState* exec)
283{
284 VM& vm = exec->vm();
285 ErrorHandlingScope errorScope(vm);
286 return vm.throwException(exec, createTerminatedExecutionException(&vm));
287}
288
289} // namespace JSC