#include "value.h"
#include "object.h"
#include "types.h"
#include "interpreter.h"
#include "operations.h"
#include "error_object.h"
using namespace KJS;
ErrorPrototypeImp::ErrorPrototypeImp(ExecState *exec,
ObjectPrototypeImp *objectProto,
FunctionPrototypeImp *funcProto)
: ObjectImp(objectProto)
{
Value protect(this);
setInternalValue(Undefined());
put(exec, namePropertyName, String("Error"), DontEnum);
put(exec, messagePropertyName, String("Unknown error"), DontEnum);
putDirect(toStringPropertyName, new ErrorProtoFuncImp(exec,funcProto), DontEnum);
}
ErrorProtoFuncImp::ErrorProtoFuncImp(ExecState *exec, FunctionPrototypeImp *funcProto)
: InternalFunctionImp(funcProto)
{
Value protect(this);
putDirect(lengthPropertyName, NumberImp::zero(), DontDelete|ReadOnly|DontEnum);
}
bool ErrorProtoFuncImp::implementsCall() const
{
return true;
}
Value ErrorProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &)
{
UString s;
Value v = thisObj.get(exec, namePropertyName);
if (v.type() != UndefinedType) {
s += v.toString(exec);
}
v = thisObj.get(exec, messagePropertyName);
if (v.type() != UndefinedType) {
s += " - " + v.toString(exec);
}
return String(s);
}
ErrorObjectImp::ErrorObjectImp(ExecState *exec, FunctionPrototypeImp *funcProto,
ErrorPrototypeImp *errorProto)
: InternalFunctionImp(funcProto)
{
Value protect(this);
putDirect(prototypePropertyName, errorProto, DontEnum|DontDelete|ReadOnly);
}
bool ErrorObjectImp::implementsConstruct() const
{
return true;
}
Object ErrorObjectImp::construct(ExecState *exec, const List &args)
{
Object proto = Object::dynamicCast(exec->lexicalInterpreter()->builtinErrorPrototype());
ObjectImp *imp = new ObjectImp(proto);
Object obj(imp);
if (!args.isEmpty() && args[0].type() != UndefinedType) {
imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
}
return obj;
}
bool ErrorObjectImp::implementsCall() const
{
return true;
}
Value ErrorObjectImp::call(ExecState *exec, Object &, const List &args)
{
return construct(exec,args);
}
NativeErrorPrototypeImp::NativeErrorPrototypeImp(ExecState *exec, ErrorPrototypeImp *errorProto,
ErrorType et, UString name, UString message)
: ObjectImp(errorProto)
{
Value protect(this);
errType = et;
putDirect(namePropertyName, new StringImp(name), 0);
putDirect(messagePropertyName, new StringImp(message), 0);
}
const ClassInfo NativeErrorImp::info = {"Error", &InternalFunctionImp::info, 0, 0};
NativeErrorImp::NativeErrorImp(ExecState *exec, FunctionPrototypeImp *funcProto,
const Object &prot)
: InternalFunctionImp(funcProto), proto(0)
{
Value protect(this);
proto = static_cast<ObjectImp*>(prot.imp());
putDirect(lengthPropertyName, NumberImp::one(), DontDelete|ReadOnly|DontEnum); putDirect(prototypePropertyName, proto, DontDelete|ReadOnly|DontEnum);
}
bool NativeErrorImp::implementsConstruct() const
{
return true;
}
Object NativeErrorImp::construct(ExecState *exec, const List &args)
{
ObjectImp *imp = new ObjectImp(proto);
Object obj(imp);
if (args[0].type() != UndefinedType)
imp->putDirect(messagePropertyName, new StringImp(args[0].toString(exec)));
return obj;
}
bool NativeErrorImp::implementsCall() const
{
return true;
}
Value NativeErrorImp::call(ExecState *exec, Object &, const List &args)
{
return construct(exec,args);
}
void NativeErrorImp::mark()
{
ObjectImp::mark();
if (proto && !proto->marked())
proto->mark();
}