Change bargull-20110123-rlZ by bargull@Bargull02 on 2011-01-23 16:44:41
in /home/anba/src/svn/openlaszlo/trunk
for http://svn.openlaszlo.org/openlaszlo/trunk
Summary: Implement for-each loop (E4X)
New Features: LPP-6975 ("for each..in" statement)
Technical Reviewer: ptw
QA Reviewer: (pending)
Overview:
The ECMA-357 (E4X) specification adds the for-each loop to the
ECMAScript language. for-each iterates over all values of an object
compared to the for-in loop which iterates over all keys of an object.
So for-each simplies iterating over an object if only the values are of
interest. With for-each, the following pattern is no longer necessary:
for (var key in o) {
var value = o[key];
// operate on value
}
The loop can be rewritten to:
for each (var value in o) {
// operate on value
}
Details:
Parser.jjt:
- added "each" to the list of tokens
- "each" is still allowed to be an identifier name, therefore added to
IdentifierOrAS3Keyword() as well
- similar to for-in, for-each comes in two variations:
ForEachStatement() and ForEachVarStatement(), added both to
IterationStatement()
- lookahead set to four instead of three because "for" and "each" are
two separate tokens
- ForEachStatement() and ForEachVarStatement() are almost identical to
ForInStatement() resp. ForVarInStatement(), they only differ in the
additional "each" token
ASTVisitor.java:
- added visit() methods for both for-each loops
- cleaned up import statements
EmptyParserVisitor.java:
- added visit() methods for both for-each loops
GenericVisitor.java:
- added basic visit() method for for-each
- updated visitStatement() to accept for-each
- commented out unused code
CommonGenerator.java:
- added translate methods for for-each:
-- every for-each is rewritten to a for-in loop
-- no additional statements must be added before or after the for-in
loop, otherwise possible labels of the for-each loop may no longer be in
place
-- to avoid evaluating the loop target expression multiple times,
another local variable is added to hold the expression's value
-- due to the label problem, that local variable is declared in the
for-in loop (Thanks to Tucker for reminding me of this trick!)
-- ForEachVarStatement() may contain an initialiser expression, this is
handled by using a comma expression in the loop head
- updated visitStatement() to accept for-each
- removed resp. commented out unused code
- cleaned up import statements
SWF9Generator.java:
- swf9/10 implements E4X natively, therefore intercept CommonGenerator's
visit() method for for-each and don't rewrite to for-in loop
- the Flash Player does not evaluate the initialiser expression in
ForEachVarStatement() (FP-6086)
- to work around FP-6086, the initialiser is rewritten to a comma expression
ParseTreePrinter.java:
- added implementation for both for-each loops
VariableAnalyzer.java:
- ForEachVarStatement needs to be special cased just like
ForVarInStatement because it does not contain a VariableDeclaration
- made Java look a bit less awkward in incrementUsed() :-)
e4x-foreach.lzl:
- a few tests to cover for-each loops
Tests:
test case in (swf8,swf10,dhtml {IE, Firefox, Safari, Opera})
Files:
M test/smoke/smokecheck.lzx
A test/smoke/e4x-foreach.lzl
M WEB-INF/lps/server/src/org/openlaszlo/sc/VariableAnalyzer.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/EmptyParserVisitor.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/CommonGenerator.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/SWF9Generator.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/ASTVisitor.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/GenericVisitor.java
M WEB-INF/lps/server/src/org/openlaszlo/sc/ParseTreePrinter.java
M WEB-INF/lps/server/sc/src/org/openlaszlo/sc/Parser.jjt
Changeset:
http://svn.openlaszlo.org/openlaszlo/patches/bargull-20110123-rlZ.tar