Move JavaScriptCore to Source
[WebKit-https.git] / Source / JavaScriptCore / runtime / ObjectPrototype.cpp
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2008 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 #include "PrototypeFunction.h"
29
30 namespace JSC {
31
32 ASSERT_CLASS_FITS_IN_CELL(ObjectPrototype);
33
34 static EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState*);
35 static EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState*);
36 static EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState*);
37 static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState*);
38 static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState*);
39 static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState*);
40 static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState*);
41 static EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState*);
42 static EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState*);
43
44 ObjectPrototype::ObjectPrototype(ExecState* exec, JSGlobalObject* globalObject, NonNullPassRefPtr<Structure> stucture, Structure* prototypeFunctionStructure)
45     : JSObject(stucture)
46     , m_hasNoPropertiesWithUInt32Names(true)
47 {
48     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().toString, objectProtoFuncToString), DontEnum);
49     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().toLocaleString, objectProtoFuncToLocaleString), DontEnum);
50     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().valueOf, objectProtoFuncValueOf), DontEnum);
51     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().hasOwnProperty, objectProtoFuncHasOwnProperty), DontEnum);
52     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().propertyIsEnumerable, objectProtoFuncPropertyIsEnumerable), DontEnum);
53     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().isPrototypeOf, objectProtoFuncIsPrototypeOf), DontEnum);
54
55     // Mozilla extensions
56     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 2, exec->propertyNames().__defineGetter__, objectProtoFuncDefineGetter), DontEnum);
57     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 2, exec->propertyNames().__defineSetter__, objectProtoFuncDefineSetter), DontEnum);
58     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().__lookupGetter__, objectProtoFuncLookupGetter), DontEnum);
59     putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 1, exec->propertyNames().__lookupSetter__, objectProtoFuncLookupSetter), DontEnum);
60 }
61
62 void ObjectPrototype::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
63 {
64     JSObject::put(exec, propertyName, value, slot);
65
66     if (m_hasNoPropertiesWithUInt32Names) {
67         bool isUInt32;
68         propertyName.toUInt32(isUInt32);
69         m_hasNoPropertiesWithUInt32Names = !isUInt32;
70     }
71 }
72
73 bool ObjectPrototype::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
74 {
75     if (m_hasNoPropertiesWithUInt32Names)
76         return false;
77     return JSObject::getOwnPropertySlot(exec, propertyName, slot);
78 }
79
80 // ------------------------------ Functions --------------------------------
81
82 // ECMA 15.2.4.2, 15.2.4.4, 15.2.4.5, 15.2.4.7
83
84 EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec)
85 {
86     JSValue thisValue = exec->hostThisValue();
87     return JSValue::encode(thisValue.toThisObject(exec));
88 }
89
90 EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec)
91 {
92     JSValue thisValue = exec->hostThisValue();
93     return JSValue::encode(jsBoolean(thisValue.toThisObject(exec)->hasOwnProperty(exec, Identifier(exec, exec->argument(0).toString(exec)))));
94 }
95
96 EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec)
97 {
98     JSValue thisValue = exec->hostThisValue();
99     JSObject* thisObj = thisValue.toThisObject(exec);
100
101     if (!exec->argument(0).isObject())
102         return JSValue::encode(jsBoolean(false));
103
104     JSValue v = asObject(exec->argument(0))->prototype();
105
106     while (true) {
107         if (!v.isObject())
108             return JSValue::encode(jsBoolean(false));
109         if (v == thisObj)
110             return JSValue::encode(jsBoolean(true));
111         v = asObject(v)->prototype();
112     }
113 }
114
115 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec)
116 {
117     JSValue thisValue = exec->hostThisValue();
118     CallData callData;
119     if (getCallData(exec->argument(1), callData) == CallTypeNone)
120         return throwVMError(exec, createSyntaxError(exec, "invalid getter usage"));
121     thisValue.toThisObject(exec)->defineGetter(exec, Identifier(exec, exec->argument(0).toString(exec)), asObject(exec->argument(1)));
122     return JSValue::encode(jsUndefined());
123 }
124
125 EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec)
126 {
127     JSValue thisValue = exec->hostThisValue();
128     CallData callData;
129     if (getCallData(exec->argument(1), callData) == CallTypeNone)
130         return throwVMError(exec, createSyntaxError(exec, "invalid setter usage"));
131     thisValue.toThisObject(exec)->defineSetter(exec, Identifier(exec, exec->argument(0).toString(exec)), asObject(exec->argument(1)));
132     return JSValue::encode(jsUndefined());
133 }
134
135 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec)
136 {
137     JSValue thisValue = exec->hostThisValue();
138     return JSValue::encode(thisValue.toThisObject(exec)->lookupGetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
139 }
140
141 EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec)
142 {
143     JSValue thisValue = exec->hostThisValue();
144     return JSValue::encode(thisValue.toThisObject(exec)->lookupSetter(exec, Identifier(exec, exec->argument(0).toString(exec))));
145 }
146
147 EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec)
148 {
149     JSValue thisValue = exec->hostThisValue();
150     return JSValue::encode(jsBoolean(thisValue.toThisObject(exec)->propertyIsEnumerable(exec, Identifier(exec, exec->argument(0).toString(exec)))));
151 }
152
153 EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec)
154 {
155     JSValue thisValue = exec->hostThisValue();
156     return JSValue::encode(thisValue.toThisJSString(exec));
157 }
158
159 EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec)
160 {
161     JSValue thisValue = exec->hostThisValue();
162     return JSValue::encode(jsMakeNontrivialString(exec, "[object ", thisValue.toThisObject(exec)->className(), "]"));
163 }
164
165 } // namespace JSC