Changeset 798 in webkit for trunk/JavaScriptCore/kjs/object.h
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/object.h
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries 3 4 * Copyright (C) 1999-2001 Harri Porten ([email protected]) 5 * Copyright (C) 2001 Peter Kelly ([email protected]) 4 6 * 5 7 * This library is free software; you can redistribute it and/or … … 17 19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 20 * Boston, MA 02111-1307, USA. 21 * 19 22 */ 23 20 24 21 25 #ifndef _KJS_OBJECT_H_ 22 26 #define _KJS_OBJECT_H_ 23 27 24 #include <stdlib.h> 25 26 #include "ustring.h" 27 28 /** 29 * @short Main namespace 30 */ 28 // Objects 29 30 #include "value.h" 31 #include "types.h" 32 31 33 namespace KJS { 32 34 35 class ObjectImpPrivate; 36 class PropertyMap; 37 class HashTable; 38 class HashEntry; 39 class ListImp; 40 41 // ECMA 262-3 8.6.1 42 // Attributes (only applicable to the Object type) 43 enum Attribute { None = 0, 44 ReadOnly = 1 << 1, // property can be only read, not written 45 DontEnum = 1 << 2, // property doesn't appear in (for .. in ..) 46 DontDelete = 1 << 3, // property can't be deleted 47 Internal = 1 << 4, // an internal property, set to by pass checks 48 Function = 1 << 5 }; // property is a function - only used by static hashtables 49 33 50 /** 34 * Types of classes derived from KJSO51 * Class Information 35 52 */ 36 enum Type { // main types 37 AbstractType = 1, 38 UndefinedType, 39 NullType, 40 BooleanType, 41 NumberType, 42 StringType, 43 ObjectType, 44 HostType, 45 ReferenceType, 46 CompletionType, 47 // extended types 48 FunctionType, 49 InternalFunctionType, 50 DeclaredFunctionType, 51 AnonymousFunctionType, 52 ConstructorType, 53 ActivationType 54 }; 55 56 /** 57 * Property attributes. 58 */ 59 enum Attribute { None = 0, 60 ReadOnly = 1 << 1, 61 DontEnum = 1 << 2, 62 DontDelete = 1 << 3, 63 Internal = 1 << 4 }; 64 65 /** 66 * Types of classes derived from @ref Object. 67 */ 68 enum Class { UndefClass, 69 ArrayClass, 70 StringClass, 71 BooleanClass, 72 NumberClass, 73 ObjectClass, 74 DateClass, 75 RegExpClass, 76 ErrorClass, 77 FunctionClass }; 78 79 /** 80 * Completion types. 81 */ 82 enum Compl { Normal, Break, Continue, ReturnValue, Throw }; 83 84 /** 85 * Error codes. 86 */ 87 enum ErrorType { NoError = 0, 88 GeneralError, 89 EvalError, 90 RangeError, 91 ReferenceError, 92 SyntaxError, 93 TypeError, 94 URIError }; 95 96 extern const double NaN; 97 extern const double Inf; 98 99 // forward declarations 100 class Imp; 101 class Boolean; 102 class Number; 103 class String; 104 class Object; 105 struct Property; 106 class PropList; 107 class List; 108 109 /** 110 * @short Type information. 111 */ 112 struct TypeInfo { 113 /** 114 * A string denoting the type name. Example: "Number". 115 */ 116 const char *name; 117 /** 118 * One of the @ref KJS::Type enums. 119 */ 120 Type type; 121 /** 122 * Pointer to the type information of the base class. 123 * NULL if there is none. 124 */ 125 const TypeInfo *base; 126 /** 127 * Additional specifier for your own use. 128 */ 129 int extra; 130 /** 131 * Reserved for future extensions (internal). 53 struct ClassInfo { 54 /** 55 * A string denoting the class name. Example: "Window". 56 */ 57 const char* className; 58 /** 59 * Pointer to the class information of the base class. 60 * 0L if there is none. 61 */ 62 const ClassInfo *parentClass; 63 /** 64 * Static hash-table of properties. 65 */ 66 const HashTable *propHashTable; 67 /** 68 * Reserved for future extension. 132 69 */ 133 70 void *dummy; … … 135 72 136 73 /** 137 * @short Main base class for every KJS object.74 * Represents an Object. This is a wrapper for ObjectImp 138 75 */ 139 class KJSO { 140 friend class ElementNode; 76 class Object : public Value { 141 77 public: 142 /** 143 * Constructor. 144 */ 145 KJSO(); 146 /** 147 * @internal 148 */ 149 KJSO(Imp *d); 150 /** 151 * Copy constructor. 152 */ 153 KJSO(const KJSO &); 154 /* 155 * Assignment operator 156 */ 157 KJSO& operator=(const KJSO &); 158 /** 159 * Destructor. 160 */ 161 virtual ~KJSO(); 162 /** 163 * @return True if this object is null, i.e. if there is no data attached 164 * to this object. Don't confuse this with the Null object. 165 */ 166 bool isNull() const; 167 /** 168 * @return True if this objects is of any other value than Undefined. 169 */ 170 bool isDefined() const; 171 /** 172 * @return the type of the object. One of the @ref KJS::Type enums. 173 */ 78 Object(); 79 explicit Object(ObjectImp *v); 80 Object(const Object &v); 81 virtual ~Object(); 82 83 Object& operator=(const Object &v); 84 85 virtual const ClassInfo *classInfo() const; 86 bool inherits(const ClassInfo *cinfo) const; 87 88 /** 89 * Converts a Value into an Object. If the value's type is not ObjectType, 90 * a null object will be returned (i.e. one with it's internal pointer set 91 * to 0). If you do not know for sure whether the value is of type 92 * ObjectType, you should check the @ref isNull() methods afterwards before 93 * calling any methods on the Object. 94 * 95 * @return The value converted to an object 96 */ 97 static Object dynamicCast(const Value &v); 98 99 /** 100 * Returns the prototype of this object. Note that this is not the same as 101 * the "prototype" property. 102 * 103 * See ECMA 8.6.2 104 * 105 * @return The object's prototype 106 */ 107 Value prototype() const; 108 109 /** 110 * Returns the class name of the object 111 * 112 * See ECMA 8.6.2 113 * 114 * @return The object's class name 115 */ 116 UString className() const; 117 118 /** 119 * Retrieves the specified property from the object. If neither the object 120 * or any other object in it's prototype chain have the property, this 121 * function will return Undefined. 122 * 123 * See ECMA 8.6.2.1 124 * 125 * @param exec The current execution state 126 * @param propertyName The name of the property to retrieve 127 * 128 * @return The specified property, or Undefined 129 */ 130 Value get(ExecState *exec, const UString &propertyName) const; 131 132 /** 133 * Sets the specified property. 134 * 135 * See ECMA 8.6.2.2 136 * 137 * @param exec The current execution state 138 * @param propertyName The name of the property to set 139 * @param propertyValue The value to set 140 */ 141 void put(ExecState *exec, const UString &propertyName, 142 const Value &value, int attr = None); 143 144 /** 145 * Used to check whether or not a particular property is allowed to be set 146 * on an object 147 * 148 * See ECMA 8.6.2.3 149 * 150 * @param exec The current execution state 151 * @param propertyName The name of the property 152 * @return true if the property can be set, otherwise false 153 */ 154 bool canPut(ExecState *exec, const UString &propertyName) const; 155 156 /** 157 * Checks to see whether the object (or any object in it's prototype chain) 158 * has a property with the specified name. 159 * 160 * See ECMA 8.6.2.4 161 * 162 * @param exec The current execution state 163 * @param propertyName The name of the property to check for 164 * @return true if the object has the property, otherwise false 165 */ 166 bool hasProperty(ExecState *exec, const UString &propertyName, 167 bool recursive = true) const; 168 169 /** 170 * Removes the specified property from the object. 171 * 172 * See ECMA 8.6.2.5 173 * 174 * @param exec The current execution state 175 * @param propertyName The name of the property to delete 176 * @return true if the property was successfully deleted or did not 177 * exist on the object. false if deleting the specified property is not 178 * allowed. 179 */ 180 bool deleteProperty(ExecState *exec, const UString &propertyName); 181 182 /** 183 * Converts the object into a primitive value. The value return may differ 184 * depending on the supplied hint 185 * 186 * See ECMA 8.6.2.6 187 * 188 * @param exec The current execution state 189 * @param hint The desired primitive type to convert to 190 * @return A primitive value converted from the objetc. Note that the 191 * type of primitive value returned may not be the same as the requested 192 * hint. 193 */ 194 Value defaultValue(ExecState *exec, Type hint) const; 195 196 /** 197 * Whether or not the object implements the construct() method. If this 198 * returns false you should not call the construct() method on this 199 * object (typically, an assertion will fail to indicate this). 200 * 201 * @return true if this object implements the construct() method, otherwise 202 * false 203 */ 204 bool implementsConstruct() const; 205 206 /** 207 * Creates a new object based on this object. Typically this means the 208 * following: 209 * 1. A new object is created 210 * 2. The prototype of the new object is set to the value of this object's 211 * "prototype" property 212 * 3. The call() method of this object is called, with the new object 213 * passed as the this value 214 * 4. The new object is returned 215 * 216 * In some cases, Host objects may differ from these semantics, although 217 * this is discouraged. 218 * 219 * If an error occurs during construction, the execution state's exception 220 * will be set. This can be tested for with @ref ExecState::hadException(). 221 * Under some circumstances, the exception object may also be returned. 222 * 223 * Note: This function should not be called if implementsConstruct() returns 224 * false, in which case it will result in an assertion failure. 225 * 226 * @param exec The current execution state 227 * @param args The arguments to be passed to call() once the new object has 228 * been created 229 * @return The newly created & initialized object 230 */ 231 Object construct(ExecState *exec, const List &args); 232 233 /** 234 * Whether or not the object implements the call() method. If this returns 235 * false you should not call the call() method on this object (typically, 236 * an assertion will fail to indicate this). 237 * 238 * @return true if this object implements the call() method, otherwise 239 * false 240 */ 241 bool implementsCall() const; 242 243 244 /** 245 * Calls this object as if it is a function. 246 * 247 * Note: This function should not be called if implementsCall() returns 248 * false, in which case it will result in an assertion failure. 249 * 250 * See ECMA 8.6.2.3 251 * 252 * @param exec The current execution state 253 * @param thisObj The obj to be used as "this" within function execution. 254 * Note that in most cases this will be different from the C++ "this" 255 * object. For example, if the ECMAScript code "window.location.toString()" 256 * is executed, call() will be invoked on the C++ object which implements 257 * the toString method, with the thisObj being window.location 258 * @param args List of arguments to be passed to the function 259 * @return The return value from the function 260 */ 261 Value call(ExecState *exec, Object &thisObj, const List &args); 262 263 /** 264 * Whether or not the object implements the hasInstance() method. If this 265 * returns false you should not call the hasInstance() method on this 266 * object (typically, an assertion will fail to indicate this). 267 * 268 * @return true if this object implements the hasInstance() method, 269 * otherwise false 270 */ 271 bool implementsHasInstance() const; 272 273 /** 274 * Checks whether value delegates behaviour to this object. Used by the 275 * instanceof operator. 276 * 277 * @param exec The current execution state 278 * @param value The value to check 279 * @return true if value delegates behaviour to this object, otherwise 280 * false 281 */ 282 Boolean hasInstance(ExecState *exec, const Value &value); 283 284 /** 285 * Returns the scope of this object. This is used when execution declared 286 * functions - the execution context for the function is initialized with 287 * extra object in it's scope. An example of this is functions declared 288 * inside other functions: 289 * 290 * function f() { 291 * 292 * function b() { 293 * return prototype; 294 * } 295 * 296 * var x = 4; 297 * // do some stuff 298 * } 299 * f.prototype = new String(); 300 * 301 * When the function f.b is executed, its scope will include properties of 302 * f. So in the example above the return value of f.b() would be the new 303 * String object that was assigned to f.prototype. 304 * 305 * @param exec The current execution state 306 * @return The function's scope 307 */ 308 const List scope() const; 309 void setScope(const List &s); 310 311 /** 312 * Returns a List of References to all the properties of the object. Used 313 * in "for x in y" statements. The list is created new, so it can be freely 314 * modified without affecting the object's properties. It should be deleted 315 * by the caller. 316 * 317 * Subclasses can override this method in ObjectImpl to provide the 318 * appearance of 319 * having extra properties other than those set specifically with put(). 320 * 321 * @param exec The current execution state 322 * @param recursive Whether or not properties in the object's prototype 323 * chain should be 324 * included in the list. 325 * @return A List of References to properties of the object. 326 **/ 327 List propList(ExecState *exec, bool recursive = true); 328 329 /** 330 * Returns the internal value of the object. This is used for objects such 331 * as String and Boolean which are wrappers for native types. The interal 332 * value is the actual value represented by the wrapper objects. 333 * 334 * @see ECMA 8.6.2 335 * @return The internal value of the object 336 */ 337 Value internalValue() const; 338 339 /** 340 * Sets the internal value of the object 341 * 342 * @see internalValue() 343 * 344 * @param v The new internal value 345 */ 346 void setInternalValue(const Value &v); 347 }; 348 349 class ObjectImp : public ValueImp { 350 public: 351 /** 352 * Creates a new ObjectImp with the specified prototype 353 * 354 * @param proto The prototype 355 */ 356 ObjectImp(const Object &proto); 357 358 /** 359 * Creates a new ObjectImp with a prototype of Null() 360 * (that is, the ECMAScript "null" value, not a null Object). 361 * 362 */ 363 ObjectImp(); 364 365 virtual ~ObjectImp(); 366 367 virtual void mark(); 368 174 369 Type type() const; 175 /** 176 * Check whether object is of a certain type 177 * @param t type to check for 178 */ 179 bool isA(Type t) const { return (type() == t); } 180 /** 181 * Check whether object is of a certain type. Allows checking of 182 * host objects, too. 183 * @param type name (Number, Boolean etc.) 184 */ 185 bool isA(const char *s) const; 186 /** 187 * Use this method when checking for objects. It's safer than checking 188 * for a single object type with @ref isA(). 189 */ 190 bool isObject() const; 191 /** 192 * Examine the inheritance structure of this object. 193 * @param t Name of the base class. 194 * @return True if object is of type t or a derived from such a type. 195 */ 196 bool derivedFrom(const char *s) const; 197 198 /** 199 * @return Conversion to primitive type (Undefined, Boolean, Number 200 * or String) 201 * @param preferred Optional hint. Either StringType or NumberType. 202 */ 203 KJSO toPrimitive(Type preferred = UndefinedType) const; // ECMA 9.1 204 /** 205 * @return Conversion to Boolean type. 206 */ 207 Boolean toBoolean() const; // ECMA 9.2 208 /** 209 * @return Conversion to Number type. 210 */ 211 Number toNumber() const; // ECMA 9.3 212 /** 213 * @return Conversion to double. 0.0 if conversion failed. 214 */ 215 double round() const; 216 /** 217 * @return Conversion to Number type containing an integer value. 218 */ 219 Number toInteger() const; // ECMA 9.4 220 /** 221 * @return Conversion to signed integer value. 222 */ 223 int toInt32() const; // ECMA 9.5 224 /** 225 * @return Conversion to unsigned integer value. 226 */ 227 unsigned int toUInt32() const; // ECMA 9.6 228 /** 229 * @return Conversion to unsigned short value. 230 */ 231 unsigned short toUInt16() const; // ECMA 9.7 232 /** 233 * @return Conversion to String type. 234 */ 235 String toString() const; // ECMA 9.8 236 /** 237 * @return Conversion to Object type. 238 */ 239 Object toObject() const; // ECMA 9.9 240 241 // Properties 242 /** 243 * Set the internal [[Prototype]] property of this object. 244 * @param p A prototype object. 245 */ 246 void setPrototype(const KJSO &p); 247 /** 248 * Set the "prototype" property of this object. Different from 249 * the internal [[Prototype]] property. 250 * @param p A prototype object. 251 */ 252 void setPrototypeProperty(const KJSO &p); 253 /** 254 * @return The internal [[prototype]] property. 255 */ 256 KJSO prototype() const; 257 /** 258 * The internal [[Get]] method. 259 * @return The value of property p. 260 */ 261 KJSO get(const UString &p) const; 262 /** 263 * The internal [[HasProperty]] method. 264 * @param p Property name. 265 * @param recursive Indicates whether prototypes are searched as well. 266 * @return Boolean value indicating whether the object already has a 267 * member with the given name p. 268 */ 269 bool hasProperty(const UString &p, bool recursive = true) const; 270 /** 271 * The internal [[Put]] method. Sets the specified property to the value v. 272 * @param p Property name. 273 * @param v Value. 274 */ 275 void put(const UString &p, const KJSO& v); 276 /** 277 * The internal [[CanPut]] method. 278 * @param p Property name. 279 * @return A boolean value indicating whether a [[Put]] operation with 280 * p succeed. 281 */ 282 bool canPut(const UString &p) const; 283 /** 284 * The internal [[Delete]] method. Removes the specified property from 285 * the object. 286 * @param p Property name. 287 * @return True if the property was deleted successfully or didn't exist 288 * in the first place. False if the DontDelete attribute was set. 289 */ 290 bool deleteProperty(const UString &p); 291 /** 292 * Same as above put() method except the additional attribute. Right now, 293 * this only works with native types as Host Objects don't reimplement 294 * this method. 295 * @param attr One of @ref KJS::Attribute. 296 */ 297 void put(const UString &p, const KJSO& v, int attr); 298 /** 299 * Convenience function for adding a Number property constructed from 300 * a double value. 301 */ 302 void put(const UString &p, double d, int attr = None); 303 /** 304 * Convenience function for adding a Number property constructed from 305 * an integer value. 306 */ 307 void put(const UString &p, int i, int attr = None); 308 /** 309 * Convenience function for adding a Number property constructed from 310 * an unsigned integer value. 311 */ 312 void put(const UString &p, unsigned int u, int attr = None); 313 314 /** 315 * Reference method. 316 * @return Reference base if object is a reference. Throws 317 * a ReferenceError otherwise. 318 */ 319 KJSO getBase() const; 320 /** 321 * Reference method. 322 * @return Property name of a reference. Null string if object is not 323 * a reference. 324 */ 325 UString getPropertyName() const; 326 /** 327 * Reference method. 328 * @return Referenced value. This object if no reference. 329 */ 330 KJSO getValue() const; 331 KJSO getValue(); /* TODO: remove in next version */ 332 /** 333 * Reference method. Set referenced value to v. 334 */ 335 ErrorType putValue(const KJSO& v); 336 337 /** 338 * @return True if object supports @ref executeCall() method. That's the 339 * case for all objects derived from FunctionType. 340 */ 341 bool implementsCall() const; 342 /** 343 * Execute function implemented via the @ref Function::execute() method. 344 * 345 * Note: check availability via @ref implementsCall() beforehand. 346 * @param thisV Object serving as the 'this' value. 347 * @param args Pointer to the list of arguments or null. 348 * @return Result of the function call. 349 */ 350 KJSO executeCall(const KJSO &thisV, const List *args); 351 KJSO executeCall(const KJSO &thisV, const List *args, const List *extraScope) const; 352 353 /** 354 * Set this object's constructor. 355 */ 356 void setConstructor(KJSO c); 357 358 /** 359 * @return A Pointer to the internal implementation. 360 */ 361 Imp *imp() const { return rep; } 362 363 #ifdef KJS_DEBUG_MEM 364 /** 365 * @internal 366 */ 367 static int count; 368 #endif 369 protected: 370 /** 371 * Pointer to the internal implementation. 372 */ 373 Imp *rep; 374 370 371 /** 372 * A pointer to a ClassInfo struct for this class. This provides a basic 373 * facility for run-time type information, and can be used to check an 374 * object's class an inheritance (see @ref inherits()). This should 375 * always return a statically declared pointer, or 0 to indicate that 376 * there is no class information. 377 * 378 * This is primarily useful if you have application-defined classes that you 379 * wish to check against for casting purposes. 380 * 381 * For example, to specify the class info for classes FooImp and BarImp, 382 * where FooImp inherits from BarImp, you would add the following in your 383 * class declarations: 384 * 385 * class BarImp : public ObjectImp { 386 * virtual const ClassInfo *classInfo() const { return &info; } 387 * static const ClassInfo info; 388 * // ... 389 * }; 390 * 391 * class FooImp : public ObjectImp { 392 * virtual const ClassInfo *classInfo() const { return &info; } 393 * static const ClassInfo info; 394 * // ... 395 * }; 396 * 397 * And in your source file: 398 * 399 * const ClassInfo BarImp::info = {0, 0, 0}; // no parent class 400 * const ClassInfo FooImp::info = {&BarImp::info, 0, 0}; 401 * 402 * @see inherits() 403 */ 404 virtual const ClassInfo *classInfo() const; 405 406 /** 407 * Checks whether this object inherits from the class with the specified 408 * classInfo() pointer. This requires that both this class and the other 409 * class return a non-NULL pointer for their classInfo() methods (otherwise 410 * it will return false). 411 * 412 * For example, for two ObjectImp pointers obj1 and obj2, you can check 413 * if obj1's class inherits from obj2's class using the following: 414 * 415 * if (obj1->inherits(obj2->classInfo())) { 416 * // ... 417 * } 418 * 419 * If you have a handle to a statically declared ClassInfo, such as in the 420 * @ref classInfo() example, you can check for inheritance without needing 421 * an instance of the other class: 422 * 423 * if (obj1->inherits(FooImp::info)) { 424 * // ... 425 * } 426 * 427 * @param cinfo The ClassInfo pointer for the class you want to check 428 * inheritance against. 429 * @return true if this object's class inherits from class with the 430 * ClassInfo pointer specified in cinfo 431 */ 432 bool inherits(const ClassInfo *cinfo) const; 433 434 // internal properties (ECMA 262-3 8.6.2) 435 436 /** 437 * Implementation of the [[Prototype]] internal property (implemented by 438 * all Objects) 439 * 440 * @see Object::prototype() 441 */ 442 Value prototype() const; 443 void setPrototype(const Value &proto); 444 445 /** 446 * Implementation of the [[Class]] internal property (implemented by all 447 * Objects) 448 * 449 * The default implementation uses classInfo(). 450 * You should either implement @ref classInfo(), or 451 * if you simply need a classname, you can reimplement @ref className() 452 * instead. 453 * 454 * @see Object::className() 455 */ 456 virtual UString className() const; 457 458 /** 459 * Implementation of the [[Get]] internal property (implemented by all 460 * Objects) 461 * 462 * @see Object::get() 463 */ 464 // [[Get]] - must be implemented by all Objects 465 virtual Value get(ExecState *exec, const UString &propertyName) const; 466 467 /** 468 * Implementation of the [[Put]] internal property (implemented by all 469 * Objects) 470 * 471 * @see Object::put() 472 */ 473 virtual void put(ExecState *exec, const UString &propertyName, 474 const Value &value, int attr = None); 475 476 /** 477 * Implementation of the [[CanPut]] internal property (implemented by all 478 * Objects) 479 * 480 * @see Object::canPut() 481 */ 482 virtual bool canPut(ExecState *exec, const UString &propertyName) const; 483 484 /** 485 * Implementation of the [[HasProperty]] internal property (implemented by 486 * all Objects) 487 * 488 * @see Object::hasProperty() 489 */ 490 virtual bool hasProperty(ExecState *exec, const UString &propertyName, 491 bool recursive = true) const; 492 493 /** 494 * Implementation of the [[Delete]] internal property (implemented by all 495 * Objects) 496 * 497 * @see Object::deleteProperty() 498 */ 499 virtual bool deleteProperty(ExecState *exec, 500 const UString &propertyName); 501 502 /** 503 * Remove all properties from this object. 504 * This doesn't take DontDelete into account, and isn't in the ECMA spec. 505 * It's simply a quick way to remove everything before destroying. 506 */ 507 void deleteAllProperties( ExecState * ); 508 509 /** 510 * Implementation of the [[DefaultValue]] internal property (implemented by 511 * all Objects) 512 * 513 * @see Object::defaultValue() 514 */ 515 virtual Value defaultValue(ExecState *exec, Type hint) const; 516 517 virtual bool implementsConstruct() const; 518 /** 519 * Implementation of the [[Construct]] internal property 520 * 521 * @see Object::construct() 522 */ 523 virtual Object construct(ExecState *exec, const List &args); 524 525 virtual bool implementsCall() const; 526 /** 527 * Implementation of the [[Call]] internal property 528 * 529 * @see Object::call() 530 */ 531 virtual Value call(ExecState *exec, Object &thisObj, 532 const List &args); 533 534 virtual bool implementsHasInstance() const; 535 /** 536 * Implementation of the [[HasInstance]] internal property 537 * 538 * @see Object::hasInstance() 539 */ 540 virtual Boolean hasInstance(ExecState *exec, const Value &value); 541 542 /** 543 * Implementation of the [[Scope]] internal property 544 * 545 * @see Object::scope() 546 */ 547 const List scope() const; 548 void setScope(const List &s); 549 550 List propList(ExecState *exec, bool recursive = true); 551 552 Value internalValue() const; 553 void setInternalValue(const Value &v); 554 555 Value toPrimitive(ExecState *exec, 556 Type preferredType = UnspecifiedType) const; 557 bool toBoolean(ExecState *exec) const; 558 double toNumber(ExecState *exec) const; 559 int toInteger(ExecState *exec) const; 560 int toInt32(ExecState *exec) const; 561 unsigned int toUInt32(ExecState *exec) const; 562 unsigned short toUInt16(ExecState *exec) const; 563 UString toString(ExecState *exec) const; 564 Object toObject(ExecState *exec) const; 565 566 ValueImp* getDirect(const UString& propertyName) const; 375 567 private: 376 void putArrayElement(const UString &p, const KJSO &v); 377 }; // end of KJSO 568 const HashEntry* findPropertyHashEntry( const UString& propertyName ) const; 569 ObjectImpPrivate *_od; 570 PropertyMap *_prop; 571 ValueImp *_proto; 572 ValueImp *_internalValue; 573 ListImp *_scope; 574 }; 378 575 379 576 /** 380 * @short Base for all implementation classes. 577 * Types of Native Errors available. For custom errors, GeneralError 578 * should be used. 381 579 */ 382 class Imp { 383 friend class KJSO; 384 friend class Collector; 385 friend class ForInNode; 386 friend class Debugger; 387 public: 388 Imp(); 389 public: 390 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; // ECMA 9.1 391 virtual Boolean toBoolean() const; // ECMA 9.2 392 virtual Number toNumber() const; // ECMA 9.3 393 virtual String toString() const; // ECMA 9.8 394 virtual Object toObject() const; // ECMA 9.9 395 396 // properties 397 virtual KJSO get(const UString &p) const; 398 virtual bool hasProperty(const UString &p, bool recursive = true) const; 399 virtual void put(const UString &p, const KJSO& v); 400 void put(const UString &p, const KJSO& v, int attr); 401 virtual bool canPut(const UString &p) const; 402 virtual bool deleteProperty(const UString &p); 403 virtual KJSO defaultValue(Type hint) const; 404 405 bool implementsCall() const; 406 407 /** 408 * @internal Reserved for mark & sweep garbage collection 409 */ 410 virtual void mark(Imp *imp = 0L); 411 bool marked() const; 412 413 Type type() const { return typeInfo()->type; } 414 /** 415 * @return The TypeInfo struct describing this object. 416 */ 417 virtual const TypeInfo* typeInfo() const { return &info; } 418 419 void setPrototype(const KJSO& p); 420 Imp* prototype() const { return proto; } 421 void setPrototypeProperty(const KJSO &p); 422 void setConstructor(const KJSO& c); 423 424 void* operator new(size_t); 425 void operator delete(void*); 426 /** 427 * @deprecated 428 */ 429 void operator delete(void*, size_t); 430 431 #ifdef KJS_DEBUG_MEM 432 /** 433 * @internal 434 */ 435 static int count; 436 #endif 437 protected: 438 virtual ~Imp(); 439 private: 440 Imp(const Imp&); 441 Imp& operator=(const Imp&); 442 void putArrayElement(const UString &p, const KJSO& v); 443 444 /** 445 * Get the property names for this object. To be used by for .. in loops 446 * @return The (pointer to the) first element of a PropList, to be deleted 447 * by the caller, or 0 if there are no enumerable properties 448 */ 449 PropList *propList(PropList *first = 0L, PropList *last = 0L, 450 bool recursive = true) const; 451 452 public: 453 // reference counting mechanism 454 inline Imp* ref() { refcount++; return this; } 455 inline bool deref() { return (!--refcount); } 456 unsigned int refcount; 457 458 private: 459 Property *prop; 460 Imp *proto; 461 static const TypeInfo info; 462 463 // reserved for memory managment - currently used as flags for garbage collection 464 // (prev != 0) = marked, (next != 0) = created, (next != this) = created and gc allowed 465 Imp *prev, *next; 466 // for future extensions 467 class ImpInternal; 468 ImpInternal *internal; 469 470 void setMarked(bool m); 471 void setGcAllowed(bool a); 472 bool gcAllowed() const; 473 void setCreated(bool c); 474 bool created() const; 475 }; 476 477 /** 478 * @short General implementation class for Objects 479 */ 480 class ObjectImp : public Imp { 481 friend class Object; 482 public: 483 ObjectImp(Class c); 484 ObjectImp(Class c, const KJSO &v); 485 ObjectImp(Class c, const KJSO &v, const KJSO &p); 486 virtual ~ObjectImp(); 487 virtual KJSO toPrimitive(Type preferred = UndefinedType) const; 488 virtual Boolean toBoolean() const; 489 virtual Number toNumber() const; 490 virtual String toString() const; 491 virtual Object toObject() const; 492 493 virtual const TypeInfo* typeInfo() const { return &info; } 494 static const TypeInfo info; 495 /** 496 * @internal Reimplemenation of @ref Imp::mark(). 497 */ 498 virtual void mark(Imp *imp = 0L); 499 private: 500 Class cl; 501 Imp *val; 502 }; 503 504 /** 505 * @short Object class encapsulating an internal value. 506 */ 507 class Object : public KJSO { 508 public: 509 Object(Imp *d); 510 Object(Class c = UndefClass); 511 Object(Class c, const KJSO& v); 512 Object(Class c, const KJSO& v, const Object& p); 513 virtual ~Object(); 514 void setClass(Class c); 515 Class getClass() const; 516 void setInternalValue(const KJSO& v); 517 KJSO internalValue(); 518 static Object create(Class c); 519 static Object create(Class c, const KJSO& val); 520 static Object create(Class c, const KJSO& val, const Object &p); 521 static Object dynamicCast(const KJSO &obj); 522 }; 523 524 /** 525 * @short Implementation base class for Host Objects. 526 */ 527 class HostImp : public Imp { 528 public: 529 HostImp(); 530 virtual ~HostImp(); 531 virtual const TypeInfo* typeInfo() const { return &info; } 532 533 virtual Boolean toBoolean() const; 534 virtual String toString() const; 535 536 static const TypeInfo info; 537 }; 538 539 class KJScriptImp; 540 /** 541 * The Global object represents the global namespace. It holds the native 542 * objects like String and functions like eval(). 543 * 544 * It also serves as a container for variables created by the user, i.e. 545 * the statement 546 * <pre> 547 * var i = 2; 548 * </pre> 549 * will basically perform a Global::current().put("i", Number(2)); operation. 550 * 551 * @short Unique global object containing initial native properties. 552 */ 553 class Global : public Object { 554 friend class KJScriptImp; 555 public: 556 /** 557 * Constructs a Global object. This is done by the interpreter once and 558 * there should be no need to create an instance on your own. Usually, 559 * you'll just want to access the current instance. 560 * For example like this: 561 * <pre> 562 * Global global(Global::current()); 563 * KJSO proto = global.objectPrototype(); 564 * </pre> 565 */ 566 Global(); 567 /** 568 * Destruct the Global object. 569 */ 570 virtual ~Global(); 571 /** 572 * @return A reference to the Global object belonging to the current 573 * interpreter instance. 574 */ 575 static Global current(); 576 /** 577 * @return A handle to Object.prototype. 578 */ 579 KJSO objectPrototype() const; 580 /** 581 * @return A handle to Function.prototype. 582 */ 583 KJSO functionPrototype() const; 584 /** 585 * Set a filter object that will intercept all put() and get() calls 586 * to the global object. If this object returns Undefined on get() the 587 * request will be passed on the global object. 588 * @deprecated 589 */ 590 void setFilter(const KJSO &f); 591 /** 592 * Return a handle to the filter object (see @ref setFilter()). 593 * Null if no filter has been installed. 594 * @deprecated 595 */ 596 KJSO filter() const; 597 /** 598 * Returns the extra user data set for this global object. Null by default. 599 * Typical usage if you need to query any info related to the currently 600 * running interpreter: 601 * 602 * MyMainWindow *m = (MyMainWindow*)Global::current().extra(); 603 */ 604 void *extra() const; 605 /** 606 * Set the extra user data for this global object. It's not used by the 607 * interpreter itself and can therefore be used to bind arbitrary data 608 * to each interpreter instance. 609 */ 610 void setExtra(void *e); 611 private: 612 Global(void *); 613 void init(); 614 void clear(); 615 }; 580 enum ErrorType { GeneralError = 0, 581 EvalError = 1, 582 RangeError = 2, 583 ReferenceError = 3, 584 SyntaxError = 4, 585 TypeError = 5, 586 URIError = 6}; 616 587 617 588 /** … … 621 592 public: 622 593 /** 623 * Factory method for error objects. The error will be registered globally 624 * and the execution will continue as if a "throw" statement was 625 * encountered. 626 * @param e Type of error. 627 * @param m Optional error message. 628 * @param l Optional line number. 629 */ 630 static KJSO create(ErrorType e, const char *m = 0, int l = -1); 631 /** 632 * Same as above except the different return type (which is not casted 633 * here). 634 */ 635 static Object createObject(ErrorType e, const char *m = 0, int l = -1); 594 * Factory method for error objects. 595 * 596 * @param exec The current execution state 597 * @param errtype Type of error. 598 * @param message Optional error message. 599 * @param lineno Optional line number. 600 * @param lineno Optional source id. 601 */ 602 static Object create(ExecState *exec, ErrorType errtype = GeneralError, 603 const char *message = 0, int lineno = -1, 604 int sourceId = -1); 605 606 /** 607 * Array of error names corresponding to @ref ErrorType 608 */ 609 static const char * const * const errorNames; 636 610 }; 637 611 638 612 }; // namespace 639 613 640 #endif 614 #endif // _KJS_OBJECT_H_
Note:
See TracChangeset
for help on using the changeset viewer.