source: imaps-frontend/node_modules/es-abstract/2022/IteratorClose.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var Call = require('./Call');
6var CompletionRecord = require('./CompletionRecord');
7var GetMethod = require('./GetMethod');
8var IsCallable = require('./IsCallable');
9var Type = require('./Type');
10
11// https://262.ecma-international.org/6.0/#sec-iteratorclose
12
13module.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};
Note: See TracBrowser for help on using the repository browser.