Closed Bug 1088554 Opened 10 years ago Closed 10 years ago

Wrong behavior with function declaration inside flow conditions

Categories

(Core :: JavaScript Engine, defect)

33 Branch
x86_64
Windows 7
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: a.zhuravlev09, Unassigned)

References

Details

Attachments

(1 file)

User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36

Steps to reproduce:

Declare function inside if/else block stetement


Actual results:

Function never have been declared


Expected results:

Function declaration must reach variable scope independently of place where it declared, even if it declared inside flow condition that never executed (http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1)
Component: Untriaged → JavaScript Engine
Product: Firefox → Core
Whiteboard: DUPEME
> Function declaration must reach variable scope independently of place where it declared, 

No, per spec this script is not syntactically valid and should fail to parse.

I'm pretty sure you're not looking for that behavior.

You also say you're not looking for the behavior people usually want here, which was covered by bug 585536, apparently....
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Resolution: --- → INVALID
See Also: → 585536
I can't check code from link that you give.
Syntactically my example absolutely valid. Much more important that this code working correcly in all other browsers (Chrome, IE, Opera, Safary).
Status: RESOLVED → UNCONFIRMED
Resolution: INVALID → ---
https://people.mozilla.org/~jorendorff/es5.html#sec-10.5

there is no any restriction about place where Function Declaration (FD) placed - result must be independent of declaration place
> Syntactically my example absolutely valid.

No, it's not.  Please do read the spec carefully.

What you have there is a FunctionDeclaration production inside an if/else.  Looking at the spec, http://es5.github.io/#x12.5 is the syntax for IfStatement, it says the syntax for the thing after "else" is "Statement".  Looking at http://es5.github.io/#x12 we see:

  Statement :
    Block
    .. etc

and since you have curly braces this must be a Block.  Looking at http://es5.github.io/#x12.1 we see that it allows a StatmentList, which is just a sequence of Statement.  Ok, so back to the full syntactic definition of Statement:

  Statement:
 Block
 VariableStatement
 EmptyStatement
 ExpressionStatement
 IfStatement
 IterationStatement
 ContinueStatement
 BreakStatement
 ReturnStatement
 WithStatement
 LabelledStatement
 SwitchStatement
 ThrowStatement
 TryStatement
 DebuggerStatement

None of those match "function foo() {}".  What _does_ match it is FunctionDeclaration.

If you keep reading the section on Statement, you will see a spec note that explicitly talks about the usage of a FunctionDeclaration where Statement is expected.

> Much more important that this code working correcly in all other browsers 

Please read bug 585536 and the note in the spec carefully...  Sites depend on this non-standard syntax working differently in different browsers.  Sad, but there we are.
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago10 years ago
Resolution: --- → INVALID
> there is no any restriction about place where Function Declaration (FD) placed

This is patently false if you actually look at the grammar the spec defines.  The restrictions are that FunctionDeclaration is only allowed at toplevel in a Program (see <http://es5.github.io/#x14>) and toplevel in a FunctionBody <http://es5.github.io/#x13> via SourceElements/SourceElement <http://es5.github.io/#x14>.
> 
> This is patently false if you actually look at the grammar the spec defines.
> The restrictions are that FunctionDeclaration is only allowed at toplevel in
> a Program (see <http://es5.github.io/#x14>) and toplevel in a FunctionBody
> <http://es5.github.io/#x13> via SourceElements/SourceElement
> <http://es5.github.io/#x14>.

definitely no
Can you show in which part of spec Function Declaration allowed only at top level of program? Functions can declare other functions on own scope and export it outside from own scope - this greatest mechanism called closure, and with limitation that you describe closures are not possible
Status: RESOLVED → UNCONFIRMED
Resolution: INVALID → ---
> Can you show in which part of spec Function Declaration allowed only at top level of
> program?

Please read comment 5 carefully.  That's not what I said.

> Functions can declare other functions on own scope

Yes, that's "and toplevel in a FunctionBody".

Note that closures are in fact possible with that.  But most uses of closures in fact use a different syntactic construct: FunctionExpression, not FunctionDeclaration.  They look pretty similar, but are allowed in different contexts.  For example, if you do:

  var f = function foo() {};

that's a FunctionExpression on the right hand side.

Note that a FunctionExpression is _also_ not allowed directly in a Block.

Please do not reopen this bug again until you have either carefully read the ES5 grammar, read one of the many things on the web that describe the situation (if you search for "javascript function inside if" in pretty much any search engine you will find it; the first hit on DuckDuckGo, Bing, Yahoo, and Google is the right thing, for example) or read bug 585536 carefully.  Ideally all three.
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago10 years ago
Resolution: --- → INVALID
In addition to what bz said, note also that substantial efforts where put into an attempt of resolving cross-browser differences around this. Sadly, they failed because too much code relies on these differences and break if any browser changes their behavior to match that of other browsers. Hence, pointing out that other browsers show different behavior doesn't help: we're aware, and we can't change ours.
Whiteboard: DUPEME
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: