index.js 877 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var SLOT = require('internal-slot');
  3. var $SyntaxError = SyntaxError;
  4. var $StopIteration = typeof StopIteration === 'object' ? StopIteration : null;
  5. module.exports = function getStopIterationIterator(origIterator) {
  6. if (!$StopIteration) {
  7. throw new $SyntaxError('this environment lacks StopIteration');
  8. }
  9. SLOT.set(origIterator, '[[Done]]', false);
  10. var siIterator = {
  11. next: function next() {
  12. var iterator = SLOT.get(this, '[[Iterator]]');
  13. var done = SLOT.get(iterator, '[[Done]]');
  14. try {
  15. return {
  16. done: done,
  17. value: done ? void undefined : iterator.next()
  18. };
  19. } catch (e) {
  20. SLOT.set(iterator, '[[Done]]', true);
  21. if (e !== $StopIteration) {
  22. throw e;
  23. }
  24. return {
  25. done: true,
  26. value: void undefined
  27. };
  28. }
  29. }
  30. };
  31. SLOT.set(siIterator, '[[Iterator]]', origIterator);
  32. return siIterator;
  33. };