source: imaps-frontend/node_modules/es-abstract/2023/IteratorClose.js

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[d565449]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
11var isIteratorRecord = require('../helpers/records/iterator-record');
12
13// https://262.ecma-international.org/14.0/#sec-iteratorclose
14
15module.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};
Note: See TracBrowser for help on using the repository browser.