error_object.cpp

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
00005  *  Copyright (C) 2003 Apple Computer, Inc.
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Lesser General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Lesser General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Lesser General Public
00018  *  License along with this library; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020  *
00021  */
00022 
00023 #include "value.h"
00024 #include "object.h"
00025 #include "types.h"
00026 #include "interpreter.h"
00027 #include "operations.h"
00028 #include "error_object.h"
00029 //#include "debugger.h"
00030 
00031 using namespace KJS;
00032 
00033 // ------------------------------ ErrorInstanceImp ----------------------------
00034 
00035 const ClassInfo ErrorInstanceImp::info = {"Error", 0, 0, 0};
00036 
00037 ErrorInstanceImp::ErrorInstanceImp(ObjectImp *proto)
00038   : ObjectImp(proto)
00039 {
00040 }
00041 
00042 // ------------------------------ ErrorPrototypeImp ----------------------------
00043 
00044 // ECMA 15.9.4
00045 ErrorPrototypeImp::ErrorPrototypeImp(ExecState *exec,
00046                                      ObjectPrototypeImp *objectProto,
00047                                      FunctionPrototypeImp *funcProto)
00048   : ObjectImp(objectProto)
00049 {
00050   Value protect(this);
00051   setInternalValue(Undefined());
00052   // The constructor will be added later in ErrorObjectImp's constructor
00053 
00054   put(exec, namePropertyName,     String("Error"), DontEnum);
00055   put(exec, messagePropertyName,  String("Unknown error"), DontEnum);
00056   putDirect(toStringPropertyName, new ErrorProtoFuncImp(exec,funcProto), DontEnum);
00057 }
00058 
00059 // ------------------------------ ErrorProtoFuncImp ----------------------------
00060 
00061 ErrorProtoFuncImp::ErrorProtoFuncImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto)
00062   : InternalFunctionImp(funcProto)
00063 {
00064   Value protect(this);
00065   putDirect(lengthPropertyName, NumberImp::zero(), DontDelete|ReadOnly|DontEnum);
00066   ident = "toString";
00067 }
00068 
00069 bool ErrorProtoFuncImp::implementsCall() const
00070 {
00071   return true;
00072 }
00073 
00074 Value ErrorProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
00075 {
00076   // toString()
00077   UString s = "Error";
00078 
00079   Value v = thisObj.get(exec, namePropertyName);
00080   if (v.type() != UndefinedType) {
00081     s = v.toString(exec);
00082   }
00083 
00084   v = thisObj.get(exec, messagePropertyName);
00085   if (v.type() != UndefinedType) {
00086     s += ": " + v.toString(exec); // Mozilla compatible format
00087   }
00088 
00089   return String(s);
00090 }
00091 
00092 // ------------------------------ ErrorObjectImp -------------------------------
00093 
00094 ErrorObjectImp::ErrorObjectImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
00095                                ErrorPrototypeImp *errorProto)
00096   : InternalFunctionImp(funcProto)
00097 {
00098   Value protect(this);
00099   // ECMA 15.11.3.1 Error.prototype
00100   putDirect(prototypePropertyName, errorProto, DontEnum|DontDelete|ReadOnly);
00101   putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum);
00102   //putDirect(namePropertyName, String(n));
00103 }
00104 
00105 bool ErrorObjectImp::implementsConstruct() const
00106 {
00107   return true;
00108 }
00109 
00110 // ECMA 15.9.3
00111 Object ErrorObjectImp::construct(ExecState *exec, const List &args)
00112 {
00113   Object proto = Object::dynamicCast(exec->lexicalInterpreter()->builtinErrorPrototype());
00114   ObjectImp *imp = new ErrorInstanceImp(proto.imp());
00115   Object obj(imp);
00116 
00117   if (!args.isEmpty() && args[0].type() != UndefinedType) {
00118     imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
00119   }
00120 
00121   return obj;
00122 }
00123 
00124 bool ErrorObjectImp::implementsCall() const
00125 {
00126   return true;
00127 }
00128 
00129 // ECMA 15.9.2
00130 Value ErrorObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00131 {
00132   // "Error()" gives the sames result as "new Error()"
00133   return construct(exec,args);
00134 }
00135 
00136 // ------------------------------ NativeErrorPrototypeImp ----------------------
00137 
00138 NativeErrorPrototypeImp::NativeErrorPrototypeImp(ExecState * /*exec*/, ErrorPrototypeImp *errorProto,
00139                                                  ErrorType et, UString name, UString message)
00140   : ObjectImp(errorProto)
00141 {
00142   Value protect(this);
00143   errType = et;
00144   putDirect(namePropertyName, new StringImp(name), 0);
00145   putDirect(messagePropertyName, new StringImp(message), 0);
00146 }
00147 
00148 // ------------------------------ NativeErrorImp -------------------------------
00149 
00150 const ClassInfo NativeErrorImp::info = {"Function", &InternalFunctionImp::info, 0, 0};
00151 
00152 NativeErrorImp::NativeErrorImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
00153                                const Object &prot)
00154   : InternalFunctionImp(funcProto), proto(0)
00155 {
00156   Value protect(this);
00157   proto = static_cast<ObjectImp*>(prot.imp());
00158 
00159   putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
00160   putDirect(prototypePropertyName, proto, DontDelete|ReadOnly|DontEnum);
00161 }
00162 
00163 bool NativeErrorImp::implementsConstruct() const
00164 {
00165   return true;
00166 }
00167 
00168 Object NativeErrorImp::construct(ExecState *exec, const List &args)
00169 {
00170   ObjectImp *imp = new ErrorInstanceImp(proto);
00171   Object obj(imp);
00172   if (args[0].type() != UndefinedType)
00173     imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
00174   return obj;
00175 }
00176 
00177 bool NativeErrorImp::implementsCall() const
00178 {
00179   return true;
00180 }
00181 
00182 Value NativeErrorImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00183 {
00184   return construct(exec,args);
00185 }
00186 
00187 void NativeErrorImp::mark()
00188 {
00189   ObjectImp::mark();
00190   if (proto && !proto->marked())
00191     proto->mark();
00192 }
KDE Home | KDE Accessibility Home | Description of Access Keys