Changeset 12728 in webkit for trunk/JavaScriptCore/kjs/value.h
- Timestamp:
- Feb 9, 2006, 10:42:01 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/value.h
r12317 r12728 34 34 #endif 35 35 36 #include <assert.h> 36 #include "JSImmediate.h" 37 #include "JSType.h" 38 #include "kxmlcore/Assertions.h" 39 #include "ustring.h" 40 37 41 #include <stdlib.h> // for size_t 38 #include "simple_number.h"39 #include "ustring.h"40 42 41 43 namespace KJS { … … 44 46 class ExecState; 45 47 class JSObject; 46 47 /**48 * Primitive types49 */50 enum Type {51 UnspecifiedType = 0,52 UndefinedType = 1,53 NullType = 2,54 BooleanType = 3,55 StringType = 4,56 NumberType = 5,57 ObjectType = 6,58 GetterSetterType = 759 };60 48 61 49 /** … … 77 65 public: 78 66 // Querying the type. 79 Type type() const;67 JSType type() const; 80 68 bool isUndefined() const; 81 69 bool isNull() const; … … 89 77 // Extracting the value. 90 78 bool getBoolean(bool&) const; 79 bool getBoolean() const; // false if not a boolean 91 80 bool getNumber(double&) const; 92 81 double getNumber() const; // NaN if not a number … … 100 89 101 90 // Basic conversions. 102 JSValue *toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const;91 JSValue *toPrimitive(ExecState *exec, JSType preferredType = UnspecifiedType) const; 103 92 bool toBoolean(ExecState *exec) const; 104 93 double toNumber(ExecState *exec) const; … … 128 117 class JSCell : public JSValue { 129 118 friend class Collector; 130 friend class UndefinedImp;131 friend class NullImp;132 friend class BooleanImp;133 119 friend class NumberImp; 134 120 friend class StringImp; … … 140 126 public: 141 127 // Querying the type. 142 virtual Type type() const = 0; 143 bool isBoolean() const; 128 virtual JSType type() const = 0; 144 129 bool isNumber() const; 145 130 bool isString() const; … … 148 133 149 134 // Extracting the value. 150 bool getBoolean(bool&) const;151 135 bool getNumber(double&) const; 152 136 double getNumber() const; // NaN if not a number … … 160 144 161 145 // Basic conversions. 162 virtual JSValue *toPrimitive(ExecState *exec, Type preferredType = UnspecifiedType) const = 0;146 virtual JSValue *toPrimitive(ExecState *exec, JSType preferredType = UnspecifiedType) const = 0; 163 147 virtual bool toBoolean(ExecState *exec) const = 0; 164 148 virtual double toNumber(ExecState *exec) const = 0; … … 175 159 }; 176 160 177 JSCell *jsUndefined(); 178 JSCell *jsNull(); 179 180 JSCell *jsBoolean(bool); 181 161 JSValue *jsUndefined(); 162 JSValue *jsNull(); 163 164 JSValue *jsBoolean(bool); 165 166 JSValue *jsNumberCell(double); 182 167 JSValue *jsNumber(double); 183 168 JSValue *jsNaN(); … … 186 171 JSCell *jsString(const char * = ""); // returns empty string if passed 0 187 172 188 extern const double NaN; 189 extern const double Inf; 190 191 class ConstantValues { 192 public: 193 static JSCell *undefined; 194 static JSCell *null; 195 static JSCell *jsFalse; 196 static JSCell *jsTrue; 197 198 static void initIfNeeded(); 199 static void mark(); 200 }; 201 202 inline JSCell *jsUndefined() 203 { 204 return ConstantValues::undefined; 205 } 206 207 inline JSCell *jsNull() 208 { 209 return ConstantValues::null; 210 } 211 212 inline JSCell *jsBoolean(bool b) 213 { 214 return b ? ConstantValues::jsTrue : ConstantValues::jsFalse; 173 extern double NaN; 174 extern double Inf; 175 176 177 inline JSValue *jsUndefined() 178 { 179 return JSImmediate::undefinedImmediate(); 180 } 181 182 inline JSValue *jsNull() 183 { 184 return JSImmediate::nullImmediate(); 215 185 } 216 186 217 187 inline JSValue *jsNaN() 218 188 { 219 return SimpleNumber::make(NaN); 189 return JSImmediate::NaNImmediate(); 190 } 191 192 inline JSValue *jsBoolean(bool b) 193 { 194 return b ? JSImmediate::trueImmediate() : JSImmediate::falseImmediate(); 195 } 196 197 inline JSValue *jsNumber(double d) 198 { 199 JSValue *v = JSImmediate::fromDouble(d); 200 return v ? v : jsNumberCell(d); 220 201 } 221 202 … … 237 218 } 238 219 239 inline bool JSCell::isBoolean() const240 {241 return type() == BooleanType;242 }243 244 220 inline bool JSCell::isNumber() const 245 221 { … … 269 245 inline JSCell *JSValue::downcast() 270 246 { 271 assert(!SimpleNumber::is(this));247 ASSERT(!JSImmediate::isImmediate(this)); 272 248 return static_cast<JSCell *>(this); 273 249 } … … 275 251 inline const JSCell *JSValue::downcast() const 276 252 { 277 assert(!SimpleNumber::is(this));253 ASSERT(!JSImmediate::isImmediate(this)); 278 254 return static_cast<const JSCell *>(this); 279 255 } … … 291 267 inline bool JSValue::isUndefinedOrNull() const 292 268 { 293 return this == jsUndefined() || this == jsNull();269 return JSImmediate::isUndefinedOrNull(this); 294 270 } 295 271 296 272 inline bool JSValue::isBoolean() const 297 273 { 298 return !SimpleNumber::is(this) && downcast()->isBoolean();274 return JSImmediate::isBoolean(this); 299 275 } 300 276 301 277 inline bool JSValue::isNumber() const 302 278 { 303 return SimpleNumber::is(this) ||downcast()->isNumber();279 return JSImmediate::isNumber(this) || !JSImmediate::isImmediate(this) && downcast()->isNumber(); 304 280 } 305 281 306 282 inline bool JSValue::isString() const 307 283 { 308 return ! SimpleNumber::is(this) && downcast()->isString();284 return !JSImmediate::isImmediate(this) && downcast()->isString(); 309 285 } 310 286 311 287 inline bool JSValue::isObject() const 312 288 { 313 return ! SimpleNumber::is(this) && downcast()->isObject();289 return !JSImmediate::isImmediate(this) && downcast()->isObject(); 314 290 } 315 291 316 292 inline bool JSValue::getBoolean(bool& v) const 317 293 { 318 return !SimpleNumber::is(this) && downcast()->getBoolean(v); 294 if (JSImmediate::isBoolean(this)) { 295 v = JSImmediate::toBoolean(this); 296 return true; 297 } 298 299 return false; 300 } 301 302 inline bool JSValue::getBoolean() const 303 { 304 return JSImmediate::isBoolean(this) ? JSImmediate::toBoolean(this) : false; 319 305 } 320 306 321 307 inline bool JSValue::getNumber(double& v) const 322 308 { 323 if ( SimpleNumber::is(this)) {324 v = SimpleNumber::value(this);309 if (JSImmediate::isImmediate(this)) { 310 v = JSImmediate::toDouble(this); 325 311 return true; 326 312 } … … 330 316 inline double JSValue::getNumber() const 331 317 { 332 return SimpleNumber::is(this) ? SimpleNumber::value(this) : downcast()->getNumber();318 return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : downcast()->getNumber(); 333 319 } 334 320 335 321 inline bool JSValue::getString(UString& s) const 336 322 { 337 return ! SimpleNumber::is(this) && downcast()->getString(s);323 return !JSImmediate::isImmediate(this) && downcast()->getString(s); 338 324 } 339 325 340 326 inline UString JSValue::getString() const 341 327 { 342 return SimpleNumber::is(this) ? UString() : downcast()->getString();328 return JSImmediate::isImmediate(this) ? UString() : downcast()->getString(); 343 329 } 344 330 345 331 inline JSObject *JSValue::getObject() 346 332 { 347 return SimpleNumber::is(this) ? 0 : downcast()->getObject();333 return JSImmediate::isImmediate(this) ? 0 : downcast()->getObject(); 348 334 } 349 335 350 336 inline const JSObject *JSValue::getObject() const 351 337 { 352 return SimpleNumber::is(this) ? 0 : downcast()->getObject();338 return JSImmediate::isImmediate(this) ? 0 : downcast()->getObject(); 353 339 } 354 340 355 341 inline bool JSValue::getUInt32(uint32_t& v) const 356 342 { 357 if ( SimpleNumber::is(this)) {358 double d = SimpleNumber::value(this);343 if (JSImmediate::isImmediate(this)) { 344 double d = JSImmediate::toDouble(this); 359 345 if (!(d >= 0) || d > 0xFFFFFFFFUL) // true for NaN 360 346 return false; … … 367 353 inline void JSValue::mark() 368 354 { 369 if (!SimpleNumber::is(this))370 355 ASSERT(!JSImmediate::isImmediate(this)); // callers should check !marked() before calling mark() 356 downcast()->mark(); 371 357 } 372 358 373 359 inline bool JSValue::marked() const 374 360 { 375 return SimpleNumber::is(this) || downcast()->marked();376 } 377 378 inline Type JSValue::type() const379 { 380 return SimpleNumber::is(this) ? NumberType: downcast()->type();381 } 382 383 inline JSValue *JSValue::toPrimitive(ExecState *exec, Type preferredType) const384 { 385 return SimpleNumber::is(this) ? const_cast<JSValue *>(this) : downcast()->toPrimitive(exec, preferredType);361 return JSImmediate::isImmediate(this) || downcast()->marked(); 362 } 363 364 inline JSType JSValue::type() const 365 { 366 return JSImmediate::isImmediate(this) ? JSImmediate::type(this) : downcast()->type(); 367 } 368 369 inline JSValue *JSValue::toPrimitive(ExecState *exec, JSType preferredType) const 370 { 371 return JSImmediate::isImmediate(this) ? const_cast<JSValue *>(this) : downcast()->toPrimitive(exec, preferredType); 386 372 } 387 373 388 374 inline bool JSValue::toBoolean(ExecState *exec) const 389 375 { 390 if (SimpleNumber::is(this)) { 391 double d = SimpleNumber::value(this); 392 return d < 0 || d > 0; // false for NaN 393 } 394 395 return downcast()->toBoolean(exec); 376 return JSImmediate::isImmediate(this) ? JSImmediate::toBoolean(this) : downcast()->toBoolean(exec); 396 377 } 397 378 398 379 inline double JSValue::toNumber(ExecState *exec) const 399 380 { 400 return SimpleNumber::is(this) ? SimpleNumber::value(this) : downcast()->toNumber(exec);381 return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : downcast()->toNumber(exec); 401 382 } 402 383 403 384 inline UString JSValue::toString(ExecState *exec) const 404 385 { 405 if (SimpleNumber::is(this)) { 406 double d = SimpleNumber::value(this); 407 if (d == 0.0) // +0.0 or -0.0 408 d = 0.0; 409 return UString::from(d); 410 } 411 412 return downcast()->toString(exec); 386 return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : downcast()->toString(exec); 413 387 } 414 388
Note:
See TracChangeset
for help on using the changeset viewer.