ObjectPrototype.cpp 9.61 KB
Newer Older
1 2
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3
 *  Copyright (C) 2008, 2011 Apple Inc. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
mjs's avatar
mjs committed
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
 *
19 20
 */

mjs's avatar
mjs committed
21
#include "config.h"
weinig@apple.com's avatar
weinig@apple.com committed
22
#include "ObjectPrototype.h"
darin's avatar
darin committed
23

24
#include "Error.h"
25
#include "GetterSetter.h"
26
#include "JSFunction.h"
27
#include "JSString.h"
28
#include "JSStringBuilder.h"
29
#include "Operations.h"
30
#include "StructureRareDataInlines.h"
31

32
namespace JSC {
33

34 35 36 37 38 39 40 41 42
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*);
weinig@apple.com's avatar
weinig@apple.com committed
43

44
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ObjectPrototype);
45

46
const ClassInfo ObjectPrototype::s_info = { "Object", &JSNonFinalObject::s_info, 0, 0, CREATE_METHOD_TABLE(ObjectPrototype) };
47

48 49
ObjectPrototype::ObjectPrototype(VM& vm, Structure* stucture)
    : JSNonFinalObject(vm, stucture)
50
{
51 52
}

53
void ObjectPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
54
{
55
    Base::finishCreation(vm);
56
    ASSERT(inherits(info()));
57
    vm.prototypeMap.addPrototype(this);
58
    
59 60 61 62 63 64 65 66 67 68
    JSC_NATIVE_FUNCTION(vm.propertyNames->toString, objectProtoFuncToString, DontEnum, 0);
    JSC_NATIVE_FUNCTION(vm.propertyNames->toLocaleString, objectProtoFuncToLocaleString, DontEnum, 0);
    JSC_NATIVE_FUNCTION(vm.propertyNames->valueOf, objectProtoFuncValueOf, DontEnum, 0);
    JSC_NATIVE_FUNCTION(vm.propertyNames->hasOwnProperty, objectProtoFuncHasOwnProperty, DontEnum, 1);
    JSC_NATIVE_FUNCTION(vm.propertyNames->propertyIsEnumerable, objectProtoFuncPropertyIsEnumerable, DontEnum, 1);
    JSC_NATIVE_FUNCTION(vm.propertyNames->isPrototypeOf, objectProtoFuncIsPrototypeOf, DontEnum, 1);
    JSC_NATIVE_FUNCTION(vm.propertyNames->__defineGetter__, objectProtoFuncDefineGetter, DontEnum, 2);
    JSC_NATIVE_FUNCTION(vm.propertyNames->__defineSetter__, objectProtoFuncDefineSetter, DontEnum, 2);
    JSC_NATIVE_FUNCTION(vm.propertyNames->__lookupGetter__, objectProtoFuncLookupGetter, DontEnum, 1);
    JSC_NATIVE_FUNCTION(vm.propertyNames->__lookupSetter__, objectProtoFuncLookupSetter, DontEnum, 1);
69 70
}

71
ObjectPrototype* ObjectPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
72
{
73 74
    ObjectPrototype* prototype = new (NotNull, allocateCell<ObjectPrototype>(vm.heap)) ObjectPrototype(vm, structure);
    prototype->finishCreation(vm, globalObject);
75
    return prototype;
76 77 78
}

// ------------------------------ Functions --------------------------------
79

80
EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec)
81
{
82
    JSValue thisValue = exec->hostThisValue().toThis(exec, StrictMode);
83
    return JSValue::encode(thisValue.toObject(exec));
84
}
85

86
EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
87
{
88
    JSValue thisValue = exec->hostThisValue().toThis(exec, StrictMode);
89
    return JSValue::encode(jsBoolean(thisValue.toObject(exec)->hasOwnProperty(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)))));
weinig@apple.com's avatar
weinig@apple.com committed
90
}
91

92
EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
93
{
94
    JSValue thisValue = exec->hostThisValue().toThis(exec, StrictMode);
95
    JSObject* thisObj = thisValue.toObject(exec);
darin@apple.com's avatar
darin@apple.com committed
96

97
    if (!exec->argument(0).isObject())
98
        return JSValue::encode(jsBoolean(false));
weinig@apple.com's avatar
weinig@apple.com committed
99

100
    JSValue v = asObject(exec->argument(0))->prototype();
weinig@apple.com's avatar
weinig@apple.com committed
101 102

    while (true) {
weinig@apple.com's avatar
weinig@apple.com committed
103
        if (!v.isObject())
104
            return JSValue::encode(jsBoolean(false));
105
        if (v == thisObj)
106
            return JSValue::encode(jsBoolean(true));
darin@apple.com's avatar
darin@apple.com committed
107
        v = asObject(v)->prototype();
weinig@apple.com's avatar
weinig@apple.com committed
108 109 110
    }
}

111
EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
112
{
113
    JSObject* thisObject = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
114 115 116
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

117
    JSValue get = exec->argument(1);
darin@apple.com's avatar
darin@apple.com committed
118
    CallData callData;
119
    if (getCallData(get, callData) == CallTypeNone)
120
        return throwVMError(exec, createTypeError(exec, ASCIILiteral("invalid getter usage")));
121 122 123 124 125 126 127

    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);

128
    return JSValue::encode(jsUndefined());
weinig@apple.com's avatar
weinig@apple.com committed
129 130
}

131
EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
132
{
133
    JSObject* thisObject = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
134 135 136
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

137
    JSValue set = exec->argument(1);
darin@apple.com's avatar
darin@apple.com committed
138
    CallData callData;
139
    if (getCallData(set, callData) == CallTypeNone)
140
        return throwVMError(exec, createTypeError(exec, ASCIILiteral("invalid setter usage")));
141 142 143 144 145 146 147

    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);

148
    return JSValue::encode(jsUndefined());
weinig@apple.com's avatar
weinig@apple.com committed
149
}
150

151
EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec)
152
{
153
    JSObject* thisObject = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
154 155 156
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

157 158 159 160
    PropertySlot slot(thisObject);
    if (thisObject->getPropertySlot(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), slot)
        && slot.isAccessor())
        return JSValue::encode(slot.getterSetter()->getter());
161 162

    return JSValue::encode(jsUndefined());
weinig@apple.com's avatar
weinig@apple.com committed
163 164
}

165
EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
166
{
167
    JSObject* thisObject = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
168 169 170
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

171 172 173 174
    PropertySlot slot(thisObject);
    if (thisObject->getPropertySlot(exec, Identifier(exec, exec->argument(0).toString(exec)->value(exec)), slot)
        && slot.isAccessor())
        return JSValue::encode(slot.getterSetter()->setter());
175 176

    return JSValue::encode(jsUndefined());
177 178
}

179
EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
180
{
181 182 183 184 185 186
    JSObject* thisObject = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
    Identifier propertyName(exec, exec->argument(0).toString(exec)->value(exec));

    PropertyDescriptor descriptor;
    bool enumerable = thisObject->getOwnPropertyDescriptor(exec, propertyName, descriptor) && descriptor.enumerable();
    return JSValue::encode(jsBoolean(enumerable));
weinig@apple.com's avatar
weinig@apple.com committed
187 188
}

189
// 15.2.4.3 Object.prototype.toLocaleString()
190
EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
191
{
192
    // 1. Let O be the result of calling ToObject passing the this value as the argument.
193
    JSObject* object = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);
194 195 196 197 198 199 200 201 202 203 204 205 206 207
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    // 2. Let toString be the result of calling the [[Get]] internal method of O passing "toString" as the argument.
    JSValue toString = object->get(exec, exec->propertyNames().toString);

    // 3. If IsCallable(toString) is false, throw a TypeError exception.
    CallData callData;
    CallType callType = getCallData(toString, callData);
    if (callType == CallTypeNone)
        return JSValue::encode(jsUndefined());

    // 4. Return the result of calling the [[Call]] internal method of toString passing O as the this value and no arguments.
    return JSValue::encode(call(exec, toString, callType, callData, object, exec->emptyList()));
weinig@apple.com's avatar
weinig@apple.com committed
208 209
}

210
EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec)
weinig@apple.com's avatar
weinig@apple.com committed
211
{
212
    JSValue thisValue = exec->hostThisValue().toThis(exec, StrictMode);
213
    if (thisValue.isUndefinedOrNull())
214
        return JSValue::encode(jsNontrivialString(exec, String(thisValue.isUndefined() ? ASCIILiteral("[object Undefined]") : ASCIILiteral("[object Null]"))));
215
    JSObject* thisObject = thisValue.toObject(exec);
216 217 218 219 220 221 222 223

    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());
224
        thisObject->structure()->setObjectToStringValue(exec->vm(), thisObject, result);
225 226
    }
    return JSValue::encode(result);
weinig@apple.com's avatar
weinig@apple.com committed
227 228
}

229
} // namespace JSC