GetIteratorFromMethod.js 727 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var Call = require('./Call');
  4. var GetV = require('./GetV');
  5. var IsCallable = require('./IsCallable');
  6. var Type = require('./Type');
  7. // https://262.ecma-international.org/14.0/#sec-getiteratorfrommethod
  8. module.exports = function GetIteratorFromMethod(obj, method) {
  9. if (!IsCallable(method)) {
  10. throw new $TypeError('method must be a function');
  11. }
  12. var iterator = Call(method, obj); // step 1
  13. if (Type(iterator) !== 'Object') {
  14. throw new $TypeError('iterator must return an object'); // step 2
  15. }
  16. var nextMethod = GetV(iterator, 'next'); // step 3
  17. return { // steps 4-5
  18. '[[Iterator]]': iterator,
  19. '[[NextMethod]]': nextMethod,
  20. '[[Done]]': false
  21. };
  22. };