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