2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include "ExceptionHelpers.h"
31 #include "JSCallbackFunction.h"
32 #include "JSClassRef.h"
33 #include "JSFunction.h"
34 #include "JSGlobalObject.h"
36 #include "JSObjectRef.h"
38 #include "JSStringRef.h"
39 #include "OpaqueJSString.h"
40 #include "PropertyNameArray.h"
41 #include <wtf/Vector.h>
45 template <class Parent>
46 inline JSCallbackObject<Parent>* JSCallbackObject<Parent>::asCallbackObject(JSValue value)
48 ASSERT(asObject(value)->inherits(&s_info));
49 return static_cast<JSCallbackObject*>(asObject(value));
52 template <class Parent>
53 JSCallbackObject<Parent>::JSCallbackObject(ExecState* exec, Structure* structure, JSClassRef jsClass, void* data)
54 : Parent(exec->globalData(), structure)
55 , m_callbackObjectData(adoptPtr(new JSCallbackObjectData(data, jsClass)))
60 // Global object constructor.
61 // FIXME: Move this into a separate JSGlobalCallbackObject class derived from this one.
62 template <class Parent>
63 JSCallbackObject<Parent>::JSCallbackObject(JSGlobalData& globalData, JSClassRef jsClass, Structure* structure)
64 : Parent(globalData, structure)
65 , m_callbackObjectData(adoptPtr(new JSCallbackObjectData(0, jsClass)))
67 finishCreation(globalData);
70 template <class Parent>
71 void JSCallbackObject<Parent>::finishCreation(ExecState* exec)
73 Base::finishCreation(exec->globalData());
74 ASSERT(Parent::inherits(&s_info));
78 // This is just for Global object, so we can assume that Base::finishCreation is JSGlobalObject::finishCreation.
79 template <class Parent>
80 void JSCallbackObject<Parent>::finishCreation(JSGlobalData& globalData)
82 ASSERT(Parent::inherits(&s_info));
83 ASSERT(Parent::isGlobalObject());
84 Base::finishCreation(globalData, this);
85 init(static_cast<JSGlobalObject*>(this)->globalExec());
88 template <class Parent>
89 void JSCallbackObject<Parent>::init(ExecState* exec)
93 Vector<JSObjectInitializeCallback, 16> initRoutines;
94 JSClassRef jsClass = classRef();
96 if (JSObjectInitializeCallback initialize = jsClass->initialize)
97 initRoutines.append(initialize);
98 } while ((jsClass = jsClass->parentClass));
100 // initialize from base to derived
101 for (int i = static_cast<int>(initRoutines.size()) - 1; i >= 0; i--) {
102 APICallbackShim callbackShim(exec);
103 JSObjectInitializeCallback initialize = initRoutines[i];
104 initialize(toRef(exec), toRef(this));
107 bool needsFinalizer = false;
108 for (JSClassRef jsClassPtr = classRef(); jsClassPtr && !needsFinalizer; jsClassPtr = jsClassPtr->parentClass)
109 needsFinalizer = jsClassPtr->finalize;
110 if (needsFinalizer) {
111 HandleSlot slot = exec->globalData().allocateGlobalHandle();
112 HandleHeap::heapFor(slot)->makeWeak(slot, m_callbackObjectData.get(), classRef());
113 HandleHeap::heapFor(slot)->writeBarrier(slot, this);
118 template <class Parent>
119 UString JSCallbackObject<Parent>::className() const
121 UString thisClassName = classRef()->className();
122 if (!thisClassName.isEmpty())
123 return thisClassName;
125 return Parent::className();
128 template <class Parent>
129 bool JSCallbackObject<Parent>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
131 JSContextRef ctx = toRef(exec);
132 JSObjectRef thisRef = toRef(this);
133 RefPtr<OpaqueJSString> propertyNameRef;
135 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
136 // optional optimization to bypass getProperty in cases when we only need to know if the property exists
137 if (JSObjectHasPropertyCallback hasProperty = jsClass->hasProperty) {
138 if (!propertyNameRef)
139 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
140 APICallbackShim callbackShim(exec);
141 if (hasProperty(ctx, thisRef, propertyNameRef.get())) {
142 slot.setCustom(this, callbackGetter);
145 } else if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
146 if (!propertyNameRef)
147 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
148 JSValueRef exception = 0;
151 APICallbackShim callbackShim(exec);
152 value = getProperty(ctx, thisRef, propertyNameRef.get(), &exception);
155 throwError(exec, toJS(exec, exception));
156 slot.setValue(jsUndefined());
160 slot.setValue(toJS(exec, value));
165 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
166 if (staticValues->contains(propertyName.impl())) {
167 JSValue value = getStaticValue(exec, propertyName);
169 slot.setValue(value);
175 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
176 if (staticFunctions->contains(propertyName.impl())) {
177 slot.setCustom(this, staticFunctionGetter);
183 return Parent::getOwnPropertySlot(exec, propertyName, slot);
186 template <class Parent>
187 bool JSCallbackObject<Parent>::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
190 if (getOwnPropertySlot(exec, propertyName, slot)) {
191 // Ideally we should return an access descriptor, but returning a value descriptor is better than nothing.
192 JSValue value = slot.getValue(exec, propertyName);
193 if (!exec->hadException())
194 descriptor.setValue(value);
195 // We don't know whether the property is configurable, but assume it is.
196 descriptor.setConfigurable(true);
197 // We don't know whether the property is enumerable (we could call getOwnPropertyNames() to find out), but assume it isn't.
198 descriptor.setEnumerable(false);
202 return Parent::getOwnPropertyDescriptor(exec, propertyName, descriptor);
205 template <class Parent>
206 void JSCallbackObject<Parent>::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
208 JSContextRef ctx = toRef(exec);
209 JSObjectRef thisRef = toRef(this);
210 RefPtr<OpaqueJSString> propertyNameRef;
211 JSValueRef valueRef = toRef(exec, value);
213 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
214 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) {
215 if (!propertyNameRef)
216 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
217 JSValueRef exception = 0;
220 APICallbackShim callbackShim(exec);
221 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
224 throwError(exec, toJS(exec, exception));
225 if (result || exception)
229 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
230 if (StaticValueEntry* entry = staticValues->get(propertyName.impl())) {
231 if (entry->attributes & kJSPropertyAttributeReadOnly)
233 if (JSObjectSetPropertyCallback setProperty = entry->setProperty) {
234 if (!propertyNameRef)
235 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
236 JSValueRef exception = 0;
239 APICallbackShim callbackShim(exec);
240 result = setProperty(ctx, thisRef, propertyNameRef.get(), valueRef, &exception);
243 throwError(exec, toJS(exec, exception));
244 if (result || exception)
250 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
251 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) {
252 if (entry->attributes & kJSPropertyAttributeReadOnly)
254 JSCallbackObject<Parent>::putDirect(exec->globalData(), propertyName, value); // put as override property
260 return Parent::put(exec, propertyName, value, slot);
263 template <class Parent>
264 bool JSCallbackObject<Parent>::deleteProperty(ExecState* exec, const Identifier& propertyName)
266 JSContextRef ctx = toRef(exec);
267 JSObjectRef thisRef = toRef(this);
268 RefPtr<OpaqueJSString> propertyNameRef;
270 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
271 if (JSObjectDeletePropertyCallback deleteProperty = jsClass->deleteProperty) {
272 if (!propertyNameRef)
273 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
274 JSValueRef exception = 0;
277 APICallbackShim callbackShim(exec);
278 result = deleteProperty(ctx, thisRef, propertyNameRef.get(), &exception);
281 throwError(exec, toJS(exec, exception));
282 if (result || exception)
286 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
287 if (StaticValueEntry* entry = staticValues->get(propertyName.impl())) {
288 if (entry->attributes & kJSPropertyAttributeDontDelete)
294 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
295 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) {
296 if (entry->attributes & kJSPropertyAttributeDontDelete)
303 return Parent::deleteProperty(exec, propertyName);
306 template <class Parent>
307 bool JSCallbackObject<Parent>::deleteProperty(ExecState* exec, unsigned propertyName)
309 return deleteProperty(exec, Identifier::from(exec, propertyName));
312 template <class Parent>
313 ConstructType JSCallbackObject<Parent>::getConstructData(ConstructData& constructData)
315 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
316 if (jsClass->callAsConstructor) {
317 constructData.native.function = construct;
318 return ConstructTypeHost;
321 return ConstructTypeNone;
324 template <class Parent>
325 EncodedJSValue JSCallbackObject<Parent>::construct(ExecState* exec)
327 JSObject* constructor = exec->callee();
328 JSContextRef execRef = toRef(exec);
329 JSObjectRef constructorRef = toRef(constructor);
331 for (JSClassRef jsClass = static_cast<JSCallbackObject<Parent>*>(constructor)->classRef(); jsClass; jsClass = jsClass->parentClass) {
332 if (JSObjectCallAsConstructorCallback callAsConstructor = jsClass->callAsConstructor) {
333 int argumentCount = static_cast<int>(exec->argumentCount());
334 Vector<JSValueRef, 16> arguments(argumentCount);
335 for (int i = 0; i < argumentCount; i++)
336 arguments[i] = toRef(exec, exec->argument(i));
337 JSValueRef exception = 0;
340 APICallbackShim callbackShim(exec);
341 result = toJS(callAsConstructor(execRef, constructorRef, argumentCount, arguments.data(), &exception));
344 throwError(exec, toJS(exec, exception));
345 return JSValue::encode(result);
349 ASSERT_NOT_REACHED(); // getConstructData should prevent us from reaching here
350 return JSValue::encode(JSValue());
353 template <class Parent>
354 bool JSCallbackObject<Parent>::hasInstance(ExecState* exec, JSValue value, JSValue)
356 JSContextRef execRef = toRef(exec);
357 JSObjectRef thisRef = toRef(this);
359 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
360 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) {
361 JSValueRef valueRef = toRef(exec, value);
362 JSValueRef exception = 0;
365 APICallbackShim callbackShim(exec);
366 result = hasInstance(execRef, thisRef, valueRef, &exception);
369 throwError(exec, toJS(exec, exception));
376 template <class Parent>
377 CallType JSCallbackObject<Parent>::getCallData(CallData& callData)
379 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
380 if (jsClass->callAsFunction) {
381 callData.native.function = call;
388 template <class Parent>
389 EncodedJSValue JSCallbackObject<Parent>::call(ExecState* exec)
391 JSContextRef execRef = toRef(exec);
392 JSObjectRef functionRef = toRef(exec->callee());
393 JSObjectRef thisObjRef = toRef(exec->hostThisValue().toThisObject(exec));
395 for (JSClassRef jsClass = static_cast<JSCallbackObject<Parent>*>(toJS(functionRef))->classRef(); jsClass; jsClass = jsClass->parentClass) {
396 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) {
397 int argumentCount = static_cast<int>(exec->argumentCount());
398 Vector<JSValueRef, 16> arguments(argumentCount);
399 for (int i = 0; i < argumentCount; i++)
400 arguments[i] = toRef(exec, exec->argument(i));
401 JSValueRef exception = 0;
404 APICallbackShim callbackShim(exec);
405 result = toJS(exec, callAsFunction(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception));
408 throwError(exec, toJS(exec, exception));
409 return JSValue::encode(result);
413 ASSERT_NOT_REACHED(); // getCallData should prevent us from reaching here
414 return JSValue::encode(JSValue());
417 template <class Parent>
418 void JSCallbackObject<Parent>::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
420 JSContextRef execRef = toRef(exec);
421 JSObjectRef thisRef = toRef(this);
423 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass) {
424 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) {
425 APICallbackShim callbackShim(exec);
426 getPropertyNames(execRef, thisRef, toRef(&propertyNames));
429 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec)) {
430 typedef OpaqueJSClassStaticValuesTable::const_iterator iterator;
431 iterator end = staticValues->end();
432 for (iterator it = staticValues->begin(); it != end; ++it) {
433 StringImpl* name = it->first.get();
434 StaticValueEntry* entry = it->second;
435 if (entry->getProperty && (!(entry->attributes & kJSPropertyAttributeDontEnum) || (mode == IncludeDontEnumProperties)))
436 propertyNames.add(Identifier(exec, name));
440 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
441 typedef OpaqueJSClassStaticFunctionsTable::const_iterator iterator;
442 iterator end = staticFunctions->end();
443 for (iterator it = staticFunctions->begin(); it != end; ++it) {
444 StringImpl* name = it->first.get();
445 StaticFunctionEntry* entry = it->second;
446 if (!(entry->attributes & kJSPropertyAttributeDontEnum) || (mode == IncludeDontEnumProperties))
447 propertyNames.add(Identifier(exec, name));
452 Parent::getOwnPropertyNames(exec, propertyNames, mode);
455 template <class Parent>
456 double JSCallbackObject<Parent>::toNumber(ExecState* exec) const
458 // We need this check to guard against the case where this object is rhs of
459 // a binary expression where lhs threw an exception in its conversion to
461 if (exec->hadException())
462 return std::numeric_limits<double>::quiet_NaN();
463 JSContextRef ctx = toRef(exec);
464 JSObjectRef thisRef = toRef(this);
466 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
467 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
468 JSValueRef exception = 0;
471 APICallbackShim callbackShim(exec);
472 value = convertToType(ctx, thisRef, kJSTypeNumber, &exception);
475 throwError(exec, toJS(exec, exception));
481 return toJS(exec, value).getNumber(dValue) ? dValue : std::numeric_limits<double>::quiet_NaN();
484 return Parent::toNumber(exec);
487 template <class Parent>
488 UString JSCallbackObject<Parent>::toString(ExecState* exec) const
490 JSContextRef ctx = toRef(exec);
491 JSObjectRef thisRef = toRef(this);
493 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
494 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) {
495 JSValueRef exception = 0;
498 APICallbackShim callbackShim(exec);
499 value = convertToType(ctx, thisRef, kJSTypeString, &exception);
502 throwError(exec, toJS(exec, exception));
506 return toJS(exec, value).getString(exec);
509 return Parent::toString(exec);
512 template <class Parent>
513 void JSCallbackObject<Parent>::setPrivate(void* data)
515 m_callbackObjectData->privateData = data;
518 template <class Parent>
519 void* JSCallbackObject<Parent>::getPrivate()
521 return m_callbackObjectData->privateData;
524 template <class Parent>
525 bool JSCallbackObject<Parent>::inherits(JSClassRef c) const
527 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
534 template <class Parent>
535 JSValue JSCallbackObject<Parent>::getStaticValue(ExecState* exec, const Identifier& propertyName)
537 JSObjectRef thisRef = toRef(this);
538 RefPtr<OpaqueJSString> propertyNameRef;
540 for (JSClassRef jsClass = classRef(); jsClass; jsClass = jsClass->parentClass)
541 if (OpaqueJSClassStaticValuesTable* staticValues = jsClass->staticValues(exec))
542 if (StaticValueEntry* entry = staticValues->get(propertyName.impl()))
543 if (JSObjectGetPropertyCallback getProperty = entry->getProperty) {
544 if (!propertyNameRef)
545 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
546 JSValueRef exception = 0;
549 APICallbackShim callbackShim(exec);
550 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
553 throwError(exec, toJS(exec, exception));
554 return jsUndefined();
557 return toJS(exec, value);
563 template <class Parent>
564 JSValue JSCallbackObject<Parent>::staticFunctionGetter(ExecState* exec, JSValue slotParent, const Identifier& propertyName)
566 JSCallbackObject* thisObj = asCallbackObject(slotParent);
568 // Check for cached or override property.
569 PropertySlot slot2(thisObj);
570 if (thisObj->Parent::getOwnPropertySlot(exec, propertyName, slot2))
571 return slot2.getValue(exec, propertyName);
573 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass) {
574 if (OpaqueJSClassStaticFunctionsTable* staticFunctions = jsClass->staticFunctions(exec)) {
575 if (StaticFunctionEntry* entry = staticFunctions->get(propertyName.impl())) {
576 if (JSObjectCallAsFunctionCallback callAsFunction = entry->callAsFunction) {
578 JSObject* o = JSCallbackFunction::create(exec, thisObj->globalObject(), callAsFunction, propertyName);
579 thisObj->putDirect(exec->globalData(), propertyName, o, entry->attributes);
586 return throwError(exec, createReferenceError(exec, "Static function property defined with NULL callAsFunction callback."));
589 template <class Parent>
590 JSValue JSCallbackObject<Parent>::callbackGetter(ExecState* exec, JSValue slotParent, const Identifier& propertyName)
592 JSCallbackObject* thisObj = asCallbackObject(slotParent);
594 JSObjectRef thisRef = toRef(thisObj);
595 RefPtr<OpaqueJSString> propertyNameRef;
597 for (JSClassRef jsClass = thisObj->classRef(); jsClass; jsClass = jsClass->parentClass)
598 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) {
599 if (!propertyNameRef)
600 propertyNameRef = OpaqueJSString::create(propertyName.ustring());
601 JSValueRef exception = 0;
604 APICallbackShim callbackShim(exec);
605 value = getProperty(toRef(exec), thisRef, propertyNameRef.get(), &exception);
608 throwError(exec, toJS(exec, exception));
609 return jsUndefined();
612 return toJS(exec, value);
615 return throwError(exec, createReferenceError(exec, "hasProperty callback returned true for a property that doesn't exist."));