IteratorClose.js 1.7 KB

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