Review request for JDK-8006984

Attila Szegedi attila.szegedi at oracle.com
Mon Feb 25 11:18:20 PST 2013


If you look at the Program grammar production in Chapter 14, it says:
Program :
SourceElementsopt
SourceElements :
SourceElement
SourceElements SourceElement
SourceElement :
Statement
FunctionDeclaration
As you can see, SourceElement is either a Statement or a FunctionDeclaration -- they're considered two different things, and through their union SourceElement they can both occur as top-level elements in a program. 

Further, the grammar production FunctionBody in Chapter 13 is:
FunctionBody :
SourceElementsopt
meaning a function body also consists of SourceElements - either Statements or FunctionDeclarations. That are the only contexts in which FunctionDeclaration can occur, it occurs nowhere else in the grammar. 

Finally, Chapter 12 "Statements" is quite articulate in *not* listing FunctionDeclaration as part of the Statement production:
Statement :
Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement
LabelledStatement
SwitchStatement
ThrowStatement
TryStatement
DebuggerStatement
There is also this non-normative "NOTE" section in Chapter 12 saying "Several widely used implementations of ECMAScript are known to support the use of FunctionDeclaration as a Statement. However there are significant and irreconcilable variations among the implementations in the semantics applied to such FunctionDeclarations. Because of these irreconcilable differences, the use of a FunctionDeclaration as a Statement results in code that is not reliably portable among implementations. …"

The specification writers clearly frowns upon this practice, for quite obvious reasons - function declarations are instantiated upon entry to the program or their parent function, not when the declaration is executed in source code statement order, therefore programmers can have mistaken assumptions about their lexical scope, because such declarations always have the VariableEnvironment of the execution context at script/function entry point as their scope, and not the LexicalEnvironment at the point of their declaration (which is what function expressions get).

We hope that by strict adherence to the specification we can steer people towards writing cleaner, more maintainable code in the future; one of goals of the ECMAScript 5.1 specification was eliminating or clarifying a lot of arcane quirks of the language; this purpose would be partially defeated if we just ignored it.

HTH,
  Attila.


On Feb 25, 2013, at 8:03 PM, Andreas Rieber <rieberandreas at gmail.com> wrote:

> Hi Attila,
> 
> i can't read that out of ecma-262 spec but i guess you read that many times more than i did. I try to follow "The Good Pars", which would make all easier.
> 
> Is there a way to access the issue tracking? I mean it would make my live easier. At the moment i follow every commit and try to get in but i don't see what is open, coming, etc.
> 
> cheers
> Andreas
> 
> PS: if i am not totally wrong - that code was from Sundar.
> 
> 
> On 25.02.13 18:46, Attila Szegedi wrote:
>> Excellent… 
>> 
>> On that note, I want to make you aware that we'll soon make Nashorn reject with a syntax error those function declarations that occur within "with" and "catch" blocks; we're shooting for 100% ECMAScript 5.1 compliance, and it disallows function declarations except directly on the top-level of script and in another function's body. In such cases, you'll need to use a function expression instead. So this:
>> 
>> var guiPkgs = { JFrame: function() { print("created"); } }; 
>> 
>> with (guiPkgs) {
>>       function main() { // <---- this is not legal in ECMAScript 5.1
>>         var frame; 
>> 
>>         function createFrame() { 
>>             frame = new JFrame(); 
>>         } 
>> 
>>         createFrame(); 
>>     } 
>> } 
>> main();
>> 
>> will soon stop working. If you want the "main" function and whatever is in it to be affected by the with() block, you'll have to use a function expression instead like this:
>> 
>> with (guiPkgs) {
>>       var main = function() { // <---- this expresses your actual intent, and is the only valid construct here in ES5.1
>>         var frame; 
>> 
>>         function createFrame() { // <-- this is still okay, as it's defined directly on the function body level of main() 
>>             frame = new JFrame(); 
>>         } 
>> 
>>         createFrame(); 
>>     } 
>> } 
>> main();
>> 
>> Attila.
>> 
>> On Feb 25, 2013, at 6:37 PM, Andreas Rieber <rieberandreas at gmail.com> wrote:
>> 
>>> Hi Attila,
>>> 
>>> i found that fix and retested. One line more and another one... couldn't break it ;-)
>>> 
>>> Andreas
>>> 
>>> On 25.02.13 16:36, Attila Szegedi wrote:
>>>> Second review request, now with the test: http://cr.openjdk.java.net/~attila/8006984/webrev.01/
>>>> 
>>>> On Feb 25, 2013, at 3:43 PM, Attila Szegedi <attila.szegedi at oracle.com> wrote:
>>>> 
>>>>> Please review JDK-8006984 at http://cr.openjdk.java.net/~attila/8006984/webrev.00
>>>>> 
>>>>> Thanks,
>>>>>  Attila.
>> 
> 



More information about the nashorn-dev mailing list