block scope + direct non-strict eval

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Jan 31 09:49:11 PST 2012


a correction below, with that correct my alternative 2 appears to be equivalent to Sam's answers

On Jan 31, 2012, at 9:20 AM, Allen Wirfs-Brock wrote:

> 
> On Jan 31, 2012, at 8:27 AM, Sam Tobin-Hochstadt wrote:
> 
>> On Tue, Jan 31, 2012 at 9:30 AM, Andy Wingo <wingo at igalia.com> wrote:
>>> Hello ecmascriptians,
>>> 
>>> I hear that TC39 wants to allow let and const into "classic mode".  This
>>> sounds like a bad idea to me, but, ok.
>> 
>> Instead, I would say that TC39 wants to eliminate the concept of "classic mode".
>> 
>>> If so, can someone say what these expressions would evaluate to, or the
>>> errors they would raise:
>> 
>> I don't think we've talked about the behavior of direct |eval| on
>> statements with |let|, but here's what I would hope we'd do:
> 
> 
> But it's an issue that I'ver been thinking about since the last meeting.
> 
> The basic difference between non-strict direct eval and strict direct eval is that strict creates a new nested environment contour that is used as both the VariableEnvironement and LexicalEnvironment while non-strict uses the currently active Variable/Lexical environment.
> 
> It there are two possible semantics for non-strict eval with lexical declaration that I have come up with are:
> 1) same as ES5,  it uses/extends the current Variable and Lexical Environments
>  function f() {
>    // variable environment
>    function xVarEnv() {return x}
>    {
>       //lexical environment
>       print(xVarEnv());  //should be: undefined
>       eval("var x=1");    //create in the variable environment
>       print(xVarEnv());  //should be: 1
>       eval("let x=2");     //create in the lexical environment
>       eval("print(x)")     //should be: 2
>       print(x);                 //should be: 2
>       print(xVarEnv()); //should be: 1
>    }
>    print(xVarEnv()); //should be: 1
> )
> 
> (note that the most interesting example have an inner block)
> 
> 2) It creates a new nested Lexical Environment (for let and const declarations) but uses the currently active Variable environment.  The lexical environment is discarded when the eval is completed.:
> 
>  function f() {
>    // variable environment
>    function xVarEnv() {return x}
>    {
>       //lexical environment
>       print(xVarEnv());  //should be: undefined
>       eval("var x=1");    //create in the variable environment
>       print(xVarEnv());  //should be: 1
>       eval("let x=2; print(x)");     //create in a new lexical environment, should print: 2
>       eval("print(x)");    //should be: 1
>       print(x);                 //should be: 1
>       print(xVarEnv()); //should be: 1
>    }
>    print(xVarEnv()); //should be: 1
> )
> 
> Note that the second alternative requires (for ES5 compatibility, hoisting functions at the top of the eval code to the Variable Environment.  This would be different from the normal ES6 scoping rules for function declarations that are logically nested  within a block.
> 
> The second alternative is basically half way between a strict and non-strict eval.  By inclination is  to stick with the simpler alternative 1
> 
> 
>> 
>>>  (function (){eval("let x = 10"); return x;})()
>> 
>> ReferenceError
> 
> alternative 1 would return 10,  note that (function (){eval("var x = 10"); return x;})() must return 10 for compatability\
> alternative 2: return binding of x outside of function.  It may be undefined.  It will be a ReferenceError if no out binding exists.
>> 
>>>  (function (){var x = 20; eval("let x = 10"); return x;})()
>>>  (function (){let x = 20; eval("let x = 10"); return x;})()
> alternative 1: SyntaxError: duplicate declaration of x
> alternative 2: 20
>>>  (function (){let x = 20; { eval("let x = 10"); return x;}})()
> 
>>> (function (){ { let x = 20; { eval("let x = 10"); return x;}}})()
> alternative 1: 10
> alternative 2: 20
>> 
>> 20
>> 
>>>  (function (){let x = 20; eval("var x = 10"); return x;})()
> alternative 1: SyntaxError: duplicate declaration of x
> alternative 2: SyntaxError: duplicate declaration of x
>> 10
>> -- 
> 
> Allen
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120131/5cbd6906/attachment-0001.html>


More information about the es-discuss mailing list