~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to JavaScriptCore/kjs/object_object.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c-basic-offset: 2 -*-
 
2
/*
 
3
 *  This file is part of the KDE libraries
 
4
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 *
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
#include "object_object.h"
 
24
 
 
25
#include "operations.h"
 
26
#include "function_object.h"
 
27
#include <stdio.h>
 
28
 
 
29
using namespace KJS;
 
30
 
 
31
// ------------------------------ ObjectPrototype --------------------------------
 
32
 
 
33
ObjectPrototype::ObjectPrototype(ExecState* exec, FunctionPrototype* funcProto)
 
34
  : JSObject() // [[Prototype]] is null
 
35
{
 
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__");
 
43
 
 
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);
 
50
 
 
51
    // Mozilla extensions
 
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);
 
56
}
 
57
 
 
58
 
 
59
// ------------------------------ ObjectProtoFunc --------------------------------
 
60
 
 
61
ObjectProtoFunc::ObjectProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name)
 
62
  : InternalFunctionImp(funcProto, name)
 
63
  , id(i)
 
64
{
 
65
  putDirect(exec->propertyNames().length, len, DontDelete|ReadOnly|DontEnum);
 
66
}
 
67
 
 
68
 
 
69
// ECMA 15.2.4.2, 15.2.4.4, 15.2.4.5, 15.2.4.7
 
70
 
 
71
JSValue *ObjectProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args)
 
72
{
 
73
    switch (id) {
 
74
        case ValueOf:
 
75
            return thisObj;
 
76
        case HasOwnProperty: {
 
77
            PropertySlot slot;
 
78
            return jsBoolean(thisObj->getOwnPropertySlot(exec, Identifier(args[0]->toString(exec)), slot));
 
79
        }
 
80
        case IsPrototypeOf: {
 
81
            if (!args[0]->isObject())
 
82
                return jsBoolean(false);
 
83
         
 
84
            JSValue *v = static_cast<JSObject *>(args[0])->prototype();
 
85
 
 
86
            while (true) {
 
87
                if (!v->isObject())
 
88
                    return jsBoolean(false);
 
89
                
 
90
                if (thisObj == static_cast<JSObject *>(v))
 
91
                    return jsBoolean(true);
 
92
                
 
93
                v = static_cast<JSObject *>(v)->prototype();
 
94
            }
 
95
        }
 
96
        case DefineGetter: 
 
97
        case DefineSetter: {
 
98
            if (!args[1]->isObject() ||
 
99
                !static_cast<JSObject *>(args[1])->implementsCall()) {
 
100
                if (id == DefineGetter)
 
101
                    return throwError(exec, SyntaxError, "invalid getter usage");
 
102
                else
 
103
                    return throwError(exec, SyntaxError, "invalid setter usage");
 
104
            }
 
105
 
 
106
            if (id == DefineGetter)
 
107
                thisObj->defineGetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
 
108
            else
 
109
                thisObj->defineSetter(exec, Identifier(args[0]->toString(exec)), static_cast<JSObject *>(args[1]));
 
110
            return jsUndefined();
 
111
        }
 
112
        case LookupGetter:
 
113
        case LookupSetter: {
 
114
            Identifier propertyName = Identifier(args[0]->toString(exec));
 
115
            
 
116
            JSObject *obj = thisObj;
 
117
            while (true) {
 
118
                JSValue *v = obj->getDirect(propertyName);
 
119
                
 
120
                if (v) {
 
121
                    if (v->type() != GetterSetterType)
 
122
                        return jsUndefined();
 
123
 
 
124
                    JSObject *funcObj;
 
125
                        
 
126
                    if (id == LookupGetter)
 
127
                        funcObj = static_cast<GetterSetterImp *>(v)->getGetter();
 
128
                    else
 
129
                        funcObj = static_cast<GetterSetterImp *>(v)->getSetter();
 
130
                
 
131
                    if (!funcObj)
 
132
                        return jsUndefined();
 
133
                    else
 
134
                        return funcObj;
 
135
                }
 
136
                
 
137
                if (!obj->prototype() || !obj->prototype()->isObject())
 
138
                    return jsUndefined();
 
139
                
 
140
                obj = static_cast<JSObject *>(obj->prototype());
 
141
            }
 
142
        }
 
143
        case PropertyIsEnumerable:
 
144
            return jsBoolean(thisObj->propertyIsEnumerable(exec, Identifier(args[0]->toString(exec))));
 
145
        case ToLocaleString:
 
146
            return jsString(thisObj->toString(exec));
 
147
        case ToString:
 
148
        default:
 
149
            return jsString("[object " + thisObj->className() + "]");
 
150
    }
 
151
}
 
152
 
 
153
// ------------------------------ ObjectObjectImp --------------------------------
 
154
 
 
155
ObjectObjectImp::ObjectObjectImp(ExecState* exec, ObjectPrototype* objProto, FunctionPrototype* funcProto)
 
156
  : InternalFunctionImp(funcProto)
 
157
{
 
158
  // ECMA 15.2.3.1
 
159
  putDirect(exec->propertyNames().prototype, objProto, DontEnum|DontDelete|ReadOnly);
 
160
 
 
161
  // no. of arguments for constructor
 
162
  putDirect(exec->propertyNames().length, jsNumber(1), ReadOnly|DontDelete|DontEnum);
 
163
}
 
164
 
 
165
 
 
166
bool ObjectObjectImp::implementsConstruct() const
 
167
{
 
168
  return true;
 
169
}
 
170
 
 
171
// ECMA 15.2.2
 
172
JSObject* ObjectObjectImp::construct(ExecState* exec, const List& args)
 
173
{
 
174
  JSValue* arg = args[0];
 
175
  switch (arg->type()) {
 
176
  case StringType:
 
177
  case BooleanType:
 
178
  case NumberType:
 
179
  case ObjectType:
 
180
      return arg->toObject(exec);
 
181
  case NullType:
 
182
  case UndefinedType:
 
183
      return new JSObject(exec->lexicalInterpreter()->builtinObjectPrototype());
 
184
  default:
 
185
      ASSERT_NOT_REACHED();
 
186
      return 0;
 
187
  }
 
188
}
 
189
 
 
190
JSValue* ObjectObjectImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List &args)
 
191
{
 
192
    return construct(exec, args);
 
193
}
 
194