Changeset 34921 in webkit for trunk/JavaScriptCore/kjs/JSValue.h


Ignore:
Timestamp:
Jul 1, 2008, 10:32:44 AM (17 years ago)
Author:
[email protected]
Message:

JavaScriptCore:

2008-07-01 Sam Weinig <[email protected]>

Reviewed by Darin Adler.

Split JSCell and JSNumberCell class declarations out of JSValue.h

  • GNUmakefile.am:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • VM/JSPropertyNameIterator.h:
  • kjs/AllInOneFile.cpp:
  • kjs/JSCell.cpp: Copied from JavaScriptCore/kjs/JSValue.cpp.
  • kjs/JSCell.h: Copied from JavaScriptCore/kjs/JSValue.h. (KJS::JSValue::getJSNumber):
  • kjs/JSNumberCell.cpp:
  • kjs/JSNumberCell.h: Copied from JavaScriptCore/kjs/JSValue.h.
  • kjs/JSObject.h:
  • kjs/JSString.cpp: (KJS::jsString): (KJS::jsOwnedString):
  • kjs/JSString.h: (KJS::JSValue::toThisJSString):
  • kjs/JSValue.cpp:
  • kjs/JSValue.h:

WebCore:

2008-07-01 Sam Weinig <[email protected]>

Reviewed by Darin Adler.

Split JSCell and JSNumberCell class declarations out of JSValue.h

  • ForwardingHeaders/kjs/JSNumberCell.h: Added.
  • bindings/scripts/CodeGeneratorJS.pm:
  • bridge/c/c_instance.cpp:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/JSValue.h

    r34821 r34921  
    2828#include "ExecState.h"
    2929#include "JSImmediate.h"
    30 #include "collector.h"
    3130#include "ustring.h"
    3231#include <stddef.h> // for size_t
     
    4039class JSString;
    4140class PropertySlot;
    42 
    4341struct ClassInfo;
    4442struct Instruction;
     
    150148};
    151149
    152 class JSCell : public JSValue {
    153     friend class Heap;
    154     friend class GetterSetter;
    155     friend class JSObject;
    156     friend class JSPropertyNameIterator;
    157     friend class JSValue;
    158     friend class JSNumberCell;
    159     friend class JSString;
    160 private:
    161     JSCell();
    162     virtual ~JSCell();
    163 
    164 public:
    165     // Querying the type.
    166     virtual JSType type() const = 0;
    167     bool isNumber() const;
    168     bool isString() const;
    169     bool isObject() const;
    170     bool isObject(const ClassInfo*) const; // FIXME: Merge with inherits.
    171 
    172     // Extracting the value.
    173     bool getNumber(double&) const;
    174     double getNumber() const; // NaN if not a number
    175     bool getString(UString&) const;
    176     UString getString() const; // null string if not a string
    177     JSObject* getObject(); // NULL if not an object
    178     const JSObject* getObject() const; // NULL if not an object
    179    
    180     virtual CallType getCallData(CallData&);
    181     virtual ConstructType getConstructData(ConstructData&);
    182 
    183     // Extracting integer values.
    184     virtual bool getUInt32(uint32_t&) const;
    185     virtual bool getTruncatedInt32(int32_t&) const;
    186     virtual bool getTruncatedUInt32(uint32_t&) const;
    187 
    188     // Basic conversions.
    189     virtual JSValue* toPrimitive(ExecState*, JSType preferredType = UnspecifiedType) const = 0;
    190     virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*&) = 0;
    191     virtual bool toBoolean(ExecState*) const = 0;
    192     virtual double toNumber(ExecState*) const = 0;
    193     virtual UString toString(ExecState*) const = 0;
    194     virtual JSObject* toObject(ExecState*) const = 0;
    195 
    196     // Garbage collection.
    197     void* operator new(size_t, ExecState*);
    198     virtual void mark();
    199     bool marked() const;
    200 
    201     // Object operations, with the toObject operation included.
    202     virtual const ClassInfo* classInfo() const;
    203     virtual void put(ExecState*, const Identifier& propertyName, JSValue*);
    204     virtual void put(ExecState*, unsigned propertyName, JSValue*);
    205     virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
    206     virtual bool deleteProperty(ExecState*, unsigned propertyName);
    207 
    208     virtual JSObject* toThisObject(ExecState*) const;
    209     virtual UString toThisString(ExecState*) const;
    210     virtual JSString* toThisJSString(ExecState*);
    211     virtual JSValue* getJSNumber();
    212 
    213 private:
    214     // Base implementation, but for non-object classes implements getPropertySlot.
    215     virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
    216     virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
    217 };
    218 
    219 class JSNumberCell : public JSCell {
    220     friend JSValue* jsNumberCell(ExecState*, double);
    221 public:
    222     double value() const { return val; }
    223 
    224     virtual JSType type() const;
    225 
    226     virtual JSValue* toPrimitive(ExecState*, JSType preferred = UnspecifiedType) const;
    227     virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
    228     virtual bool toBoolean(ExecState*) const;
    229     virtual double toNumber(ExecState*) const;
    230     virtual UString toString(ExecState*) const;
    231     virtual JSObject* toObject(ExecState*) const;
    232 
    233     virtual UString toThisString(ExecState*) const;
    234     virtual JSObject* toThisObject(ExecState*) const;
    235     virtual JSValue* getJSNumber();
    236 
    237     void* operator new(size_t size, ExecState* exec)
    238     {
    239 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
    240         return exec->heap()->inlineAllocateNumber(size);
    241 #else
    242         return exec->heap()->allocateNumber(size);
    243 #endif
    244     }
    245 
    246 private:
    247     JSNumberCell(double v)
    248         : val(v)
    249     {
    250     }
    251 
    252     virtual bool getUInt32(uint32_t&) const;
    253     virtual bool getTruncatedInt32(int32_t&) const;
    254     virtual bool getTruncatedUInt32(uint32_t&) const;
    255 
    256     double val;
    257 };
    258 
    259 JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
    260 JSString* jsString(ExecState*, const char* = ""); // returns empty string if passed 0
    261 
    262 // should be used for strings that are owned by an object that will
    263 // likely outlive the JSValue this makes, such as the parse tree or a
    264 // DOM object that contains a UString
    265 JSString* jsOwnedString(ExecState*, const UString&);
    266 
    267 extern const double NaN;
    268 extern const double Inf;
    269 
    270 // Beware marking this function ALWAYS_INLINE: It takes a PIC branch, so
    271 // inlining it may not always be a win.
    272 inline JSValue* jsNumberCell(ExecState* exec, double d)
    273 {
    274     return new (exec) JSNumberCell(d);
    275 }
    276 
    277 inline JSValue* jsNaN(ExecState* exec)
    278 {
    279     return jsNumberCell(exec, NaN);
    280 }
    281 
    282 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, double d)
    283 {
    284     JSValue* v = JSImmediate::from(d);
    285     return v ? v : jsNumberCell(exec, d);
    286 }
    287 
    288 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, int i)
    289 {
    290     JSValue* v = JSImmediate::from(i);
    291     return v ? v : jsNumberCell(exec, i);
    292 }
    293 
    294 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned i)
    295 {
    296     JSValue* v = JSImmediate::from(i);
    297     return v ? v : jsNumberCell(exec, i);
    298 }
    299 
    300 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long i)
    301 {
    302     JSValue* v = JSImmediate::from(i);
    303     return v ? v : jsNumberCell(exec, i);
    304 }
    305 
    306 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long i)
    307 {
    308     JSValue* v = JSImmediate::from(i);
    309     return v ? v : jsNumberCell(exec, i);
    310 }
    311 
    312 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long long i)
    313 {
    314     JSValue* v = JSImmediate::from(i);
    315     return v ? v : jsNumberCell(exec, static_cast<double>(i));
    316 }
    317 
    318 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long long i)
    319 {
    320     JSValue* v = JSImmediate::from(i);
    321     return v ? v : jsNumberCell(exec, static_cast<double>(i));
    322 }
    323 
    324150inline JSValue::JSValue()
    325151{
     
    330156}
    331157
    332 inline JSCell::JSCell()
    333 {
    334 }
    335 
    336 inline JSCell::~JSCell()
    337 {
    338 }
    339 
    340 inline bool JSCell::isNumber() const
    341 {
    342     return type() == NumberType;
    343 }
    344 
    345 inline bool JSCell::isString() const
    346 {
    347     return type() == StringType;
    348 }
    349 
    350 inline bool JSCell::isObject() const
    351 {
    352     return type() == ObjectType;
    353 }
    354 
    355 inline bool JSCell::marked() const
    356 {
    357     return Heap::isCellMarked(this);
    358 }
    359 
    360 inline void JSCell::mark()
    361 {
    362     return Heap::markCell(this);
    363 }
    364 
    365 ALWAYS_INLINE JSCell* JSValue::asCell()
    366 {
    367     ASSERT(!JSImmediate::isImmediate(this));
    368     return static_cast<JSCell*>(this);
    369 }
    370 
    371 ALWAYS_INLINE const JSCell* JSValue::asCell() const
    372 {
    373     ASSERT(!JSImmediate::isImmediate(this));
    374     return static_cast<const JSCell*>(this);
    375 }
    376 
    377158inline bool JSValue::isUndefined() const
    378159{
     
    393174{
    394175    return JSImmediate::isBoolean(this);
    395 }
    396 
    397 inline bool JSValue::isNumber() const
    398 {
    399     return JSImmediate::isNumber(this) || (!JSImmediate::isImmediate(this) && asCell()->isNumber());
    400 }
    401 
    402 inline bool JSValue::isString() const
    403 {
    404     return !JSImmediate::isImmediate(this) && asCell()->isString();
    405 }
    406 
    407 inline bool JSValue::isObject() const
    408 {
    409     return !JSImmediate::isImmediate(this) && asCell()->isObject();
    410176}
    411177
     
    425191}
    426192
    427 inline bool JSValue::getNumber(double& v) const
    428 {
    429     if (JSImmediate::isImmediate(this)) {
    430         v = JSImmediate::toDouble(this);
    431         return true;
    432     }
    433     return asCell()->getNumber(v);
    434 }
    435 
    436 inline double JSValue::getNumber() const
    437 {
    438     return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->getNumber();
    439 }
    440 
    441 inline double JSValue::uncheckedGetNumber() const
    442 {
    443     ASSERT(JSImmediate::isImmediate(this) || asCell()->isNumber());
    444     return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : static_cast<const JSNumberCell*>(this)->value();
    445 }
    446 
    447 inline bool JSValue::getString(UString& s) const
    448 {
    449     return !JSImmediate::isImmediate(this) && asCell()->getString(s);
    450 }
    451 
    452 inline UString JSValue::getString() const
    453 {
    454     return JSImmediate::isImmediate(this) ? UString() : asCell()->getString();
    455 }
    456 
    457 inline JSObject *JSValue::getObject()
    458 {
    459     return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
    460 }
    461 
    462 inline const JSObject *JSValue::getObject() const
    463 {
    464     return JSImmediate::isImmediate(this) ? 0 : asCell()->getObject();
    465 }
    466 
    467 inline CallType JSValue::getCallData(CallData& callData)
    468 {
    469     return JSImmediate::isImmediate(this) ? CallTypeNone : asCell()->getCallData(callData);
    470 }
    471 
    472 inline ConstructType JSValue::getConstructData(ConstructData& constructData)
    473 {
    474     return JSImmediate::isImmediate(this) ? ConstructTypeNone : asCell()->getConstructData(constructData);
    475 }
    476 
    477 ALWAYS_INLINE bool JSValue::getUInt32(uint32_t& v) const
    478 {
    479     return JSImmediate::isImmediate(this) ? JSImmediate::getUInt32(this, v) : asCell()->getUInt32(v);
    480 }
    481 
    482 ALWAYS_INLINE bool JSValue::getTruncatedInt32(int32_t& v) const
    483 {
    484     return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedInt32(this, v) : asCell()->getTruncatedInt32(v);
    485 }
    486 
    487 inline bool JSValue::getTruncatedUInt32(uint32_t& v) const
    488 {
    489     return JSImmediate::isImmediate(this) ? JSImmediate::getTruncatedUInt32(this, v) : asCell()->getTruncatedUInt32(v);
    490 }
    491 
    492 inline void JSValue::mark()
    493 {
    494     ASSERT(!JSImmediate::isImmediate(this)); // callers should check !marked() before calling mark()
    495     asCell()->mark();
    496 }
    497 
    498 inline bool JSValue::marked() const
    499 {
    500     return JSImmediate::isImmediate(this) || asCell()->marked();
    501 }
    502 
    503 inline JSType JSValue::type() const
    504 {
    505     return JSImmediate::isImmediate(this) ? JSImmediate::type(this) : asCell()->type();
    506 }
    507 
    508 inline JSValue* JSValue::toPrimitive(ExecState* exec, JSType preferredType) const
    509 {
    510     return JSImmediate::isImmediate(this) ? const_cast<JSValue*>(this) : asCell()->toPrimitive(exec, preferredType);
    511 }
    512 
    513 inline bool JSValue::getPrimitiveNumber(ExecState* exec, double& number, JSValue*& value)
    514 {
    515     if (JSImmediate::isImmediate(this)) {
    516         number = JSImmediate::toDouble(this);
    517         value = this;
    518         return true;
    519     }
    520     return asCell()->getPrimitiveNumber(exec, number, value);
    521 }
    522 
    523 inline bool JSValue::toBoolean(ExecState *exec) const
    524 {
    525     return JSImmediate::isImmediate(this) ? JSImmediate::toBoolean(this) : asCell()->toBoolean(exec);
    526 }
    527 
    528 ALWAYS_INLINE double JSValue::toNumber(ExecState *exec) const
    529 {
    530     return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : asCell()->toNumber(exec);
    531 }
    532 
    533 ALWAYS_INLINE JSValue* JSValue::toJSNumber(ExecState* exec) const
    534 {
    535     return JSImmediate::isNumber(this) ? const_cast<JSValue*>(this) : jsNumber(exec, this->toNumber(exec));
    536 }
    537 
    538 inline UString JSValue::toString(ExecState *exec) const
    539 {
    540     return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toString(exec);
    541 }
    542 
    543 inline JSObject* JSValue::toObject(ExecState* exec) const
    544 {
    545     return JSImmediate::isImmediate(this) ? JSImmediate::toObject(this, exec) : asCell()->toObject(exec);
    546 }
    547 
    548193ALWAYS_INLINE int32_t JSValue::toInt32(ExecState* exec) const
    549194{
     
    602247}
    603248
    604 inline JSObject* JSValue::toThisObject(ExecState* exec) const
    605 {
    606     if (UNLIKELY(JSImmediate::isImmediate(this)))
    607         return JSImmediate::toObject(this, exec);
    608     return asCell()->toThisObject(exec);
    609 }
    610 
    611 inline UString JSValue::toThisString(ExecState* exec) const
    612 {
    613     return JSImmediate::isImmediate(this) ? JSImmediate::toString(this) : asCell()->toThisString(exec);
    614 }
    615 
    616 inline JSString* JSValue::toThisJSString(ExecState* exec)
    617 {
    618     return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);
    619 }
    620 
    621 inline JSValue* JSValue::getJSNumber()
    622 {
    623     return JSImmediate::isNumber(this) ? this : (JSImmediate::isImmediate(this) ? 0 : asCell()->getJSNumber());
    624 }
    625 
    626249} // namespace KJS
    627250
Note: See TracChangeset for help on using the changeset viewer.