[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 | // https://262.ecma-international.org/6.0/#sec-iteratorclose
|
---|
| 12 |
|
---|
| 13 | module.exports = function IteratorClose(iterator, completion) {
|
---|
| 14 | if (Type(iterator) !== 'Object') {
|
---|
| 15 | throw new $TypeError('Assertion failed: Type(iterator) is not Object');
|
---|
| 16 | }
|
---|
| 17 | if (!IsCallable(completion) && !(completion instanceof CompletionRecord)) {
|
---|
| 18 | throw new $TypeError('Assertion failed: completion is not a thunk representing a Completion Record, nor a Completion Record instance');
|
---|
| 19 | }
|
---|
| 20 | var completionThunk = completion instanceof CompletionRecord ? function () { return completion['?'](); } : completion;
|
---|
| 21 |
|
---|
| 22 | var iteratorReturn = GetMethod(iterator, 'return');
|
---|
| 23 |
|
---|
| 24 | if (typeof iteratorReturn === 'undefined') {
|
---|
| 25 | return completionThunk();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | var completionRecord;
|
---|
| 29 | try {
|
---|
| 30 | var innerResult = Call(iteratorReturn, iterator, []);
|
---|
| 31 | } catch (e) {
|
---|
| 32 | // if we hit here, then "e" is the innerResult completion that needs re-throwing
|
---|
| 33 |
|
---|
| 34 | // if the completion is of type "throw", this will throw.
|
---|
| 35 | completionThunk();
|
---|
| 36 | completionThunk = null; // ensure it's not called twice.
|
---|
| 37 |
|
---|
| 38 | // if not, then return the innerResult completion
|
---|
| 39 | throw e;
|
---|
| 40 | }
|
---|
| 41 | completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
|
---|
| 42 | completionThunk = null; // ensure it's not called twice.
|
---|
| 43 |
|
---|
| 44 | if (Type(innerResult) !== 'Object') {
|
---|
| 45 | throw new $TypeError('iterator .return must return an object');
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | return completionRecord;
|
---|
| 49 | };
|
---|