ExceptionHelpers.cpp 6.16 KB
Newer Older
1
/*
2
 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "ExceptionHelpers.h"

32
#include "CodeBlock.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
33
#include "CallFrame.h"
34
#include "ErrorInstance.h"
35
#include "JSGlobalObjectFunctions.h"
darin@apple.com's avatar
darin@apple.com committed
36
#include "JSObject.h"
37
#include "JSNotAnObject.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
38
#include "Interpreter.h"
39
#include "Nodes.h"
40

41
namespace JSC {
42

43 44
ASSERT_HAS_TRIVIAL_DESTRUCTOR(InterruptedExecutionError);

45
const ClassInfo InterruptedExecutionError::s_info = { "InterruptedExecutionError", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(InterruptedExecutionError) };
46

47
JSValue InterruptedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
48
{
49
    if (hint == PreferString)
50
        return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution exceeded timeout.")));
51
    return JSValue(QNaN);
52
}
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
JSObject* createInterruptedExecutionException(JSGlobalData* globalData)
{
    return InterruptedExecutionError::create(*globalData);
}

bool isInterruptedExecutionException(JSObject* object)
{
    return object->inherits(&InterruptedExecutionError::s_info);
}

bool isInterruptedExecutionException(JSValue value)
{
    return value.inherits(&InterruptedExecutionError::s_info);
}


70 71
ASSERT_HAS_TRIVIAL_DESTRUCTOR(TerminatedExecutionError);

72
const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
weinig@apple.com's avatar
weinig@apple.com committed
73

74
JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
75
{
76
    if (hint == PreferString)
77
        return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
78
    return JSValue(QNaN);
79
}
80

81 82 83 84
JSObject* createTerminatedExecutionException(JSGlobalData* globalData)
{
    return TerminatedExecutionError::create(*globalData);
}
85

86
bool isTerminatedExecutionException(JSObject* object)
87
{
88
    return object->inherits(&TerminatedExecutionError::s_info);
89 90
}

91
bool isTerminatedExecutionException(JSValue value)
92
{
93
    return value.inherits(&TerminatedExecutionError::s_info);
94 95
}

96

97
JSObject* createStackOverflowError(ExecState* exec)
98
{
99
    return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
100 101
}

102 103
JSObject* createStackOverflowError(JSGlobalObject* globalObject)
{
104
    return createRangeError(globalObject, ASCIILiteral("Maximum call stack size exceeded."));
105 106
}

107
JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
108
{
109
    String message(makeString("Can't find variable: ", ident.string()));
110
    return createReferenceError(exec, message);
111 112
}
    
113
JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value)
114
{
115
    String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a valid argument for '", op, "'");
116 117 118
    JSObject* exception = createTypeError(exec, errorMessage);
    ASSERT(exception->isErrorInstance());
    static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
119 120 121
    return exception;
}

122
JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
123
{
124
    String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a constructor");
125 126 127
    JSObject* exception = createTypeError(exec, errorMessage);
    ASSERT(exception->isErrorInstance());
    static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
128
    return exception;
129
}
130

131
JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
132
{
133
    String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not a function");
134 135 136
    JSObject* exception = createTypeError(exec, errorMessage);
    ASSERT(exception->isErrorInstance());
    static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
137
    return exception;
138 139
}

140
JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
141
{
142
    String errorMessage = makeString("'", value.toString(exec)->value(exec), "' is not an object");
143 144 145
    JSObject* exception = createTypeError(exec, errorMessage);
    ASSERT(exception->isErrorInstance());
    static_cast<ErrorInstance*>(exception)->setAppendSourceToMessage();
146
    return exception;
147 148
}

149
JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
150
{
151
    return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, "'"));
152 153 154 155
}

JSObject* createOutOfMemoryError(JSGlobalObject* globalObject)
{
156
    return createError(globalObject, ASCIILiteral("Out of memory"));
157
}
158 159

JSObject* throwOutOfMemoryError(ExecState* exec)
160
{
161
    return throwError(exec, createOutOfMemoryError(exec->lexicalGlobalObject()));
162 163
}

164
JSObject* throwStackOverflowError(ExecState* exec)
165
{
166
    Interpreter::ErrorHandlingMode mode(exec);
167 168
    return throwError(exec, createStackOverflowError(exec));
}
169

170
} // namespace JSC