Changeset 4630 in webkit for trunk/JavaScriptCore/kjs/ustring.cpp


Ignore:
Timestamp:
Jul 12, 2003, 9:01:36 AM (22 years ago)
Author:
darin
Message:

Reviewed by Dave.

  • fixed 3272777 -- array object indices treated as integers by Safari, but as strings in other web browsers

JavaScriptCore did not implement the proper rule for what an array index is.

  • kjs/array_object.cpp: (getArrayIndex): Added. Implements the rule from the specification, which also provides a handy "not an array index" value of 232-1. (ArrayInstanceImp::get): Use getArrayIndex. (ArrayInstanceImp::put): Ditto. (ArrayInstanceImp::hasProperty): Ditto. (ArrayInstanceImp::setLength): Ditto.
  • kjs/identifier.h: Removed now-unused toULong, and added toStrictUInt32, in both cases forwarding functions that forward to UString.
  • kjs/ustring.h: Added toStringUInt32.
  • kjs/ustring.cpp: (UString::toStrictUInt32): Added. Converts a string to a 32-bit unsigned integer, and rejects any string that does not exactly match the way the integer would be formatted on output. This is the rule documented in the ECMA language standard.
File:
1 edited

Legend:

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

    r4206 r4630  
    659659}
    660660
     661uint32_t UString::toStrictUInt32(bool *ok) const
     662{
     663  if (ok)
     664    *ok = false;
     665
     666  // Empty string is not OK.
     667  int len = rep->len;
     668  if (len == 0)
     669    return 0;
     670  const UChar *p = rep->dat;
     671  unsigned short c = p->unicode();
     672
     673  // If the first digit is 0, only 0 itself is OK.
     674  if (c == '0') {
     675    if (len == 1 && ok)
     676      *ok = true;
     677    return 0;
     678  }
     679 
     680  // Convert to UInt32, checking for overflow.
     681  uint32_t i = 0;
     682  while (1) {
     683    // Process character, turning it into a digit.
     684    if (c < '0' || c > '9')
     685      return 0;
     686    const unsigned d = c - '0';
     687   
     688    // Check for overflow.
     689    const unsigned maxProduct = 0xFFFFFFFFU - d;
     690    if (i > maxProduct / 10)
     691      return 0;
     692   
     693    // Add in another digit.
     694    i *= 10;
     695    i += d;
     696   
     697    // Handle end of string.
     698    if (--len == 0) {
     699      if (ok)
     700        *ok = true;
     701      return i;
     702    }
     703   
     704    // Get next character.
     705    c = (++p)->unicode();
     706  }
     707}
     708
    661709int UString::find(const UString &f, int pos) const
    662710{
Note: See TracChangeset for help on using the changeset viewer.