]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/error_object.cpp
JavaScriptCore-461.tar.gz
[apple/javascriptcore.git] / kjs / error_object.cpp
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 "error_object.h"
23
24 #include "JSGlobalObject.h"
25 #include "object.h"
26 #include "operations.h"
27 #include "types.h"
28 #include "value.h"
29
30 namespace KJS {
31
32 // ------------------------------ ErrorInstance ----------------------------
33
34 const ClassInfo ErrorInstance::info = { "Error", 0, 0 };
35
36 ErrorInstance::ErrorInstance(JSObject* prototype)
37 : JSObject(prototype)
38 {
39 }
40
41 // ------------------------------ ErrorPrototype ----------------------------
42
43 // ECMA 15.9.4
44 ErrorPrototype::ErrorPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype)
45 : ErrorInstance(objectPrototype)
46 {
47 // The constructor will be added later in ErrorObjectImp's constructor
48
49 putDirect(exec->propertyNames().name, jsString("Error"), DontEnum);
50 putDirect(exec->propertyNames().message, jsString("Unknown error"), DontEnum);
51
52 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, errorProtoFuncToString), DontEnum);
53 }
54
55 JSValue* errorProtoFuncToString(ExecState* exec, JSObject* thisObj, const List&)
56 {
57 UString s = "Error";
58
59 JSValue* v = thisObj->get(exec, exec->propertyNames().name);
60 if (!v->isUndefined())
61 s = v->toString(exec);
62
63 v = thisObj->get(exec, exec->propertyNames().message);
64 if (!v->isUndefined())
65 // Mozilla compatible format
66 s += ": " + v->toString(exec);
67
68 return jsString(s);
69 }
70
71 // ------------------------------ ErrorObjectImp -------------------------------
72
73 ErrorObjectImp::ErrorObjectImp(ExecState* exec, FunctionPrototype* funcProto, ErrorPrototype* errorProto)
74 : InternalFunctionImp(funcProto, errorProto->classInfo()->className)
75 {
76 // ECMA 15.11.3.1 Error.prototype
77 putDirect(exec->propertyNames().prototype, errorProto, DontEnum|DontDelete|ReadOnly);
78 putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum);
79 }
80
81 bool ErrorObjectImp::implementsConstruct() const
82 {
83 return true;
84 }
85
86 // ECMA 15.9.3
87 JSObject* ErrorObjectImp::construct(ExecState* exec, const List& args)
88 {
89 JSObject* proto = static_cast<JSObject*>(exec->lexicalGlobalObject()->errorPrototype());
90 JSObject* imp = new ErrorInstance(proto);
91 JSObject* obj(imp);
92
93 if (!args[0]->isUndefined())
94 imp->putDirect(exec->propertyNames().message, jsString(args[0]->toString(exec)));
95
96 return obj;
97 }
98
99 // ECMA 15.9.2
100 JSValue* ErrorObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List& args)
101 {
102 // "Error()" gives the sames result as "new Error()"
103 return construct(exec, args);
104 }
105
106 // ------------------------------ NativeErrorPrototype ----------------------
107
108 NativeErrorPrototype::NativeErrorPrototype(ExecState* exec, ErrorPrototype* errorProto, const UString& name, const UString& message)
109 : JSObject(errorProto)
110 {
111 putDirect(exec->propertyNames().name, jsString(name), 0);
112 putDirect(exec->propertyNames().message, jsString(message), 0);
113 }
114
115 // ------------------------------ NativeErrorImp -------------------------------
116
117 const ClassInfo NativeErrorImp::info = { "Function", &InternalFunctionImp::info, 0 };
118
119 NativeErrorImp::NativeErrorImp(ExecState* exec, FunctionPrototype* funcProto, NativeErrorPrototype* prot)
120 : InternalFunctionImp(funcProto, Identifier(prot->getDirect(exec->propertyNames().name)->getString()))
121 , proto(prot)
122 {
123 putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
124 putDirect(exec->propertyNames().prototype, proto, DontDelete|ReadOnly|DontEnum);
125 }
126
127 bool NativeErrorImp::implementsConstruct() const
128 {
129 return true;
130 }
131
132 JSObject* NativeErrorImp::construct(ExecState* exec, const List& args)
133 {
134 JSObject* imp = new ErrorInstance(proto);
135 JSObject* obj(imp);
136 if (!args[0]->isUndefined())
137 imp->putDirect(exec->propertyNames().message, jsString(args[0]->toString(exec)));
138 return obj;
139 }
140
141 JSValue* NativeErrorImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
142 {
143 return construct(exec, args);
144 }
145
146 void NativeErrorImp::mark()
147 {
148 JSObject::mark();
149 if (proto && !proto->marked())
150 proto->mark();
151 }
152
153 } // namespace KJS