Changeset 27022 in webkit for trunk/JavaScriptCore/API/JSCallbackObjectFunctions.h
- Timestamp:
- Oct 24, 2007, 11:38:35 PM (18 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/API/JSCallbackObjectFunctions.h
r27021 r27022 2 2 /* 3 3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 4 * Copyright (C) 2007 Eric Seidel <[email protected]> 4 5 * 5 6 * Redistribution and use in source and binary forms, with or without … … 26 27 27 28 #include <wtf/Platform.h> 28 #include "JSCallbackObject.h"29 30 29 #include "APICast.h" 31 30 #include "JSCallbackFunction.h" 32 31 #include "JSClassRef.h" 33 32 #include "JSObjectRef.h" 33 #include "JSGlobalObject.h" 34 34 #include "JSStringRef.h" 35 35 #include "PropertyNameArray.h" … … 39 39 namespace KJS { 40 40 41 const ClassInfo JSCallbackObject::info = { "CallbackObject", 0, 0, 0 }; 42 43 JSCallbackObject::JSCallbackObject(ExecState* exec, JSClassRef jsClass, JSValue* prototype, void* data) 44 : JSObject(prototype) 41 template <class Base> 42 JSCallbackObject<Base>::JSCallbackObject(ExecState* exec, JSClassRef jsClass, JSValue* prototype, void* data) 43 : Base(prototype) 45 44 , m_class(0) 46 45 , m_isInitialized(false) … … 49 48 } 50 49 51 void JSCallbackObject::init(ExecState* exec, JSClassRef jsClass, void* data) 50 template <class Base> 51 void JSCallbackObject<Base>::init(ExecState* exec, JSClassRef jsClass, void* data) 52 52 { 53 53 m_privateData = data; … … 56 56 if (oldClass) 57 57 JSClassRelease(oldClass); 58 58 59 59 if (!exec) 60 60 return; 61 61 62 62 Vector<JSObjectInitializeCallback, 16> initRoutines; 63 63 do { … … 75 75 } 76 76 77 JSCallbackObject::~JSCallbackObject() 77 template <class Base> 78 JSCallbackObject<Base>::~JSCallbackObject() 78 79 { 79 80 JSObjectRef thisRef = toRef(this); … … 83 84 finalize(thisRef); 84 85 } 85 86 86 87 JSClassRelease(m_class); 87 88 } 88 89 89 void JSCallbackObject::initializeIfNeeded(ExecState* exec) 90 template <class Base> 91 void JSCallbackObject<Base>::initializeIfNeeded(ExecState* exec) 90 92 { 91 93 if (m_isInitialized) … … 94 96 } 95 97 96 UString JSCallbackObject::className() const 98 template <class Base> 99 UString JSCallbackObject<Base>::className() const 97 100 { 98 101 if (!m_class->className.isNull()) … … 102 105 } 103 106 104 bool JSCallbackObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) 107 template <class Base> 108 bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) 105 109 { 106 110 JSContextRef ctx = toRef(exec); 107 111 JSObjectRef thisRef = toRef(this); 108 112 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep()); 109 113 110 114 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) { 111 115 // optional optimization to bypass getProperty in cases when we only need to know if the property exists … … 126 130 } 127 131 } 128 132 129 133 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 130 134 if (staticValues->contains(propertyName.ustring().rep())) { … … 141 145 } 142 146 } 143 147 144 148 return JSObject::getOwnPropertySlot(exec, propertyName, slot); 145 149 } 146 150 147 bool JSCallbackObject::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) 151 template <class Base> 152 bool JSCallbackObject<Base>::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) 148 153 { 149 154 return getOwnPropertySlot(exec, Identifier::from(propertyName), slot); 150 155 } 151 156 152 void JSCallbackObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr) 157 template <class Base> 158 void JSCallbackObject<Base>::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr) 153 159 { 154 160 JSContextRef ctx = toRef(exec); … … 156 162 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep()); 157 163 JSValueRef valueRef = toRef(value); 158 164 159 165 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) { 160 166 if (JSObjectSetPropertyCallback setProperty = jsClass->setProperty) { … … 163 169 return; 164 170 } 165 171 166 172 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 167 173 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) { … … 181 187 if (entry->attributes & kJSPropertyAttributeReadOnly) 182 188 return; 183 putDirect(propertyName, value, attr); // put as override property189 JSCallbackObject<Base>::putDirect(propertyName, value, attr); // put as override property 184 190 return; 185 191 } 186 192 } 187 193 } 188 194 189 195 return JSObject::put(exec, propertyName, value, attr); 190 196 } 191 197 192 void JSCallbackObject::put(ExecState* exec, unsigned propertyName, JSValue* value, int attr) 198 template <class Base> 199 void JSCallbackObject<Base>::put(ExecState* exec, unsigned propertyName, JSValue* value, int attr) 193 200 { 194 201 return put(exec, Identifier::from(propertyName), value, attr); 195 202 } 196 203 197 bool JSCallbackObject::deleteProperty(ExecState* exec, const Identifier& propertyName) 204 template <class Base> 205 bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, const Identifier& propertyName) 198 206 { 199 207 JSContextRef ctx = toRef(exec); … … 207 215 return true; 208 216 } 209 217 210 218 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 211 219 if (StaticValueEntry* entry = staticValues->get(propertyName.ustring().rep())) { … … 224 232 } 225 233 } 226 234 227 235 return JSObject::deleteProperty(exec, propertyName); 228 236 } 229 237 230 bool JSCallbackObject::deleteProperty(ExecState* exec, unsigned propertyName) 238 template <class Base> 239 bool JSCallbackObject<Base>::deleteProperty(ExecState* exec, unsigned propertyName) 231 240 { 232 241 return deleteProperty(exec, Identifier::from(propertyName)); 233 242 } 234 243 235 bool JSCallbackObject::implementsConstruct() const 244 template <class Base> 245 bool JSCallbackObject<Base>::implementsConstruct() const 236 246 { 237 247 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 238 248 if (jsClass->callAsConstructor) 239 249 return true; 240 250 241 251 return false; 242 252 } 243 253 244 JSObject* JSCallbackObject::construct(ExecState* exec, const List& args) 254 template <class Base> 255 JSObject* JSCallbackObject<Base>::construct(ExecState* exec, const List& args) 245 256 { 246 257 JSContextRef execRef = toRef(exec); … … 262 273 } 263 274 264 bool JSCallbackObject::implementsHasInstance() const 275 template <class Base> 276 bool JSCallbackObject<Base>::implementsHasInstance() const 265 277 { 266 278 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 267 279 if (jsClass->hasInstance) 268 280 return true; 269 281 270 282 return false; 271 283 } 272 284 273 bool JSCallbackObject::hasInstance(ExecState *exec, JSValue *value) 285 template <class Base> 286 bool JSCallbackObject<Base>::hasInstance(ExecState *exec, JSValue *value) 274 287 { 275 288 JSContextRef execRef = toRef(exec); 276 289 JSObjectRef thisRef = toRef(this); 277 290 278 291 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 279 292 if (JSObjectHasInstanceCallback hasInstance = jsClass->hasInstance) { … … 281 294 return hasInstance(execRef, thisRef, toRef(value), toRef(exec->exceptionSlot())); 282 295 } 283 284 ASSERT (0); // implementsHasInstance should prevent us from reaching here296 297 ASSERT_NOT_REACHED(); // implementsHasInstance should prevent us from reaching here 285 298 return 0; 286 299 } 287 300 288 301 289 bool JSCallbackObject::implementsCall() const 302 template <class Base> 303 bool JSCallbackObject<Base>::implementsCall() const 290 304 { 291 305 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) … … 296 310 } 297 311 298 JSValue* JSCallbackObject::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args) 312 template <class Base> 313 JSValue* JSCallbackObject<Base>::callAsFunction(ExecState* exec, JSObject* thisObj, const List &args) 299 314 { 300 315 JSContextRef execRef = toRef(exec); 301 316 JSObjectRef thisRef = toRef(this); 302 317 JSObjectRef thisObjRef = toRef(thisObj); 303 318 304 319 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) { 305 320 if (JSObjectCallAsFunctionCallback callAsFunction = jsClass->callAsFunction) { … … 312 327 } 313 328 } 314 315 ASSERT (0); // implementsCall should prevent us from reaching here329 330 ASSERT_NOT_REACHED(); // implementsCall should prevent us from reaching here 316 331 return 0; 317 332 } 318 333 319 void JSCallbackObject::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames) 334 template <class Base> 335 void JSCallbackObject<Base>::getPropertyNames(ExecState* exec, PropertyNameArray& propertyNames) 320 336 { 321 337 JSContextRef execRef = toRef(exec); 322 338 JSObjectRef thisRef = toRef(this); 323 339 324 340 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) { 325 341 if (JSObjectGetPropertyNamesCallback getPropertyNames = jsClass->getPropertyNames) { … … 327 343 getPropertyNames(execRef, thisRef, toRef(&propertyNames)); 328 344 } 329 345 330 346 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) { 331 347 typedef OpaqueJSClass::StaticValuesTable::const_iterator iterator; … … 338 354 } 339 355 } 340 356 341 357 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { 342 358 typedef OpaqueJSClass::StaticFunctionsTable::const_iterator iterator; … … 350 366 } 351 367 } 352 368 353 369 JSObject::getPropertyNames(exec, propertyNames); 354 370 } 355 371 356 double JSCallbackObject::toNumber(ExecState* exec) const 372 template <class Base> 373 double JSCallbackObject<Base>::toNumber(ExecState* exec) const 357 374 { 358 375 JSContextRef ctx = toRef(exec); 359 376 JSObjectRef thisRef = toRef(this); 360 377 361 378 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 362 379 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) { … … 365 382 return toJS(value)->getNumber(); 366 383 } 367 384 368 385 return JSObject::toNumber(exec); 369 386 } 370 387 371 UString JSCallbackObject::toString(ExecState* exec) const 388 template <class Base> 389 UString JSCallbackObject<Base>::toString(ExecState* exec) const 372 390 { 373 391 JSContextRef ctx = toRef(exec); 374 392 JSObjectRef thisRef = toRef(this); 375 393 376 394 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 377 395 if (JSObjectConvertToTypeCallback convertToType = jsClass->convertToType) { … … 380 398 return toJS(value)->getString(); 381 399 } 382 400 383 401 return JSObject::toString(exec); 384 402 } 385 403 386 void JSCallbackObject::setPrivate(void* data) 404 template <class Base> 405 void JSCallbackObject<Base>::setPrivate(void* data) 387 406 { 388 407 m_privateData = data; 389 408 } 390 409 391 void* JSCallbackObject::getPrivate() 410 template <class Base> 411 void* JSCallbackObject<Base>::getPrivate() 392 412 { 393 413 return m_privateData; 394 414 } 395 415 396 bool JSCallbackObject::inherits(JSClassRef c) const 416 template <class Base> 417 bool JSCallbackObject<Base>::inherits(JSClassRef c) const 397 418 { 398 419 for (JSClassRef jsClass = m_class; jsClass; jsClass = jsClass->parentClass) 399 420 if (jsClass == c) 400 421 return true; 401 422 402 423 return false; 403 424 } 404 425 405 JSValue* JSCallbackObject::cachedValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot) 426 template <class Base> 427 JSValue* JSCallbackObject<Base>::cachedValueGetter(ExecState*, JSObject*, const Identifier&, const PropertySlot& slot) 406 428 { 407 429 JSValue* v = slot.slotBase(); … … 410 432 } 411 433 412 JSValue* JSCallbackObject::staticValueGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 434 template <class Base> 435 JSValue* JSCallbackObject<Base>::staticValueGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 413 436 { 414 437 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 415 438 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 416 439 417 440 JSObjectRef thisRef = toRef(thisObj); 418 441 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep()); 419 442 420 443 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass) 421 444 if (OpaqueJSClass::StaticValuesTable* staticValues = jsClass->staticValues) … … 426 449 return toJS(value); 427 450 } 428 451 429 452 return throwError(exec, ReferenceError, "Static value property defined with NULL getProperty callback."); 430 453 } 431 454 432 JSValue* JSCallbackObject::staticFunctionGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 455 template <class Base> 456 JSValue* JSCallbackObject<Base>::staticFunctionGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 433 457 { 434 458 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 435 459 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 436 460 437 461 if (JSValue* cachedOrOverrideValue = thisObj->getDirect(propertyName)) 438 462 return cachedOrOverrideValue; 439 463 440 464 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass) { 441 465 if (OpaqueJSClass::StaticFunctionsTable* staticFunctions = jsClass->staticFunctions) { … … 449 473 } 450 474 } 451 475 452 476 return throwError(exec, ReferenceError, "Static function property defined with NULL callAsFunction callback."); 453 477 } 454 478 455 JSValue* JSCallbackObject::callbackGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 479 template <class Base> 480 JSValue* JSCallbackObject<Base>::callbackGetter(ExecState* exec, JSObject*, const Identifier& propertyName, const PropertySlot& slot) 456 481 { 457 482 ASSERT(slot.slotBase()->inherits(&JSCallbackObject::info)); 458 483 JSCallbackObject* thisObj = static_cast<JSCallbackObject*>(slot.slotBase()); 459 484 460 485 JSObjectRef thisRef = toRef(thisObj); 461 486 JSStringRef propertyNameRef = toRef(propertyName.ustring().rep()); 462 487 463 488 for (JSClassRef jsClass = thisObj->m_class; jsClass; jsClass = jsClass->parentClass) 464 489 if (JSObjectGetPropertyCallback getProperty = jsClass->getProperty) { … … 467 492 return toJS(value); 468 493 } 469 494 470 495 return throwError(exec, ReferenceError, "hasProperty callback returned true for a property that doesn't exist."); 471 496 }
Note:
See TracChangeset
for help on using the changeset viewer.