initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / ObjectPrototype.cpp
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2008, 2011 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 "ObjectPrototype.h"
23
24 #include "Error.h"
25 #include "JSFunction.h"
26 #include "JSString.h"
27 #include "JSStringBuilder.h"
28
29 namespace JSC {
30
31 static EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState*);
32 static EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState*);
33 static EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState*);
34 static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState*);
35 static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState*);
36 static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState*);
37 static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState*);
38 static EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState*);
39 static EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState*);
40
41 }
42
43 #include "ObjectPrototype.lut.h"
44
45 namespace JSC {
46
47 const ClassInfo ObjectPrototype::s_info = { "Object", &JSNonFinalObject::s_info, 0, ExecState::objectPrototypeTable };
48
49 /* Source for ObjectPrototype.lut.h
50 @begin objectPrototypeTable
51   toString              objectProtoFuncToString                 DontEnum|Function 0
52   toLocaleString        objectProtoFuncToLocaleString           DontEnum|Function 0
53   valueOf               objectProtoFuncValueOf                  DontEnum|Function 0
54   hasOwnProperty        objectProtoFuncHasOwnProperty           DontEnum|Function 1
55   propertyIsEnumerable  objectProtoFuncPropertyIsEnumerable     DontEnum|Function 1
56   isPrototypeOf         objectProtoFuncIsPrototypeOf            DontEnum|Function 1
57   __defineGetter__      objectProtoFuncDefineGetter             DontEnum|Function 2
58   __defineSetter__      objectProtoFuncDefineSetter             DontEnum|Function 2
59   __lookupGetter__      objectProtoFuncLookupGetter             DontEnum|Function 1
60   __lookupSetter__      objectProtoFuncLookupSetter             DontEnum|Function 1
61 @end
62 */
63
64 ASSERT_CLASS_FITS_IN_CELL(ObjectPrototype);
65
66 ObjectPrototype::ObjectPrototype(ExecState* exec, Structure* stucture)
67     : JSNonFinalObject(exec->globalData(), stucture)
68     , m_hasNoPropertiesWithUInt32Names(true)
69 {
70 }
71
72 void ObjectPrototype::finishCreation(JSGlobalData& globalData, JSGlobalObject*)
73 {
74     Base::finishCreation(globalData);
75     ASSERT(inherits(&s_info));
76 }
77
78 void ObjectPrototype::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
79 {
80     JSNonFinalObject::put(exec, propertyName, value, slot);
81
82     if (m_hasNoPropertiesWithUInt32Names) {
83         bool isUInt32;
84         propertyName.toUInt32(isUInt32);
85         m_hasNoPropertiesWithUInt32Names = !isUInt32;
86     }
87 }
88
89 bool ObjectPrototype::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
90 {
91     if (m_hasNoPropertiesWithUInt32Names)
92         return false;
93     return JSNonFinalObject::getOwnPropertySlot(exec, propertyName, slot);
94 }
95
96 bool ObjectPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
97 {
98     return getStaticFunctionSlot<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), this, propertyName, slot);
99 }
100
101 bool ObjectPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
102 {
103     return getStaticFunctionDescriptor<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), this, propertyName, descriptor);
104 }
105
106 // ------------------------------ Functions --------------------------------
107
108 EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec)
109 {
110     JSValue thisValue = exec->hostThisValue();
111     return JSValue::encode(thisValue.toObject(exec));
112 }
113
114 EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec)
115 {
116     JSValue thisValue = exec->hostThisValue();
117     return JSValue::encode(jsBoolean(thisValue.toObject(exec)->hasOwnProperty(exec, Identifier(exec, exec->argument(0).toString(exec)))));
118 }
119
120 EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec)
121 {
122     JSValue thisValue = exec->hostThisValue();
123     JSObject* thisObj = thisValue.toObject(exec);
124
125     if (!exec->argument(0).isObject())
126         return JSValue::encode(jsBoolean(false));
127
128     JSValue v = asObject(exec->argument(0))->prototype();
129
130     while (true) {
131         if (!v.isObject())
132             return JSValue::encode(jsBoolean(false));
133         if (v == thisObj)
134             return JSValue::encode(jsBoolean(true));
135         v = asObject(v)->prototype();
136     }
137 }
138
139 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec)
140 {
141     JSValue thisValue = exec->hostThisValue();
142     CallData callData;
143     if (getCallData(exec->argument(1), callData) == CallTypeNone)
144         return throwVMError(exec, createSyntaxError(exec, "invalid getter usage"));
145     thisValue.toThisObject(exec)->defineGetter(exec, Identifier(exec, exec->argument(0).toString(exec)), asObject(exec->argument(1)));
146     return JSValue::encode(jsUndefined());
147 }
148
149 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec)
150 {
151     JSValue thisValue = exec->hostThisValue();
152     CallData callData;
153     if (getCallData(exec->argument(1), callData) == CallTypeNone)
154         return throwVMError(exec, createSyntaxError(exec, "invalid setter usage"));
155     thisValue.toThisObject(exec)->defineSetter(exec, Identifier(exec, exec->argument(0).toString(exec)), asObject(exec->argument(1)));
156     return JSValue::encode(jsUndefined());
157 }
158
159 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec)
160 {
161     JSValue thisValue = exec->hostThisValue();
162     return JSValue::encode(thisValue.toThisObject(exec)->lookupGetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
163 }
164
165 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec)
166 {
167     JSValue thisValue = exec->hostThisValue();
168     return JSValue::encode(thisValue.toThisObject(exec)->lookupSetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
169 }
170
171 EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec)
172 {
173     JSValue thisValue = exec->hostThisValue();
174     return JSValue::encode(jsBoolean(thisValue.toObject(exec)->propertyIsEnumerable(exec, Identifier(exec, exec->argument(0).toString(exec)))));
175 }
176
177 // 15.2.4.3 Object.prototype.toLocaleString()
178 EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec)
179 {
180     // 1. Let O be the result of calling ToObject passing the this value as the argument.
181     JSObject* object = exec->hostThisValue().toObject(exec);
182     if (exec->hadException())
183         return JSValue::encode(jsUndefined());
184
185     // 2. Let toString be the result of calling the [[Get]] internal method of O passing "toString" as the argument.
186     JSValue toString = object->get(exec, exec->propertyNames().toString);
187
188     // 3. If IsCallable(toString) is false, throw a TypeError exception.
189     CallData callData;
190     CallType callType = getCallData(toString, callData);
191     if (callType == CallTypeNone)
192         return JSValue::encode(jsUndefined());
193
194     // 4. Return the result of calling the [[Call]] internal method of toString passing O as the this value and no arguments.
195     return JSValue::encode(call(exec, toString, callType, callData, object, exec->emptyList()));
196 }
197
198 EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec)
199 {
200     JSValue thisValue = exec->hostThisValue();
201     if (thisValue.isUndefinedOrNull())
202         return JSValue::encode(jsNontrivialString(exec, thisValue.isUndefined() ? "[object Undefined]" : "[object Null]"));
203     return JSValue::encode(jsMakeNontrivialString(exec, "[object ", thisValue.toObject(exec)->className(), "]"));
204 }
205
206 } // namespace JSC