Error.cpp 7.51 KB
Newer Older
weinig@apple.com's avatar
weinig@apple.com committed
1 2 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
/*
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
 *  Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
 *  Copyright (C) 2007 Eric Seidel (eric@webkit.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "Error.h"

#include "ConstructData.h"
#include "ErrorConstructor.h"
oliver@apple.com's avatar
oliver@apple.com committed
29
#include "JSFunction.h"
weinig@apple.com's avatar
weinig@apple.com committed
30 31
#include "JSGlobalObject.h"
#include "JSObject.h"
32
#include "JSString.h"
weinig@apple.com's avatar
weinig@apple.com committed
33
#include "NativeErrorConstructor.h"
34
#include "SourceCode.h"
weinig@apple.com's avatar
weinig@apple.com committed
35

36
namespace JSC {
weinig@apple.com's avatar
weinig@apple.com committed
37

38 39 40 41 42 43 44
static const char* linePropertyName = "line";
static const char* sourceIdPropertyName = "sourceId";
static const char* sourceURLPropertyName = "sourceURL";

JSObject* createError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
45
    return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message);
46 47 48 49 50
}

JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
51
    return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
52 53 54 55 56
}

JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
57
    return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
58 59 60 61 62
}

JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
63
    return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
64 65 66 67 68
}

JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
69
    return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
70 71 72 73 74
}

JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
75
    return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
76 77 78 79 80
}

JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
{
    ASSERT(!message.isEmpty());
81
    return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

JSObject* createError(ExecState* exec, const UString& message)
{
    return createError(exec->lexicalGlobalObject(), message);
}

JSObject* createEvalError(ExecState* exec, const UString& message)
{
    return createEvalError(exec->lexicalGlobalObject(), message);
}

JSObject* createRangeError(ExecState* exec, const UString& message)
{
    return createRangeError(exec->lexicalGlobalObject(), message);
}

JSObject* createReferenceError(ExecState* exec, const UString& message)
{
    return createReferenceError(exec->lexicalGlobalObject(), message);
}

JSObject* createSyntaxError(ExecState* exec, const UString& message)
{
    return createSyntaxError(exec->lexicalGlobalObject(), message);
}

JSObject* createTypeError(ExecState* exec, const UString& message)
{
    return createTypeError(exec->lexicalGlobalObject(), message);
}

JSObject* createURIError(ExecState* exec, const UString& message)
{
    return createURIError(exec->lexicalGlobalObject(), message);
}

119
JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
120 121 122 123 124
{
    intptr_t sourceID = source.provider()->asID();
    const UString& sourceURL = source.provider()->url();

    if (line != -1)
125
        error->putWithAttributes(globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
ggaren@apple.com's avatar
ggaren@apple.com committed
126
    if (sourceID != -1)
127
        error->putWithAttributes(globalData, Identifier(globalData, sourceIdPropertyName), jsNumber((double)sourceID), ReadOnly | DontDelete);
128
    if (!sourceURL.isNull())
129
        error->putWithAttributes(globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
130

weinig@apple.com's avatar
weinig@apple.com committed
131 132 133
    return error;
}

134
JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source)
weinig@apple.com's avatar
weinig@apple.com committed
135
{
136
    return addErrorInfo(&exec->globalData(), error, line, source);
137 138 139 140 141 142
}

bool hasErrorInfo(ExecState* exec, JSObject* error)
{
    return error->hasProperty(exec, Identifier(exec, linePropertyName))
        || error->hasProperty(exec, Identifier(exec, sourceIdPropertyName))
143
        || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
weinig@apple.com's avatar
weinig@apple.com committed
144 145
}

146
JSValue throwError(ExecState* exec, JSValue error)
weinig@apple.com's avatar
weinig@apple.com committed
147
{
148
    exec->globalData().exception = error;
weinig@apple.com's avatar
weinig@apple.com committed
149 150 151
    return error;
}

152
JSObject* throwError(ExecState* exec, JSObject* error)
weinig@apple.com's avatar
weinig@apple.com committed
153
{
154
    exec->globalData().exception = error;
weinig@apple.com's avatar
weinig@apple.com committed
155 156 157
    return error;
}

158 159 160 161 162 163 164 165 166 167
JSObject* throwTypeError(ExecState* exec)
{
    return throwError(exec, createTypeError(exec, "Type error"));
}

JSObject* throwSyntaxError(ExecState* exec)
{
    return throwError(exec, createSyntaxError(exec, "Syntax error"));
}

168
class StrictModeTypeErrorFunction : public InternalFunction {
169
private:
170
    StrictModeTypeErrorFunction(JSGlobalObject* globalObject, Structure* structure, const UString& message)
171
        : InternalFunction(globalObject, structure)
172 173 174
        , m_message(message)
    {
    }
175 176

public:
177 178
    typedef InternalFunction Base;

179 180
    static StrictModeTypeErrorFunction* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, const UString& message)
    {
181 182 183
        StrictModeTypeErrorFunction* function = new (allocateCell<StrictModeTypeErrorFunction>(*exec->heap())) StrictModeTypeErrorFunction(globalObject, structure, message);
        function->finishCreation(exec->globalData(), exec->globalData().propertyNames->emptyIdentifier);
        return function;
184
    }
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    
    static EncodedJSValue JSC_HOST_CALL constructThrowTypeError(ExecState* exec)
    {
        throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
        return JSValue::encode(jsNull());
    }
    
    ConstructType getConstructData(ConstructData& constructData)
    {
        constructData.native.function = constructThrowTypeError;
        return ConstructTypeHost;
    }
    
    static EncodedJSValue JSC_HOST_CALL callThrowTypeError(ExecState* exec)
    {
        throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
        return JSValue::encode(jsNull());
    }

    CallType getCallData(CallData& callData)
    {
        callData.native.function = callThrowTypeError;
        return CallTypeHost;
    }

private:
    UString m_message;
};

214
ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction);
215 216 217

JSValue createTypeErrorFunction(ExecState* exec, const UString& message)
{
218
    return StrictModeTypeErrorFunction::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->internalFunctionStructure(), message);
219 220
}

221
} // namespace JSC