Bug 18053

Summary: Can't eval() an anonymous function
Product: WebKit Reporter: David Kilzer (:ddkilzer) <ddkilzer>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Normal CC: barraclough, ggaren, kent.hansen, ogoffart, oliver, pmuellr
Priority: P2 Keywords: HasReduction, InRadar
Version: 525.x (Safari 3.1)   
Hardware: Mac   
OS: OS X 10.5   
Attachments:
Description Flags
Test case
none
Better test case none

Description David Kilzer (:ddkilzer) 2008-03-24 17:39:23 PDT
* SUMMARY
Attempting to call eval() on a string containing an anonymous function fails in WebKit.  Works in Firefox 2.0.0.x.

* STEPS TO REPRODUCE
1. Launch Safari/WebKit.
2. Open attached test case.

RESULTS
Prints "FAILED" message on Safari/WebKit.  Prints "SUCCESS" message on Firefox 2.0.0.x.

REGRESSION
Unknown.
Comment 1 David Kilzer (:ddkilzer) 2008-03-24 17:39:56 PDT
Created attachment 20016 [details]
Test case
Comment 2 David Kilzer (:ddkilzer) 2008-03-24 17:42:05 PDT
See also Bug 12131 for issues with using onload on <script> tags.

Comment 3 David Kilzer (:ddkilzer) 2008-03-24 17:42:49 PDT
<rdar://problem/5817598>
Comment 4 David Kilzer (:ddkilzer) 2008-03-25 09:02:15 PDT
Created attachment 20031 [details]
Better test case
Comment 5 Maciej Stachowiak 2008-04-16 12:19:10 PDT
Technically, it is correct to treat this as a syntax error (it is treated as an erroneous function declaration, rather than as a unction expression). Do sites depend on this, or do other browsers handle it?
Comment 6 David Kilzer (:ddkilzer) 2008-04-16 13:32:22 PDT
(In reply to comment #5)
> Technically, it is correct to treat this as a syntax error (it is treated as an
> erroneous function declaration, rather than as a unction expression). Do sites
> depend on this, or do other browsers handle it?

Not that I'm aware of.  This bug was filed in response to an email to the safari-iphone-web-dev mailing list:

http://lists.apple.com/archives/Safari-iPhone-Web-Dev/2008/Mar/msg00020.html
Comment 7 Maciej Stachowiak 2008-04-16 16:53:07 PDT
The original email on that thread claims it works in Firefox and IE - someone should test in other browsers.
Comment 8 Geoffrey Garen 2008-04-29 11:21:33 PDT
This used to fail in IE, which is why we didn't implement it. It passes in IE7, though, so we should probably copy the behavior.
Comment 9 Simo Kinnunen 2008-12-21 12:04:59 PST
(In reply to comment #8)
> This used to fail in IE, which is why we didn't implement it. It passes in IE7,
> though, so we should probably copy the behavior.
> 

You may or may not be aware of this, but it works just fine if you make it an expression - for example, eval('(function() { alert("PASSED") })')().

Comment 10 Patrick Mueller 2009-12-04 07:38:46 PST
Trolling through JSC bugs, tried the "Better test case" on some browsers on the Mac:

WebKit nightly r51580:
   FAILED: SyntaxError: Parse error

Opera 10.10:
   SUCCESS

Chrome 4.0.249.22:
   FAILED: SyntaxError: Unexpected token (

FireFox 3.5.5:
   FAILED: SyntaxError: syntax error

Given the initial report that it "works in Firefox 2.0.0.x", and the fact that it no longer works in FireFox 3.5.5, I'd just close this as a WORKSFORME at this point.
Comment 11 Kent Hansen 2010-02-04 07:37:00 PST
This is bugging QtScript after switching to JSC, since our old parser used to support it: http://bugreports.qt.nokia.com/browse/QTBUG-5757

It still works with SpiderMonkey from Mozilla trunk (the one I have compiled is from Jan 4 2010).
Should be possible to fix by rewriting a couple of the grammar rules. Haven't tried it on this new parser though: https://bugs.webkit.org/show_bug.cgi?id=34019
Comment 12 Kent Hansen 2010-05-25 08:32:21 PDT
Relevant discussion from ES mailing list:
https://mail.mozilla.org/pipermail/es5-discuss/2009-June/002822.html

As per Geoffrey's reply, JavaScriptCore did actually support this syntax at one point in time, but then went the same way as FireFox (compliance argument).
I'm guessing the chances of that being reverted now are slim.
Comment 13 Gavin Barraclough 2012-03-07 17:46:16 PST
Our behaviour matches FireFox, Chrome, and the spec.  If we change this, it will be to more closely match harmony defined behaviour in modules, this is not something we want to support.