00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00030
00031 using namespace KJS;
00032
00033
00034
00035 const ClassInfo ErrorInstanceImp::info = {"Error", 0, 0, 0};
00036
00037 ErrorInstanceImp::ErrorInstanceImp(ObjectImp *proto)
00038 : ObjectImp(proto)
00039 {
00040 }
00041
00042
00043
00044
00045 ErrorPrototypeImp::ErrorPrototypeImp(ExecState *exec,
00046 ObjectPrototypeImp *objectProto,
00047 FunctionPrototypeImp *funcProto)
00048 : ObjectImp(objectProto)
00049 {
00050 Value protect(this);
00051 setInternalValue(Undefined());
00052
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
00060
00061 ErrorProtoFuncImp::ErrorProtoFuncImp(ExecState * , 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 &)
00075 {
00076
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);
00087 }
00088
00089 return String(s);
00090 }
00091
00092
00093
00094 ErrorObjectImp::ErrorObjectImp(ExecState * , FunctionPrototypeImp *funcProto,
00095 ErrorPrototypeImp *errorProto)
00096 : InternalFunctionImp(funcProto)
00097 {
00098 Value protect(this);
00099
00100 putDirect(prototypePropertyName, errorProto, DontEnum|DontDelete|ReadOnly);
00101 putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum);
00102
00103 }
00104
00105 bool ErrorObjectImp::implementsConstruct() const
00106 {
00107 return true;
00108 }
00109
00110
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
00130 Value ErrorObjectImp::call(ExecState *exec, Object &, const List &args)
00131 {
00132
00133 return construct(exec,args);
00134 }
00135
00136
00137
00138 NativeErrorPrototypeImp::NativeErrorPrototypeImp(ExecState * , 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
00149
00150 const ClassInfo NativeErrorImp::info = {"Function", &InternalFunctionImp::info, 0, 0};
00151
00152 NativeErrorImp::NativeErrorImp(ExecState * , 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);
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 &, 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 }