Comment #2 on issue 2506 by [email protected]: Missing support for const
in for-in loops
https://code.google.com/p/v8/issues/detail?id=2506
I took a look at this issue.
Parser::ParseForStatement() calls Parser::ParseVariableDeclarations() for
parsing the lhs.
In case the lhs is a 'const', function Parser::ParseVariableDeclarations()
doesn't return a value for variable 'out'.
// If there was a single non-const declaration, return it in the output
// parameter for possible use by for/in.
if (nvars == 1 && !is_const) {
*out = name;
}
Since 'out' is NULL ('name' in Parser::ParseVariableDeclarations()) , the
branch that sets up the loop in case of lhs being a const is never executed.
if (name != NULL && CheckInOrOf(accept_OF, &mode)) {
...
}
Removing the check '!is_const' in Parser::ParseVariableDeclarations()
solves the parsing issue, however I'm not sure about the semantics. This is
what I get:
d8> for (const x in [1, 2, 3]) print(x)
undefined
undefined
undefined
undefined
d8> for (const x = 1 in [1, 2, 3]) print(x)
1
1
1
undefined
In the first case, 'const x' is created without a value (undefined). Its
result is printed 3 times (number of elements in the array).
In the second case, 'const x' is created with value '1'. Its result is
printed 3 times (number of element in the array).
In addition to that, if I try to run the same piece of code again I got:
d8> for (const x = 1 in [1, 2, 3]) print(x)
(d8):1: TypeError: Identifier 'x' has already been declared
for (const x = 1 in [1, 2, 3]) print(x)
According to ES-262, the syntax, "for (const x in array)" is correct
(13.6.4.1 Static Semantics: Early Errors, for ( ForDeclaration in
Expression ) Statement, where ForDeclaration can be 'const' but not 'let')
and 'const' can only be assigned once AFAIK. So, is this the intended
behaviour or should the semantics be different?
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.