IteratorClose.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var Call = require('./Call');
  5. var CompletionRecord = require('./CompletionRecord');
  6. var GetMethod = require('./GetMethod');
  7. var IsCallable = require('./IsCallable');
  8. var Type = require('./Type');
  9. var assertRecord = require('../helpers/assertRecord');
  10. // https://262.ecma-international.org/14.0/#sec-iteratorclose
  11. module.exports = function IteratorClose(iteratorRecord, completion) {
  12. assertRecord(Type, 'Iterator Record', 'iteratorRecord', iteratorRecord);
  13. if (Type(iteratorRecord['[[Iterator]]']) !== 'Object') {
  14. throw new $TypeError('Assertion failed: iteratorRecord.[[Iterator]] must be an Object'); // step 1
  15. }
  16. if (!IsCallable(completion) && !(completion instanceof CompletionRecord)) { // step 2
  17. throw new $TypeError('Assertion failed: completion is not a thunk representing a Completion Record, nor a Completion Record instance');
  18. }
  19. var completionThunk = completion instanceof CompletionRecord ? function () { return completion['?'](); } : completion;
  20. var iterator = iteratorRecord['[[Iterator]]']; // step 3
  21. var iteratorReturn;
  22. try {
  23. iteratorReturn = GetMethod(iterator, 'return'); // step 4
  24. } catch (e) {
  25. completionThunk(); // throws if `completion` is a throw completion // step 6
  26. completionThunk = null; // ensure it's not called twice.
  27. throw e; // step 7
  28. }
  29. if (typeof iteratorReturn === 'undefined') {
  30. return completionThunk(); // step 5.a - 5.b
  31. }
  32. var innerResult;
  33. try {
  34. innerResult = Call(iteratorReturn, iterator, []);
  35. } catch (e) {
  36. // if we hit here, then "e" is the innerResult completion that needs re-throwing
  37. completionThunk(); // throws if `completion` is a throw completion // step 6
  38. completionThunk = null; // ensure it's not called twice.
  39. // if not, then return the innerResult completion
  40. throw e; // step 7
  41. }
  42. var completionRecord = completionThunk(); // if innerResult worked, then throw if the completion does
  43. completionThunk = null; // ensure it's not called twice.
  44. if (Type(innerResult) !== 'Object') {
  45. throw new $TypeError('iterator .return must return an object');
  46. }
  47. return completionRecord;
  48. };