object_object.cpp
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 "object_object.h"
00029 #include "function_object.h"
00030 #include <stdio.h>
00031 #include <assert.h>
00032
00033 using namespace KJS;
00034
00035
00036
00037 ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec,
00038 FunctionPrototypeImp *funcProto)
00039 : ObjectImp()
00040 {
00041 Value protect(this);
00042 put(exec,"toString", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString, 0)), DontEnum);
00043 put(exec,"valueOf", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf, 0)), DontEnum);
00044 #ifndef KJS_PURE_ECMA // standard compliance location is the Global object
00045
00046 put(exec, "eval",
00047 Object(new GlobalFuncImp(exec, funcProto,GlobalFuncImp::Eval, 1)),
00048 DontEnum);
00049 #endif
00050 }
00051
00052
00053
00054
00055 ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState *exec,
00056 FunctionPrototypeImp *funcProto,
00057 int i, int len)
00058 : InternalFunctionImp(funcProto), id(i)
00059 {
00060 Value protect(this);
00061 put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum);
00062 }
00063
00064
00065 bool ObjectProtoFuncImp::implementsCall() const
00066 {
00067 return true;
00068 }
00069
00070
00071
00072 Value ObjectProtoFuncImp::call(ExecState *, Object &thisObj, const List &)
00073 {
00074 if (id == ValueOf)
00075 return thisObj;
00076 else
00077 return String("[object "+thisObj.className()+"]");
00078 }
00079
00080
00081
00082 ObjectObjectImp::ObjectObjectImp(ExecState *exec,
00083 ObjectPrototypeImp *objProto,
00084 FunctionPrototypeImp *funcProto)
00085 : InternalFunctionImp(funcProto)
00086 {
00087 Value protect(this);
00088
00089 put(exec,"prototype", Object(objProto), DontEnum|DontDelete|ReadOnly);
00090
00091
00092 put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum);
00093 }
00094
00095
00096 bool ObjectObjectImp::implementsConstruct() const
00097 {
00098 return true;
00099 }
00100
00101
00102 Object ObjectObjectImp::construct(ExecState *exec, const List &args)
00103 {
00104
00105 if (args.isEmpty()) {
00106 Object proto = exec->interpreter()->builtinObjectPrototype();
00107 Object result(new ObjectImp(proto));
00108 return result;
00109 }
00110
00111 Value arg = *(args.begin());
00112 Object obj = Object::dynamicCast(arg);
00113 if (!obj.isNull()) {
00114 return obj;
00115 }
00116
00117 switch (arg.type()) {
00118 case StringType:
00119 case BooleanType:
00120 case NumberType:
00121 return arg.toObject(exec);
00122 default:
00123 assert(!"unhandled switch case in ObjectConstructor");
00124 case NullType:
00125 case UndefinedType:
00126 Object proto = exec->interpreter()->builtinObjectPrototype();
00127 return Object(new ObjectImp(proto));
00128 }
00129 }
00130
00131 bool ObjectObjectImp::implementsCall() const
00132 {
00133 return true;
00134 }
00135
00136 Value ObjectObjectImp::call(ExecState *exec, Object &, const List &args)
00137 {
00138 Value result;
00139
00140 List argList;
00141
00142 if (args.isEmpty()) {
00143 result = construct(exec,argList);
00144 } else {
00145 Value arg = args[0];
00146 if (arg.type() == NullType || arg.type() == UndefinedType) {
00147 argList.append(arg);
00148 result = construct(exec,argList);
00149 } else
00150 result = arg.toObject(exec);
00151 }
00152 return result;
00153 }
00154
This file is part of the documentation for kdelibs Version 3.1.5.