Bug 32458 - /g regex Literals do not save state across function invocations
Summary: /g regex Literals do not save state across function invocations
Status: RESOLVED INVALID
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:
Depends on:
Blocks:
 
Reported: 2009-12-11 22:05 PST by Joseph Pecoraro
Modified: 2009-12-12 07:53 PST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Pecoraro 2009-12-11 22:05:30 PST
JavaScriptCore's literal regexs, do not save state (lastIndex) in functions, but other JavaScript implementations do:

I believe the relevant section of the of ES3 Specification is:
15.10.6.2 RegExp.prototype.exec(string)

Local Regex:
------------

  var s='123aaa456';
  function foo(a) { return /\d+/g.exec(a); };
  [foo(s), foo(s)]


  Results:
  jsc:    [["123"],["123"]]  <-- Different Value
  v8:     [["123"],["456"]]
  rhino:  [["123"],["456"]]
  spider: [["123"],["456"]]


Note that when the regex is not local, but something that stays in scope the entire time, it works as expected.

Global Regex:
-------------

  var s = '123aaa456';
  var r = /\d+/g;
  function foo(a) { return r.exec(a); };
  [foo(s), foo(s)]


  Results are all good:
  jsc:    [["123"],["456"]]
  v8:     [["123"],["456"]]
  rhino:  [["123"],["456"]]
  spider: [["123"],["456"]]
Comment 1 Oliver Hunt 2009-12-12 00:23:14 PST
Brendan isn't our behaviour consistent with that expected from ES5?
Comment 2 Joseph Pecoraro 2009-12-12 07:53:17 PST
(In reply to comment #1)
> Brendan isn't our behaviour consistent with that expected from ES5?

Oliver, you are correct. I talked to the person (on IRC) that originally found this  and he mentioned that this may be ES5 behavior.

From the ES5 spec's section on "Changes from ES3" there is:
7.8.5: Regular expression literals now return a unique object each time the literal is evaluated. This change is detectable by any programs that test the object identity of such literal values or that are sensitive to the shared side effects.

Old ES3 Text:
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.

ES5 Text:
A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated.

Thanks, I'm closing this as INVALID, because it works correctly.