IteratorNext.js 840 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var Call = require('./Call');
  5. var Type = require('./Type');
  6. var assertRecord = require('../helpers/assertRecord');
  7. // https://262.ecma-international.org/14.0/#sec-iteratornext
  8. module.exports = function IteratorNext(iteratorRecord) {
  9. assertRecord(Type, 'Iterator Record', 'iteratorRecord', iteratorRecord);
  10. var result;
  11. if (arguments.length < 2) { // step 1
  12. result = Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]']); // step 1.a
  13. } else { // step 2
  14. result = Call(iteratorRecord['[[NextMethod]]'], iteratorRecord['[[Iterator]]'], [arguments[1]]); // step 2.a
  15. }
  16. if (Type(result) !== 'Object') {
  17. throw new $TypeError('iterator next must return an object'); // step 3
  18. }
  19. return result; // step 4
  20. };