Uploaded image for project: 'Apache Flex'
  1. Apache Flex
  2. FLEX-34984

for-each loops cross-compile incorrectly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • Apache FlexJS 0.5.0
    • Apache FalconJX 0.6.0
    • Falcon, FlexJS
    • None

    Description

      When you write a for-each loop, the iterable gets re-evaluated on every iteration.

      Example code:

      for each (var item:Object in Test.getItems()) {
      	Test.doSomething(item);
      }
      

      Cross-compiled:

        for (var foreachiter0 in Test.getItems()) 
        {
        var item = Test.getItems()[foreachiter0];
        {
          Test.doSomething(item);
        }}
      

      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:

      var foreachiter0_target = Test.getItems();
      for (var foreachiter0 in foreachiter0_target)
      {
          var item = foreachiter0_target[foreachiter0];
          Test.doSomething(item);
      }
      

      I can see problematic code in three places:

      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

      Attachments

        Activity

          People

            aharui Alex Harui
            adufilie Andy Dufilie
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: