2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2008 Apple Inc. All rights reserved.
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.
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.
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
22 #include "ErrorPrototype.h"
25 #include "JSFunction.h"
27 #include "JSStringBuilder.h"
28 #include "ObjectPrototype.h"
29 #include "StringRecursionChecker.h"
34 ASSERT_CLASS_FITS_IN_CELL(ErrorPrototype);
36 static EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState*);
40 #include "ErrorPrototype.lut.h"
44 const ClassInfo ErrorPrototype::s_info = { "Error", &ErrorInstance::s_info, 0, ExecState::errorPrototypeTable, CREATE_METHOD_TABLE(ErrorPrototype) };
46 /* Source for ErrorPrototype.lut.h
47 @begin errorPrototypeTable
48 toString errorProtoFuncToString DontEnum|Function 0
52 ASSERT_CLASS_FITS_IN_CELL(ErrorPrototype);
54 ErrorPrototype::ErrorPrototype(ExecState* exec, Structure* structure)
55 : ErrorInstance(exec->globalData(), structure)
59 void ErrorPrototype::finishCreation(ExecState* exec, JSGlobalObject*)
61 Base::finishCreation(exec->globalData(), "");
62 ASSERT(inherits(&s_info));
63 putDirect(exec->globalData(), exec->propertyNames().name, jsNontrivialString(exec, "Error"), DontEnum);
66 bool ErrorPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
68 return getStaticFunctionSlot<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(cell), propertyName, slot);
71 bool ErrorPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
73 return getStaticFunctionDescriptor<ErrorInstance>(exec, ExecState::errorPrototypeTable(exec), jsCast<ErrorPrototype*>(object), propertyName, descriptor);
76 // ------------------------------ Functions ---------------------------
78 // ECMA-262 5.1, 15.11.4.4
79 EncodedJSValue JSC_HOST_CALL errorProtoFuncToString(ExecState* exec)
81 // 1. Let O be the this value.
82 JSValue thisValue = exec->hostThisValue();
84 // 2. If Type(O) is not Object, throw a TypeError exception.
85 if (!thisValue.isObject())
86 return throwVMTypeError(exec);
87 JSObject* thisObj = asObject(thisValue);
89 // Guard against recursion!
90 StringRecursionChecker checker(exec, thisObj);
91 if (JSValue earlyReturnValue = checker.earlyReturnValue())
92 return JSValue::encode(earlyReturnValue);
94 // 3. Let name be the result of calling the [[Get]] internal method of O with argument "name".
95 JSValue name = thisObj->get(exec, exec->propertyNames().name);
96 if (exec->hadException())
97 return JSValue::encode(jsUndefined());
99 // 4. If name is undefined, then let name be "Error"; else let name be ToString(name).
101 if (name.isUndefined())
102 nameString = "Error";
104 nameString = name.toString(exec);
105 if (exec->hadException())
106 return JSValue::encode(jsUndefined());
109 // 5. Let msg be the result of calling the [[Get]] internal method of O with argument "message".
110 JSValue message = thisObj->get(exec, exec->propertyNames().message);
111 if (exec->hadException())
112 return JSValue::encode(jsUndefined());
115 // 6. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
116 // 7. If msg is undefined, then let msg be the empty String; else let msg be ToString(msg).
117 UString messageString;
118 if (message.isUndefined())
121 messageString = message.toString(exec);
122 if (exec->hadException())
123 return JSValue::encode(jsUndefined());
126 // 8. If name is the empty String, return msg.
127 if (!nameString.length())
128 return JSValue::encode(message.isString() ? message : jsString(exec, messageString));
130 // 9. If msg is the empty String, return name.
131 if (!messageString.length())
132 return JSValue::encode(name.isString() ? name : jsNontrivialString(exec, nameString));
134 // 10. Return the result of concatenating name, ":", a single space character, and msg.
135 return JSValue::encode(jsMakeNontrivialString(exec, nameString, ": ", messageString));