Prev: seethis VERY DIFFERENT
Next: regex help?
From: Dmitry A. Soshnikov on 3 Dec 2009 13:36 On Dec 3, 5:38 pm, kangax <kan...(a)gmail.com> wrote: [...] > > From what I can tell, this assignment should work. It is variable (and > function) *declarations* that specification is talking about. > Why? `eval` in strict mode doesn't inherit `LexicalEnvironment` from the calling context (10.4.2, draft ES-5, v.043), but creates own `LexicalEnvironment` and `VariableEnvironment`. So, this code in strict mode shouldn't affect on `v` from the calling context: var v; eval("v = 2; alert(v);"); // 2 alert(v); // undefined This should be correct behavior. Actually, it should be treated as `v` property of the global environment record. /ds
From: Asen Bozhilov on 3 Dec 2009 14:06 Dmitry A. Soshnikov wrote: > var v; > eval("v = 2; alert(v);"); // 2 > alert(v); // undefined > > This should be correct behavior. Actually, it should be treated as `v` > property of the global environment record. I have a question about eval in ECMA 5 in strict mode. If eval doesn't inherit LexicalEnvironment` and `VariableEnvironment` from calling execution context. What is properly behavior of code like that: (function(){ var v; eval('window.alert(v);'); //undefined or Reference Error? window.alert(v); //undefined }()); In `eval' from where will be resolving property `v'. In other words, what exactly contain Scope Chain of `eval' in strict mode? Regards.
From: kangax on 3 Dec 2009 16:01 Dmitry A. Soshnikov wrote: > On Dec 3, 5:38 pm, kangax <kan...(a)gmail.com> wrote: > > [...] > >> From what I can tell, this assignment should work. It is variable (and >> function) *declarations* that specification is talking about. >> > > Why? `eval` in strict mode doesn't inherit `LexicalEnvironment` from > the calling context (10.4.2, draft ES-5, v.043), but creates own > `LexicalEnvironment` and `VariableEnvironment`. This isn't how I understand it, but I might be wrong ;) Let's see. As per 10.4.2, eval code results in NewDeclarativeEnvironment being called with LexicalEnvironment parameter (step 3 in 10.4.2). That LexicalEnvironment is the same value as LexicalEnvironment of calling execution context (step 2 in 10.4.2). Now, NewDeclarativeEnvironment takes its parameter � LexicalEnvironment of current execution context � and assigns it as an outer lexical environment of newly created environment record (10.2.2.2). There's your "inheritance". So, eval code now has newly created (empty) lexical environment with its "outer" environment referencing that of current execution context. If that's what it is, `v` should resolve to a property of outer environment (as per GetIdentifierReference � 10.2.2.1), not eval inner one, and assign `2` to that property. And, by the way, if I'm wrong and eval code results in environment that doesn't "inherit" its outer environments, then `v = 2` should really result in an error, since it would be an undeclared assignment (and undeclared assignments result in ReferenceError in strict mode). [...] -- kangax
From: Dmitry A. Soshnikov on 4 Dec 2009 03:59 On Dec 4, 12:01 am, kangax <kan...(a)gmail.com> wrote: > Dmitry A. Soshnikov wrote: > > On Dec 3, 5:38 pm, kangax <kan...(a)gmail.com> wrote: > > > [...] > > >> From what I can tell, this assignment should work. It is variable (and > >> function) *declarations* that specification is talking about. > > > Why? `eval` in strict mode doesn't inherit `LexicalEnvironment` from > > the calling context (10.4.2, draft ES-5, v.043), but creates own > > `LexicalEnvironment` and `VariableEnvironment`. > > This isn't how I understand it, but I might be wrong ;) > No, you're not wrong but absolutely right ;) > Now, NewDeclarativeEnvironment > takes its parameter LexicalEnvironment of current execution context > and assigns it as an outer lexical environment of newly created > environment record (10.2.2.2). > > There's your "inheritance". > > So, eval code now has newly created (empty) lexical environment with its > "outer" environment referencing that of current execution context. > > If that's what it is, `v` should resolve to a property of outer > environment (as per GetIdentifierReference 10.2.2.1), not eval inner > one, and assign `2` to that property. > Yep, true, I didn't take in mind carefully 10.2.2.2 (but I sure read it and knew that by step 4 outer environment is set). By the "doesn't inherit" I meant that eval's `LexicalEnvironment` in strict mode won't be the *same* as `LexicalEnvironment` of the calling context, but, yep, that's true, that eval's `LexicalEnvironment` "inherits" `LexicalEnvironment` of the calling context as `outer` property. Which means - eval's code in this case cannot affect on it by `var`s or `FD`s, but still can modify resolved properties of it's `LexicalEnvironment` which is "Scope chain" now as has `outer` property. > > And, by the way, if I'm wrong and eval code results in environment that > doesn't "inherit" its outer environments, then `v = 2` should really > result in an error, since it would be an undeclared assignment (and > undeclared assignments result in ReferenceError in strict mode). > Yes, also true by note of 11.13.1. /ds
From: Dmitry A. Soshnikov on 4 Dec 2009 04:09
On Dec 3, 10:06 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote: > Dmitry A. Soshnikov wrote: > > var v; > > eval("v = 2; alert(v);"); // 2 > > alert(v); // undefined > > > This should be correct behavior. Actually, it should be treated as `v` > > property of the global environment record. > > If eval doesn't inherit LexicalEnvironment` and > `VariableEnvironment` from calling execution context. Well, it does, and exactly "inherits" but not "borrows" as in non- strict mode, when `LexicalEnvironment` of the calling context becomes `LexicalEnvironment` of the eval context. In strict mode `LexicalEnvironment` of the calling context saves in `outer` property of the newly created `DeclarativeEnvironment` for eval context. > What is properly behavior of code like that: > > (function(){ > var v; > eval('window.alert(v);'); //undefined or Reference Error? > window.alert(v); //undefined > > }()); > `undefined` as will be found in `outer` lexical environment. > In `eval' from where will be resolving property `v'. In other words, > what exactly contain Scope Chain of `eval' in strict mode? > In current draft there's no "Scope chain" terminology anymore. There's `LexicalEnvironment` (10.2) which contains `EnvironmentRecord` and a possibly null reference to an `outer` `LexicalEnvironment`. So `LexicalEnvironment` is sort of scope chain. /ds |