Bug 23418 - Regular expression literals are not being "shared" inside a function
Summary: Regular expression literals are not being "shared" inside a function
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords: HasReduction, InRadar
Depends on:
Blocks:
 
Reported: 2009-01-19 06:25 PST by webkit
Modified: 2009-01-20 06:01 PST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description webkit 2009-01-19 06:25:27 PST
Try out the following code:

>>>

var a = function()
{
   this.regexp = /WebKit/;
}

b = new a();
c = new a();

alert( b.regexp == c.regexp );

<<<

It alerts "false" in WebKit (r39853) and IE. With FF and Opera instead, we have "true", which I though was a browser bug.

But, after a check at the ECMA-262 specs, it looks like "true" is the correct thing:

>>>

7.8.5 Regular Expression Literals

A regular expression literal is an input element that is converted to a RegExp object (section 15.10) when it is scanned. The object is created before evaluation of the containing program or function begins. Evaluation of the literal produces a reference to that object; it does not create a new object.

<<<

I know it's weird, but webkit is buggy here, and it is always better to have browsers working in the same way.
Comment 1 webkit 2009-01-19 06:28:02 PST
Btw... one can easily fix the code by using the "new Regex()" syntax instead. But, this was hard to understand at a first glace.
Comment 2 Mark Rowe (bdash) 2009-01-19 11:23:09 PST
<rdar://problem/6507230>
Comment 3 Geoffrey Garen 2009-01-19 11:45:20 PST
The latest proposed version of the ECMA specification gives the nod to WebKit's/IE's behavior, calling the previous specification a bug:

[http://www.ecmascript.org/es4/spec/incompatibilities.pdf]

1.1. Evaluation of a RegExp Literal Results in a new RegExp 
Description 
In ES3 a regular expression literal like /a*b/mg denotes a single unique RegExp object that is created the 
first time the literal is encountered during evaluation.  In ES4 a new RegExp object is created every time 
the literal is encountered during evaluation. 
 
Rationale 
This is a bug fix.  The create-once behavior in ES3 is contrary to programmer expectation.3  If a RegExp 
object created by a literal is used for matching, its lastIndex property will be left in a non-initial state 
after a match; programmers who expect lastIndex to be reset to zero the next time the literal is evaluated 
(because they expect a new RegExp object to be created) will be surprised.  The bug bites even advanced 
programmers. 
 
Impact 
The changed behavior is observable because RegExp objects are mutable and the literal no longer 
corresponds to a single, unique object.  In practice the change is most observable in that the lastIndex 
property of the (new) RegExp object is in an initial state every time the literal is evaluated, as desired. 
 
Implementation precedent 
Internet Explorer 6 and Safari 3 create a new RegExp object every time the literal is encountered during 
evaluation. 
Comment 4 webkit 2009-01-20 05:41:41 PST
I'm partially happy on hearing that. Actually we have faced that ECMA bug, and then filled this ticket. It's good to see that things are to be changed, but we have also the current browser base to support.

So, it looks like I'll be filling a ticket at FF and Opera now ;)

Thanks!
Comment 5 webkit 2009-01-20 06:01:50 PST
Ok... filled the tickets at FF and Opera:

https://bugzilla.mozilla.org/show_bug.cgi?id=474412
https://bugs.opera.com/browse/CORE-17776

Let's see what those guys have to say about it.