check-correctness-of-iteration.js 980 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var wellKnownSymbol = require('../internals/well-known-symbol');
  2. var ITERATOR = wellKnownSymbol('iterator');
  3. var SAFE_CLOSING = false;
  4. try {
  5. var called = 0;
  6. var iteratorWithReturn = {
  7. next: function () {
  8. return { done: !!called++ };
  9. },
  10. 'return': function () {
  11. SAFE_CLOSING = true;
  12. }
  13. };
  14. iteratorWithReturn[ITERATOR] = function () {
  15. return this;
  16. };
  17. // eslint-disable-next-line es-x/no-array-from, no-throw-literal -- required for testing
  18. Array.from(iteratorWithReturn, function () { throw 2; });
  19. } catch (error) { /* empty */ }
  20. module.exports = function (exec, SKIP_CLOSING) {
  21. if (!SKIP_CLOSING && !SAFE_CLOSING) return false;
  22. var ITERATION_SUPPORT = false;
  23. try {
  24. var object = {};
  25. object[ITERATOR] = function () {
  26. return {
  27. next: function () {
  28. return { done: ITERATION_SUPPORT = true };
  29. }
  30. };
  31. };
  32. exec(object);
  33. } catch (error) { /* empty */ }
  34. return ITERATION_SUPPORT;
  35. };