| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 | var isObject = require('es-object-atoms/isObject');
|
|---|
| 5 |
|
|---|
| 6 | var Call = require('./Call');
|
|---|
| 7 | var CompletionRecord = require('./CompletionRecord');
|
|---|
| 8 | var GetMethod = require('./GetMethod');
|
|---|
| 9 | var IsCallable = require('./IsCallable');
|
|---|
| 10 |
|
|---|
| 11 | var isIteratorRecord = require('../helpers/records/iterator-record');
|
|---|
| 12 |
|
|---|
| 13 | // https://262.ecma-international.org/15.0/#sec-iteratorclose
|
|---|
| 14 |
|
|---|
| 15 | module.exports = function IteratorClose(iteratorRecord, completion) {
|
|---|
| 16 | if (!isIteratorRecord(iteratorRecord)) {
|
|---|
| 17 | throw new $TypeError('Assertion failed: `iteratorRecord` must be an Iterator Record'); // step 1
|
|---|
| 18 | }
|
|---|
| 19 | if (!isObject(iteratorRecord['[[Iterator]]'])) {
|
|---|
| 20 | throw new $TypeError('Assertion failed: iteratorRecord.[[Iterator]] must be an Object'); // step 1
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | if (!IsCallable(completion) && !(completion instanceof CompletionRecord)) { // step 2
|
|---|
| 24 | throw new $TypeError('Assertion failed: completion is not a thunk representing a Completion Record, nor a Completion Record instance');
|
|---|
| 25 | }
|
|---|
| 26 | var completionThunk = completion instanceof CompletionRecord ? function () { return completion['?'](); } : completion;
|
|---|
| 27 |
|
|---|
| 28 | var iterator = iteratorRecord['[[Iterator]]']; // step 3
|
|---|
| 29 |
|
|---|
| 30 | var iteratorReturn;
|
|---|
| 31 | try {
|
|---|
| 32 | iteratorReturn = GetMethod(iterator, 'return'); // step 4
|
|---|
| 33 | } catch (e) {
|
|---|
| 34 | completionThunk(); // throws if `completion` is a throw completion // step 6
|
|---|
| 35 | // eslint-disable-next-line no-useless-assignment
|
|---|
| 36 | completionThunk = null; // ensure it's not called twice.
|
|---|
| 37 | throw e; // step 7
|
|---|
| 38 | }
|
|---|
| 39 | if (typeof iteratorReturn === 'undefined') {
|
|---|
| 40 | return completionThunk(); // step 5.a - 5.b
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | var innerResult;
|
|---|
| 44 | try {
|
|---|
| 45 | innerResult = Call(iteratorReturn, iterator, []);
|
|---|
| 46 | } catch (e) {
|
|---|
| 47 | // if we hit here, then "e" is the innerResult completion that needs re-throwing
|
|---|
| 48 |
|
|---|
| 49 | completionThunk(); // throws if `completion` is a throw completion // step 6
|
|---|
| 50 | // eslint-disable-next-line no-useless-assignment
|
|---|
| 51 | completionThunk = null; // ensure it's not called twice.
|
|---|
| 52 |
|
|---|
| 53 | // if not, then return the innerResult completion
|
|---|
| 54 | throw e; // step 7
|
|---|
| 55 | }
|
|---|
| 56 | var completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
|
|---|
| 57 | // eslint-disable-next-line no-useless-assignment
|
|---|
| 58 | completionThunk = null; // ensure it's not called twice.
|
|---|
| 59 |
|
|---|
| 60 | if (!isObject(innerResult)) {
|
|---|
| 61 | throw new $TypeError('iterator .return must return an object');
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return completionRecord;
|
|---|
| 65 | };
|
|---|