GetIteratorFromMethod.js 775 B

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