1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
3 | * Copyright (C) 2008-2017 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 "GetterSetter.h" |
26 | #include "HasOwnPropertyCache.h" |
27 | #include "JSFunction.h" |
28 | #include "JSString.h" |
29 | #include "JSCInlines.h" |
30 | #include "PropertySlot.h" |
31 | #include "StructureInlines.h" |
32 | #include "StructureRareDataInlines.h" |
33 | |
34 | namespace JSC { |
35 | |
36 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState*); |
37 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState*); |
38 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState*); |
39 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState*); |
40 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState*); |
41 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState*); |
42 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState*); |
43 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState*); |
44 | static EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState*); |
45 | |
46 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ObjectPrototype); |
47 | |
48 | const ClassInfo ObjectPrototype::s_info = { "Object" , &JSNonFinalObject::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ObjectPrototype) }; |
49 | |
50 | ObjectPrototype::ObjectPrototype(VM& vm, Structure* stucture) |
51 | : JSNonFinalObject(vm, stucture) |
52 | { |
53 | } |
54 | |
55 | void ObjectPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject) |
56 | { |
57 | Base::finishCreation(vm); |
58 | ASSERT(inherits(vm, info())); |
59 | didBecomePrototype(); |
60 | |
61 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->toString, objectProtoFuncToString, static_cast<unsigned>(PropertyAttribute::DontEnum), 0); |
62 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->toLocaleString, objectProtoFuncToLocaleString, static_cast<unsigned>(PropertyAttribute::DontEnum), 0); |
63 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->valueOf, objectProtoFuncValueOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 0); |
64 | JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->hasOwnProperty, objectProtoFuncHasOwnProperty, static_cast<unsigned>(PropertyAttribute::DontEnum), 1, HasOwnPropertyIntrinsic); |
65 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->propertyIsEnumerable, objectProtoFuncPropertyIsEnumerable, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
66 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->isPrototypeOf, objectProtoFuncIsPrototypeOf, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
67 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->__defineGetter__, objectProtoFuncDefineGetter, static_cast<unsigned>(PropertyAttribute::DontEnum), 2); |
68 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->__defineSetter__, objectProtoFuncDefineSetter, static_cast<unsigned>(PropertyAttribute::DontEnum), 2); |
69 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->__lookupGetter__, objectProtoFuncLookupGetter, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
70 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->__lookupSetter__, objectProtoFuncLookupSetter, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
71 | } |
72 | |
73 | ObjectPrototype* ObjectPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure) |
74 | { |
75 | ObjectPrototype* prototype = new (NotNull, allocateCell<ObjectPrototype>(vm.heap)) ObjectPrototype(vm, structure); |
76 | prototype->finishCreation(vm, globalObject); |
77 | return prototype; |
78 | } |
79 | |
80 | // ------------------------------ Functions -------------------------------- |
81 | |
82 | EncodedJSValue JSC_HOST_CALL objectProtoFuncValueOf(ExecState* exec) |
83 | { |
84 | JSValue thisValue = exec->thisValue().toThis(exec, StrictMode); |
85 | JSObject* valueObj = thisValue.toObject(exec); |
86 | if (UNLIKELY(!valueObj)) |
87 | return encodedJSValue(); |
88 | return JSValue::encode(valueObj); |
89 | } |
90 | |
91 | EncodedJSValue JSC_HOST_CALL objectProtoFuncHasOwnProperty(ExecState* exec) |
92 | { |
93 | VM& vm = exec->vm(); |
94 | auto scope = DECLARE_THROW_SCOPE(vm); |
95 | |
96 | JSValue thisValue = exec->thisValue().toThis(exec, StrictMode); |
97 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
98 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
99 | JSObject* thisObject = thisValue.toObject(exec); |
100 | EXCEPTION_ASSERT(!!scope.exception() == !thisObject); |
101 | if (UNLIKELY(!thisObject)) |
102 | return encodedJSValue(); |
103 | |
104 | Structure* structure = thisObject->structure(vm); |
105 | HasOwnPropertyCache* hasOwnPropertyCache = vm.ensureHasOwnPropertyCache(); |
106 | if (Optional<bool> result = hasOwnPropertyCache->get(structure, propertyName)) { |
107 | ASSERT(*result == thisObject->hasOwnProperty(exec, propertyName)); |
108 | scope.assertNoException(); |
109 | return JSValue::encode(jsBoolean(*result)); |
110 | } |
111 | |
112 | PropertySlot slot(thisObject, PropertySlot::InternalMethodType::GetOwnProperty); |
113 | bool result = thisObject->hasOwnProperty(exec, propertyName, slot); |
114 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
115 | |
116 | hasOwnPropertyCache->tryAdd(vm, slot, thisObject, propertyName, result); |
117 | return JSValue::encode(jsBoolean(result)); |
118 | } |
119 | |
120 | EncodedJSValue JSC_HOST_CALL objectProtoFuncIsPrototypeOf(ExecState* exec) |
121 | { |
122 | VM& vm = exec->vm(); |
123 | auto scope = DECLARE_THROW_SCOPE(vm); |
124 | |
125 | JSValue thisValue = exec->thisValue().toThis(exec, StrictMode); |
126 | JSObject* thisObj = thisValue.toObject(exec); |
127 | EXCEPTION_ASSERT(!!scope.exception() == !thisObj); |
128 | if (UNLIKELY(!thisObj)) |
129 | return encodedJSValue(); |
130 | |
131 | if (!exec->argument(0).isObject()) |
132 | return JSValue::encode(jsBoolean(false)); |
133 | |
134 | JSValue v = asObject(exec->argument(0))->getPrototype(vm, exec); |
135 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
136 | |
137 | while (true) { |
138 | if (!v.isObject()) |
139 | return JSValue::encode(jsBoolean(false)); |
140 | if (v == thisObj) |
141 | return JSValue::encode(jsBoolean(true)); |
142 | v = asObject(v)->getPrototype(vm, exec); |
143 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
144 | } |
145 | } |
146 | |
147 | EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineGetter(ExecState* exec) |
148 | { |
149 | VM& vm = exec->vm(); |
150 | auto scope = DECLARE_THROW_SCOPE(vm); |
151 | |
152 | JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); |
153 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
154 | |
155 | JSValue get = exec->argument(1); |
156 | CallData callData; |
157 | if (getCallData(vm, get, callData) == CallType::None) |
158 | return throwVMTypeError(exec, scope, "invalid getter usage"_s ); |
159 | |
160 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
161 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
162 | |
163 | PropertyDescriptor descriptor; |
164 | descriptor.setGetter(get); |
165 | descriptor.setEnumerable(true); |
166 | descriptor.setConfigurable(true); |
167 | |
168 | bool shouldThrow = true; |
169 | scope.release(); |
170 | thisObject->methodTable(vm)->defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow); |
171 | |
172 | return JSValue::encode(jsUndefined()); |
173 | } |
174 | |
175 | EncodedJSValue JSC_HOST_CALL objectProtoFuncDefineSetter(ExecState* exec) |
176 | { |
177 | VM& vm = exec->vm(); |
178 | auto scope = DECLARE_THROW_SCOPE(vm); |
179 | |
180 | JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); |
181 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
182 | |
183 | JSValue set = exec->argument(1); |
184 | CallData callData; |
185 | if (getCallData(vm, set, callData) == CallType::None) |
186 | return throwVMTypeError(exec, scope, "invalid setter usage"_s ); |
187 | |
188 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
189 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
190 | |
191 | PropertyDescriptor descriptor; |
192 | descriptor.setSetter(set); |
193 | descriptor.setEnumerable(true); |
194 | descriptor.setConfigurable(true); |
195 | |
196 | bool shouldThrow = true; |
197 | scope.release(); |
198 | thisObject->methodTable(vm)->defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow); |
199 | |
200 | return JSValue::encode(jsUndefined()); |
201 | } |
202 | |
203 | EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupGetter(ExecState* exec) |
204 | { |
205 | VM& vm = exec->vm(); |
206 | auto scope = DECLARE_THROW_SCOPE(vm); |
207 | |
208 | JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); |
209 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
210 | |
211 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
212 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
213 | |
214 | PropertySlot slot(thisObject, PropertySlot::InternalMethodType::GetOwnProperty); |
215 | bool hasProperty = thisObject->getPropertySlot(exec, propertyName, slot); |
216 | EXCEPTION_ASSERT(!scope.exception() || !hasProperty); |
217 | if (hasProperty) { |
218 | if (slot.isAccessor()) { |
219 | GetterSetter* getterSetter = slot.getterSetter(); |
220 | return getterSetter->isGetterNull() ? JSValue::encode(jsUndefined()) : JSValue::encode(getterSetter->getter()); |
221 | } |
222 | if (slot.attributes() & PropertyAttribute::CustomAccessor) { |
223 | PropertyDescriptor descriptor; |
224 | ASSERT(slot.slotBase()); |
225 | if (slot.slotBase()->getOwnPropertyDescriptor(exec, propertyName, descriptor)) |
226 | return descriptor.getterPresent() ? JSValue::encode(descriptor.getter()) : JSValue::encode(jsUndefined()); |
227 | } |
228 | } |
229 | |
230 | return JSValue::encode(jsUndefined()); |
231 | } |
232 | |
233 | EncodedJSValue JSC_HOST_CALL objectProtoFuncLookupSetter(ExecState* exec) |
234 | { |
235 | VM& vm = exec->vm(); |
236 | auto scope = DECLARE_THROW_SCOPE(vm); |
237 | |
238 | JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); |
239 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
240 | |
241 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
242 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
243 | |
244 | PropertySlot slot(thisObject, PropertySlot::InternalMethodType::GetOwnProperty); |
245 | bool hasProperty = thisObject->getPropertySlot(exec, propertyName, slot); |
246 | EXCEPTION_ASSERT(!scope.exception() || !hasProperty); |
247 | if (hasProperty) { |
248 | if (slot.isAccessor()) { |
249 | GetterSetter* getterSetter = slot.getterSetter(); |
250 | return getterSetter->isSetterNull() ? JSValue::encode(jsUndefined()) : JSValue::encode(getterSetter->setter()); |
251 | } |
252 | if (slot.attributes() & PropertyAttribute::CustomAccessor) { |
253 | PropertyDescriptor descriptor; |
254 | ASSERT(slot.slotBase()); |
255 | if (slot.slotBase()->getOwnPropertyDescriptor(exec, propertyName, descriptor)) |
256 | return descriptor.setterPresent() ? JSValue::encode(descriptor.setter()) : JSValue::encode(jsUndefined()); |
257 | } |
258 | } |
259 | |
260 | return JSValue::encode(jsUndefined()); |
261 | } |
262 | |
263 | EncodedJSValue JSC_HOST_CALL objectProtoFuncPropertyIsEnumerable(ExecState* exec) |
264 | { |
265 | VM& vm = exec->vm(); |
266 | auto scope = DECLARE_THROW_SCOPE(vm); |
267 | |
268 | auto propertyName = exec->argument(0).toPropertyKey(exec); |
269 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
270 | |
271 | JSObject* thisObject = exec->thisValue().toThis(exec, StrictMode).toObject(exec); |
272 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
273 | |
274 | scope.release(); |
275 | PropertyDescriptor descriptor; |
276 | bool enumerable = thisObject->getOwnPropertyDescriptor(exec, propertyName, descriptor) && descriptor.enumerable(); |
277 | return JSValue::encode(jsBoolean(enumerable)); |
278 | } |
279 | |
280 | // 15.2.4.3 Object.prototype.toLocaleString() |
281 | EncodedJSValue JSC_HOST_CALL objectProtoFuncToLocaleString(ExecState* exec) |
282 | { |
283 | VM& vm = exec->vm(); |
284 | auto scope = DECLARE_THROW_SCOPE(vm); |
285 | |
286 | // 1. Let V be the this value. |
287 | JSValue thisValue = exec->thisValue(); |
288 | |
289 | // 2. Invoke(V, "toString") |
290 | |
291 | // Let O be the result of calling ToObject passing the this value as the argument. |
292 | JSObject* object = thisValue.toThis(exec, StrictMode).toObject(exec); |
293 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
294 | |
295 | // Let toString be the O.[[Get]]("toString", V) |
296 | PropertySlot slot(thisValue, PropertySlot::InternalMethodType::Get); |
297 | bool hasProperty = object->getPropertySlot(exec, vm.propertyNames->toString, slot); |
298 | EXCEPTION_ASSERT(!scope.exception() || !hasProperty); |
299 | JSValue toString = hasProperty ? slot.getValue(exec, vm.propertyNames->toString) : jsUndefined(); |
300 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
301 | |
302 | // If IsCallable(toString) is false, throw a TypeError exception. |
303 | CallData callData; |
304 | CallType callType = getCallData(vm, toString, callData); |
305 | if (callType == CallType::None) |
306 | return throwVMTypeError(exec, scope); |
307 | |
308 | // Return the result of calling the [[Call]] internal method of toString passing the this value and no arguments. |
309 | RELEASE_AND_RETURN(scope, JSValue::encode(call(exec, toString, callType, callData, thisValue, *vm.emptyList))); |
310 | } |
311 | |
312 | EncodedJSValue JSC_HOST_CALL objectProtoFuncToString(ExecState* exec) |
313 | { |
314 | VM& vm = exec->vm(); |
315 | auto scope = DECLARE_THROW_SCOPE(vm); |
316 | |
317 | JSValue thisValue = exec->thisValue().toThis(exec, StrictMode); |
318 | if (thisValue.isUndefinedOrNull()) |
319 | return JSValue::encode(thisValue.isUndefined() ? vm.smallStrings.undefinedObjectString() : vm.smallStrings.nullObjectString()); |
320 | JSObject* thisObject = thisValue.toObject(exec); |
321 | EXCEPTION_ASSERT(!!scope.exception() == !thisObject); |
322 | if (!thisObject) |
323 | return JSValue::encode(jsUndefined()); |
324 | |
325 | auto result = thisObject->structure(vm)->objectToStringValue(); |
326 | if (result) |
327 | return JSValue::encode(result); |
328 | |
329 | PropertyName toStringTagSymbol = vm.propertyNames->toStringTagSymbol; |
330 | RELEASE_AND_RETURN(scope, JSValue::encode(thisObject->getPropertySlot(exec, toStringTagSymbol, [&] (bool found, PropertySlot& toStringTagSlot) -> JSValue { |
331 | if (found) { |
332 | JSValue stringTag = toStringTagSlot.getValue(exec, toStringTagSymbol); |
333 | RETURN_IF_EXCEPTION(scope, { }); |
334 | if (stringTag.isString()) { |
335 | JSString* result = jsString(exec, vm.smallStrings.objectStringStart(), asString(stringTag), vm.smallStrings.singleCharacterString(']')); |
336 | RETURN_IF_EXCEPTION(scope, { }); |
337 | thisObject->structure(vm)->setObjectToStringValue(exec, vm, result, toStringTagSlot); |
338 | return result; |
339 | } |
340 | } |
341 | |
342 | String tag = thisObject->methodTable(vm)->toStringName(thisObject, exec); |
343 | RETURN_IF_EXCEPTION(scope, { }); |
344 | String newString = tryMakeString("[object " , WTFMove(tag), "]" ); |
345 | if (!newString) |
346 | return throwOutOfMemoryError(exec, scope); |
347 | |
348 | auto result = jsNontrivialString(&vm, newString); |
349 | thisObject->structure(vm)->setObjectToStringValue(exec, vm, result, toStringTagSlot); |
350 | return result; |
351 | }))); |
352 | } |
353 | |
354 | } // namespace JSC |
355 | |