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