blob: 0dd4f07f5b1240b1d9ec21fbe2e8835491d7f986 [file] [log] [blame]
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +00001/*
weinig@apple.comaf0231a2009-01-08 01:46:14 +00002 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +00003 *
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
oliver@apple.com90b88ae2008-07-19 01:44:24 +000032#include "CodeBlock.h"
ggaren@apple.com5169fc92008-11-17 22:11:26 +000033#include "CallFrame.h"
barraclough@apple.come979a652010-11-16 01:30:25 +000034#include "ErrorInstance.h"
andersca@apple.com8c52c942009-01-23 19:40:56 +000035#include "JSGlobalObjectFunctions.h"
darin@apple.com3dcb6362008-06-16 04:00:19 +000036#include "JSObject.h"
oliver@apple.com90b88ae2008-07-19 01:44:24 +000037#include "JSNotAnObject.h"
ggaren@apple.com901a8a22008-11-17 20:57:18 +000038#include "Interpreter.h"
cwzwarich@webkit.org3ff0e6a2008-11-07 00:18:07 +000039#include "Nodes.h"
fpizlo@apple.coma4b4cbe2013-01-12 04:47:03 +000040#include "Operations.h"
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000041
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000042namespace JSC {
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000043
mhahnenberg@apple.comc58d54d2011-12-16 19:06:44 +000044ASSERT_HAS_TRIVIAL_DESTRUCTOR(InterruptedExecutionError);
45
mhahnenberg@apple.com3e084662011-09-26 07:05:28 +000046const ClassInfo InterruptedExecutionError::s_info = { "InterruptedExecutionError", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(InterruptedExecutionError) };
weinig@apple.com3412bb42008-09-01 21:22:54 +000047
mhahnenberg@apple.com6e506e62011-11-01 01:15:06 +000048JSValue InterruptedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
weinig@apple.comf41a7da2011-09-08 22:38:44 +000049{
mhahnenberg@apple.com7b72b792011-10-12 20:23:08 +000050 if (hint == PreferString)
benjamin@webkit.org762e2c62012-09-04 21:19:25 +000051 return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution exceeded timeout.")));
fpizlo@apple.com3494d022012-11-05 18:15:23 +000052 return JSValue(QNaN);
weinig@apple.comf41a7da2011-09-08 22:38:44 +000053}
commit-queue@webkit.org6c25c522011-08-09 20:46:17 +000054
weinig@apple.com76c11462011-09-11 05:16:09 +000055JSObject* createInterruptedExecutionException(JSGlobalData* globalData)
56{
57 return InterruptedExecutionError::create(*globalData);
58}
59
60bool isInterruptedExecutionException(JSObject* object)
61{
62 return object->inherits(&InterruptedExecutionError::s_info);
63}
64
65bool isInterruptedExecutionException(JSValue value)
66{
67 return value.inherits(&InterruptedExecutionError::s_info);
68}
69
70
mhahnenberg@apple.comc58d54d2011-12-16 19:06:44 +000071ASSERT_HAS_TRIVIAL_DESTRUCTOR(TerminatedExecutionError);
72
mhahnenberg@apple.com3e084662011-09-26 07:05:28 +000073const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
oliver@apple.comfcacd3c2011-07-18 17:47:13 +000074
mhahnenberg@apple.com6e506e62011-11-01 01:15:06 +000075JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
weinig@apple.comf41a7da2011-09-08 22:38:44 +000076{
mhahnenberg@apple.com7b72b792011-10-12 20:23:08 +000077 if (hint == PreferString)
benjamin@webkit.org762e2c62012-09-04 21:19:25 +000078 return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
fpizlo@apple.com3494d022012-11-05 18:15:23 +000079 return JSValue(QNaN);
weinig@apple.comf41a7da2011-09-08 22:38:44 +000080}
ddkilzer@apple.com69637d42009-03-23 18:27:22 +000081
barraclough@apple.com8da6d972010-11-16 21:11:26 +000082JSObject* createTerminatedExecutionException(JSGlobalData* globalData)
eric@webkit.org5d91a8d2010-04-07 04:38:23 +000083{
oliver@apple.comfcacd3c2011-07-18 17:47:13 +000084 return TerminatedExecutionError::create(*globalData);
eric@webkit.org5d91a8d2010-04-07 04:38:23 +000085}
86
weinig@apple.com76c11462011-09-11 05:16:09 +000087bool isTerminatedExecutionException(JSObject* object)
88{
89 return object->inherits(&TerminatedExecutionError::s_info);
90}
91
92bool isTerminatedExecutionException(JSValue value)
93{
94 return value.inherits(&TerminatedExecutionError::s_info);
95}
96
97
barraclough@apple.com9c099f92010-06-06 23:34:36 +000098JSObject* createStackOverflowError(ExecState* exec)
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +000099{
benjamin@webkit.org762e2c62012-09-04 21:19:25 +0000100 return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
oliver@apple.com0ef2c5b2009-10-07 02:06:03 +0000101}
102
barraclough@apple.com40b4ad32010-07-14 00:27:13 +0000103JSObject* createStackOverflowError(JSGlobalObject* globalObject)
104{
benjamin@webkit.org762e2c62012-09-04 21:19:25 +0000105 return createRangeError(globalObject, ASCIILiteral("Maximum call stack size exceeded."));
barraclough@apple.com40b4ad32010-07-14 00:27:13 +0000106}
107
barraclough@apple.com8da6d972010-11-16 21:11:26 +0000108JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000109{
benjamin@webkit.orgc9b7a202012-09-08 05:46:29 +0000110 String message(makeString("Can't find variable: ", ident.string()));
barraclough@apple.come979a652010-11-16 01:30:25 +0000111 return createReferenceError(exec, message);
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000112}
113
barraclough@apple.come979a652010-11-16 01:30:25 +0000114JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value)
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000115{
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000116 String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a valid argument for '", op, "'");
barraclough@apple.come979a652010-11-16 01:30:25 +0000117 JSObject* exception = createTypeError(exec, errorMessage);
118 ASSERT(exception->isErrorInstance());
119 static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000120 return exception;
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000121}
122
barraclough@apple.come979a652010-11-16 01:30:25 +0000123JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000124{
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000125 String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a constructor");
barraclough@apple.come979a652010-11-16 01:30:25 +0000126 JSObject* exception = createTypeError(exec, errorMessage);
127 ASSERT(exception->isErrorInstance());
128 static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000129 return exception;
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000130}
131
barraclough@apple.com8da6d972010-11-16 21:11:26 +0000132JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000133{
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000134 String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a function");
barraclough@apple.come979a652010-11-16 01:30:25 +0000135 JSObject* exception = createTypeError(exec, errorMessage);
136 ASSERT(exception->isErrorInstance());
137 static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000138 return exception;
139}
140
barraclough@apple.com8da6d972010-11-16 21:11:26 +0000141JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000142{
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000143 String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not an object");
barraclough@apple.come979a652010-11-16 01:30:25 +0000144 JSObject* exception = createTypeError(exec, errorMessage);
145 ASSERT(exception->isErrorInstance());
146 static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
oliver@apple.com90b88ae2008-07-19 01:44:24 +0000147 return exception;
mrowe@apple.com2f6dfdf2008-05-22 01:20:45 +0000148}
149
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000150JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
oliver@apple.comaad311c2010-10-18 04:08:37 +0000151{
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +0000152 return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, "'"));
barraclough@apple.com7e6bd6d2011-01-10 20:20:15 +0000153}
154
155JSObject* createOutOfMemoryError(JSGlobalObject* globalObject)
156{
benjamin@webkit.org762e2c62012-09-04 21:19:25 +0000157 return createError(globalObject, ASCIILiteral("Out of memory"));
barraclough@apple.com7e6bd6d2011-01-10 20:20:15 +0000158}
oliver@apple.comaad311c2010-10-18 04:08:37 +0000159
barraclough@apple.com2607dd02010-10-27 20:46:09 +0000160JSObject* throwOutOfMemoryError(ExecState* exec)
161{
barraclough@apple.com7e6bd6d2011-01-10 20:20:15 +0000162 return throwError(exec, createOutOfMemoryError(exec->lexicalGlobalObject()));
barraclough@apple.com2607dd02010-10-27 20:46:09 +0000163}
164
165JSObject* throwStackOverflowError(ExecState* exec)
166{
mark.lam@apple.com8b97fde2012-10-22 22:09:58 +0000167 Interpreter::ErrorHandlingMode mode(exec);
barraclough@apple.com2607dd02010-10-27 20:46:09 +0000168 return throwError(exec, createStackOverflowError(exec));
169}
170
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000171} // namespace JSC