jorendorff / es-spec-html

An HTML version of the ECMAScript draft specification autogenerated from the source
http://ecma-international.org/ecma-262/5.1/
51 stars 17 forks source link

Specify behaviour of forEach when structure is mutated during iteration #83

Closed infinity0 closed 9 years ago

infinity0 commented 9 years ago
var o = [0, 1, 2], seen = [];
o.forEach(function (value, i, obj) {
  seen.push(value);
  if (value === 1) {
    o.pop();
    o.push(3);
  }
});

This results in o === seen === [0, 1, 3] in both Firefox 31.4 and Chrome 40.0. Similar results are obtained for Set and Map as well. However, the spec for Array.prototype.forEach says:

Elements which are appended to the array after the call to forEach begins will not be visited by callbackfn

It would also be nice if the spec for Set.prototype.forEach and Map.prototype.forEach mentioned this behaviour explicitly. Currently, the Set spec "sort of" implies this behaviour, and the Map spec does not mention it at all:

[Set] However, a value will be revisited if it is deleted after it has been visited and then re-added before the to forEach call completes.

jordanbtucker commented 9 years ago

Thanks for the research. You should submit a bug report at https://bugs.ecmascript.org/.

Bug reports regarding the ES6 draft should be reported at https://bugs.ecmascript.org/. Bugs regarding this program, which produces an HTML version of the ES6 draft, should be reported here on GitHub.

infinity0 commented 9 years ago

Thanks, filed as https://bugs.ecmascript.org/show_bug.cgi?id=4013