2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008 Apple Inc. All rights reserved.
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.
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.
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
22 #include "object_object.h"
24 #include "JSGlobalObject.h"
25 #include "operations.h"
26 #include "function_object.h"
31 // ------------------------------ ObjectPrototype --------------------------------
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
&);
43 ObjectPrototype::ObjectPrototype(ExecState
* exec
, FunctionPrototype
* functionPrototype
)
44 : JSObject() // [[Prototype]] is null
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__");
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
);
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
);
69 // ------------------------------ Functions --------------------------------
71 // ECMA 15.2.4.2, 15.2.4.4, 15.2.4.5, 15.2.4.7
73 JSValue
* objectProtoFuncValueOf(ExecState
*, JSObject
* thisObj
, const List
&)
78 JSValue
* objectProtoFuncHasOwnProperty(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
80 return jsBoolean(thisObj
->hasOwnProperty(exec
, Identifier(args
[0]->toString(exec
))));
83 JSValue
* objectProtoFuncIsPrototypeOf(ExecState
*, JSObject
* thisObj
, const List
& args
)
85 if (!args
[0]->isObject())
86 return jsBoolean(false);
88 JSValue
* v
= static_cast<JSObject
*>(args
[0])->prototype();
92 return jsBoolean(false);
93 if (thisObj
== static_cast<JSObject
*>(v
))\v
94 return jsBoolean(true);
95 v
= static_cast<JSObject
*>(v
)->prototype();
99 JSValue
* objectProtoFuncDefineGetter(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
101 if (!args
[1]->isObject() || !static_cast<JSObject
*>(args
[1])->implementsCall())
102 return throwError(exec
, SyntaxError
, "invalid getter usage");
104 thisObj
->defineGetter(exec
, Identifier(args
[0]->toString(exec
)), static_cast<JSObject
*>(args
[1]));
105 return jsUndefined();
108 JSValue
* objectProtoFuncDefineSetter(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
110 if (!args
[1]->isObject() || !static_cast<JSObject
*>(args
[1])->implementsCall())
111 return throwError(exec
, SyntaxError
, "invalid setter usage");
113 thisObj
->defineSetter(exec
, Identifier(args
[0]->toString(exec
)), static_cast<JSObject
*>(args
[1]));
114 return jsUndefined();
117 JSValue
* objectProtoFuncLookupGetter(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
119 Identifier propertyName
= Identifier(args
[0]->toString(exec
));
120 JSObject
* obj
= thisObj
;
122 JSValue
* v
= obj
->getDirect(propertyName
);
124 if (v
->type() != GetterSetterType
)
125 return jsUndefined();
126 JSObject
* funcObj
= static_cast<GetterSetterImp
*>(v
)->getGetter();
128 return jsUndefined();
132 if (!obj
->prototype() || !obj
->prototype()->isObject())
133 return jsUndefined();
134 obj
= static_cast<JSObject
*>(obj
->prototype());
138 JSValue
* objectProtoFuncLookupSetter(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
140 Identifier propertyName
= Identifier(args
[0]->toString(exec
));
141 JSObject
* obj
= thisObj
;
143 JSValue
* v
= obj
->getDirect(propertyName
);
145 if (v
->type() != GetterSetterType
)
146 return jsUndefined();
147 JSObject
* funcObj
= static_cast<GetterSetterImp
*>(v
)->getSetter();
149 return jsUndefined();
153 if (!obj
->prototype() || !obj
->prototype()->isObject())
154 return jsUndefined();
155 obj
= static_cast<JSObject
*>(obj
->prototype());
159 JSValue
* objectProtoFuncPropertyIsEnumerable(ExecState
* exec
, JSObject
* thisObj
, const List
& args
)
161 return jsBoolean(thisObj
->propertyIsEnumerable(exec
, Identifier(args
[0]->toString(exec
))));
164 JSValue
* objectProtoFuncToLocaleString(ExecState
* exec
, JSObject
* thisObj
, const List
&)
166 return jsString(thisObj
->toString(exec
));
169 JSValue
* objectProtoFuncToString(ExecState
*, JSObject
* thisObj
, const List
&)
171 return jsString("[object " + thisObj
->className() + "]");
174 // ------------------------------ ObjectObjectImp --------------------------------
176 ObjectObjectImp::ObjectObjectImp(ExecState
* exec
, ObjectPrototype
* objProto
, FunctionPrototype
* funcProto
)
177 : InternalFunctionImp(funcProto
, "Object")
180 putDirect(exec
->propertyNames().prototype
, objProto
, DontEnum
|DontDelete
|ReadOnly
);
182 // no. of arguments for constructor
183 putDirect(exec
->propertyNames().length
, jsNumber(1), ReadOnly
|DontDelete
|DontEnum
);
187 bool ObjectObjectImp::implementsConstruct() const
193 JSObject
* ObjectObjectImp::construct(ExecState
* exec
, const List
& args
)
195 JSValue
* arg
= args
[0];
196 switch (arg
->type()) {
201 return arg
->toObject(exec
);
204 return new JSObject(exec
->lexicalGlobalObject()->objectPrototype());
206 ASSERT_NOT_REACHED();
211 JSValue
* ObjectObjectImp::callAsFunction(ExecState
* exec
, JSObject
* /*thisObj*/, const List
&args
)
213 return construct(exec
, args
);