Closed Bug 61266 Opened 24 years ago Closed 24 years ago

ECMAScript compliance: passing a RegExp to RegExp() constructor

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

VERIFIED FIXED

People

(Reporter: david, Assigned: rogerl)

References

Details

(Keywords: js1.5)

Attachments

(2 files)

ECMA v3 15.10.4.1 says that you should be able to pass a RegExp object
to the RegExp() constructor, and basically create a new RegExp object
identical to the one you passed.  SpiderMonkey doesn't do this: instead
it just converts the RegExp you pass to a String and then uses that string
as the new pattern.

For example:

js> r = new RegExp("foo");
/foo/
js> s = new RegExp(r)
//foo//                         <-- should return /foo/
js> s = new RegExp(r, "g")
//foo//g                        <-- should throw a TypeError


Here is the relevant excerpt from the spec:

15.10.4.1 new RegExp(pattern, flags)
If pattern is an object R whose [[Class]] property is "RegExp" and flags is
undefined, then let P be the pattern
used to construct R and let F be the flags used to construct R. If pattern is an
object R whose [[Class]] property is
"RegExp" and flags is not undefined, then throw a TypeError exception.
Otherwise, let P be the empty string if
pattern is undefined and ToString(pattern) otherwise, and let F be the empty
string if flags is undefined and
ToString(flags) otherwise.
Keywords: js1.5
There is an additional problem.  When RegExp() is called as a function 
without the new operator, it appears to behave exactly as it does when called
with the new operator, but ECMA 15.10.3.1 says it should behave slightly
differently.  If passed a RegExp object, it should return that object unchanged.

It doesn't do that, as can be seen with code like this:

js> r = /foo/;
/foo/
js> s = RegExp(r)
//foo//

Here's the relevant text from the spec.:

15.10.3 The RegExp Constructor Called as a Function

15.10.3.1 RegExp(pattern, flags)

If pattern is an object R whose [[Class]] property is "RegExp" and flags is
undefined, then return R unchanged.
Otherwise call the RegExp constructor (section 15.10.4.1), passing it the
pattern and flags arguments and return
the object constructed by that constructor.
Status: NEW → ASSIGNED
Testcases added to JS test suite to cover both of the ECMA sections 
David has pointed out above: 


           js/tests/ecma_3/RegExp/15.10.3.1-1.js 
                                  15.10.3.1-2.js 
                                  15.10.4.1-1.js 
                                  15.10.4.1-2.js 
                                  15.10.4.1-3.js 
                                  15.10.4.1-4.js 
                                  15.10.4.1-5-n.js
Blocks: 66234
Generated a meta bug to capture all current R.E. bugs.
Fix checked in
Status: ASSIGNED → RESOLVED
Closed: 24 years ago
Resolution: --- → FIXED
Verified Fixed - ran this test suite directory on Linux, WinNT, and Mac

                    js/tests/ecma_3/RegExp/ 

with both debug and optimized versions of the JS shell, and got 0 errors.  
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: