| [9af201e] | 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
|---|
| 4 |
|
|---|
| 5 | var $TypeError = require('es-errors/type');
|
|---|
| 6 | var $SyntaxError = require('es-errors/syntax');
|
|---|
| 7 | var isObject = require('es-object-atoms/isObject');
|
|---|
| 8 | var $asyncIterator = GetIntrinsic('%Symbol.asyncIterator%', true);
|
|---|
| 9 |
|
|---|
| 10 | var inspect = require('object-inspect');
|
|---|
| 11 | var hasSymbols = require('has-symbols')();
|
|---|
| 12 |
|
|---|
| 13 | var getIteratorMethod = require('../helpers/getIteratorMethod');
|
|---|
| 14 | var AdvanceStringIndex = require('./AdvanceStringIndex');
|
|---|
| 15 | var Call = require('./Call');
|
|---|
| 16 | var GetMethod = require('./GetMethod');
|
|---|
| 17 |
|
|---|
| 18 | var ES = {
|
|---|
| 19 | AdvanceStringIndex: AdvanceStringIndex,
|
|---|
| 20 | GetMethod: GetMethod
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | // https://262.ecma-international.org/9.0/#sec-getiterator
|
|---|
| 24 |
|
|---|
| 25 | module.exports = function GetIterator(obj, hint, method) {
|
|---|
| 26 | var actualHint = hint;
|
|---|
| 27 | if (arguments.length < 2) {
|
|---|
| 28 | actualHint = 'sync';
|
|---|
| 29 | }
|
|---|
| 30 | if (actualHint !== 'sync' && actualHint !== 'async') {
|
|---|
| 31 | throw new $TypeError("Assertion failed: `hint` must be one of 'sync' or 'async', got " + inspect(hint));
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | var actualMethod = method;
|
|---|
| 35 | if (arguments.length < 3) {
|
|---|
| 36 | if (actualHint === 'async') {
|
|---|
| 37 | if (hasSymbols && $asyncIterator) {
|
|---|
| 38 | actualMethod = GetMethod(obj, $asyncIterator);
|
|---|
| 39 | }
|
|---|
| 40 | if (typeof actualMethod === 'undefined') {
|
|---|
| 41 | throw new $SyntaxError("async from sync iterators aren't currently supported");
|
|---|
| 42 | }
|
|---|
| 43 | } else {
|
|---|
| 44 | actualMethod = getIteratorMethod(ES, obj);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | var iterator = Call(actualMethod, obj);
|
|---|
| 48 | if (!isObject(iterator)) {
|
|---|
| 49 | throw new $TypeError('iterator must return an object');
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return iterator;
|
|---|
| 53 |
|
|---|
| 54 | // TODO: This should return an IteratorRecord
|
|---|
| 55 | /*
|
|---|
| 56 | var nextMethod = GetV(iterator, 'next');
|
|---|
| 57 | return {
|
|---|
| 58 | '[[Iterator]]': iterator,
|
|---|
| 59 | '[[NextMethod]]': nextMethod,
|
|---|
| 60 | '[[Done]]': false
|
|---|
| 61 | };
|
|---|
| 62 | */
|
|---|
| 63 | };
|
|---|