Permalink
codeql-action/node_modules/es-abstract/2022/IteratorClose.js
Newer
100644
51 lines (39 sloc)
1.66 KB
Ignoring revisions in .git-blame-ignore-revs.
1
'use strict';
2
3
var GetIntrinsic = require('get-intrinsic');
4
5
var $TypeError = GetIntrinsic('%TypeError%');
6
7
var Call = require('./Call');
8
var CompletionRecord = require('./CompletionRecord');
9
var GetMethod = require('./GetMethod');
10
var IsCallable = require('./IsCallable');
11
var Type = require('./Type');
12
13
// https://262.ecma-international.org/6.0/#sec-iteratorclose
14
15
module.exports = function IteratorClose(iterator, completion) {
16
if (Type(iterator) !== 'Object') {
17
throw new $TypeError('Assertion failed: Type(iterator) is not Object');
18
}
19
if (!IsCallable(completion) && !(completion instanceof CompletionRecord)) {
20
throw new $TypeError('Assertion failed: completion is not a thunk representing a Completion Record, nor a Completion Record instance');
21
}
22
var completionThunk = completion instanceof CompletionRecord ? function () { return completion['?'](); } : completion;
23
24
var iteratorReturn = GetMethod(iterator, 'return');
25
26
if (typeof iteratorReturn === 'undefined') {
27
return completionThunk();
28
}
29
30
var completionRecord;
31
try {
32
var innerResult = Call(iteratorReturn, iterator, []);
33
} catch (e) {
34
// if we hit here, then "e" is the innerResult completion that needs re-throwing
35
36
// if the completion is of type "throw", this will throw.
37
completionThunk();
38
completionThunk = null; // ensure it's not called twice.
39
40
// if not, then return the innerResult completion
41
throw e;
42
}
43
completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
44
completionThunk = null; // ensure it's not called twice.
45
46
if (Type(innerResult) !== 'Object') {
47
throw new $TypeError('iterator .return must return an object');
48
}
49
50
return completionRecord;
51
};