[ https://issues.apache.org/jira/browse/FLEX-34984?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15075728#comment-15075728 ]
Andy Dufilie commented on FLEX-34984: ------------------------------------- I just tried the nightly build and there is a new problem. The new var foreachiter0_target gets placed incorrectly between a loop label and the loop, causing an "undefined label" error when using {{continue loopLabel}}. AS input: {code} public static function test():void { outerLoop: for each (var num:Number in [1, 2]) { for each (var str:String in ["x", "y", "z"]) { trace(num, str); if (str == "y") continue outerLoop; } } } {code} JS output: {code} foo.bar.Test.test = function() { outerLoop : var foreachiter0_target = [1, 2]; for (var foreachiter0 in foreachiter0_target) { var num = foreachiter0_target[foreachiter0]; { var foreachiter1_target = ["x", "y", "z"]; for (var foreachiter1 in foreachiter1_target) { var str = foreachiter1_target[foreachiter1]; { org.apache.flex.utils.Language.trace(num, str); if (str == "y") continue outerLoop; }} }} ; }; {code} Closure error: {code} SEVERE: C:/Weave/WeaveJS/bin/js-debug/weavejs/util/JS.js:37: ERROR - Parse error. undefined label "outerLoop" continue outerLoop; ^ {code} > for-each loops cross-compile incorrectly > ---------------------------------------- > > Key: FLEX-34984 > URL: https://issues.apache.org/jira/browse/FLEX-34984 > Project: Apache Flex > Issue Type: Bug > Components: Falcon, FlexJS > Affects Versions: Apache FlexJS 0.5.0 > Reporter: Andy Dufilie > Assignee: Alex Harui > Fix For: Apache FlexJS 0.6.0 > > > When you write a for-each loop, the iterable gets re-evaluated on every > iteration. > Example code: > {code} > for each (var item:Object in Test.getItems()) { > Test.doSomething(item); > } > {code} > Cross-compiled: > {code} > for (var foreachiter0 in Test.getItems()) > { > var item = Test.getItems()[foreachiter0]; > { > Test.doSomething(item); > }} > {code} > It should not call Test.getItems() on every loop iteration. Instead, it > should generate a new local variable prior to entering the loop to store the > result: > {code} > var foreachiter0_target = Test.getItems(); > for (var foreachiter0 in foreachiter0_target) > { > var item = foreachiter0_target[foreachiter0]; > Test.doSomething(item); > } > {code} > I can see problematic code in three places: > * JSGeneratingReducer.java: > https://github.com/apache/flex-falcon/blob/develop/compiler.js/src/org/apache/flex/compiler/internal/as/codegen/JSGeneratingReducer.java#L3426 > * ForEachEmitter.java: > https://github.com/apache/flex-falcon/blob/develop/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/ForEachEmitter.java > * JSVF2JSEmitter.java: > https://github.com/apache/flex-falcon/blob/develop/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/vf2js/JSVF2JSEmitter.java#L1708 > Another solution would be to use a for...of loop, Standard in ES6, though > that won't work in Internet Explorer: > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of -- This message was sent by Atlassian JIRA (v6.3.4#6332)