kjs Library API Documentation

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  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Lesser General Public
00017  *  License along with this library; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "value.h"
00023 #include "object.h"
00024 #include "types.h"
00025 #include "interpreter.h"
00026 #include "operations.h"
00027 #include "error_object.h"
00028 //#include "debugger.h"
00029 
00030 using namespace KJS;
00031 
00032 // ------------------------------ ErrorPrototypeImp ----------------------------
00033 
00034 // ECMA 15.9.4
00035 ErrorPrototypeImp::ErrorPrototypeImp(ExecState *exec,
00036                                      ObjectPrototypeImp *objectProto,
00037                                      FunctionPrototypeImp *funcProto)
00038   : ObjectImp(Object(objectProto))
00039 {
00040   Value protect(this);
00041   setInternalValue(Undefined());
00042   // The constructor will be added later in ErrorObjectImp's constructor
00043 
00044   put(exec, "name",     String("Error"), DontEnum);
00045   put(exec, "message",  String("Unknown error"), DontEnum);
00046   put(exec, "toString", Object(new ErrorProtoFuncImp(exec,funcProto)), DontEnum);
00047 }
00048 
00049 // ------------------------------ ErrorProtoFuncImp ----------------------------
00050 
00051 ErrorProtoFuncImp::ErrorProtoFuncImp(ExecState *exec, FunctionPrototypeImp *funcProto)
00052   : InternalFunctionImp(funcProto)
00053 {
00054   Value protect(this);
00055   put(exec,"length",Number(0),DontDelete|ReadOnly|DontEnum);
00056 }
00057 
00058 bool ErrorProtoFuncImp::implementsCall() const
00059 {
00060   return true;
00061 }
00062 
00063 Value ErrorProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &/*args*/)
00064 {
00065   // toString()
00066   UString s = "Error";
00067 
00068   Value v = thisObj.get(exec,"name");
00069   if (v.type() != UndefinedType) {
00070     s = v.toString(exec);
00071   }
00072 
00073   v = thisObj.get(exec,"message");
00074   if (v.type() != UndefinedType) {
00075     s += ": "+v.toString(exec);
00076   }
00077 
00078   return String(s);
00079 }
00080 
00081 // ------------------------------ ErrorObjectImp -------------------------------
00082 
00083 ErrorObjectImp::ErrorObjectImp(ExecState *exec, FunctionPrototypeImp *funcProto,
00084                                ErrorPrototypeImp *errorProto)
00085   : InternalFunctionImp(funcProto)
00086 {
00087   Value protect(this);
00088   // ECMA 15.11.3.1 Error.prototype
00089   put(exec, "prototype", Object(errorProto), DontEnum|DontDelete|ReadOnly);
00090   //put(exec, "name", String(n));
00091 }
00092 
00093 bool ErrorObjectImp::implementsConstruct() const
00094 {
00095   return true;
00096 }
00097 
00098 // ECMA 15.9.3
00099 Object ErrorObjectImp::construct(ExecState *exec, const List &args)
00100 {
00101   Object proto = Object::dynamicCast(exec->interpreter()->builtinErrorPrototype());
00102   Object obj(new ObjectImp(proto));
00103 
00104   if (!args.isEmpty() && args[0].type() != UndefinedType) {
00105     obj.put(exec,"message", String(args[0].toString(exec)));
00106   }
00107 
00108   return obj;
00109 }
00110 
00111 bool ErrorObjectImp::implementsCall() const
00112 {
00113   return true;
00114 }
00115 
00116 // ECMA 15.9.2
00117 Value ErrorObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00118 {
00119   // "Error()" gives the sames result as "new Error()"
00120   return construct(exec,args);
00121 }
00122 
00123 // ------------------------------ NativeErrorPrototypeImp ----------------------
00124 
00125 NativeErrorPrototypeImp::NativeErrorPrototypeImp(ExecState *exec, ErrorPrototypeImp *errorProto,
00126                                                  ErrorType et, UString name, UString message)
00127   : ObjectImp(Object(errorProto))
00128 {
00129   Value protect(this);
00130   errType = et;
00131   put(exec,"name",String(name));
00132   put(exec,"message",String(message));
00133 }
00134 
00135 // ------------------------------ NativeErrorImp -------------------------------
00136 
00137 const ClassInfo NativeErrorImp::info = {"Error", &InternalFunctionImp::info, 0, 0};
00138 
00139 NativeErrorImp::NativeErrorImp(ExecState *exec, FunctionPrototypeImp *funcProto,
00140                                const Object &prot)
00141   : InternalFunctionImp(funcProto), proto(0)
00142 {
00143   Value protect(this);
00144   proto = static_cast<ObjectImp*>(prot.imp());
00145 
00146   put(exec,"length",Number(1),DontDelete|ReadOnly|DontEnum); // ECMA 15.11.7.5
00147   put(exec,"prototype",prot);
00148 }
00149 
00150 bool NativeErrorImp::implementsConstruct() const
00151 {
00152   return true;
00153 }
00154 
00155 Object NativeErrorImp::construct(ExecState *exec, const List &args)
00156 {
00157   Object obj(new ObjectImp(Object(proto)));
00158   if (args[0].type() != UndefinedType)
00159     obj.put(exec, "message", String(args[0].toString(exec)));
00160   return obj;
00161 }
00162 
00163 bool NativeErrorImp::implementsCall() const
00164 {
00165   return true;
00166 }
00167 
00168 Value NativeErrorImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
00169 {
00170   return construct(exec,args);
00171 }
00172 
00173 void NativeErrorImp::mark()
00174 {
00175   ObjectImp::mark();
00176   if (proto && !proto->marked())
00177     proto->mark();
00178 }
00179 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001