Changeset 798 in webkit for trunk/JavaScriptCore/kjs/object_object.cpp
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/object_object.cpp
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries … … 16 17 * License along with this library; if not, write to the Free Software 17 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 * $Id$ 18 21 */ 19 22 20 #include "kjs.h" 23 #include "value.h" 24 #include "object.h" 25 #include "types.h" 26 #include "interpreter.h" 21 27 #include "operations.h" 22 28 #include "object_object.h" 23 #include " types.h"29 #include "function_object.h" 24 30 #include <stdio.h> 31 #include <assert.h> 25 32 26 33 using namespace KJS; 27 34 28 ObjectObject::ObjectObject(const Object &funcProto, const Object &objProto) 29 : ConstructorImp(funcProto, 1) 35 // ------------------------------ ObjectPrototypeImp -------------------------------- 36 37 ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec, 38 FunctionPrototypeImp *funcProto) 39 : ObjectImp() // [[Prototype]] is Null() 30 40 { 31 // ECMA 15.2.3.1 32 setPrototypeProperty(objProto); 41 Value protect(this); 42 put(exec,"toString", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString, 0)), DontEnum); 43 put(exec,"valueOf", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf, 0)), DontEnum); 33 44 } 34 45 35 Completion ObjectObject::execute(const List &args) 46 47 // ------------------------------ ObjectProtoFuncImp -------------------------------- 48 49 ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState *exec, 50 FunctionPrototypeImp *funcProto, 51 int i, int len) 52 : InternalFunctionImp(funcProto), id(i) 36 53 { 37 KJSO result; 54 Value protect(this); 55 put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum); 56 } 38 57 39 List argList; 40 if (args.isEmpty()) { 41 result = construct(argList); 42 } else { 43 KJSO arg = args[0]; 44 if (arg.isA(NullType) || arg.isA(UndefinedType)) { 45 argList.append(arg); 46 result = construct(argList); 47 } else 48 result = arg.toObject(); 49 } 50 return Completion(ReturnValue, result); 58 59 bool ObjectProtoFuncImp::implementsCall() const 60 { 61 return true; 62 } 63 64 // ECMA 15.2.4.2 + 15.2.4.3 65 66 Value ObjectProtoFuncImp::call(ExecState */*exec*/, Object &thisObj, const List &/*args*/) 67 { 68 if (id == ValueOf) 69 return thisObj; 70 else /* ToString */ 71 return String("[object "+thisObj.className()+"]"); 72 } 73 74 // ------------------------------ ObjectObjectImp -------------------------------- 75 76 ObjectObjectImp::ObjectObjectImp(ExecState *exec, 77 ObjectPrototypeImp *objProto, 78 FunctionPrototypeImp *funcProto) 79 : InternalFunctionImp(funcProto) 80 { 81 Value protect(this); 82 // ECMA 15.2.3.1 83 put(exec,"prototype", Object(objProto), DontEnum|DontDelete|ReadOnly); 84 85 // no. of arguments for constructor 86 put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum); 87 } 88 89 90 bool ObjectObjectImp::implementsConstruct() const 91 { 92 return true; 51 93 } 52 94 53 95 // ECMA 15.2.2 54 Object ObjectObject ::construct(const List &args)96 Object ObjectObjectImp::construct(ExecState *exec, const List &args) 55 97 { 56 98 // if no arguments have been passed ... 57 if (args.isEmpty()) 58 return Object::create(ObjectClass); 99 if (args.isEmpty()) { 100 Object proto = exec->interpreter()->builtinObjectPrototype(); 101 Object result(new ObjectImp(proto)); 102 return result; 103 } 59 104 60 KJSO arg = *args.begin();105 Value arg = *(args.begin()); 61 106 Object obj = Object::dynamicCast(arg); 62 107 if (!obj.isNull()) { 63 /* TODO: handle host objects */64 108 return obj; 65 109 } … … 69 113 case BooleanType: 70 114 case NumberType: 71 return arg.toObject( );115 return arg.toObject(exec); 72 116 default: 73 117 assert(!"unhandled switch case in ObjectConstructor"); 74 118 case NullType: 75 119 case UndefinedType: 76 return Object::create(ObjectClass); 120 Object proto = exec->interpreter()->builtinObjectPrototype(); 121 return Object(new ObjectImp(proto)); 77 122 } 78 123 } 79 124 80 ObjectPrototype::ObjectPrototype() 81 : ObjectImp(ObjectClass) 125 bool ObjectObjectImp::implementsCall() const 82 126 { 83 // the spec says that [[Property]] should be `null'. 84 // Not sure if Null or C's NULL is needed. 127 return true; 85 128 } 86 129 87 bool ObjectPrototype::hasProperty(const UString &p, bool recursive) const 130 Value ObjectObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args) 88 131 { 89 if ( p == "toString" || p == "valueOf" ) 90 return true; 91 return /*recursive &&*/ ObjectImp::hasProperty(p, recursive); 132 Value result; 133 134 List argList; 135 // Construct a new Object 136 if (args.isEmpty()) { 137 result = construct(exec,argList); 138 } else { 139 Value arg = args[0]; 140 if (arg.type() == NullType || arg.type() == UndefinedType) { 141 argList.append(arg); 142 result = construct(exec,argList); 143 } else 144 result = arg.toObject(exec); 145 } 146 return result; 92 147 } 93 148 94 KJSO ObjectPrototype::get(const UString &p) const95 {96 if (p == "toString")97 return Function(new ObjectProtoFunc(ToString));98 else if (p == "valueOf")99 return Function(new ObjectProtoFunc(ValueOf));100 else101 return Imp::get(p);102 }103 104 ObjectProtoFunc::ObjectProtoFunc(int i)105 : id(i)106 {107 }108 109 // ECMA 15.2.4.2 + 15.2.4.3110 Completion ObjectProtoFunc::execute(const List &)111 {112 Object thisObj = Object::dynamicCast(thisValue());113 114 /* TODO: what to do with non-objects. Is this possible at all ? */115 // Yes, this happens with Host Object at least (David)116 if (thisObj.isNull()) {117 UString str = "[object ";118 str += thisValue().isNull() ? "null" : thisValue().imp()->typeInfo()->name;119 str += "]";120 return Completion(ReturnValue, String(str));121 }122 123 // valueOf()124 if (id == ObjectPrototype::ValueOf)125 /* TODO: host objects*/126 return Completion(ReturnValue, thisObj);127 128 // toString()129 UString str;130 switch(thisObj.getClass()) {131 case StringClass:132 str = "[object String]";133 break;134 case BooleanClass:135 str = "[object Boolean]";136 break;137 case NumberClass:138 str = "[object Number]";139 break;140 case ObjectClass:141 {142 str = "[object ";143 str += thisValue().isNull() ? "Object" : thisValue().imp()->typeInfo()->name;144 str += "]";145 break;146 }147 default:148 str = "[undefined object]";149 }150 151 return Completion(ReturnValue, String(str));152 }
Note:
See TracChangeset
for help on using the changeset viewer.