ErrorPrototype.cpp 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *  Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "ErrorPrototype.h"

24
#include "Error.h"
25
#include "JSFunction.h"
26
#include "JSString.h"
27
#include "JSStringBuilder.h"
28
#include "ObjectPrototype.h"
29
#include "StringRecursionChecker.h"
30

31
namespace JSC {
32

ggaren@apple.com's avatar
ggaren@apple.com committed
33 34
ASSERT_CLASS_FITS_IN_CELL(ErrorPrototype);

35
static EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState*);
36

37 38 39 40 41 42
}

#include "ErrorPrototype.lut.h"

namespace JSC {

43
const ClassInfo ErrorPrototype::s_info = { "Error", &ErrorInstance::s_info, 0, ExecState::errorPrototypeTable, CREATE_METHOD_TABLE(ErrorPrototype) };
44 45 46 47 48 49 50 51 52

/* Source for ErrorPrototype.lut.h
@begin errorPrototypeTable
  toString          errorProtoFuncToString         DontEnum|Function 0
@end
*/

ASSERT_CLASS_FITS_IN_CELL(ErrorPrototype);

53
ErrorPrototype::ErrorPrototype(ExecState* exec, Structure* structure)
54
    : ErrorInstance(exec->globalData(), structure)
55
{
56
}
57

58
void ErrorPrototype::finishCreation(ExecState* exec, JSGlobalObject*)
59
{
60
    Base::finishCreation(exec->globalData(), "");
61
    ASSERT(inherits(&s_info));
62
    putDirect(exec->globalData(), exec->propertyNames().name, jsNontrivialString(exec, "Error"), DontEnum);
63 64
}

65
bool ErrorPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
66
{
67
    return getStaticFunctionSlot<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(cell), propertyName, slot);
68 69
}

70
bool ErrorPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
71
{
72
    return getStaticFunctionDescriptor<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(object), propertyName, descriptor);
73 74 75 76
}

// ------------------------------ Functions ---------------------------

77
// ECMA-262 5.1, 15.11.4.4
78
EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState* exec)
79
{
80 81
    // 1. Let O be the this value.
    JSValue thisValue = exec->hostThisValue();
82

83 84 85 86 87 88
    // 2. If Type(O) is not Object, throw a TypeError exception.
    if (!thisValue.isObject())
        return throwVMTypeError(exec);
    JSObject* thisObj = asObject(thisValue);

    // Guard against recursion!
89
    StringRecursionChecker checker(exec, thisObj);
90 91
    if (JSValue earlyReturnValue = checker.earlyReturnValue())
        return JSValue::encode(earlyReturnValue);
92

93
    // 3. Let name be the result of calling the [[Get]] internal method of O with argument "name".
94
    JSValue name = thisObj->get(exec, exec->propertyNames().name);
95 96 97 98
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    // 4. If name is undefined, then let name be "Error"; else let name be ToString(name).
99
    String nameString;
100 101 102
    if (name.isUndefined())
        nameString = "Error";
    else {
103
        nameString = name.toString(exec)->value(exec);
104 105 106 107 108
        if (exec->hadException())
            return JSValue::encode(jsUndefined());
    }

    // 5. Let msg be the result of calling the [[Get]] internal method of O with argument "message".
109
    JSValue message = thisObj->get(exec, exec->propertyNames().message);
110 111 112 113 114 115
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    // (sic)
    // 6. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
    // 7. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
116
    String messageString;
117 118 119
    if (message.isUndefined())
        messageString = "";
    else {
120
        messageString = message.toString(exec)->value(exec);
121 122 123
        if (exec->hadException())
            return JSValue::encode(jsUndefined());
    }
124

125 126 127
    // 8. If name is the empty String, return msg.
    if (!nameString.length())
        return JSValue::encode(message.isString() ? message : jsString(exec, messageString));
128

129 130 131 132 133 134
    // 9. If msg is the empty String, return name.
    if (!messageString.length())
        return JSValue::encode(name.isString() ? name : jsNontrivialString(exec, nameString));

    // 10. Return the result of concatenating name, ":", a single space character, and msg.
    return JSValue::encode(jsMakeNontrivialString(exec, nameString, ": ", messageString));
135 136
}

137
} // namespace JSC