Ignore:
Timestamp:
Aug 8, 2003, 8:21:12 AM (22 years ago)
Author:
darin
Message:

Reviewed by John Sullivan.

  • fixed 3365527 -- subscripting JavaScript strings does not work (leads to hang at www.newmagna.com.au)

The JavaScript specification says nothing about this, but other browsers seem to give
read-only access to the characters in a string as if the string was an array of characters.

  • kjs/array_object.cpp: (ArrayInstanceImp::get): Update to use a public toArrayIndex function instead of our own getArrayIndex function, so we can share with string. (ArrayInstanceImp::put): Ditto. (ArrayInstanceImp::hasProperty): Ditto. (ArrayInstanceImp::setLength): Ditto.
  • kjs/ustring.h: Add toArrayIndex.
  • kjs/ustring.cpp: (UString::toArrayIndex): Added. Implements the rule from array.
  • kjs/identifier.h: Add a forwarding function so we can use toArrayIndex.
  • kjs/string_object.cpp: (StringInstanceImp::get): Return a single character string if the property name is an array index. (StringInstanceImp::hasProperty): Return true for property names that are suitable array indices.
File:
1 edited

Legend:

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

    r4639 r4792  
    7171}
    7272
    73 // Rule from ECMA 15.2 about what an array index is.
    74 // Must exactly match string form of an unsigned integer, and be less than 2^32 - 1.
    75 bool getArrayIndex(const Identifier &propertyName, unsigned &index)
    76 {
    77   bool ok;
    78   unsigned i = propertyName.toStrictUInt32(&ok);
    79   if (!ok || i >= 0xFFFFFFFFU)
    80     return false;
    81   index = i;
    82   return true;
    83 }
    84 
    8573Value ArrayInstanceImp::get(ExecState *exec, const Identifier &propertyName) const
    8674{
     
    8876    return Number(length);
    8977
    90   unsigned index;
    91   if (getArrayIndex(propertyName, index)) {
     78  bool ok;
     79  unsigned index = propertyName.toArrayIndex(&ok);
     80  if (ok) {
    9281    if (index >= length)
    9382      return Undefined();
     
    121110  }
    122111 
    123   unsigned index;
    124   if (getArrayIndex(propertyName, index)) {
     112  bool ok;
     113  unsigned index = propertyName.toArrayIndex(&ok);
     114  if (ok) {
    125115    put(exec, index, value, attr);
    126116    return;
     
    154144    return true;
    155145 
    156   unsigned index;
    157   if (getArrayIndex(propertyName, index)) {
     146  bool ok;
     147  unsigned index = propertyName.toArrayIndex(&ok);
     148  if (ok) {
    158149    if (index >= length)
    159150      return false;
     
    262253    while (it != sparseProperties.end()) {
    263254      Reference ref = it++;
    264       unsigned index;
    265       if (getArrayIndex(ref.getPropertyName(exec), index) && index > newLength) {
     255      bool ok;
     256      unsigned index = ref.getPropertyName(exec).toArrayIndex(&ok);
     257      if (ok && index > newLength) {
    266258        ref.deleteValue(exec);
    267259      }
Note: See TracChangeset for help on using the changeset viewer.