a5537234bb32ca686dd06b2afe9a07a9d0257f34
[WebKit-https.git] / Source / JavaScriptCore / API / JSCallbackObjectFunctions.h
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
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 "JSStringRef.h"
38 #include "OpaqueJSString.h"
39 #include "PropertyNameArray.h"
40 #include <wtf/Vector.h>
41
42 namespace JSC {
43
44 template <class Parent>
45 inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(JSValue value)
46 {
47     ASSERT(asObject(value)->inherits(info()));
48     return jsCast<JSCallbackObject*>(asObject(value));
49 }
50
51 template <class Parent>
52 inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(EncodedJSValue value)
53 {
54     ASSERT(asObject(JSValue::decode(value))->inherits(info()));
55     return jsCast<JSCallbackObject*>(asObject(JSValue::decode(value)));
56 }
57
58 template <class Parent>
59 JSCallbackObject<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.
67 template <class Parent>
68 JSCallbackObject<Parent>::JSCallbackObject(VM& vm, JSClassRef jsClass, Structure* structure)
69     : Parent(vm, structure)
70     , m_callbackObjectData(std::make_unique<JSCallbackObjectData>(nullptr, jsClass))
71 {
72 }
73
74 template <class Parent>
75 JSCallbackObject<Parent>::~JSCallbackObject()
76 {
77     JSObjectRef thisRef = toRef(static_cast<JSObject*>(this));
78     for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
79         if (JSObjectFinalizeCallback finalize = jsClass->finalize)
80             finalize(thisRef);
81     }
82 }
83     
84 template <class Parent>
85 void JSCallbackObject<Parent>::finishCreation(ExecState* exec)
86 {
87     Base::finishCreation(exec->vm());
88     ASSERT(Parent::inherits(info()));
89     init(exec);
90 }
91
92 // This is just for Global object, so we can assume that Base::finishCreation is JSGlobalObject::finishCreation.
93 template <class Parent>
94 void JSCallbackObject<Parent>::finishCreation(VM& vm)
95 {
96     ASSERT(Parent::inherits(info()));
97     ASSERT(Parent::isGlobalObject());
98     Base::finishCreation(vm);
99     init(jsCast<JSGlobalObject*>(this)->globalExec());
100 }
101
102 template <class Parent>
103 void JSCallbackObject<Parent>::init(ExecState* exec)
104 {
105     ASSERT(exec);
106     
107     Vector<JSObjectInitializeCallback, 16> initRoutines;
108     JSClassRef jsClass = classRef();
109     do {
110         if (JSObjectInitializeCallback initialize = jsClass->initialize)
111             initRoutines.append(initialize);
112     } while ((jsClass = jsClass->parentClass));
113     
114     // initialize from base to derived
115     for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
116         JSLock::DropAllLocks dropAllLocks(exec);
117         JSObjectInitializeCallback initialize = initRoutines[i];
118         initialize(toRef(exec), toRef(this));
119     }
120 }
121
122 template <class Parent>
123 String JSCallbackObject<Parent>::className(const JSObject* object)
124 {
125     const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object);
126     String thisClassName = thisObject->classRef()->className();
127     if (!thisClassName.isEmpty())
128         return thisClassName;
129     
130     return Parent::className(object);
131 }
132
133 template <class Parent>
134 bool JSCallbackObject<Parent>::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
135 {
136     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
137     JSContextRef ctx = toRef(exec);
138     JSObjectRef thisRef = toRef(thisObject);
139     RefPtr<OpaqueJSString> propertyNameRef;
140     
141     if (StringImpl* name = propertyName.uid()) {
142         for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
143             // optional optimization to bypass getProperty in cases when we only need to know if the property exists
144             if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
145                 if (!propertyNameRef)
146                     propertyNameRef = OpaqueJSString::create(name);
147                 JSLock::DropAllLocks dropAllLocks(exec);
148                 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
149                     slot.setCustom(thisObject, ReadOnly | DontEnum, callbackGetter);
150                     return true;
151                 }
152             } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
153                 if (!propertyNameRef)
154                     propertyNameRef = OpaqueJSString::create(name);
155                 JSValueRef exception = 0;
156                 JSValueRef value;
157                 {
158                     JSLock::DropAllLocks dropAllLocks(exec);
159                     value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception);
160                 }
161                 if (exception) {
162                     exec->vm().throwException(exec, toJS(exec, exception));
163                     slot.setValue(thisObject, ReadOnly | DontEnum, jsUndefined());
164                     return true;
165                 }
166                 if (value) {
167                     slot.setValue(thisObject, ReadOnly | DontEnum, toJS(exec, value));
168                     return true;
169                 }
170             }
171             
172             if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
173                 if (staticValues->contains(name)) {
174                     JSValue value = thisObject->getStaticValue(exec, propertyName);
175                     if (value) {
176                         slot.setValue(thisObject, ReadOnly | DontEnum, value);
177                         return true;
178                     }
179                 }
180             }
181             
182             if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
183                 if (staticFunctions->contains(name)) {
184                     slot.setCustom(thisObject, ReadOnly | DontEnum, staticFunctionGetter);
185                     return true;
186                 }
187             }
188         }
189     }
190
191     return Parent::getOwnPropertySlot(thisObject, exec, propertyName, slot);
192 }
193
194 template <class Parent>
195 bool JSCallbackObject<Parent>::getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned propertyName, PropertySlot& slot)
196 {
197     return object->methodTable()->getOwnPropertySlot(object, exec, Identifier::from(exec, propertyName), slot);
198 }
199
200 template <class Parent>
201 JSValue JSCallbackObject<Parent>::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint)
202 {
203     const JSCallbackObject* thisObject = jsCast<const JSCallbackObject*>(object);
204     JSContextRef ctx = toRef(exec);
205     JSObjectRef thisRef = toRef(thisObject);
206     ::JSType jsHint = hint == PreferString ? kJSTypeString : kJSTypeNumber;
207
208     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
209         if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
210             JSValueRef exception = 0;
211             JSValueRef result = convertToType(ctx, thisRef, jsHint, &exception);
212             if (exception) {
213                 exec->vm().throwException(exec, toJS(exec, exception));
214                 return jsUndefined();
215             }
216             if (result)
217                 return toJS(exec, result);
218         }
219     }
220     
221     return Parent::defaultValue(object, exec, hint);
222 }
223
224 template <class Parent>
225 bool JSCallbackObject<Parent>::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
226 {
227     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
228     JSContextRef ctx = toRef(exec);
229     JSObjectRef thisRef = toRef(thisObject);
230     RefPtr<OpaqueJSString> propertyNameRef;
231     JSValueRef valueRef = toRef(exec, value);
232     
233     if (StringImpl* name = propertyName.uid()) {
234         for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
235             if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
236                 if (!propertyNameRef)
237                     propertyNameRef = OpaqueJSString::create(name);
238                 JSValueRef exception = 0;
239                 bool result;
240                 {
241                     JSLock::DropAllLocks dropAllLocks(exec);
242                     result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
243                 }
244                 if (exception)
245                     exec->vm().throwException(exec, toJS(exec, exception));
246                 if (result || exception)
247                     return result;
248             }
249             
250             if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
251                 if (StaticValueEntry* entry = staticValues->get(name)) {
252                     if (entry->attributes & kJSPropertyAttributeReadOnly)
253                         return false;
254                     if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
255                         JSValueRef exception = 0;
256                         bool result;
257                         {
258                             JSLock::DropAllLocks dropAllLocks(exec);
259                             result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception);
260                         }
261                         if (exception)
262                             exec->vm().throwException(exec, toJS(exec, exception));
263                         if (result || exception)
264                             return result;
265                     }
266                 }
267             }
268             
269             if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
270                 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
271                     PropertySlot getSlot(thisObject, PropertySlot::InternalMethodType::VMInquiry);
272                     if (Parent::getOwnPropertySlot(thisObject, exec, propertyName, getSlot))
273                         return Parent::put(thisObject, exec, propertyName, value, slot);
274                     if (entry->attributes & kJSPropertyAttributeReadOnly)
275                         return false;
276                     return thisObject->JSCallbackObject<Parent>::putDirect(exec->vm(), propertyName, value); // put as override property
277                 }
278             }
279         }
280     }
281
282     return Parent::put(thisObject, exec, propertyName, value, slot);
283 }
284
285 template <class Parent>
286 bool JSCallbackObject<Parent>::putByIndex(JSCell* cell, ExecState* exec, unsigned propertyIndex, JSValue value, bool shouldThrow)
287 {
288     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
289     JSContextRef ctx = toRef(exec);
290     JSObjectRef thisRef = toRef(thisObject);
291     RefPtr<OpaqueJSString> propertyNameRef;
292     JSValueRef valueRef = toRef(exec, value);
293     Identifier propertyName = Identifier::from(exec, propertyIndex);
294
295     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
296         if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
297             if (!propertyNameRef)
298                 propertyNameRef = OpaqueJSString::create(propertyName.impl());
299             JSValueRef exception = 0;
300             bool result;
301             {
302                 JSLock::DropAllLocks dropAllLocks(exec);
303                 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
304             }
305             if (exception)
306                 exec->vm().throwException(exec, toJS(exec, exception));
307             if (result || exception)
308                 return result;
309         }
310
311         if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
312             if (StaticValueEntry* entry = staticValues->get(propertyName.impl())) {
313                 if (entry->attributes & kJSPropertyAttributeReadOnly)
314                     return false;
315                 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
316                     JSValueRef exception = 0;
317                     bool result;
318                     {
319                         JSLock::DropAllLocks dropAllLocks(exec);
320                         result = setProperty(ctx, thisRef, entry->propertyNameRef.get(), valueRef, &exception);
321                     }
322                     if (exception)
323                         exec->vm().throwException(exec, toJS(exec, exception));
324                     if (result || exception)
325                         return result;
326                 }
327             }
328         }
329
330         if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
331             if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) {
332                 if (entry->attributes & kJSPropertyAttributeReadOnly)
333                     return false;
334                 break;
335             }
336         }
337     }
338
339     return Parent::putByIndex(thisObject, exec, propertyIndex, value, shouldThrow);
340 }
341
342 template <class Parent>
343 bool JSCallbackObject<Parent>::deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
344 {
345     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
346     JSContextRef ctx = toRef(exec);
347     JSObjectRef thisRef = toRef(thisObject);
348     RefPtr<OpaqueJSString> propertyNameRef;
349     
350     if (StringImpl* name = propertyName.uid()) {
351         for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
352             if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
353                 if (!propertyNameRef)
354                     propertyNameRef = OpaqueJSString::create(name);
355                 JSValueRef exception = 0;
356                 bool result;
357                 {
358                     JSLock::DropAllLocks dropAllLocks(exec);
359                     result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception);
360                 }
361                 if (exception)
362                     exec->vm().throwException(exec, toJS(exec, exception));
363                 if (result || exception)
364                     return true;
365             }
366             
367             if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
368                 if (StaticValueEntry* entry = staticValues->get(name)) {
369                     if (entry->attributes & kJSPropertyAttributeDontDelete)
370                         return false;
371                     return true;
372                 }
373             }
374             
375             if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
376                 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
377                     if (entry->attributes & kJSPropertyAttributeDontDelete)
378                         return false;
379                     return true;
380                 }
381             }
382         }
383     }
384
385     return Parent::deleteProperty(thisObject, exec, propertyName);
386 }
387
388 template <class Parent>
389 bool JSCallbackObject<Parent>::deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName)
390 {
391     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
392     return thisObject->methodTable()->deleteProperty(thisObject, exec, Identifier::from(exec, propertyName));
393 }
394
395 template <class Parent>
396 ConstructType JSCallbackObject<Parent>::getConstructData(JSCell* cell, ConstructData& constructData)
397 {
398     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
399     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
400         if (jsClass->callAsConstructor) {
401             constructData.native.function = construct;
402             return ConstructType::Host;
403         }
404     }
405     return ConstructType::None;
406 }
407
408 template <class Parent>
409 EncodedJSValue JSCallbackObject<Parent>::construct(ExecState* exec)
410 {
411     JSObject* constructor = exec->callee();
412     JSContextRef execRef = toRef(exec);
413     JSObjectRef constructorRef = toRef(constructor);
414     
415     for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
416         if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
417             size_t argumentCount = exec->argumentCount();
418             Vector<JSValueRef, 16> arguments;
419             arguments.reserveInitialCapacity(argumentCount);
420             for (size_t i = 0; i < argumentCount; ++i)
421                 arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
422             JSValueRef exception = 0;
423             JSObject* result;
424             {
425                 JSLock::DropAllLocks dropAllLocks(exec);
426                 result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception));
427             }
428             if (exception)
429                 exec->vm().throwException(exec, toJS(exec, exception));
430             return JSValue::encode(result);
431         }
432     }
433     
434     RELEASE_ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
435     return JSValue::encode(JSValue());
436 }
437
438 template <class Parent>
439 bool JSCallbackObject<Parent>::customHasInstance(JSObject* object, ExecState* exec, JSValue value)
440 {
441     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
442     JSContextRef execRef = toRef(exec);
443     JSObjectRef thisRef = toRef(thisObject);
444     
445     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
446         if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
447             JSValueRef valueRef = toRef(exec, value);
448             JSValueRef exception = 0;
449             bool result;
450             {
451                 JSLock::DropAllLocks dropAllLocks(exec);
452                 result = hasInstance(execRef, thisRef, valueRef, &exception);
453             }
454             if (exception)
455                 exec->vm().throwException(exec, toJS(exec, exception));
456             return result;
457         }
458     }
459     return false;
460 }
461
462 template <class Parent>
463 CallType JSCallbackObject<Parent>::getCallData(JSCell* cell, CallData& callData)
464 {
465     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
466     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
467         if (jsClass->callAsFunction) {
468             callData.native.function = call;
469             return CallType::Host;
470         }
471     }
472     return CallType::None;
473 }
474
475 template <class Parent>
476 EncodedJSValue JSCallbackObject<Parent>::call(ExecState* exec)
477 {
478     JSContextRef execRef = toRef(exec);
479     JSObjectRef functionRef = toRef(exec->callee());
480     JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)));
481     
482     for (JSClassRef jsClass = jsCast<JSCallbackObject<Parent>*>(toJS(functionRef))->classRef(); jsClass; jsClass = jsClass->parentClass) {
483         if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
484             size_t argumentCount = exec->argumentCount();
485             Vector<JSValueRef, 16> arguments;
486             arguments.reserveInitialCapacity(argumentCount);
487             for (size_t i = 0; i < argumentCount; ++i)
488                 arguments.uncheckedAppend(toRef(exec, exec->uncheckedArgument(i)));
489             JSValueRef exception = 0;
490             JSValue result;
491             {
492                 JSLock::DropAllLocks dropAllLocks(exec);
493                 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
494             }
495             if (exception)
496                 exec->vm().throwException(exec, toJS(exec, exception));
497             return JSValue::encode(result);
498         }
499     }
500     
501     RELEASE_ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
502     return JSValue::encode(JSValue());
503 }
504
505 template <class Parent>
506 void JSCallbackObject<Parent>::getOwnNonIndexPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
507 {
508     JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(object);
509     JSContextRef execRef = toRef(exec);
510     JSObjectRef thisRef = toRef(thisObject);
511     
512     for (JSClassRef jsClass = thisObject->classRef(); jsClass; jsClass = jsClass->parentClass) {
513         if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
514             JSLock::DropAllLocks dropAllLocks(exec);
515             getPropertyNames(execRef, thisRef, toRef(&propertyNames));
516         }
517         
518         if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
519             typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
520             iterator end = staticValues->end();
521             for (iterator it = staticValues->begin(); it != end; ++it) {
522                 StringImpl* name = it->key.get();
523                 StaticValueEntry* entry = it->value.get();
524                 if (entry->getProperty && (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties())) {
525                     ASSERT(!name->isSymbol());
526                     propertyNames.add(Identifier::fromString(exec, String(name)));
527                 }
528             }
529         }
530         
531         if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
532             typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
533             iterator end = staticFunctions->end();
534             for (iterator it = staticFunctions->begin(); it != end; ++it) {
535                 StringImpl* name = it->key.get();
536                 StaticFunctionEntry* entry = it->value.get();
537                 if (!(entry->attributes & kJSPropertyAttributeDontEnum) || mode.includeDontEnumProperties()) {
538                     ASSERT(!name->isSymbol());
539                     propertyNames.add(Identifier::fromString(exec, String(name)));
540                 }
541             }
542         }
543     }
544     
545     Parent::getOwnNonIndexPropertyNames(thisObject, exec, propertyNames, mode);
546 }
547
548 template <class Parent>
549 void JSCallbackObject<Parent>::setPrivate(void* data)
550 {
551     m_callbackObjectData->privateData = data;
552 }
553
554 template <class Parent>
555 void* JSCallbackObject<Parent>::getPrivate()
556 {
557     return m_callbackObjectData->privateData;
558 }
559
560 template <class Parent>
561 bool JSCallbackObject<Parent>::inherits(JSClassRef c) const
562 {
563     for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
564         if (jsClass == c)
565             return true;
566     }
567     return false;
568 }
569
570 template <class Parent>
571 JSValue JSCallbackObject<Parent>::getStaticValue(ExecState* exec, PropertyName propertyName)
572 {
573     JSObjectRef thisRef = toRef(this);
574     
575     if (StringImpl* name = propertyName.uid()) {
576         for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
577             if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
578                 if (StaticValueEntry* entry = staticValues->get(name)) {
579                     if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
580                         JSValueRef exception = 0;
581                         JSValueRef value;
582                         {
583                             JSLock::DropAllLocks dropAllLocks(exec);
584                             value = getProperty(toRef(exec), thisRef, entry->propertyNameRef.get(), &exception);
585                         }
586                         if (exception) {
587                             exec->vm().throwException(exec, toJS(exec, exception));
588                             return jsUndefined();
589                         }
590                         if (value)
591                             return toJS(exec, value);
592                     }
593                 }
594             }
595         }
596     }
597
598     return JSValue();
599 }
600
601 template <class Parent>
602 EncodedJSValue JSCallbackObject<Parent>::staticFunctionGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
603 {
604     JSCallbackObject* thisObj = asCallbackObject(thisValue);
605     
606     // Check for cached or override property.
607     PropertySlot slot2(thisObj, PropertySlot::InternalMethodType::VMInquiry);
608     if (Parent::getOwnPropertySlot(thisObj, exec, propertyName, slot2))
609         return JSValue::encode(slot2.getValue(exec, propertyName));
610
611     if (StringImpl* name = propertyName.uid()) {
612         for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
613             if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
614                 if (StaticFunctionEntry* entry = staticFunctions->get(name)) {
615                     if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
616                         VM& vm = exec->vm();
617                         JSObject* o = JSCallbackFunction::create(vm, thisObj->globalObject(), callAsFunction, name);
618                         thisObj->putDirect(vm, propertyName, o, entry->attributes);
619                         return JSValue::encode(o);
620                     }
621                 }
622             }
623         }
624     }
625
626     return JSValue::encode(exec->vm().throwException(exec, createReferenceError(exec, ASCIILiteral("Static function property defined with NULL callAsFunction callback."))));
627 }
628
629 template <class Parent>
630 EncodedJSValue JSCallbackObject<Parent>::callbackGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName)
631 {
632     JSCallbackObject* thisObj = asCallbackObject(thisValue);
633     
634     JSObjectRef thisRef = toRef(thisObj);
635     RefPtr<OpaqueJSString> propertyNameRef;
636     
637     if (StringImpl* name = propertyName.uid()) {
638         for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
639             if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
640                 if (!propertyNameRef)
641                     propertyNameRef = OpaqueJSString::create(name);
642                 JSValueRef exception = 0;
643                 JSValueRef value;
644                 {
645                     JSLock::DropAllLocks dropAllLocks(exec);
646                     value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
647                 }
648                 if (exception) {
649                     exec->vm().throwException(exec, toJS(exec, exception));
650                     return JSValue::encode(jsUndefined());
651                 }
652                 if (value)
653                     return JSValue::encode(toJS(exec, value));
654             }
655         }
656     }
657
658     return JSValue::encode(exec->vm().throwException(exec, createReferenceError(exec, ASCIILiteral("hasProperty callback returned true for a property that doesn't exist."))));
659 }
660
661 } // namespace JSC