2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2008, 2011 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 "ObjectPrototype.h"
25 #include "JSFunction.h"
27 #include "JSStringBuilder.h"
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*);
43 #include "ObjectPrototype.lut.h"
47 const ClassInfo ObjectPrototype::s_info = { "Object", &JSNonFinalObject::s_info, 0, ExecState::objectPrototypeTable };
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
64 ASSERT_CLASS_FITS_IN_CELL(ObjectPrototype);
66 ObjectPrototype::ObjectPrototype(ExecState* exec, Structure* stucture)
67 : JSNonFinalObject(exec->globalData(), stucture)
68 , m_hasNoPropertiesWithUInt32Names(true)
72 void ObjectPrototype::finishCreation(JSGlobalData& globalData, JSGlobalObject*)
74 Base::finishCreation(globalData);
75 ASSERT(inherits(&s_info));
78 void ObjectPrototype::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
80 JSNonFinalObject::put(exec, propertyName, value, slot);
82 if (m_hasNoPropertiesWithUInt32Names) {
84 propertyName.toUInt32(isUInt32);
85 m_hasNoPropertiesWithUInt32Names = !isUInt32;
89 bool ObjectPrototype::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
91 if (m_hasNoPropertiesWithUInt32Names)
93 return JSNonFinalObject::getOwnPropertySlot(exec, propertyName, slot);
96 bool ObjectPrototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
98 return getStaticFunctionSlot<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), this, propertyName, slot);
101 bool ObjectPrototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
103 return getStaticFunctionDescriptor<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), this, propertyName, descriptor);
106 // ------------------------------ Functions --------------------------------
108 EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec)
110 JSValue thisValue = exec->hostThisValue();
111 return JSValue::encode(thisValue.toObject(exec));
114 EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec)
116 JSValue thisValue = exec->hostThisValue();
117 return JSValue::encode(jsBoolean(thisValue.toObject(exec)->hasOwnProperty(exec, Identifier(exec, exec->argument(0).toString(exec)))));
120 EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec)
122 JSValue thisValue = exec->hostThisValue();
123 JSObject* thisObj = thisValue.toObject(exec);
125 if (!exec->argument(0).isObject())
126 return JSValue::encode(jsBoolean(false));
128 JSValue v = asObject(exec->argument(0))->prototype();
132 return JSValue::encode(jsBoolean(false));
134 return JSValue::encode(jsBoolean(true));
135 v = asObject(v)->prototype();
139 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec)
141 JSValue thisValue = exec->hostThisValue();
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());
149 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec)
151 JSValue thisValue = exec->hostThisValue();
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());
159 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec)
161 JSValue thisValue = exec->hostThisValue();
162 return JSValue::encode(thisValue.toThisObject(exec)->lookupGetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
165 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec)
167 JSValue thisValue = exec->hostThisValue();
168 return JSValue::encode(thisValue.toThisObject(exec)->lookupSetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
171 EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec)
173 JSValue thisValue = exec->hostThisValue();
174 return JSValue::encode(jsBoolean(thisValue.toObject(exec)->propertyIsEnumerable(exec, Identifier(exec, exec->argument(0).toString(exec)))));
177 // 15.2.4.3 Object.prototype.toLocaleString()
178 EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec)
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());
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);
188 // 3. If IsCallable(toString) is false, throw a TypeError exception.
190 CallType callType = getCallData(toString, callData);
191 if (callType == CallTypeNone)
192 return JSValue::encode(jsUndefined());
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()));
198 EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec)
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(), "]"));