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