Ignore:
Timestamp:
Aug 14, 2002, 8:19:38 PM (23 years ago)
Author:
mjs
Message:

Simplified array handling by using NULL to represent empty cells
instead of the Undefined object, so we can use calloc, realloc and
memset instead of loops. Inspired by a suggestion of Darin's.

  • kjs/array_object.cpp: (ArrayInstanceImp::ArrayInstanceImp): (ArrayInstanceImp::~ArrayInstanceImp): (ArrayInstanceImp::get): (ArrayInstanceImp::hasProperty): (ArrayInstanceImp::deleteProperty): (ArrayInstanceImp::setLength): (ArrayInstanceImp::mark):
File:
1 edited

Legend:

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

    r1821 r1822  
    4444  , length(initialLength)
    4545  , capacity(length)
    46   , storage(length ? new (ValueImp *)[length] : 0)
    47 {
    48   unsigned l = length;
    49   for (unsigned i = 0; i < l; ++i) {
    50     storage[i] = Undefined().imp();
    51   }
     46  , storage(length ? (ValueImp **)calloc(length, sizeof(ValueImp *)) : 0)
     47{
    5248}
    5349
     
    6763ArrayInstanceImp::~ArrayInstanceImp()
    6864{
    69   delete [] storage;
     65  free (storage);
    7066}
    7167
     
    7874  unsigned index = propertyName.toULong(&ok);
    7975  if (ok) {
    80     if (index >= length)
     76    if (index >= length || storage[index] == NULL)
    8177      return Undefined();
    8278    return Value(storage[index]);
     
    8884Value ArrayInstanceImp::get(ExecState *exec, unsigned index) const
    8985{
    90   if (index >= length)
     86  if (index >= length || storage[index] == NULL)
    9187    return Undefined();
    9288  return Value(storage[index]);
     
    128124    if (index >= length)
    129125      return false;
    130     return storage[index]->type() != UndefinedType;
     126    return storage[index] != NULL && storage[index]->type() != UndefinedType;
    131127  }
    132128 
     
    138134  if (index >= length)
    139135    return false;
    140   return storage[index]->type() != UndefinedType;
     136  return storage[index] != NULL && storage[index]->type() != UndefinedType;
    141137}
    142138
     
    151147    if (index >= length)
    152148      return true;
    153     storage[index] = Undefined().imp();
     149    storage[index] = NULL;
    154150    return true;
    155151  }
     
    162158  if (index >= length)
    163159    return true;
    164   storage[index] = Undefined().imp();
     160  storage[index] = NULL;
    165161  return true;
    166162}
     
    169165{
    170166  if (newLength < length) {
    171     const unsigned l = length;
    172     for (unsigned i = newLength; i < l; ++i)
    173       storage[i] = Undefined().imp();
     167    memset(storage + newLength, 0, sizeof(ValueImp *) * (length - newLength));
    174168  }
    175169  if (newLength > capacity) {
    176170    unsigned newCapacity = (newLength * 3 + 1) / 2;
    177     ValueImp **newStorage = new (ValueImp *)[newCapacity];
    178     const unsigned l = length;
    179     for (unsigned i = 0; i < l; ++i)
    180       newStorage[i] = storage[i];
    181     for (unsigned i = l; i < newLength; i++)
    182       newStorage[i] = Undefined().imp();
    183     delete [] storage;
    184     storage = newStorage;
     171    storage = (ValueImp **)realloc(storage, newCapacity * sizeof (ValueImp *));
     172    memset(storage + capacity, 0, sizeof(ValueImp *) * (newCapacity - capacity));
    185173    capacity = newCapacity;
    186174  }
     
    194182  for (unsigned i = 0; i < l; ++i) {
    195183    ValueImp *imp = storage[i];
    196     if (!imp->marked())
     184    if (imp != NULL && !imp->marked())
    197185      imp->mark();
    198186  }
Note: See TracChangeset for help on using the changeset viewer.