fix logic
[personal-kdelibs.git] / kjs / error_object.cpp
blob1f176d7344100c16c913d260a68a9e86ec68dc8b
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2003 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "error_object.h"
24 #include <config.h>
26 #include "value.h"
27 #include "object.h"
28 #include "types.h"
29 #include "interpreter.h"
30 #include "operations.h"
31 //#include "debugger.h"
33 using namespace KJS;
35 // ------------------------------ ErrorInstance ----------------------------
37 const ClassInfo ErrorInstance::info = {"Error", 0, 0, 0};
39 ErrorInstance::ErrorInstance(JSObject *proto)
40 : JSObject(proto)
44 // ------------------------------ ErrorPrototype ----------------------------
46 // ECMA 15.9.4
47 ErrorPrototype::ErrorPrototype(ExecState* exec,
48 ObjectPrototype* objectProto,
49 FunctionPrototype* funcProto)
50 : JSObject(objectProto)
52 // Interpreter::initGlobalObject sets the constructor property
53 // on the prototypes for this and the native error types
55 put(exec, exec->propertyNames().name, jsString("Error"), DontEnum);
56 put(exec, exec->propertyNames().message, jsString("Unknown error"), DontEnum);
57 putDirectFunction(new ErrorProtoFunc(exec, funcProto, exec->propertyNames().toString), DontEnum);
60 // ------------------------------ ErrorProtoFunc ----------------------------
62 ErrorProtoFunc::ErrorProtoFunc(ExecState* exec, FunctionPrototype* funcProto, const Identifier& name)
63 : InternalFunctionImp(funcProto, name)
65 putDirect(exec->propertyNames().length, jsNumber(0), DontDelete|ReadOnly|DontEnum);
68 JSValue* ErrorProtoFunc::callAsFunction(ExecState* exec, JSObject* thisObj, const List &/*args*/)
70 // toString()
71 UString s = "Error";
73 JSValue* v = thisObj->get(exec, exec->propertyNames().name);
74 if (!v->isUndefined()) {
75 s = v->toString(exec);
78 v = thisObj->get(exec, exec->propertyNames().message);
79 if (!v->isUndefined()) {
80 s += ": " + v->toString(exec); // Mozilla compatible format
83 return jsString(s);
86 // ------------------------------ ErrorObjectImp -------------------------------
88 ErrorObjectImp::ErrorObjectImp(ExecState* exec, FunctionPrototype* funcProto, ErrorPrototype* errorProto)
89 : InternalFunctionImp(funcProto)
91 // ECMA 15.11.3.1 Error.prototype
92 putDirect(exec->propertyNames().prototype, errorProto, DontEnum|DontDelete|ReadOnly);
93 putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum);
94 //putDirect(namePropertyName, jsString(n));
97 bool ErrorObjectImp::implementsConstruct() const
99 return true;
102 // ECMA 15.9.3
103 JSObject* ErrorObjectImp::construct(ExecState* exec, const List& args)
105 JSObject* proto = static_cast<JSObject*>(exec->lexicalInterpreter()->builtinErrorPrototype());
106 JSObject* imp = new ErrorInstance(proto);
107 JSObject* obj(imp);
109 if (!args[0]->isUndefined())
110 imp->putDirect(exec->propertyNames().message, jsString(args[0]->toString(exec)));
112 return obj;
115 // ECMA 15.9.2
116 JSValue* ErrorObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List &args)
118 // "Error()" gives the sames result as "new Error()"
119 return construct(exec, args);
122 // ------------------------------ NativeErrorPrototype ----------------------
124 NativeErrorPrototype::NativeErrorPrototype(ExecState* exec, ErrorPrototype* errorProto, ErrorType et, UString name, UString message)
125 : JSObject(errorProto)
127 errType = et;
128 putDirect(exec->propertyNames().name, jsString(name), 0);
129 putDirect(exec->propertyNames().message, jsString(message), 0);
132 // ------------------------------ NativeErrorImp -------------------------------
134 const ClassInfo NativeErrorImp::info = {"Function", &InternalFunctionImp::info, 0, 0};
136 NativeErrorImp::NativeErrorImp(ExecState* exec, FunctionPrototype* funcProto, JSObject* prot)
137 : InternalFunctionImp(funcProto)
138 , proto(prot)
140 putDirect(exec->propertyNames().length, jsNumber(1), DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
141 putDirect(exec->propertyNames().prototype, proto, DontDelete|ReadOnly|DontEnum);
144 bool NativeErrorImp::implementsConstruct() const
146 return true;
149 JSObject* NativeErrorImp::construct(ExecState* exec, const List& args)
151 JSObject* imp = new ErrorInstance(proto);
152 JSObject* obj(imp);
153 if (!args[0]->isUndefined())
154 imp->putDirect(exec->propertyNames().message, jsString(args[0]->toString(exec)));
155 return obj;
158 JSValue* NativeErrorImp::callAsFunction(ExecState* exec, JSObject*, const List& args)
160 return construct(exec, args);
163 void NativeErrorImp::mark()
165 InternalFunctionImp::mark();
166 if (proto && !proto->marked())
167 proto->mark();