[Webkit-unassigned] [Bug 31689] New: RegExp#exec's returned Array-like object behaves differently from regular Arrays

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Thu Nov 19 14:40:28 PST 2009


https://bugs.webkit.org/show_bug.cgi?id=31689

           Summary: RegExp#exec's returned Array-like object behaves
                    differently from regular Arrays
           Product: WebKit
           Version: 528+ (Nightly build)
          Platform: All
        OS/Version: Mac OS X 10.6
            Status: UNCONFIRMED
          Severity: Normal
          Priority: P2
         Component: JavaScriptCore
        AssignedTo: webkit-unassigned at lists.webkit.org
        ReportedBy: dark.panda+bugs at gmail.com


Created an attachment (id=43524)
 --> (https://bugs.webkit.org/attachment.cgi?id=43524)
RegExpMatchesArray::fillArrayInstance patch

RegExpMatchesArray objects returned from RegExp#exec behave differently than
regular Arrays when using the "in" operator and the Array#forEach method. 

The RegExp#exec method returns an Array of captured matches from within the
tested String. For optional captures, "undefined" is inserted into the Array as
a missing match. The returned Array seems to be correct when you try to access
the values directly using Array notation but using "in" or forEach will skip
over any "undefined" values as if they aren't there.

For instance:

m = /(a)(_)?.+(c)(_)?.+/.exec("abcd");
console.log(m);
console.log(2 in m);
m.forEach(function(x) {
  console.log(x);
});

Should produce:

["abcd", "a", undefined, "c", undefined]
true
abcd
a
undefined
c
undefined

However, WebKit produces:

["abcd", "a", undefined, "c", undefined]
false
abcd
a
c

Using a regular Array similar to the one produced by RegExp#exec produces the
expected result, so there is some inconsistency here.

The expected behaviour is based on what is produced by the Rhino, KJS and V8
engines. (In V8, the undefined values are inexplicably labeled "null" in their
console output but checking with the strict equality operator shows that the
undefined values are indeed undefined and not null. In IE8... well, the results
are stranger still, as it acts in the exact opposite manner that WebKit does
and produces empty Strings instead of undefineds or nulls to boot!)

A quick fix (small patch attached for the sake of convenience) appears to be to
add an extra condition to JavaScriptCore/runtime/RegExpConstructor.cpp in
RegExpMatchesArray::fillArrayInstance at line 135 to make sure that those
undefined values are inserted properly:

131     for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
132         int start = d->lastOvector()[2 * i];
133         if (start >= 0)
134             JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start,
d->lastOvector()[2 * i + 1] - start));
135         else
136             JSArray::put(exec, i, jsUndefined());
137     }

-- 
Configure bugmail: https://bugs.webkit.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.



More information about the webkit-unassigned mailing list