[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var Call = require('./Call');
|
---|
| 6 | var CompletionRecord = require('./CompletionRecord');
|
---|
| 7 | var GetMethod = require('./GetMethod');
|
---|
| 8 | var IsCallable = require('./IsCallable');
|
---|
| 9 | var Type = require('./Type');
|
---|
| 10 |
|
---|
| 11 | var isIteratorRecord = require('../helpers/records/iterator-record');
|
---|
| 12 |
|
---|
| 13 | // https://262.ecma-international.org/14.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 (Type(iteratorRecord['[[Iterator]]']) !== 'Object') {
|
---|
| 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 | completionThunk = null; // ensure it's not called twice.
|
---|
| 36 | throw e; // step 7
|
---|
| 37 | }
|
---|
| 38 | if (typeof iteratorReturn === 'undefined') {
|
---|
| 39 | return completionThunk(); // step 5.a - 5.b
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | var innerResult;
|
---|
| 43 | try {
|
---|
| 44 | innerResult = Call(iteratorReturn, iterator, []);
|
---|
| 45 | } catch (e) {
|
---|
| 46 | // if we hit here, then "e" is the innerResult completion that needs re-throwing
|
---|
| 47 |
|
---|
| 48 | completionThunk(); // throws if `completion` is a throw completion // step 6
|
---|
| 49 | completionThunk = null; // ensure it's not called twice.
|
---|
| 50 |
|
---|
| 51 | // if not, then return the innerResult completion
|
---|
| 52 | throw e; // step 7
|
---|
| 53 | }
|
---|
| 54 | var completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
|
---|
| 55 | completionThunk = null; // ensure it's not called twice.
|
---|
| 56 |
|
---|
| 57 | if (Type(innerResult) !== 'Object') {
|
---|
| 58 | throw new $TypeError('iterator .return must return an object');
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return completionRecord;
|
---|
| 62 | };
|
---|