]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/object_object.cpp
JavaScriptCore-466.1.6.tar.gz
[apple/javascriptcore.git] / kjs / object_object.cpp
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "config.h"
22 #include "object_object.h"
23
24 #include "JSGlobalObject.h"
25 #include "operations.h"
26 #include "function_object.h"
27 #include <stdio.h>
28
29 namespace KJS {
30
31 // ------------------------------ ObjectPrototype --------------------------------
32
33 static JSValue* objectProtoFuncValueOf(ExecState*, JSObject*, const List&);
34 static JSValue* objectProtoFuncHasOwnProperty(ExecState*, JSObject*, const List&);
35 static JSValue* objectProtoFuncIsPrototypeOf(ExecState*, JSObject*, const List&);
36 static JSValue* objectProtoFuncDefineGetter(ExecState*, JSObject*, const List&);
37 static JSValue* objectProtoFuncDefineSetter(ExecState*, JSObject*, const List&);
38 static JSValue* objectProtoFuncLookupGetter(ExecState*, JSObject*, const List&);
39 static JSValue* objectProtoFuncLookupSetter(ExecState*, JSObject*, const List&);
40 static JSValue* objectProtoFuncPropertyIsEnumerable(ExecState*, JSObject*, const List&);
41 static JSValue* objectProtoFuncToLocaleString(ExecState*, JSObject*, const List&);
42
43 ObjectPrototype::ObjectPrototype(ExecState* exec, FunctionPrototype* functionPrototype)
44 : JSObject() // [[Prototype]] is null
45 {
46 static const Identifier* hasOwnPropertyPropertyName = new Identifier("hasOwnProperty");
47 static const Identifier* propertyIsEnumerablePropertyName = new Identifier("propertyIsEnumerable");
48 static const Identifier* isPrototypeOfPropertyName = new Identifier("isPrototypeOf");
49 static const Identifier* defineGetterPropertyName = new Identifier("__defineGetter__");
50 static const Identifier* defineSetterPropertyName = new Identifier("__defineSetter__");
51 static const Identifier* lookupGetterPropertyName = new Identifier("__lookupGetter__");
52 static const Identifier* lookupSetterPropertyName = new Identifier("__lookupSetter__");
53
54 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, objectProtoFuncToString), DontEnum);
55 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toLocaleString, objectProtoFuncToLocaleString), DontEnum);
56 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().valueOf, objectProtoFuncValueOf), DontEnum);
57 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 1, *hasOwnPropertyPropertyName, objectProtoFuncHasOwnProperty), DontEnum);
58 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 1, *propertyIsEnumerablePropertyName, objectProtoFuncPropertyIsEnumerable), DontEnum);
59 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 1, *isPrototypeOfPropertyName, objectProtoFuncIsPrototypeOf), DontEnum);
60
61 // Mozilla extensions
62 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 2, *defineGetterPropertyName, objectProtoFuncDefineGetter), DontEnum);
63 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 2, *defineSetterPropertyName, objectProtoFuncDefineSetter), DontEnum);
64 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 1, *lookupGetterPropertyName, objectProtoFuncLookupGetter), DontEnum);
65 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 1, *lookupSetterPropertyName, objectProtoFuncLookupSetter), DontEnum);
66 }
67
68
69 // ------------------------------ Functions --------------------------------
70
71 // ECMA 15.2.4.2, 15.2.4.4, 15.2.4.5, 15.2.4.7
72
73 JSValue* objectProtoFuncValueOf(ExecState*, JSObject* thisObj, const List&)
74 {
75 return thisObj;
76 }
77
78 JSValue* objectProtoFuncHasOwnProperty(ExecState* exec, JSObject* thisObj, const List& args)
79 {
80 return jsBoolean(thisObj->hasOwnProperty(exec, Identifier(args[0]->toString(exec))));
81 }
82
83 JSValue* objectProtoFuncIsPrototypeOf(ExecState*, JSObject* thisObj, const List& args)
84 {
85 if (!args[0]->isObject())
86 return jsBoolean(false);
87
88 JSValue* v = static_cast<JSObject*>(args[0])->prototype();
89
90 while (true) {
91 if (!v->isObject())
92 return jsBoolean(false);
93 if (thisObj == static_cast<JSObject*>(v))\v
94 return jsBoolean(true);
95 v = static_cast<JSObject*>(v)->prototype();
96 }
97 }
98
99 JSValue* objectProtoFuncDefineGetter(ExecState* exec, JSObject* thisObj, const List& args)
100 {
101 if (!args[1]->isObject() || !static_cast<JSObject*>(args[1])->implementsCall())
102 return throwError(exec, SyntaxError, "invalid getter usage");
103
104 thisObj->defineGetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
105 return jsUndefined();
106 }
107
108 JSValue* objectProtoFuncDefineSetter(ExecState* exec, JSObject* thisObj, const List& args)
109 {
110 if (!args[1]->isObject() || !static_cast<JSObject*>(args[1])->implementsCall())
111 return throwError(exec, SyntaxError, "invalid setter usage");
112
113 thisObj->defineSetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
114 return jsUndefined();
115 }
116
117 JSValue* objectProtoFuncLookupGetter(ExecState* exec, JSObject* thisObj, const List& args)
118 {
119 Identifier propertyName = Identifier(args[0]->toString(exec));
120 JSObject* obj = thisObj;
121 while (true) {
122 JSValue* v = obj->getDirect(propertyName);
123 if (v) {
124 if (v->type() != GetterSetterType)
125 return jsUndefined();
126 JSObject* funcObj = static_cast<GetterSetterImp*>(v)->getGetter();
127 if (!funcObj)
128 return jsUndefined();
129 return funcObj;
130 }
131
132 if (!obj->prototype() || !obj->prototype()->isObject())
133 return jsUndefined();
134 obj = static_cast<JSObject*>(obj->prototype());
135 }
136 }
137
138 JSValue* objectProtoFuncLookupSetter(ExecState* exec, JSObject* thisObj, const List& args)
139 {
140 Identifier propertyName = Identifier(args[0]->toString(exec));
141 JSObject* obj = thisObj;
142 while (true) {
143 JSValue* v = obj->getDirect(propertyName);
144 if (v) {
145 if (v->type() != GetterSetterType)
146 return jsUndefined();
147 JSObject* funcObj = static_cast<GetterSetterImp*>(v)->getSetter();
148 if (!funcObj)
149 return jsUndefined();
150 return funcObj;
151 }
152
153 if (!obj->prototype() || !obj->prototype()->isObject())
154 return jsUndefined();
155 obj = static_cast<JSObject*>(obj->prototype());
156 }
157 }
158
159 JSValue* objectProtoFuncPropertyIsEnumerable(ExecState* exec, JSObject* thisObj, const List& args)
160 {
161 return jsBoolean(thisObj->propertyIsEnumerable(exec, Identifier(args[0]->toString(exec))));
162 }
163
164 JSValue* objectProtoFuncToLocaleString(ExecState* exec, JSObject* thisObj, const List&)
165 {
166 return jsString(thisObj->toString(exec));
167 }
168
169 JSValue* objectProtoFuncToString(ExecState*, JSObject* thisObj, const List&)
170 {
171 return jsString("[object " + thisObj->className() + "]");
172 }
173
174 // ------------------------------ ObjectObjectImp --------------------------------
175
176 ObjectObjectImp::ObjectObjectImp(ExecState* exec, ObjectPrototype* objProto, FunctionPrototype* funcProto)
177 : InternalFunctionImp(funcProto, "Object")
178 {
179 // ECMA 15.2.3.1
180 putDirect(exec->propertyNames().prototype, objProto, DontEnum|DontDelete|ReadOnly);
181
182 // no. of arguments for constructor
183 putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
184 }
185
186
187 bool ObjectObjectImp::implementsConstruct() const
188 {
189 return true;
190 }
191
192 // ECMA 15.2.2
193 JSObject* ObjectObjectImp::construct(ExecState* exec, const List& args)
194 {
195 JSValue* arg = args[0];
196 switch (arg->type()) {
197 case StringType:
198 case BooleanType:
199 case NumberType:
200 case ObjectType:
201 return arg->toObject(exec);
202 case NullType:
203 case UndefinedType:
204 return new JSObject(exec->lexicalGlobalObject()->objectPrototype());
205 default:
206 ASSERT_NOT_REACHED();
207 return 0;
208 }
209 }
210
211 JSValue* ObjectObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List &args)
212 {
213 return construct(exec, args);
214 }
215
216 } // namespace KJS