1/*
2 * Copyright (C) 2006, 2008, 2016 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "APICast.h"
28#include "Error.h"
29#include "ExceptionHelpers.h"
30#include "JSCallbackFunction.h"
31#include "JSClassRef.h"
32#include "JSFunction.h"
33#include "JSGlobalObject.h"
34#include "JSLock.h"
35#include "JSObjectRef.h"
36#include "JSString.h"
37#include "OpaqueJSString.h"
38#include "PropertyNameArray.h"
39#include <wtf/Vector.h>
40
41namespace JSC {
42
43template <class Parent>
44inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(JSValue value)
45{
46 ASSERT(asObject(value)->inherits(*value.getObject()->vm(), info()));
47 return jsCast<JSCallbackObject*>(asObject(value));
48}
49
50template <class Parent>
51inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(EncodedJSValue encodedValue)
52{
53 JSValue value = JSValue::decode(encodedValue);
54 ASSERT(asObject(value)->inherits(*value.getObject()->vm(), info()));
55 return jsCast<JSCallbackObject*>(asObject(value));
56}
57
58template <class Parent>
59JSCallbackObject<Parent>::JSCallbackObject(ExecState* exec, Structure* structure, JSClassRef jsClass, void* data)
60 : Parent(exec->vm(), structure)
61 , m_callbackObjectData(std::make_unique<JSCallbackObjectData>(data, jsClass))
62{
63}
64
65// Global object constructor.
66// FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
67template <class Parent>
68JSCallbackObject<Parent>::JSCallbackObject(VM& vm, JSClassRef jsClass, Structure* structure)
69 : Parent(vm, structure)
70 , m_callbackObjectData(std::make_unique<JSCallbackObjectData>(nullptr, jsClass))
71{
72}
73
74template <class Parent>
75JSCallbackObject<Parent>::~JSCallbackObject()
76{
77 VM* vm = this->HeapCell::vm();
78 vm->currentlyDestructingCallbackObject = this;
79 ASSERT(m_classInfo);
80 vm->currentlyDestructingCallbackObjectClassInfo = m_classInfo;
81 JSObjectRef thisRef = toRef(static_cast<JSObject*>(this));
82 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
83 if (JSObjectFinalizeCallback finalize = jsClass->finalize)
84 finalize(thisRef);
85 }
86 vm->currentlyDestructingCallbackObject = nullptr;
87 vm->currentlyDestructingCallbackObjectClassInfo = nullptr;
88}
89
90template <class Parent>
91void JSCallbackObject<Parent>::finishCreation(ExecState* exec)
92{
93 VM& vm = exec->vm();
94 Base::finishCreation(vm);
95 ASSERT(Parent::inherits(vm, info()));
96 init(exec);
97}
98
99// This is just for Global object, so we can assume that Base::finishCreation is JSGlobalObject::finishCreation.
100template <class Parent>
101void JSCallbackObject<Parent>::finishCreation(VM& vm)
102{
103 ASSERT(Parent::inherits(vm, info()));
104 ASSERT(Parent::isGlobalObject());
105 Base::finishCreation(vm);
106 init(jsCast<JSGlobalObject*>(this)->globalExec());
107}
108
109template <class Parent>
110void JSCallbackObject<Parent>::init(ExecState* exec)
111{
112 ASSERT(exec);
113
114 Vector<JSObjectInitializeCallback, 16> initRoutines;
115 JSClassRef jsClass = classRef();
116 do {
117 if (JSObjectInitializeCallback initialize = jsClass->initialize)
118 initRoutines.append(initialize);
119 } while ((jsClass = jsClass->parentClass));
120
121 // initialize from base to derived
122 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
123 JSLock::DropAllLocks dropAllLocks(exec);
124 JSObjectInitializeCallback initialize = initRoutines[i];
125 initialize(toRef(exec), toRef(this));
126 }
127
128 m_classInfo = this->classInfo();
129}
130
131template <class Parent>
132String JSCallbackObject<Parent>::className(const JSObject* object, VM& vm)
133{
134 const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object);
135 String thisClassName = thisObject->classRef()->className();
136 if (!thisClassName.isEmpty())
137 return thisClassName;
138
139 return Parent::className(object, vm);
140}
141
142template <class Parent>
143String JSCallbackObject<Parent>::toStringName(const JSObject* object, ExecState* exec)
144{
145 VM& vm = exec->vm();
146 const ClassInfo* info = object->classInfo(vm);
147 ASSERT(info);
148 return info->methodTable.className(object, vm);
149}
150
151template <class Parent>
152bool JSCallbackObject<Parent>::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
153{
154 VM& vm = exec->vm();
155 auto scope = DECLARE_THROW_SCOPE(vm);
156
157 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
158 JSContextRef ctx = toRef(exec);
159 JSObjectRef thisRef = toRef(thisObject);
160 RefPtr<OpaqueJSString> propertyNameRef;
161
162 if (StringImpl* name = propertyName.uid()) {
163 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
164 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
165 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
166 if (!propertyNameRef)
167 propertyNameRef = OpaqueJSString::tryCreate(name);
168 JSLock::DropAllLocks dropAllLocks(exec);
169 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
170 slot.setCustom(thisObject, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum, callbackGetter);
171 return true;
172 }
173 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
174 if (!propertyNameRef)
175 propertyNameRef = OpaqueJSString::tryCreate(name);
176 JSValueRef exception = 0;
177 JSValueRef value;
178 {
179 JSLock::DropAllLocks dropAllLocks(exec);
180 value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception);
181 }
182 if (exception) {
183 throwException(exec, scope, toJS(exec, exception));
184 slot.setValue(thisObject, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum, jsUndefined());
185 return true;
186 }
187 if (value) {
188 slot.setValue(thisObject, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum, toJS(exec, value));
189 return true;
190 }
191 }
192
193 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
194 if (staticValues->contains(name)) {
195 JSValue value = thisObject->getStaticValue(exec, propertyName);
196 if (value) {
197 slot.setValue(thisObject, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum, value);
198 return true;
199 }
200 }
201 }
202
203 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
204 if (staticFunctions->contains(name)) {
205 slot.setCustom(thisObject, PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum, staticFunctionGetter);
206 return true;
207 }
208 }
209 }
210 }
211
212 return Parent::getOwnPropertySlot(thisObject, exec, propertyName, slot);
213}
214
215template <class Parent>
216bool JSCallbackObject<Parent>::getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot)
217{
218 return object->methodTable(exec->vm())->getOwnPropertySlot(object, exec, Identifier::from(exec, propertyName), slot);
219}
220
221template <class Parent>
222JSValue JSCallbackObject<Parent>::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint)
223{
224 VM& vm = exec->vm();
225 auto scope = DECLARE_THROW_SCOPE(vm);
226
227 const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object);
228 JSContextRef ctx = toRef(exec);
229 JSObjectRef thisRef = toRef(thisObject);
230 ::JSType jsHint = hint == PreferString ? kJSTypeString : kJSTypeNumber;
231
232 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
233 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
234 JSValueRef exception = 0;
235 JSValueRef result = convertToType(ctx, thisRef, jsHint, &exception);
236 if (exception) {
237 throwException(exec, scope, toJS(exec, exception));
238 return jsUndefined();
239 }
240 if (result)
241 return toJS(exec, result);
242 }
243 }
244
245 return Parent::defaultValue(object, exec, hint);
246}
247
248template <class Parent>
249bool JSCallbackObject<Parent>::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
250{
251 VM& vm = exec->vm();
252 auto scope = DECLARE_THROW_SCOPE(vm);
253
254 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
255 JSContextRef ctx = toRef(exec);
256 JSObjectRef thisRef = toRef(thisObject);
257 RefPtr<OpaqueJSString> propertyNameRef;
258 JSValueRef valueRef = toRef(exec, value);
259
260 if (StringImpl* name = propertyName.uid()) {
261 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
262 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
263 if (!propertyNameRef)
264 propertyNameRef = OpaqueJSString::tryCreate(name);
265 JSValueRef exception = 0;
266 bool result;
267 {
268 JSLock::DropAllLocks dropAllLocks(exec);
269 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
270 }
271 if (exception)
272 throwException(exec, scope, toJS(exec, exception));
273 if (result || exception)
274 return result;
275 }
276
277 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
278 if (StaticValueEntry* entry = staticValues->get(name)) {
279 if (entry->attributes & kJSPropertyAttributeReadOnly)
280 return false;
281 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
282 JSValueRef exception = 0;
283 bool result;
284 {
285 JSLock::DropAllLocks dropAllLocks(exec);
286 result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception);
287 }
288 if (exception)
289 throwException(exec, scope, toJS(exec, exception));
290 if (result || exception)
291 return result;
292 }
293 }
294 }
295
296 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
297 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
298 PropertySlot getSlot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
299 if (Parent::getOwnPropertySlot(thisObject, exec, propertyName, getSlot))
300 return Parent::put(thisObject, exec, propertyName, value, slot);
301 if (entry->attributes & kJSPropertyAttributeReadOnly)
302 return false;
303 return thisObject->JSCallbackObject<Parent>::putDirect(vm, propertyName, value); // put as override property
304 }
305 }
306 }
307 }
308
309 return Parent::put(thisObject, exec, propertyName, value, slot);
310}
311
312template <class Parent>
313bool JSCallbackObject<Parent>::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyIndex, JSValue value, bool shouldThrow)
314{
315 VM& vm = exec->vm();
316 auto scope = DECLARE_THROW_SCOPE(vm);
317
318 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
319 JSContextRef ctx = toRef(exec);
320 JSObjectRef thisRef = toRef(thisObject);
321 RefPtr<OpaqueJSString> propertyNameRef;
322 JSValueRef valueRef = toRef(exec, value);
323 Identifier propertyName = Identifier::from(exec, propertyIndex);
324
325 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
326 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
327 if (!propertyNameRef)
328 propertyNameRef = OpaqueJSString::tryCreate(propertyName.impl());
329 JSValueRef exception = 0;
330 bool result;
331 {
332 JSLock::DropAllLocks dropAllLocks(exec);
333 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
334 }
335 if (exception)
336 throwException(exec, scope, toJS(exec, exception));
337 if (result || exception)
338 return result;
339 }
340
341 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
342 if (StaticValueEntry* entry = staticValues->get(propertyName.impl())) {
343 if (entry->attributes & kJSPropertyAttributeReadOnly)
344 return false;
345 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
346 JSValueRef exception = 0;
347 bool result;
348 {
349 JSLock::DropAllLocks dropAllLocks(exec);
350 result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception);
351 }
352 if (exception)
353 throwException(exec, scope, toJS(exec, exception));
354 if (result || exception)
355 return result;
356 }
357 }
358 }
359
360 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
361 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) {
362 if (entry->attributes & kJSPropertyAttributeReadOnly)
363 return false;
364 break;
365 }
366 }
367 }
368
369 return Parent::putByIndex(thisObject, exec, propertyIndex, value, shouldThrow);
370}
371
372template <class Parent>
373bool JSCallbackObject<Parent>::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
374{
375 VM& vm = exec->vm();
376 auto scope = DECLARE_THROW_SCOPE(vm);
377
378 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
379 JSContextRef ctx = toRef(exec);
380 JSObjectRef thisRef = toRef(thisObject);
381 RefPtr<OpaqueJSString> propertyNameRef;
382
383 if (StringImpl* name = propertyName.uid()) {
384 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
385 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
386 if (!propertyNameRef)
387 propertyNameRef = OpaqueJSString::tryCreate(name);
388 JSValueRef exception = 0;
389 bool result;
390 {
391 JSLock::DropAllLocks dropAllLocks(exec);
392 result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception);
393 }
394 if (exception)
395 throwException(exec, scope, toJS(exec, exception));
396 if (result || exception)
397 return true;
398 }
399
400 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
401 if (StaticValueEntry* entry = staticValues->get(name)) {
402 if (entry->attributes & kJSPropertyAttributeDontDelete)
403 return false;
404 return true;
405 }
406 }
407
408 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
409 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
410 if (entry->attributes & kJSPropertyAttributeDontDelete)
411 return false;
412 return true;
413 }
414 }
415 }
416 }
417
418 return Parent::deleteProperty(thisObject, exec, propertyName);
419}
420
421template <class Parent>
422bool JSCallbackObject<Parent>::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName)
423{
424 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
425 return thisObject->methodTable(exec->vm())->deleteProperty(thisObject, exec, Identifier::from(exec, propertyName));
426}
427
428template <class Parent>
429ConstructType JSCallbackObject<Parent>::getConstructData(JSCell* cell, ConstructData& constructData)
430{
431 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
432 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
433 if (jsClass->callAsConstructor) {
434 constructData.native.function = construct;
435 return ConstructType::Host;
436 }
437 }
438 return ConstructType::None;
439}
440
441template <class Parent>
442EncodedJSValue JSCallbackObject<Parent>::construct(ExecState* exec)
443{
444 VM& vm = exec->vm();
445 auto scope = DECLARE_THROW_SCOPE(vm);
446
447 JSObject* constructor = exec->jsCallee();
448 JSContextRef execRef = toRef(exec);
449 JSObjectRef constructorRef = toRef(constructor);
450
451 for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
452 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
453 size_t argumentCount = exec->argumentCount();
454 Vector<JSValueRef, 16> arguments;
455 arguments.reserveInitialCapacity(argumentCount);
456 for (size_t i = 0; i < argumentCount; ++i)
457 arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
458 JSValueRef exception = 0;
459 JSObject* result;
460 {
461 JSLock::DropAllLocks dropAllLocks(exec);
462 result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception));
463 }
464 if (exception)
465 throwException(exec, scope, toJS(exec, exception));
466 return JSValue::encode(result);
467 }
468 }
469
470 RELEASE_ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
471 return JSValue::encode(JSValue());
472}
473
474template <class Parent>
475bool JSCallbackObject<Parent>::customHasInstance(JSObject* object, ExecState* exec, JSValue value)
476{
477 VM& vm = exec->vm();
478 auto scope = DECLARE_THROW_SCOPE(vm);
479
480 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
481 JSContextRef execRef = toRef(exec);
482 JSObjectRef thisRef = toRef(thisObject);
483
484 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
485 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
486 JSValueRef valueRef = toRef(exec, value);
487 JSValueRef exception = 0;
488 bool result;
489 {
490 JSLock::DropAllLocks dropAllLocks(exec);
491 result = hasInstance(execRef, thisRef, valueRef, &exception);
492 }
493 if (exception)
494 throwException(exec, scope, toJS(exec, exception));
495 return result;
496 }
497 }
498 return false;
499}
500
501template <class Parent>
502CallType JSCallbackObject<Parent>::getCallData(JSCell* cell, CallData& callData)
503{
504 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
505 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
506 if (jsClass->callAsFunction) {
507 callData.native.function = call;
508 return CallType::Host;
509 }
510 }
511 return CallType::None;
512}
513
514template <class Parent>
515EncodedJSValue JSCallbackObject<Parent>::call(ExecState* exec)
516{
517 VM& vm = exec->vm();
518 auto scope = DECLARE_THROW_SCOPE(vm);
519
520 JSContextRef execRef = toRef(exec);
521 JSObjectRef functionRef = toRef(exec->jsCallee());
522 JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)));
523
524 for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(toJS(functionRef))->classRef(); jsClass; jsClass = jsClass->parentClass) {
525 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
526 size_t argumentCount = exec->argumentCount();
527 Vector<JSValueRef, 16> arguments;
528 arguments.reserveInitialCapacity(argumentCount);
529 for (size_t i = 0; i < argumentCount; ++i)
530 arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
531 JSValueRef exception = 0;
532 JSValue result;
533 {
534 JSLock::DropAllLocks dropAllLocks(exec);
535 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
536 }
537 if (exception)
538 throwException(exec, scope, toJS(exec, exception));
539 return JSValue::encode(result);
540 }
541 }
542
543 RELEASE_ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
544 return JSValue::encode(JSValue());
545}
546
547template <class Parent>
548void JSCallbackObject<Parent>::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
549{
550 JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
551 JSContextRef execRef = toRef(exec);
552 JSObjectRef thisRef = toRef(thisObject);
553
554 for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
555 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
556 JSLock::DropAllLocks dropAllLocks(exec);
557 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
558 }
559
560 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
561 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
562 iterator end = staticValues->end();
563 for (iterator it = staticValues->begin(); it != end; ++it) {
564 StringImpl* name = it->key.get();
565 StaticValueEntry* entry = it->value.get();
566 if (entry->getProperty && (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties())) {
567 ASSERT(!name->isSymbol());
568 propertyNames.add(Identifier::fromString(exec, String(name)));
569 }
570 }
571 }
572
573 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
574 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
575 iterator end = staticFunctions->end();
576 for (iterator it = staticFunctions->begin(); it != end; ++it) {
577 StringImpl* name = it->key.get();
578 StaticFunctionEntry* entry = it->value.get();
579 if (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties()) {
580 ASSERT(!name->isSymbol());
581 propertyNames.add(Identifier::fromString(exec, String(name)));
582 }
583 }
584 }
585 }
586
587 Parent::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
588}
589
590template <class Parent>
591void JSCallbackObject<Parent>::setPrivate(void* data)
592{
593 m_callbackObjectData->privateData = data;
594}
595
596template <class Parent>
597void* JSCallbackObject<Parent>::getPrivate()
598{
599 return m_callbackObjectData->privateData;
600}
601
602template <class Parent>
603bool JSCallbackObject<Parent>::inherits(JSClassRef c) const
604{
605 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
606 if (jsClass == c)
607 return true;
608 }
609 return false;
610}
611
612template <class Parent>
613JSValue JSCallbackObject<Parent>::getStaticValue(ExecState* exec, PropertyName propertyName)
614{
615 VM& vm = exec->vm();
616 auto scope = DECLARE_THROW_SCOPE(vm);
617
618 JSObjectRef thisRef = toRef(this);
619
620 if (StringImpl* name = propertyName.uid()) {
621 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
622 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
623 if (StaticValueEntry* entry = staticValues->get(name)) {
624 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
625 JSValueRef exception = 0;
626 JSValueRef value;
627 {
628 JSLock::DropAllLocks dropAllLocks(exec);
629 value = getProperty(toRef(exec), thisRef, entry->propertyNameRef.get(), &exception);
630 }
631 if (exception) {
632 throwException(exec, scope, toJS(exec, exception));
633 return jsUndefined();
634 }
635 if (value)
636 return toJS(exec, value);
637 }
638 }
639 }
640 }
641 }
642
643 return JSValue();
644}
645
646template <class Parent>
647EncodedJSValue JSCallbackObject<Parent>::staticFunctionGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
648{
649 VM& vm = exec->vm();
650 auto scope = DECLARE_THROW_SCOPE(vm);
651
652 JSCallbackObject* thisObj = asCallbackObject(thisValue);
653
654 // Check for cached or override property.
655 PropertySlot slot2(thisObj, PropertySlot::InternalMethodType::VMInquiry);
656 if (Parent::getOwnPropertySlot(thisObj, exec, propertyName, slot2))
657 return JSValue::encode(slot2.getValue(exec, propertyName));
658
659 if (StringImpl* name = propertyName.uid()) {
660 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
661 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
662 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
663 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
664 JSObject* o = JSCallbackFunction::create(vm, thisObj->globalObject(vm), callAsFunction, name);
665 thisObj->putDirect(vm, propertyName, o, entry->attributes);
666 return JSValue::encode(o);
667 }
668 }
669 }
670 }
671 }
672
673 return JSValue::encode(throwException(exec, scope, createReferenceError(exec, "Static function property defined with NULL callAsFunction callback."_s)));
674}
675
676template <class Parent>
677EncodedJSValue JSCallbackObject<Parent>::callbackGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
678{
679 VM& vm = exec->vm();
680 auto scope = DECLARE_THROW_SCOPE(vm);
681
682 JSCallbackObject* thisObj = asCallbackObject(thisValue);
683
684 JSObjectRef thisRef = toRef(thisObj);
685 RefPtr<OpaqueJSString> propertyNameRef;
686
687 if (StringImpl* name = propertyName.uid()) {
688 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
689 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
690 if (!propertyNameRef)
691 propertyNameRef = OpaqueJSString::tryCreate(name);
692 JSValueRef exception = 0;
693 JSValueRef value;
694 {
695 JSLock::DropAllLocks dropAllLocks(exec);
696 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
697 }
698 if (exception) {
699 throwException(exec, scope, toJS(exec, exception));
700 return JSValue::encode(jsUndefined());
701 }
702 if (value)
703 return JSValue::encode(toJS(exec, value));
704 }
705 }
706 }
707
708 return JSValue::encode(throwException(exec, scope, createReferenceError(exec, "hasProperty callback returned true for a property that doesn't exist."_s)));
709}
710
711} // namespace JSC
712