Bug 23418
| Summary: | Regular expression literals are not being "shared" inside a function | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | webkit |
| Component: | JavaScriptCore | Assignee: | Nobody <webkit-unassigned> |
| Status: | RESOLVED WONTFIX | ||
| Severity: | Normal | Keywords: | HasReduction, InRadar |
| Priority: | P2 | ||
| Version: | 528+ (Nightly build) | ||
| Hardware: | PC | ||
| OS: | OS X 10.5 | ||
webkit
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.
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
webkit
Btw... one can easily fix the code by using the "new Regex()" syntax instead. But, this was hard to understand at a first glace.
Mark Rowe (bdash)
<rdar://problem/6507230>
Geoffrey Garen
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.
webkit
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!
webkit
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.