Changeset 10084 in webkit for trunk/JavaScriptCore/kjs/object.h
- Timestamp:
- Aug 7, 2005, 9:07:46 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/object.h
r10076 r10084 23 23 */ 24 24 25 26 #ifndef _KJS_OBJECT_H_ 27 #define _KJS_OBJECT_H_ 25 #ifndef KJS_OBJECT_H 26 #define KJS_OBJECT_H 28 27 29 28 // Objects … … 39 38 #endif 40 39 41 #include "types.h"42 40 #include "interpreter.h" 43 #include "reference_list.h"44 41 #include "property_map.h" 45 42 #include "property_slot.h" … … 51 48 class HashEntry; 52 49 class ListImp; 50 51 // ECMA 262-3 8.6.1 52 // Property attributes 53 enum Attribute { None = 0, 54 ReadOnly = 1 << 1, // property can be only read, not written 55 DontEnum = 1 << 2, // property doesn't appear in (for .. in ..) 56 DontDelete = 1 << 3, // property can't be deleted 57 Internal = 1 << 4, // an internal property, set to bypass checks 58 Function = 1 << 5 }; // property is a function - only used by static hashtables 53 59 54 60 /** … … 75 81 }; 76 82 77 inline Object Value::toObject(ExecState *exec) const { return rep->dispatchToObject(exec); } 78 79 class ObjectImp : public ValueImp { 83 class ObjectImp : public AllocatedValueImp { 80 84 public: 81 85 /** … … 84 88 * @param proto The prototype 85 89 */ 86 ObjectImp(const Object &proto);87 90 ObjectImp(ObjectImp *proto); 88 91 89 92 /** 90 93 * Creates a new ObjectImp with a prototype of Null() 91 * (that is, the ECMAScript "null" value, not a null Object).94 * (that is, the ECMAScript "null" value, not a null object pointer). 92 95 * 93 96 */ 94 97 ObjectImp(); 95 96 virtual ~ObjectImp();97 98 98 99 virtual void mark(); … … 170 171 171 172 /** 173 * Returns the prototype of this object. Note that this is not the same as 174 * the "prototype" property. 175 * 176 * See ECMA 8.6.2 177 * 178 * @return The object's prototype 179 */ 180 /** 172 181 * Implementation of the [[Prototype]] internal property (implemented by 173 182 * all Objects) 174 * 175 * @see Object::prototype() 176 */ 177 Value prototype() const; 178 void setPrototype(const Value &proto); 179 183 */ 184 ValueImp *prototype() const; 185 void setPrototype(ValueImp *proto); 186 187 /** 188 * Returns the class name of the object 189 * 190 * See ECMA 8.6.2 191 * 192 * @return The object's class name 193 */ 180 194 /** 181 195 * Implementation of the [[Class]] internal property (implemented by all … … 186 200 * if you simply need a classname, you can reimplement className() 187 201 * instead. 188 *189 * @see Object::className()190 202 */ 191 203 virtual UString className() const; 192 204 205 /** 206 * Retrieves the specified property from the object. If neither the object 207 * or any other object in it's prototype chain have the property, this 208 * function will return Undefined. 209 * 210 * See ECMA 8.6.2.1 211 * 212 * @param exec The current execution state 213 * @param propertyName The name of the property to retrieve 214 * 215 * @return The specified property, or Undefined 216 */ 193 217 /** 194 218 * Implementation of the [[Get]] internal property (implemented by all 195 219 * Objects) 196 *197 * @see Object::get()198 220 */ 199 221 // [[Get]] - must be implemented by all Objects 200 Value 201 Value 202 203 bool getProperty(ExecState *exec, const Identifier& propertyName, Value & result) const;204 bool getProperty(ExecState *exec, unsigned propertyName, Value & result) const;222 ValueImp *get(ExecState *exec, const Identifier &propertyName) const; 223 ValueImp *get(ExecState *exec, unsigned propertyName) const; 224 225 bool getProperty(ExecState *exec, const Identifier& propertyName, ValueImp*& result) const; 226 bool getProperty(ExecState *exec, unsigned propertyName, ValueImp*& result) const; 205 227 206 228 bool getPropertySlot(ExecState *, const Identifier&, PropertySlot&); 207 229 bool getPropertySlot(ExecState *, unsigned, PropertySlot&); 230 208 231 virtual bool getOwnPropertySlot(ExecState *, const Identifier&, PropertySlot&); 209 232 virtual bool getOwnPropertySlot(ExecState *, unsigned index, PropertySlot&); 210 233 211 234 /** 235 * Sets the specified property. 236 * 237 * See ECMA 8.6.2.2 238 * 239 * @param exec The current execution state 240 * @param propertyName The name of the property to set 241 * @param propertyValue The value to set 242 */ 243 /** 212 244 * Implementation of the [[Put]] internal property (implemented by all 213 245 * Objects) 214 *215 * @see Object::put()216 246 */ 217 247 virtual void put(ExecState *exec, const Identifier &propertyName, 218 const Value &value, int attr = None);248 ValueImp *value, int attr = None); 219 249 virtual void put(ExecState *exec, unsigned propertyName, 220 const Value &value, int attr = None); 221 250 ValueImp *value, int attr = None); 251 252 /** 253 * Used to check whether or not a particular property is allowed to be set 254 * on an object 255 * 256 * See ECMA 8.6.2.3 257 * 258 * @param exec The current execution state 259 * @param propertyName The name of the property 260 * @return true if the property can be set, otherwise false 261 */ 222 262 /** 223 263 * Implementation of the [[CanPut]] internal property (implemented by all 224 264 * Objects) 225 *226 * @see Object::canPut()227 265 */ 228 266 virtual bool canPut(ExecState *exec, const Identifier &propertyName) const; 229 267 268 /** 269 * Checks to see whether the object (or any object in it's prototype chain) 270 * has a property with the specified name. 271 * 272 * See ECMA 8.6.2.4 273 * 274 * @param exec The current execution state 275 * @param propertyName The name of the property to check for 276 * @return true if the object has the property, otherwise false 277 */ 230 278 /** 231 279 * Implementation of the [[HasProperty]] internal property (implemented by 232 280 * all Objects) 233 * 234 * @see Object::hasProperty() 235 */ 236 bool hasProperty(ExecState *exec, const Identifier &propertyName) const; 281 */ 282 bool hasProperty(ExecState *exec, 283 const Identifier &propertyName) const; 237 284 bool hasProperty(ExecState *exec, unsigned propertyName) const; 238 285 286 /** 287 * Checks to see whether the object has a property with the specified name. 288 * 289 * See ECMA 15.2.4.5 290 * 291 * @param exec The current execution state 292 * @param propertyName The name of the property to check for 293 * @return true if the object has the property, otherwise false 294 */ 239 295 virtual bool hasOwnProperty(ExecState *exec, const Identifier &propertyName) const; 240 296 virtual bool hasOwnProperty(ExecState *exec, unsigned propertyName) const; 297 298 /** 299 * Removes the specified property from the object. 300 * 301 * See ECMA 8.6.2.5 302 * 303 * @param exec The current execution state 304 * @param propertyName The name of the property to delete 305 * @return true if the property was successfully deleted or did not 306 * exist on the object. false if deleting the specified property is not 307 * allowed. 308 */ 241 309 /** 242 310 * Implementation of the [[Delete]] internal property (implemented by all 243 311 * Objects) 244 *245 * @see Object::deleteProperty()246 312 */ 247 313 virtual bool deleteProperty(ExecState *exec, … … 257 323 258 324 /** 325 * Converts the object into a primitive value. The value return may differ 326 * depending on the supplied hint 327 * 328 * See ECMA 8.6.2.6 329 * 330 * @param exec The current execution state 331 * @param hint The desired primitive type to convert to 332 * @return A primitive value converted from the objetc. Note that the 333 * type of primitive value returned may not be the same as the requested 334 * hint. 335 */ 336 /** 259 337 * Implementation of the [[DefaultValue]] internal property (implemented by 260 338 * all Objects) 261 * 262 * @see Object::defaultValue() 263 */ 264 virtual Value defaultValue(ExecState *exec, Type hint) const; 265 339 */ 340 virtual ValueImp *defaultValue(ExecState *exec, Type hint) const; 341 342 /** 343 * Whether or not the object implements the construct() method. If this 344 * returns false you should not call the construct() method on this 345 * object (typically, an assertion will fail to indicate this). 346 * 347 * @return true if this object implements the construct() method, otherwise 348 * false 349 */ 266 350 virtual bool implementsConstruct() const; 351 352 /** 353 * Creates a new object based on this object. Typically this means the 354 * following: 355 * 1. A new object is created 356 * 2. The prototype of the new object is set to the value of this object's 357 * "prototype" property 358 * 3. The call() method of this object is called, with the new object 359 * passed as the this value 360 * 4. The new object is returned 361 * 362 * In some cases, Host objects may differ from these semantics, although 363 * this is discouraged. 364 * 365 * If an error occurs during construction, the execution state's exception 366 * will be set. This can be tested for with ExecState::hadException(). 367 * Under some circumstances, the exception object may also be returned. 368 * 369 * Note: This function should not be called if implementsConstruct() returns 370 * false, in which case it will result in an assertion failure. 371 * 372 * @param exec The current execution state 373 * @param args The arguments to be passed to call() once the new object has 374 * been created 375 * @return The newly created & initialized object 376 */ 267 377 /** 268 378 * Implementation of the [[Construct]] internal property 269 * 270 * @see Object::construct() 271 */ 272 virtual Object construct(ExecState *exec, const List &args); 273 virtual Object construct(ExecState *exec, const List &args, const UString &sourceURL, int lineNumber); 274 379 */ 380 virtual ObjectImp *construct(ExecState *exec, const List &args); 381 virtual ObjectImp *construct(ExecState *exec, const List &args, const UString &sourceURL, int lineNumber); 382 383 /** 384 * Whether or not the object implements the call() method. If this returns 385 * false you should not call the call() method on this object (typically, 386 * an assertion will fail to indicate this). 387 * 388 * @return true if this object implements the call() method, otherwise 389 * false 390 */ 275 391 virtual bool implementsCall() const; 392 393 /** 394 * Calls this object as if it is a function. 395 * 396 * Note: This function should not be called if implementsCall() returns 397 * false, in which case it will result in an assertion failure. 398 * 399 * See ECMA 8.6.2.3 400 * 401 * @param exec The current execution state 402 * @param thisObj The obj to be used as "this" within function execution. 403 * Note that in most cases this will be different from the C++ "this" 404 * object. For example, if the ECMAScript code "window.location->toString()" 405 * is executed, call() will be invoked on the C++ object which implements 406 * the toString method, with the thisObj being window.location 407 * @param args List of arguments to be passed to the function 408 * @return The return value from the function 409 */ 276 410 /** 277 411 * Implementation of the [[Call]] internal property 278 * 279 * @see Object::call() 280 */ 281 virtual Value call(ExecState *exec, Object &thisObj, 282 const List &args); 283 412 */ 413 ValueImp *call(ExecState *exec, ObjectImp *thisObj, const List &args); 414 virtual ValueImp *callAsFunction(ExecState *exec, ObjectImp *thisObj, const List &args); 415 416 /** 417 * Whether or not the object implements the hasInstance() method. If this 418 * returns false you should not call the hasInstance() method on this 419 * object (typically, an assertion will fail to indicate this). 420 * 421 * @return true if this object implements the hasInstance() method, 422 * otherwise false 423 */ 284 424 virtual bool implementsHasInstance() const; 425 426 /** 427 * Checks whether value delegates behavior to this object. Used by the 428 * instanceof operator. 429 * 430 * @param exec The current execution state 431 * @param value The value to check 432 * @return true if value delegates behavior to this object, otherwise 433 * false 434 */ 285 435 /** 286 436 * Implementation of the [[HasInstance]] internal property 287 * 288 * @see Object::hasInstance() 289 */ 290 virtual Boolean hasInstance(ExecState *exec, const Value &value); 291 437 */ 438 virtual bool hasInstance(ExecState *exec, ValueImp *value); 439 440 /** 441 * Returns the scope of this object. This is used when execution declared 442 * functions - the execution context for the function is initialized with 443 * extra object in it's scope. An example of this is functions declared 444 * inside other functions: 445 * 446 * \code 447 * function f() { 448 * 449 * function b() { 450 * return prototype; 451 * } 452 * 453 * var x = 4; 454 * // do some stuff 455 * } 456 * f.prototype = new String(); 457 * \endcode 458 * 459 * When the function f.b is executed, its scope will include properties of 460 * f. So in the example above the return value of f.b() would be the new 461 * String object that was assigned to f.prototype. 462 * 463 * @param exec The current execution state 464 * @return The function's scope 465 */ 292 466 /** 293 467 * Implementation of the [[Scope]] internal property 294 *295 * @see Object::scope()296 468 */ 297 469 const ScopeChain &scope() const { return _scope; } 298 470 void setScope(const ScopeChain &s) { _scope = s; } 299 471 472 /** 473 * Returns a List of References to all the properties of the object. Used 474 * in "for x in y" statements. The list is created new, so it can be freely 475 * modified without affecting the object's properties. It should be deleted 476 * by the caller. 477 * 478 * Subclasses can override this method in ObjectImpl to provide the 479 * appearance of 480 * having extra properties other than those set specifically with put(). 481 * 482 * @param exec The current execution state 483 * @param recursive Whether or not properties in the object's prototype 484 * chain should be 485 * included in the list. 486 * @return A List of References to properties of the object. 487 **/ 300 488 virtual ReferenceList propList(ExecState *exec, bool recursive = true); 301 489 302 Value internalValue() const; 303 void setInternalValue(const Value &v); 490 /** 491 * Returns the internal value of the object. This is used for objects such 492 * as String and Boolean which are wrappers for native types. The interal 493 * value is the actual value represented by the wrapper objects. 494 * 495 * @see ECMA 8.6.2 496 * @return The internal value of the object 497 */ 498 ValueImp *internalValue() const; 499 500 /** 501 * Sets the internal value of the object 502 * 503 * @see internalValue() 504 * 505 * @param v The new internal value 506 */ 507 304 508 void setInternalValue(ValueImp *v); 305 509 306 Value toPrimitive(ExecState *exec, 307 Type preferredType = UnspecifiedType) const; 510 ValueImp *toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const; 308 511 bool toBoolean(ExecState *exec) const; 309 512 double toNumber(ExecState *exec) const; 310 513 UString toString(ExecState *exec) const; 311 Object 514 ObjectImp *toObject(ExecState *exec) const; 312 515 313 516 // This get method only looks at the property map. … … 345 548 346 549 ValueImp *proto = imp->_proto; 347 if ( proto->dispatchType() != ObjectType)550 if (!proto->isObject()) 348 551 break; 349 552 … … 403 606 * @param lineno Optional source id. 404 607 */ 405 static Object 406 const char *message = 0, int lineno = -1,407 int sourceId = -1, const UString *sourceURL = 0);608 static ObjectImp *create(ExecState *exec, ErrorType errtype = GeneralError, 609 const char *message = 0, int lineno = -1, 610 int sourceId = -1, const UString *sourceURL = 0); 408 611 409 612 /** … … 413 616 }; 414 617 415 inline bool ValueImp::isObject(const ClassInfo *info) const 416 { return isObject() && static_cast<const ObjectImp *>(this)->inherits(info); } 417 418 inline ObjectImp *ValueImp::asObject() 419 { return isObject() ? static_cast<ObjectImp *>(this) : 0; } 420 421 inline Object::Object(ObjectImp *o) : Value(o) { } 422 423 inline ObjectImp *Object::imp() const 424 { return static_cast<ObjectImp *>(Value::imp()); } 425 426 inline const ClassInfo *Object::classInfo() const 427 { return imp()->classInfo(); } 428 429 inline bool Object::inherits(const ClassInfo *cinfo) const 430 { return imp()->inherits(cinfo); } 431 432 inline Value Object::prototype() const 433 { return Value(imp()->prototype()); } 434 435 inline UString Object::className() const 436 { return imp()->className(); } 437 438 inline Value Object::get(ExecState *exec, const Identifier &propertyName) const 439 { return imp()->get(exec,propertyName); } 440 441 inline Value Object::get(ExecState *exec, unsigned propertyName) const 442 { return imp()->get(exec,propertyName); } 443 444 inline void Object::put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr) 445 { imp()->put(exec,propertyName,value,attr); } 446 447 inline void Object::put(ExecState *exec, unsigned propertyName, const Value &value, int attr) 448 { imp()->put(exec,propertyName,value,attr); } 449 450 inline bool Object::canPut(ExecState *exec, const Identifier &propertyName) const 451 { return imp()->canPut(exec,propertyName); } 452 453 inline bool Object::hasProperty(ExecState *exec, const Identifier &propertyName) const 454 { return imp()->hasProperty(exec, propertyName); } 455 456 inline bool Object::hasOwnProperty(ExecState *exec, const Identifier &propertyName) const 457 { return imp()->hasOwnProperty(exec, propertyName); } 458 459 inline bool Object::deleteProperty(ExecState *exec, const Identifier &propertyName) 460 { return imp()->deleteProperty(exec,propertyName); } 461 462 inline bool Object::deleteProperty(ExecState *exec, unsigned propertyName) 463 { return imp()->deleteProperty(exec,propertyName); } 464 465 inline Value Object::defaultValue(ExecState *exec, Type hint) const 466 { return imp()->defaultValue(exec,hint); } 467 468 inline bool Object::implementsConstruct() const 469 { return imp()->implementsConstruct(); } 470 471 inline Object Object::construct(ExecState *exec, const List &args) 472 { return imp()->construct(exec,args); } 473 474 inline Object Object::construct(ExecState *exec, const List &args, const UString &sourceURL, int lineNumber) 475 { return imp()->construct(exec,args,sourceURL,lineNumber); } 476 477 inline bool Object::implementsCall() const 478 { return imp()->implementsCall(); } 479 480 inline bool Object::implementsHasInstance() const 481 { return imp()->implementsHasInstance(); } 482 483 inline Boolean Object::hasInstance(ExecState *exec, const Value &value) 484 { return imp()->hasInstance(exec,value); } 485 486 inline const ScopeChain &Object::scope() const 487 { return imp()->scope(); } 488 489 inline void Object::setScope(const ScopeChain &s) 490 { imp()->setScope(s); } 491 492 inline ReferenceList Object::propList(ExecState *exec, bool recursive) 493 { return imp()->propList(exec,recursive); } 494 495 inline Value Object::internalValue() const 496 { return imp()->internalValue(); } 497 498 inline void Object::setInternalValue(const Value &v) 499 { imp()->setInternalValue(v); } 500 501 inline void Object::saveProperties(SavedProperties &p) const 502 { imp()->saveProperties(p); } 503 inline void Object::restoreProperties(const SavedProperties &p) 504 { imp()->restoreProperties(p); } 618 inline bool AllocatedValueImp::isObject(const ClassInfo *info) const 619 { return isObject() && static_cast<const ObjectImp *>(this)->inherits(info); } 620 621 inline ObjectImp::ObjectImp(ObjectImp *proto) 622 : _proto(proto), _internalValue(0) 623 { 624 } 625 626 inline ObjectImp::ObjectImp() 627 : _proto(jsNull()), _internalValue(0) 628 { 629 } 630 631 inline ValueImp *ObjectImp::internalValue() const 632 { 633 return _internalValue; 634 } 635 636 inline void ObjectImp::setInternalValue(ValueImp *v) 637 { 638 _internalValue = v; 639 } 640 641 inline ValueImp *ObjectImp::prototype() const 642 { 643 return _proto; 644 } 645 646 inline void ObjectImp::setPrototype(ValueImp *proto) 647 { 648 _proto = proto; 649 } 650 651 inline bool ObjectImp::inherits(const ClassInfo *info) const 652 { 653 for (const ClassInfo *ci = classInfo(); ci; ci = ci->parentClass) 654 if (ci == info) 655 return true; 656 return false; 657 } 505 658 506 659 } // namespace 507 660 508 #endif // _KJS_OBJECT_H_661 #endif // KJS_OBJECT_H
Note:
See TracChangeset
for help on using the changeset viewer.