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 "JSString.h"
25 #include "ObjectPrototype.h"
26 #include "PrototypeFunction.h"
27 #include "UString.h"
28
29 namespace JSC {
30
31 ASSERT_CLASS_FITS_IN_CELL(ErrorPrototype);
32
33 static JSValuePtr errorProtoFuncToString(ExecState*, JSObject*, JSValuePtr, const ArgList&);
34
35 // ECMA 15.9.4
ErrorPrototype(ExecState * exec,PassRefPtr<Structure> structure,Structure * prototypeFunctionStructure)36 ErrorPrototype::ErrorPrototype(ExecState* exec, PassRefPtr<Structure> structure, Structure* prototypeFunctionStructure)
37 : ErrorInstance(structure)
38 {
39 // The constructor will be added later in ErrorConstructor's constructor
40
41 putDirectWithoutTransition(exec->propertyNames().name, jsNontrivialString(exec, "Error"), DontEnum);
42 putDirectWithoutTransition(exec->propertyNames().message, jsNontrivialString(exec, "Unknown error"), DontEnum);
43
44 putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, errorProtoFuncToString), DontEnum);
45 }
46
errorProtoFuncToString(ExecState * exec,JSObject *,JSValuePtr thisValue,const ArgList &)47 JSValuePtr errorProtoFuncToString(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&)
48 {
49 JSObject* thisObj = thisValue.toThisObject(exec);
50
51 UString s = "Error";
52
53 JSValuePtr v = thisObj->get(exec, exec->propertyNames().name);
54 if (!v.isUndefined())
55 s = v.toString(exec);
56
57 v = thisObj->get(exec, exec->propertyNames().message);
58 if (!v.isUndefined()) {
59 // Mozilla-compatible format.
60 s += ": ";
61 s += v.toString(exec);
62 }
63
64 return jsNontrivialString(exec, s);
65 }
66
67 } // namespace JSC
68