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 | var Type = require('./Type');
|
---|
18 |
|
---|
19 | // https://262.ecma-international.org/11.0/#sec-getiterator
|
---|
20 |
|
---|
21 | module.exports = function GetIterator(obj, hint, method) {
|
---|
22 | var actualHint = hint;
|
---|
23 | if (arguments.length < 2) {
|
---|
24 | actualHint = 'sync';
|
---|
25 | }
|
---|
26 | if (actualHint !== 'sync' && actualHint !== 'async') {
|
---|
27 | throw new $TypeError("Assertion failed: `hint` must be one of 'sync' or 'async', got " + inspect(hint));
|
---|
28 | }
|
---|
29 |
|
---|
30 | var actualMethod = method;
|
---|
31 | if (arguments.length < 3) {
|
---|
32 | if (actualHint === 'async') {
|
---|
33 | if (hasSymbols && $asyncIterator) {
|
---|
34 | actualMethod = GetMethod(obj, $asyncIterator);
|
---|
35 | }
|
---|
36 | if (actualMethod === undefined) {
|
---|
37 | throw new $SyntaxError("async from sync iterators aren't currently supported");
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | actualMethod = getIteratorMethod(
|
---|
41 | {
|
---|
42 | AdvanceStringIndex: AdvanceStringIndex,
|
---|
43 | GetMethod: GetMethod,
|
---|
44 | IsArray: IsArray
|
---|
45 | },
|
---|
46 | obj
|
---|
47 | );
|
---|
48 | }
|
---|
49 | }
|
---|
50 | var iterator = Call(actualMethod, obj);
|
---|
51 | if (Type(iterator) !== 'Object') {
|
---|
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 | };
|
---|