Bug 11001 - WebKit doesn't support RegExp.compile method
Summary: WebKit doesn't support RegExp.compile method
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 420+
Hardware: Mac OS X 10.4
: P2 Enhancement
Assignee: Alexey Proskuryakov
URL: http://ask.metafilter.com/mefi/47114
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-23 16:56 PDT by evariste
Modified: 2007-11-06 13:31 PST (History)
4 users (show)

See Also:


Attachments
proposed fix (85.75 KB, patch)
2007-10-30 10:48 PDT, Alexey Proskuryakov
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description evariste 2006-09-23 16:56:01 PDT
Javascript's RegExp.compile is not in the standard, but it's widely implemented by browsers including MSIE, Opera, and Firefox. Safari/WebKit should implement it too, for compatibile behavior with these browsers on identical code.
Comment 1 Alexey Proskuryakov 2007-10-30 10:48:30 PDT
Created attachment 16949 [details]
proposed fix

Maybe I'm missing something about this method, as I don't see any use for it - but tests do pass :)
Comment 2 Darin Adler 2007-10-30 11:09:45 PDT
Comment on attachment 16949 [details]
proposed fix

Seems fine. r=me

We should figure out what good this is! And make tests that cover its value.
Comment 3 David Kilzer (:ddkilzer) 2007-10-30 11:38:44 PDT
(In reply to comment #1)
> Maybe I'm missing something about this method, as I don't see any use for it -
> but tests do pass :)

(In reply to comment #2)
> We should figure out what good this is! And make tests that cover its value.

The notion of compiling a regular expression makes it possible to save time when using the regex repeatedly in a loop because it's saved in an intermediate format that's immediately useable instead of having to reparse the text representation every time.  It's similar to creating a "prepared database statement" in many SQL APIs (such as Perl's DBI and Java's JDBC and the new JavaScript Database API).

Perhaps this doesn't matter as much in JavaScript, though, since the expression is compiled with the source?

Comment 4 Alexey Proskuryakov 2007-10-30 11:47:16 PDT
AFAICT, any RegExp construction compiles it anyway - (new RegExp).compile(...) is the same as new RegExp(...) in this regard.

http://blog.stevenlevithan.com/archives/levels-of-javascript-regex-knowledge
Level 4 (of 7) Haxz0r:
...Knows that properties of the global RegExp object and the compile() method are deprecated.
Comment 5 Darin Adler 2007-10-30 11:52:04 PDT
Sure, I'm familiar with the broad general notion of compiling a regular expression.

But in our JavaScript implementation, at least, that happens when you create a RegExp object. What puzzles me (and presumably, Alexey) is what additional value the compile function provides.
Comment 6 Steven Levithan 2007-10-30 12:54:44 PDT
(In reply to comment #5)
> what additional value [does] the compile function provide

In theory, it should provide absolutely no value, which I presume is why it has been deprecated/removed since JavaScript 1.5 (ECMA-262 3rd Edition). Personally I would recommend that WebKit *not* implement it, to encourage people who use it to stop doing so. The method might have had some relevance in previous versions of JavaScript or previous browser implementations, but there is no reason to use it anymore.
Comment 7 Darin Adler 2007-10-30 13:08:49 PDT
If there are existing websites that use it, we probably should put the implementation in.

Leaving it out of Safari, if it's in IE and Firefox, will more likely make them think Safari is broken rather than encouraging them to remove it.
Comment 8 Steven Levithan 2007-10-30 17:32:25 PDT
Yeah, I know that makes sense, but I hate to see meaningless cruft like this used by people who come from programming languages where compiling a regex can actually have significant performance impact.

Actually, I can think of one thing that is different between using compile and just creating a new RegExp object. AFAIK, using compile on an existing regex will not discard custom properties on the object. However, I hope no one is relying on this feature (if I am in fact correct) since compile() has been deprecated for nearly a decade.
Comment 9 Alexey Proskuryakov 2007-10-31 01:46:15 PDT
Committed revision 27303.

I don't know any sites that use RegExp.compile, but given that it is widely documented and implemented, I'm sure there are quite a few.

> AFAIK, using compile on an existing regex will not discard
> custom properties on the object.

This is correct. Also, I can imagine situations where modifying an object leads to slightly nicer looking code than re-creating it. After all, compile() doesn't cause any architectural problems for anyone AFAICT, so I don't think we need to actively discourage it.
Comment 10 Geoffrey Garen 2007-11-06 13:31:53 PST
To add to what ap said above, his patch fixed an oft-duped compatibility problem with bmwusa.com.