#include "config.h"
#include "bool_object.h"
#include "operations.h"
#include "error_object.h"
using namespace KJS;
const ClassInfo BooleanInstance::info = {"Boolean", 0, 0, 0};
BooleanInstance::BooleanInstance(JSObject *proto)
: JSWrapperObject(proto)
{
}
BooleanPrototype::BooleanPrototype(ExecState* exec, ObjectPrototype* objectProto, FunctionPrototype* funcProto)
: BooleanInstance(objectProto)
{
putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum);
putDirectFunction(new BooleanProtoFunc(exec, funcProto, BooleanProtoFunc::ValueOf, 0, exec->propertyNames().valueOf), DontEnum);
setInternalValue(jsBoolean(false));
}
BooleanProtoFunc::BooleanProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
: InternalFunctionImp(funcProto, name)
, id(i)
{
putDirect(exec->propertyNames().length, len, DontDelete|ReadOnly|DontEnum);
}
JSValue *BooleanProtoFunc::callAsFunction(ExecState* exec, JSObject *thisObj, const List &)
{
if (!thisObj->inherits(&BooleanInstance::info))
return throwError(exec, TypeError);
JSValue *v = static_cast<BooleanInstance*>(thisObj)->internalValue();
assert(v);
if (id == ToString)
return jsString(v->toString(exec));
return jsBoolean(v->toBoolean(exec));
}
BooleanObjectImp::BooleanObjectImp(ExecState* exec, FunctionPrototype* funcProto, BooleanPrototype* booleanProto)
: InternalFunctionImp(funcProto)
{
putDirect(exec->propertyNames().prototype, booleanProto, DontEnum|DontDelete|ReadOnly);
putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
}
bool BooleanObjectImp::implementsConstruct() const
{
return true;
}
JSObject *BooleanObjectImp::construct(ExecState *exec, const List &args)
{
BooleanInstance *obj(new BooleanInstance(exec->lexicalInterpreter()->builtinBooleanPrototype()));
bool b;
if (args.size() > 0)
b = args.begin()->toBoolean(exec);
else
b = false;
obj->setInternalValue(jsBoolean(b));
return obj;
}
JSValue *BooleanObjectImp::callAsFunction(ExecState *exec, JSObject *, const List &args)
{
if (args.isEmpty())
return jsBoolean(false);
else
return jsBoolean(args[0]->toBoolean(exec));
}