ExtendScript / extendscriptr

An NPM command line tool to compile modern javascript (es5 & es6) into executable extendscript (es3)
Do What The F*ck You Want To Public License
153 stars 15 forks source link

for..of loop is not working #51

Closed KaivnD closed 5 years ago

KaivnD commented 5 years ago

Hi everybody! I've got a issue about for...of... loop in my code. Here is the things,

...

for ( let path of layer.pathItems ) {
    //do some stuff for this path
}

...

When I use for ... of loop , I got a an error like

Cannot execute script in target engine 'main'!
#54 Symbol is undefine

Here is the code , which is translated, about for of loop in my source code


      this.items = this.doc.pageItems;
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = this.doc.layers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var layer = _step.value;
          this[layer.name] = layer.groupItems;
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator["return"] != null) {
            _iterator["return"]();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }
ff6347 commented 5 years ago

My guess is that it has a problem looping the Collection because a collection is not an Array. Could you try to push all those items into an array and then use the for … of … loop?

I think we had a similar problem with a forEach loop.

ff6347 commented 5 years ago

Or we are missing a polyfill for Symbol.

KaivnD commented 5 years ago

@fabianmoronzirfas Thanks for reply This Array is not empty

for(let i = 0; i < layers.length; i ++) // And this loop works

I think the polyfill for Symbol is missing

ff6347 commented 5 years ago

Did you try to use the

 for let item of …

With something other then a collection? The thing is the collection object has the property length but might not work with the other type of loop

ff6347 commented 5 years ago

Can you run a test like this:

const items = [];
for(let i = 0; i < layer.pathItems.length; i++){
  items.push(layer.pathItems[i]);
}

for ( let path of items ) {
    //do some stuff for this path
}

And tell me if the same error occurs?

ff6347 commented 5 years ago

@KaivnD ping

ff6347 commented 5 years ago

Related? #4

KaivnD commented 5 years ago

Sorry, I'm busy doing my work lately. I'm quite sure that layers is not empty in my code, the point is, I was trying to use for...of... loop to iterating my layers array , which is from app.activeDocument.layers in Illustrator. I just test the for...of... loop is not working, so I use for(let i =0; i < layers.length; i ++) to iterating array instead, that's all. Lately I've found another issue, I'll open another issue

ff6347 commented 5 years ago

It's not about the layers being empty it's about layers being a collection and not an array in the illustrator DOM.

That's why I asked you to run this test code where all the layers are stored in an actual array to see if the for … of … loop works.

If it works it's an illustrator problem if it doesn't it's my problem. :)

KaivnD commented 5 years ago

@fabianmoronzirfas Ok, I get it, let me test it

KaivnD commented 5 years ago

I run the test, @fabianmoronzirfas and yes , you are right about this, app.activeDocument.layers is not an actual array. So , there is no way to use this layers as a actual array ?

KaivnD commented 5 years ago

And I realize this issue is about the use of for..of.. statement , my intention is to use this statement in my code, but I've got error when use this statement for app.activeDocument.layers, I never try the actual array. So I'm free to use this statement in my source code except something like app.activeDocument.layers, problem solved! Thank you for your patience! @fabianmoronzirfas

ff6347 commented 5 years ago

Great. 👍🏽