22:16
<TabAtkins>
Sorry, I don't want to test this if someone already has the knowledge off-hand: if you array-destructure an actual array, does it invoke the iterator protocol or just yoink from the numbered properties directly. and does this apply to array-likes as well?
22:25
<Richard Gibson>
yes to both; array-destructuring is defined to depend upon the iterator protocol (which for most built-ins, particularly including Array.prototype, is mutable by ECMAScript code)
DestructuringAssignmentEvaluation
22:27
<TabAtkins>
k, cool
22:27
<TabAtkins>
that means i don't need to worry about making this more efficient in another proposal ^_^
23:00
<bakkot>
for this reason you will sometimes see people doing let {0: x, 1: y} = array, because it skips the iterator protocol
23:13
<Richard Gibson>

aw crud, it looks like every implementation diverges from the spec for const [] = …

$ eshost -sx '
  "use strict"; 
  const arr = ["a", "b", "c"];
  Object.defineProperty(Array.prototype, Symbol.iterator, {
    get() {
      print("get Symbol.iterator", this);
      return function*() { 
        print("@@iterator", this, ...arguments);
        for (let i = 0; i < this.length; i++) { 
          const v = this[i];
          print("yield", v); 
          yield v; 
        } return; 
      }; 
    }, 
  }); 
  print("\n# read all"); const [...all] = arr; 
  print("\n# read one"); const [first] = arr; 
  print("\n# read none"); const [] = arr; 
  print("\n" + JSON.stringify({ first, all }));
'
#### ChakraCore, engine262, GraalJS, Hermes, JavaScriptCore, Moddable XS, QuickJS, SpiderMonkey, V8
# read all
get Symbol.iterator a,b,c
@@iterator a,b,c
yield a
yield b
yield c

# read one
get Symbol.iterator a,b,c
@@iterator a,b,c
yield a

# read none
get Symbol.iterator a,b,c

{"first":"a","all":["a","b","c"]}

(all three should invoke @@iterator and get next in GetIteratorFromMethod via GetIterator)

23:34
<bakkot>
I suspect that's due to implementing the spec as it was prior to https://github.com/tc39/ecma262/pull/1021, and then only doing the update necessary to make https://github.com/tc39/test262/pull/1248 pass
23:40
<bakkot>
wait, no
23:40
<bakkot>
Richard Gibson your test is just wrong
23:40
<bakkot>
generators don't do anything when invoked
23:41
<bakkot>
(except evaluate parameter defaults IIRC)
23:41
<bakkot>
they only do something when you first call next
23:41
<bakkot>
"use strict";
const arr = ["a", "b", "c"];
Object.defineProperty(Array.prototype, Symbol.iterator, {
  get() {
    print("get Symbol.iterator", this);
    let thiz = this;
    return function() {
      print("@@iterator", this, ...arguments);
      let i = 0;
      return {
        get next() {
          print("get next");
          return () => {
            return i >= thiz.length ? { done: true } : { done: false, value: thiz[i++] };
          };
        },
      };
    };
  },
});
print("\n# read all"); const [...all] = arr;
print("\n# read one"); const [first] = arr;
print("\n# read none"); const [] = arr;
print("\n" + JSON.stringify({ first, all }));

gives

# read none
get Symbol.iterator a,b,c
@@iterator a,b,c
get next

in all engines, just as it should

23:44
<Richard Gibson>
phew
23:52
<Richard Gibson>
well, not all engines: https://github.com/Moddable-OpenSource/moddable/issues/1223
23:58
<bakkot>
ah yeah I don't have xs installed I guess