I'm in the process of simplifying the structure of the AST, doing things like resolving variable references, flattening nested blocks and transforming switch statements into nested ifs. One thing that I'm unclear about is wether blocks add anything to the semantics other than just grouping statements. For example, is the program
var foo = 1; print(foo);
equivalent to
{ var foo = 1; print(foo); }
?
It is hard to decipher the ECMAScript spec to guarantee that it is so (which I think it is). Anyone have an opinion?
Marc
Afficher les réponses par date
For example, is the program
var foo = 1; print(foo);
equivalent to
{ var foo = 1; print(foo); }
?
It is hard to decipher the ECMAScript spec to guarantee that it is so (which I think it is). Anyone have an opinion?
I think that ECMAScript has only one scope per function, and so your transfomation is valid, however, you'll have to test this.
It may be different because the foo variable was previously a global in the scope external to the block, while now it will be a local of the function, and undefined until the initialization is reached.
- Maxime
On 2010-06-07, at 1:22 PM, chevalma@iro.umontreal.ca wrote:
For example, is the program
var foo = 1; print(foo);
equivalent to
{ var foo = 1; print(foo); }
?
It is hard to decipher the ECMAScript spec to guarantee that it is so (which I think it is). Anyone have an opinion?
I think that ECMAScript has only one scope per function, and so your transfomation is valid, however, you'll have to test this.
It may be different because the foo variable was previously a global in the scope external to the block, while now it will be a local of the function, and undefined until the initialization is reached.
In my example I'm comparing *programs*, i.e.
1) a sequence of 2 statements at top level 2) a single block at top level which contains 2 statements
There are no function definitions in this code. Note that in v8 the program
{ var foo = 1; print(foo); } print(foo);
prints 1 twice. So foo is defined as a global variable.
Marc
There are no function definitions in this code. Note that in v8 the program
{ var foo = 1; print(foo); } print(foo);
prints 1 twice. So foo is defined as a global variable.
I think V8 has the correct semantics. There is nothing in the ECMAScript spec saying that block statements carry their own scope, as far as I can remember.
- Maxime