~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/JavaScriptCore/runtime/ErrorPrototype.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 
3
 *  Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
 
4
 *
 
5
 *  This library is free software; you can redistribute it and/or
 
6
 *  modify it under the terms of the GNU Lesser General Public
 
7
 *  License as published by the Free Software Foundation; either
 
8
 *  version 2 of the License, or (at your option) any later version.
 
9
 *
 
10
 *  This library is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 *  Lesser General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU Lesser General Public
 
16
 *  License along with this library; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include "ErrorPrototype.h"
 
23
 
 
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
 
 
33
ASSERT_HAS_TRIVIAL_DESTRUCTOR(ErrorPrototype);
 
34
 
 
35
static EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState*);
 
36
 
 
37
}
 
38
 
 
39
#include "ErrorPrototype.lut.h"
 
40
 
 
41
namespace JSC {
 
42
 
 
43
const ClassInfo ErrorPrototype::s_info = { "Error", &ErrorInstance::s_info, 0, ExecState::errorPrototypeTable, CREATE_METHOD_TABLE(ErrorPrototype) };
 
44
 
 
45
/* Source for ErrorPrototype.lut.h
 
46
@begin errorPrototypeTable
 
47
  toString          errorProtoFuncToString         DontEnum|Function 0
 
48
@end
 
49
*/
 
50
 
 
51
ErrorPrototype::ErrorPrototype(ExecState* exec, Structure* structure)
 
52
    : ErrorInstance(exec->globalData(), structure)
 
53
{
 
54
}
 
55
 
 
56
void ErrorPrototype::finishCreation(ExecState* exec, JSGlobalObject*)
 
57
{
 
58
    Base::finishCreation(exec->globalData(), "");
 
59
    ASSERT(inherits(&s_info));
 
60
    putDirect(exec->globalData(), exec->propertyNames().name, jsNontrivialString(exec, String(ASCIILiteral("Error"))), DontEnum);
 
61
}
 
62
 
 
63
bool ErrorPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
 
64
{
 
65
    return getStaticFunctionSlot<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(cell), propertyName, slot);
 
66
}
 
67
 
 
68
bool ErrorPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
 
69
{
 
70
    return getStaticFunctionDescriptor<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(object), propertyName, descriptor);
 
71
}
 
72
 
 
73
// ------------------------------ Functions ---------------------------
 
74
 
 
75
// ECMA-262 5.1, 15.11.4.4
 
76
EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState* exec)
 
77
{
 
78
    // 1. Let O be the this value.
 
79
    JSValue thisValue = exec->hostThisValue();
 
80
 
 
81
    // 2. If Type(O) is not Object, throw a TypeError exception.
 
82
    if (!thisValue.isObject())
 
83
        return throwVMTypeError(exec);
 
84
    JSObject* thisObj = asObject(thisValue);
 
85
 
 
86
    // Guard against recursion!
 
87
    StringRecursionChecker checker(exec, thisObj);
 
88
    if (JSValue earlyReturnValue = checker.earlyReturnValue())
 
89
        return JSValue::encode(earlyReturnValue);
 
90
 
 
91
    // 3. Let name be the result of calling the [[Get]] internal method of O with argument "name".
 
92
    JSValue name = thisObj->get(exec, exec->propertyNames().name);
 
93
    if (exec->hadException())
 
94
        return JSValue::encode(jsUndefined());
 
95
 
 
96
    // 4. If name is undefined, then let name be "Error"; else let name be ToString(name).
 
97
    String nameString;
 
98
    if (name.isUndefined())
 
99
        nameString = ASCIILiteral("Error");
 
100
    else {
 
101
        nameString = name.toString(exec)->value(exec);
 
102
        if (exec->hadException())
 
103
            return JSValue::encode(jsUndefined());
 
104
    }
 
105
 
 
106
    // 5. Let msg be the result of calling the [[Get]] internal method of O with argument "message".
 
107
    JSValue message = thisObj->get(exec, exec->propertyNames().message);
 
108
    if (exec->hadException())
 
109
        return JSValue::encode(jsUndefined());
 
110
 
 
111
    // (sic)
 
112
    // 6. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
 
113
    // 7. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
 
114
    String messageString;
 
115
    if (message.isUndefined())
 
116
        messageString = String();
 
117
    else {
 
118
        messageString = message.toString(exec)->value(exec);
 
119
        if (exec->hadException())
 
120
            return JSValue::encode(jsUndefined());
 
121
    }
 
122
 
 
123
    // 8. If name is the empty String, return msg.
 
124
    if (!nameString.length())
 
125
        return JSValue::encode(message.isString() ? message : jsString(exec, messageString));
 
126
 
 
127
    // 9. If msg is the empty String, return name.
 
128
    if (!messageString.length())
 
129
        return JSValue::encode(name.isString() ? name : jsNontrivialString(exec, nameString));
 
130
 
 
131
    // 10. Return the result of concatenating name, ":", a single space character, and msg.
 
132
    return JSValue::encode(jsMakeNontrivialString(exec, nameString, ": ", messageString));
 
133
}
 
134
 
 
135
} // namespace JSC