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