ObjectPrototype.cpp [plain text]
#include "config.h"
#include "ObjectPrototype.h"
#include "Error.h"
#include "JSFunction.h"
#include "JSString.h"
#include "JSStringBuilder.h"
namespace JSC {
static EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState*);
static EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState*);
}
#include "ObjectPrototype.lut.h"
namespace JSC {
ASSERT_HAS_TRIVIAL_DESTRUCTOR(ObjectPrototype);
const ClassInfo ObjectPrototype::s_info = { "Object", &JSNonFinalObject::s_info, 0, ExecState::objectPrototypeTable, CREATE_METHOD_TABLE(ObjectPrototype) };
ASSERT_CLASS_FITS_IN_CELL(ObjectPrototype);
ObjectPrototype::ObjectPrototype(ExecState* exec, Structure* stucture)
: JSNonFinalObject(exec->globalData(), stucture)
, m_hasNoPropertiesWithUInt32Names(true)
{
}
void ObjectPrototype::finishCreation(JSGlobalData& globalData, JSGlobalObject*)
{
Base::finishCreation(globalData);
ASSERT(inherits(&s_info));
}
void ObjectPrototype::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
{
ObjectPrototype* thisObject = jsCast<ObjectPrototype*>(cell);
Base::put(cell, exec, propertyName, value, slot);
if (thisObject->m_hasNoPropertiesWithUInt32Names) {
bool isUInt32;
propertyName.toUInt32(isUInt32);
thisObject->m_hasNoPropertiesWithUInt32Names = !isUInt32;
}
}
bool ObjectPrototype::defineOwnProperty(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
{
ObjectPrototype* thisObject = jsCast<ObjectPrototype*>(object);
bool result = Base::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
if (thisObject->m_hasNoPropertiesWithUInt32Names) {
bool isUInt32;
propertyName.toUInt32(isUInt32);
thisObject->m_hasNoPropertiesWithUInt32Names = !isUInt32;
}
return result;
}
bool ObjectPrototype::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
{
ObjectPrototype* thisObject = jsCast<ObjectPrototype*>(cell);
if (thisObject->m_hasNoPropertiesWithUInt32Names)
return false;
return Base::getOwnPropertySlotByIndex(thisObject, exec, propertyName, slot);
}
bool ObjectPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
{
return getStaticFunctionSlot<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), jsCast<ObjectPrototype*>(cell), propertyName, slot);
}
bool ObjectPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
{
return getStaticFunctionDescriptor<JSNonFinalObject>(exec, ExecState::objectPrototypeTable(exec), jsCast<ObjectPrototype*>(object), propertyName, descriptor);
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
return JSValue::encode(thisValue.toObject(exec));
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
return JSValue::encode(jsBoolean(thisValue.toObject(exec)->hasOwnProperty(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)))));
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
JSObject* thisObj = thisValue.toObject(exec);
if (!exec->argument(0).isObject())
return JSValue::encode(jsBoolean(false));
JSValue v = asObject(exec->argument(0))->prototype();
while (true) {
if (!v.isObject())
return JSValue::encode(jsBoolean(false));
if (v == thisObj)
return JSValue::encode(jsBoolean(true));
v = asObject(v)->prototype();
}
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec)
{
JSObject* thisObject = exec->hostThisValue().toObject(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSValue get = exec->argument(1);
CallData callData;
if (getCallData(get, callData) == CallTypeNone)
return throwVMError(exec, createSyntaxError(exec, "invalid getter usage"));
PropertyDescriptor descriptor;
descriptor.setGetter(get);
descriptor.setEnumerable(true);
descriptor.setConfigurable(true);
thisObject->methodTable()->defineOwnProperty(thisObject, exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), descriptor, false);
return JSValue::encode(jsUndefined());
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec)
{
JSObject* thisObject = exec->hostThisValue().toObject(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSValue set = exec->argument(1);
CallData callData;
if (getCallData(set, callData) == CallTypeNone)
return throwVMError(exec, createSyntaxError(exec, "invalid setter usage"));
PropertyDescriptor descriptor;
descriptor.setSetter(set);
descriptor.setEnumerable(true);
descriptor.setConfigurable(true);
thisObject->methodTable()->defineOwnProperty(thisObject, exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), descriptor, false);
return JSValue::encode(jsUndefined());
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec)
{
JSObject* thisObject = exec->hostThisValue().toObject(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
PropertyDescriptor descriptor;
if (thisObject->getPropertyDescriptor(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), descriptor)
&& descriptor.getterPresent())
return JSValue::encode(descriptor.getter());
return JSValue::encode(jsUndefined());
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec)
{
JSObject* thisObject = exec->hostThisValue().toObject(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
PropertyDescriptor descriptor;
if (thisObject->getPropertyDescriptor(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), descriptor)
&& descriptor.setterPresent())
return JSValue::encode(descriptor.setter());
return JSValue::encode(jsUndefined());
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
return JSValue::encode(jsBoolean(thisValue.toObject(exec)->propertyIsEnumerable(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)))));
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec)
{
JSObject* object = exec->hostThisValue().toObject(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
JSValue toString = object->get(exec, exec->propertyNames().toString);
CallData callData;
CallType callType = getCallData(toString, callData);
if (callType == CallTypeNone)
return JSValue::encode(jsUndefined());
return JSValue::encode(call(exec, toString, callType, callData, object, exec->emptyList()));
}
EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec)
{
JSValue thisValue = exec->hostThisValue();
if (thisValue.isUndefinedOrNull())
return JSValue::encode(jsNontrivialString(exec, thisValue.isUndefined() ? "[object Undefined]" : "[object Null]"));
JSObject* thisObject = thisValue.toObject(exec);
JSString* result = thisObject->structure()->objectToStringValue();
if (!result) {
RefPtr<StringImpl> newString = WTF::tryMakeString("[object ", thisObject->methodTable()->className(thisObject), "]");
if (!newString)
return JSValue::encode(throwOutOfMemoryError(exec));
result = jsNontrivialString(exec, newString.release());
thisObject->structure()->setObjectToStringValue(exec->globalData(), thisObject, result);
}
return JSValue::encode(result);
}
}