Changeset 1832 in webkit for trunk/JavaScriptCore/kjs/value.cpp


Ignore:
Timestamp:
Aug 15, 2002, 12:51:48 PM (23 years ago)
Author:
darin
Message:

Tweaks and small bug fixes to Maciej's excellent new fixnum optimization.
Also updated or removed comments that call it "fixnum" instead of "simple number".

  • kjs/simple_number.h: Change constant names so they don't SHOUT the way macro names do. Added constants for shift, min, and max. Fixed off-by-1 error that prevented us from using the extreme values on either end. Base the range of numbers on a fixed 32 bits constant rather than the size of a long, because code elsewhere depends on positive numbers fitting into both "unsigned" and "UInt32" while assuming it doesn't need to check; we can easily change this later. Used int types rather than long for essentially the same reason. Fixed the value-extraction function so it will work for negative numbers even if the shift is logical, not arithmetic, by using division instead. Renamed functions to be quite terse since they are inside a class.
  • kjs/value.h:
  • kjs/value.cpp: (ValueImp::dispatchToObject): Call NumberImp::toObject in a "non-virtual" way rather than repeating the code here. (ValueImp::dispatchToUInt32): Handle the negative number case correctly. (ValueImp::dispatchGetBase): Call ValueImp::getBase in a "non-virtual" way rather than repeating the code here. (ValueImp::dispatchGetPropertyName): Call ValueImp::getPropertyName in a "non-virtual" way rather than repeating the code here. (ValueImp::dispatchPutValue): Call ValueImp::putValue in a "non-virtual" way rather than repeating the code here. (ValueImp::dispatchDeleteValue): Call ValueImp::deleteValue in a "non-virtual" way rather than repeating the code here. (Number::Number): Fixed a bug where the double-based constructor was casting to long, so wouldn't do the "remainder" check.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/value.cpp

    r1825 r1832  
    6565bool ValueImp::marked() const
    6666{
    67   // simple numbers are always considered marked
    68   return SimpleNumber::isSimpleNumber(this) || (_flags & VI_MARKED);
     67  // Simple numbers are always considered marked.
     68  return SimpleNumber::is(this) || (_flags & VI_MARKED);
    6969}
    7070
    7171void ValueImp::setGcAllowed()
    7272{
     73  //fprintf(stderr,"ValueImp::setGcAllowed %p\n",(void*)this);
    7374  // simple numbers are never seen by the collector so setting this
    7475  // flag is irrelevant
    75   if (!SimpleNumber::isSimpleNumber(this)) {
    76     //fprintf(stderr,"ValueImp::setGcAllowed %p\n",(void*)this);
     76  if (!SimpleNumber::is(this))
    7777    _flags |= VI_GCALLOWED;
    78   }
    7978}
    8079
     
    178177}
    179178
    180 
    181 // Dispatchers for virtual functions, to special-case fixnums which
    182 // won't be real pointers
     179// Dispatchers for virtual functions, to special-case simple numbers which
     180// won't be real pointers.
    183181
    184182Type ValueImp::dispatchType() const
    185183{
    186   if (SimpleNumber::isSimpleNumber(this)) {
     184  if (SimpleNumber::is(this))
    187185    return NumberType;
    188   } else {
    189     return this->type();
    190   }
     186  return type();
    191187}
    192188
    193189Value ValueImp::dispatchToPrimitive(ExecState *exec, Type preferredType) const
    194190{
    195   if (SimpleNumber::isSimpleNumber(this)) {
    196     return Number((NumberImp*)this);
    197   } else {
    198     return this->toPrimitive(exec, preferredType);
    199   }
     191  if (SimpleNumber::is(this))
     192    return Value(const_cast<ValueImp *>(this));
     193  return toPrimitive(exec, preferredType);
    200194}
    201195
    202196bool ValueImp::dispatchToBoolean(ExecState *exec) const
    203197{
    204   if (SimpleNumber::isSimpleNumber(this)) {
    205     return SimpleNumber::longValue(this);
    206   } else {
    207     return this->toBoolean(exec);
    208   }
     198  if (SimpleNumber::is(this))
     199    return SimpleNumber::value(this);
     200  return toBoolean(exec);
    209201}
    210202
    211203double ValueImp::dispatchToNumber(ExecState *exec) const
    212204{
    213   if (SimpleNumber::isSimpleNumber(this)) {
    214     return SimpleNumber::longValue(this);
    215   } else {
    216     return this->toNumber(exec);
    217   }
     205  if (SimpleNumber::is(this))
     206    return SimpleNumber::value(this);
     207  return toNumber(exec);
    218208}
    219209
    220210UString ValueImp::dispatchToString(ExecState *exec) const
    221211{
    222   if (SimpleNumber::isSimpleNumber(this)) {
    223     return UString::from(SimpleNumber::longValue(this));
    224   } else {
    225     return this->toString(exec);
    226   }
     212  if (SimpleNumber::is(this))
     213    return UString::from(SimpleNumber::value(this));
     214  return toString(exec);
    227215}
    228216
    229217Object ValueImp::dispatchToObject(ExecState *exec) const
    230218{
    231   if (SimpleNumber::isSimpleNumber(this)) {
    232       List args;
    233       args.append(Number(static_cast<NumberImp*>(const_cast<ValueImp *>(this))));
    234       return Object::dynamicCast(exec->interpreter()->builtinNumber().construct(exec,args));
    235   } else {
    236     return this->toObject(exec);
    237   }
     219  if (SimpleNumber::is(this))
     220    return static_cast<const NumberImp *>(this)->NumberImp::toObject(exec);
     221  return toObject(exec);
    238222}
    239223
    240224bool ValueImp::dispatchToUInt32(unsigned& result) const
    241225{
    242   if (SimpleNumber::isSimpleNumber(this)) {
    243     result = SimpleNumber::longValue(this);
     226  if (SimpleNumber::is(this)) {
     227    long i = SimpleNumber::value(this);
     228    if (i < 0)
     229      return false;
     230    result = (unsigned)i;
    244231    return true;
    245   } else {
    246     return this->toUInt32(result);
    247   }
     232  }
     233  return toUInt32(result);
    248234}
    249235
    250236Value ValueImp::dispatchGetBase(ExecState *exec) const
    251237{
    252   if (SimpleNumber::isSimpleNumber(this)) {
    253     Object err = Error::create(exec, ReferenceError, I18N_NOOP("Invalid reference base"));
    254     exec->setException(err);
    255     return err;
    256   } else {
    257     return this->getBase(exec);
    258   }
     238  if (SimpleNumber::is(this))
     239    return ValueImp::getBase(exec);
     240  return getBase(exec);
    259241}
    260242
    261243UString ValueImp::dispatchGetPropertyName(ExecState *exec) const
    262244{
    263   if (SimpleNumber::isSimpleNumber(this)) {
    264     return UString();
    265   } else {
    266     return this->getPropertyName(exec);
    267   }
     245  if (SimpleNumber::is(this))
     246    return ValueImp::getPropertyName(exec);
     247  return getPropertyName(exec);
    268248}
    269249
    270250void ValueImp::dispatchPutValue(ExecState *exec, const Value& w)
    271251{
    272   if (SimpleNumber::isSimpleNumber(this)) {
    273     Object err = Error::create(exec,ReferenceError);
    274     exec->setException(err);
    275   } else {
    276     return this->putValue(exec, w);
    277   }
     252  if (SimpleNumber::is(this))
     253    ValueImp::putValue(exec, w);
     254  putValue(exec, w);
    278255}
    279256
    280257bool ValueImp::dispatchDeleteValue(ExecState *exec)
    281258{
    282   if (SimpleNumber::isSimpleNumber(this)) {
    283     Object err = Error::create(exec,ReferenceError);
    284     exec->setException(err);
    285     return false;
    286   } else {
    287     return this->deleteValue(exec);
    288   }
     259  if (SimpleNumber::is(this))
     260    return ValueImp::deleteValue(exec);
     261  return deleteValue(exec);
    289262}
    290263
     
    409382
    410383Number::Number(int i)
    411   : Value(SimpleNumber::fitsInSimpleNumber(i) ? SimpleNumber::makeSimpleNumber(i) : new NumberImp(static_cast<double>(i))) { }
     384  : Value(SimpleNumber::fits(i) ? SimpleNumber::make(i) : new NumberImp(static_cast<double>(i))) { }
    412385
    413386Number::Number(unsigned int u)
    414   : Value(SimpleNumber::fitsInSimpleNumber(u) ? SimpleNumber::makeSimpleNumber(u) : new NumberImp(static_cast<double>(u))) { }
     387  : Value(SimpleNumber::fits(u) ? SimpleNumber::make(u) : new NumberImp(static_cast<double>(u))) { }
    415388
    416389Number::Number(double d)
    417   : Value(SimpleNumber::fitsInSimpleNumber((long)d) ? SimpleNumber::makeSimpleNumber((long)d) : new NumberImp(d)) { }
     390  : Value(SimpleNumber::fits(d) ? SimpleNumber::make((long)d) : new NumberImp(d)) { }
    418391
    419392Number::Number(long int l)
    420   : Value(SimpleNumber::fitsInSimpleNumber(l) ? SimpleNumber::makeSimpleNumber(l) : new NumberImp(static_cast<double>(l))) { }
     393  : Value(SimpleNumber::fits(l) ? SimpleNumber::make(l) : new NumberImp(static_cast<double>(l))) { }
    421394
    422395Number::Number(long unsigned int l)
    423   : Value(SimpleNumber::fitsInSimpleNumber(l) ? SimpleNumber::makeSimpleNumber(l) : new NumberImp(static_cast<double>(l))) { }
     396  : Value(SimpleNumber::fits(l) ? SimpleNumber::make(l) : new NumberImp(static_cast<double>(l))) { }
    424397
    425398Number Number::dynamicCast(const Value &v)
     
    433406double Number::value() const
    434407{
    435   if (SimpleNumber::isSimpleNumber(rep)) {
    436     return (double)SimpleNumber::longValue(rep);
    437   } else {
    438     assert(rep);
    439     return ((NumberImp*)rep)->value();
    440   }
     408  if (SimpleNumber::is(rep))
     409    return (double)SimpleNumber::value(rep);
     410  assert(rep);
     411  return ((NumberImp*)rep)->value();
    441412}
    442413
Note: See TracChangeset for help on using the changeset viewer.