#include "value.h"
#include "object.h"
#include "types.h"
#include "interpreter.h"
#include "operations.h"
#include "object_object.h"
#include "function_object.h"
#include <stdio.h>
#include <assert.h>
using namespace KJS;
ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec,
FunctionPrototypeImp *funcProto)
: ObjectImp() {
Value protect(this);
putDirect(toStringPropertyName, new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString, 0), DontEnum);
putDirect(valueOfPropertyName, new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf, 0), DontEnum);
}
ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState *exec,
FunctionPrototypeImp *funcProto,
int i, int len)
: InternalFunctionImp(funcProto), id(i)
{
Value protect(this);
putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
}
bool ObjectProtoFuncImp::implementsCall() const
{
return true;
}
Value ObjectProtoFuncImp::call(ExecState *, Object &thisObj, const List &)
{
if (id == ValueOf)
return thisObj;
else
return String("[object "+thisObj.className()+"]");
}
ObjectObjectImp::ObjectObjectImp(ExecState *exec,
ObjectPrototypeImp *objProto,
FunctionPrototypeImp *funcProto)
: InternalFunctionImp(funcProto)
{
Value protect(this);
putDirect(prototypePropertyName, objProto, DontEnum|DontDelete|ReadOnly);
putDirect(lengthPropertyName, NumberImp::one(), ReadOnly|DontDelete|DontEnum);
}
bool ObjectObjectImp::implementsConstruct() const
{
return true;
}
Object ObjectObjectImp::construct(ExecState *exec, const List &args)
{
if (args.isEmpty()) {
Object proto = exec->lexicalInterpreter()->builtinObjectPrototype();
Object result(new ObjectImp(proto));
return result;
}
Value arg = *(args.begin());
Object obj = Object::dynamicCast(arg);
if (!obj.isNull()) {
return obj;
}
switch (arg.type()) {
case StringType:
case BooleanType:
case NumberType:
return arg.toObject(exec);
default:
assert(!"unhandled switch case in ObjectConstructor");
case NullType:
case UndefinedType:
Object proto = exec->lexicalInterpreter()->builtinObjectPrototype();
return Object(new ObjectImp(proto));
}
}
bool ObjectObjectImp::implementsCall() const
{
return true;
}
Value ObjectObjectImp::call(ExecState *exec, Object &, const List &args)
{
Value result;
List argList;
if (args.isEmpty()) {
result = construct(exec,argList);
} else {
Value arg = args[0];
if (arg.type() == NullType || arg.type() == UndefinedType) {
argList.append(arg);
result = construct(exec,argList);
} else
result = arg.toObject(exec);
}
return result;
}