1
// -*- c-basic-offset: 2 -*-
3
* This file is part of the KDE libraries
4
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
#include "object_object.h"
25
#include "operations.h"
26
#include "function_object.h"
31
// ------------------------------ ObjectPrototype --------------------------------
33
ObjectPrototype::ObjectPrototype(ExecState* exec, FunctionPrototype* funcProto)
34
: JSObject() // [[Prototype]] is null
36
static const Identifier* hasOwnPropertyPropertyName = new Identifier("hasOwnProperty");
37
static const Identifier* propertyIsEnumerablePropertyName = new Identifier("propertyIsEnumerable");
38
static const Identifier* isPrototypeOfPropertyName = new Identifier("isPrototypeOf");
39
static const Identifier* defineGetterPropertyName = new Identifier("__defineGetter__");
40
static const Identifier* defineSetterPropertyName = new Identifier("__defineSetter__");
41
static const Identifier* lookupGetterPropertyName = new Identifier("__lookupGetter__");
42
static const Identifier* lookupSetterPropertyName = new Identifier("__lookupSetter__");
44
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum);
45
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::ToLocaleString, 0, exec->propertyNames().toLocaleString), DontEnum);
46
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::ValueOf, 0, exec->propertyNames().valueOf), DontEnum);
47
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::HasOwnProperty, 1, *hasOwnPropertyPropertyName), DontEnum);
48
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::PropertyIsEnumerable, 1, *propertyIsEnumerablePropertyName), DontEnum);
49
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::IsPrototypeOf, 1, *isPrototypeOfPropertyName), DontEnum);
52
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::DefineGetter, 2, *defineGetterPropertyName), DontEnum);
53
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::DefineSetter, 2, *defineSetterPropertyName), DontEnum);
54
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::LookupGetter, 1, *lookupGetterPropertyName), DontEnum);
55
putDirectFunction(new ObjectProtoFunc(exec, funcProto, ObjectProtoFunc::LookupSetter, 1, *lookupSetterPropertyName), DontEnum);
59
// ------------------------------ ObjectProtoFunc --------------------------------
61
ObjectProtoFunc::ObjectProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
62
: InternalFunctionImp(funcProto, name)
65
putDirect(exec->propertyNames().length, len, DontDelete|ReadOnly|DontEnum);
69
// ECMA 15.2.4.2, 15.2.4.4, 15.2.4.5, 15.2.4.7
71
JSValue *ObjectProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
76
case HasOwnProperty: {
78
return jsBoolean(thisObj->getOwnPropertySlot(exec, Identifier(args[0]->toString(exec)), slot));
81
if (!args[0]->isObject())
82
return jsBoolean(false);
84
JSValue *v = static_cast<JSObject *>(args[0])->prototype();
88
return jsBoolean(false);
90
if (thisObj == static_cast<JSObject *>(v))
91
return jsBoolean(true);
93
v = static_cast<JSObject *>(v)->prototype();
98
if (!args[1]->isObject() ||
99
!static_cast<JSObject *>(args[1])->implementsCall()) {
100
if (id == DefineGetter)
101
return throwError(exec, SyntaxError, "invalid getter usage");
103
return throwError(exec, SyntaxError, "invalid setter usage");
106
if (id == DefineGetter)
107
thisObj->defineGetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
109
thisObj->defineSetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
110
return jsUndefined();
114
Identifier propertyName = Identifier(args[0]->toString(exec));
116
JSObject *obj = thisObj;
118
JSValue *v = obj->getDirect(propertyName);
121
if (v->type() != GetterSetterType)
122
return jsUndefined();
126
if (id == LookupGetter)
127
funcObj = static_cast<GetterSetterImp *>(v)->getGetter();
129
funcObj = static_cast<GetterSetterImp *>(v)->getSetter();
132
return jsUndefined();
137
if (!obj->prototype() || !obj->prototype()->isObject())
138
return jsUndefined();
140
obj = static_cast<JSObject *>(obj->prototype());
143
case PropertyIsEnumerable:
144
return jsBoolean(thisObj->propertyIsEnumerable(exec, Identifier(args[0]->toString(exec))));
146
return jsString(thisObj->toString(exec));
149
return jsString("[object " + thisObj->className() + "]");
153
// ------------------------------ ObjectObjectImp --------------------------------
155
ObjectObjectImp::ObjectObjectImp(ExecState* exec, ObjectPrototype* objProto, FunctionPrototype* funcProto)
156
: InternalFunctionImp(funcProto)
159
putDirect(exec->propertyNames().prototype, objProto, DontEnum|DontDelete|ReadOnly);
161
// no. of arguments for constructor
162
putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
166
bool ObjectObjectImp::implementsConstruct() const
172
JSObject* ObjectObjectImp::construct(ExecState* exec, const List& args)
174
JSValue* arg = args[0];
175
switch (arg->type()) {
180
return arg->toObject(exec);
183
return new JSObject(exec->lexicalInterpreter()->builtinObjectPrototype());
185
ASSERT_NOT_REACHED();
190
JSValue* ObjectObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List &args)
192
return construct(exec, args);